blob: 06bf8b5304ac0ba6349dcd2cd9a702dc14a508b5 [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"
Vladimir Marko86c87522020-05-11 16:55:55 +010021#include "arch/arm/jni_frame_arm.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000022#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/bit_utils.h"
24#include "base/bit_utils_iterator.h"
Vladimir Marko7968cae2021-01-19 12:02:35 +000025#include "class_root-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010026#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010027#include "code_generator_utils.h"
28#include "common_arm.h"
29#include "compiled_method.h"
30#include "entrypoints/quick/quick_entrypoints.h"
31#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010032#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070033#include "heap_poisoning.h"
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +010034#include "interpreter/mterp/nterp.h"
Vladimir Marko6fd16062018-06-26 11:02:04 +010035#include "intrinsics.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010036#include "intrinsics_arm_vixl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010037#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010038#include "mirror/array-inl.h"
39#include "mirror/class-inl.h"
Vladimir Markoac3fcff2020-11-17 12:17:58 +000040#include "mirror/var_handle.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000041#include "scoped_thread_state_change-inl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010042#include "thread.h"
43#include "utils/arm/assembler_arm_vixl.h"
44#include "utils/arm/managed_register_arm.h"
45#include "utils/assembler.h"
46#include "utils/stack_checks.h"
47
Vladimir Marko0a516052019-10-14 13:00:44 +000048namespace art {
Scott Wakelingfe885462016-09-22 10:24:38 +010049namespace arm {
50
51namespace vixl32 = vixl::aarch32;
52using namespace vixl32; // NOLINT(build/namespaces)
53
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010054using helpers::DRegisterFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010055using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080056using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010057using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010058using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010059using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010060using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000061using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010062using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000063using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000064using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010065using helpers::LocationFrom;
66using helpers::LowRegisterFrom;
67using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080068using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010069using helpers::OutputRegister;
70using helpers::OutputSRegister;
71using helpers::OutputVRegister;
72using helpers::RegisterFrom;
73using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000074using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010075
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +010076using vixl::EmissionCheckScope;
Artem Serov0fb37192016-12-06 18:13:40 +000077using vixl::ExactAssemblyScope;
78using vixl::CodeBufferCheckScope;
79
Scott Wakelingfe885462016-09-22 10:24:38 +010080using RegisterList = vixl32::RegisterList;
81
82static bool ExpectedPairLayout(Location location) {
83 // We expected this for both core and fpu register pairs.
84 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
85}
Artem Serovd4cc5b22016-11-04 11:19:09 +000086// Use a local definition to prevent copying mistakes.
87static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
88static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010089static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010090
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010091// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
92// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010093// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010094// the offset explicitly.
95constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
96
Roland Levillain5daa4952017-07-03 17:23:56 +010097// Using a base helps identify when we hit Marking Register check breakpoints.
98constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
99
Scott Wakelingfe885462016-09-22 10:24:38 +0100100#ifdef __
101#error "ARM Codegen VIXL macro-assembler macro already defined."
102#endif
103
Scott Wakelingfe885462016-09-22 10:24:38 +0100104// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
105#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
107
108// Marker that code is yet to be, and must, be implemented.
109#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
110
Vladimir Marko88abba22017-05-03 17:09:25 +0100111static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
112 return rt.IsLow() && rn.IsLow() && offset < 32u;
113}
114
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100115class EmitAdrCode {
116 public:
117 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
118 : assembler_(assembler), rd_(rd), label_(label) {
Vladimir Markod887ed82018-08-14 13:52:12 +0000119 DCHECK(!assembler->AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100120 adr_location_ = assembler->GetCursorOffset();
121 assembler->adr(EncodingSize(Wide), rd, label);
122 }
123
124 ~EmitAdrCode() {
125 DCHECK(label_->IsBound());
126 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
127 // TODO: Maybe extend VIXL to allow ADR for return address?
128 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
129 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
130 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
131 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
132 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
133 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
134 // Add the Thumb mode bit.
135 raw_adr[2] |= 0x01u;
136 }
137
138 private:
139 ArmVIXLMacroAssembler* const assembler_;
140 vixl32::Register rd_;
141 vixl32::Label* const label_;
142 int32_t adr_location_;
143};
144
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100145static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
146 InvokeRuntimeCallingConventionARMVIXL calling_convention;
147 RegisterSet caller_saves = RegisterSet::Empty();
148 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
149 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
150 // that the the kPrimNot result register is the same as the first argument register.
151 return caller_saves;
152}
153
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100154// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
155// for each live D registers they treat two corresponding S registers as live ones.
156//
157// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
158// from a list of contiguous S registers a list of contiguous D registers (processing first/last
159// S registers corner cases) and save/restore this new list treating them as D registers.
160// - decreasing code size
161// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
162// restored and then used in regular non SlowPath code as D register.
163//
164// For the following example (v means the S register is live):
165// D names: | D0 | D1 | D2 | D4 | ...
166// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
167// Live? | | v | v | v | v | v | v | | ...
168//
169// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
170// as D registers.
171//
172// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
173// for lists of floating-point registers.
174static size_t SaveContiguousSRegisterList(size_t first,
175 size_t last,
176 CodeGenerator* codegen,
177 size_t stack_offset) {
178 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
179 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
180 DCHECK_LE(first, last);
181 if ((first == last) && (first == 0)) {
182 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
183 return stack_offset + kSRegSizeInBytes;
184 }
185 if (first % 2 == 1) {
186 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
187 stack_offset += kSRegSizeInBytes;
188 }
189
190 bool save_last = false;
191 if (last % 2 == 0) {
192 save_last = true;
193 --last;
194 }
195
196 if (first < last) {
197 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
198 DCHECK_EQ((last - first + 1) % 2, 0u);
199 size_t number_of_d_regs = (last - first + 1) / 2;
200
201 if (number_of_d_regs == 1) {
202 __ Vstr(d_reg, MemOperand(sp, stack_offset));
203 } else if (number_of_d_regs > 1) {
204 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
205 vixl32::Register base = sp;
206 if (stack_offset != 0) {
207 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000208 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100209 }
210 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
211 }
212 stack_offset += number_of_d_regs * kDRegSizeInBytes;
213 }
214
215 if (save_last) {
216 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
217 stack_offset += kSRegSizeInBytes;
218 }
219
220 return stack_offset;
221}
222
223static size_t RestoreContiguousSRegisterList(size_t first,
224 size_t last,
225 CodeGenerator* codegen,
226 size_t stack_offset) {
227 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
228 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
229 DCHECK_LE(first, last);
230 if ((first == last) && (first == 0)) {
231 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
232 return stack_offset + kSRegSizeInBytes;
233 }
234 if (first % 2 == 1) {
235 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
236 stack_offset += kSRegSizeInBytes;
237 }
238
239 bool restore_last = false;
240 if (last % 2 == 0) {
241 restore_last = true;
242 --last;
243 }
244
245 if (first < last) {
246 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
247 DCHECK_EQ((last - first + 1) % 2, 0u);
248 size_t number_of_d_regs = (last - first + 1) / 2;
249 if (number_of_d_regs == 1) {
250 __ Vldr(d_reg, MemOperand(sp, stack_offset));
251 } else if (number_of_d_regs > 1) {
252 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
253 vixl32::Register base = sp;
254 if (stack_offset != 0) {
255 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000256 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100257 }
258 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
259 }
260 stack_offset += number_of_d_regs * kDRegSizeInBytes;
261 }
262
263 if (restore_last) {
264 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
265 stack_offset += kSRegSizeInBytes;
266 }
267
268 return stack_offset;
269}
270
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100271static LoadOperandType GetLoadOperandType(DataType::Type type) {
272 switch (type) {
273 case DataType::Type::kReference:
274 return kLoadWord;
275 case DataType::Type::kBool:
276 case DataType::Type::kUint8:
277 return kLoadUnsignedByte;
278 case DataType::Type::kInt8:
279 return kLoadSignedByte;
280 case DataType::Type::kUint16:
281 return kLoadUnsignedHalfword;
282 case DataType::Type::kInt16:
283 return kLoadSignedHalfword;
284 case DataType::Type::kInt32:
285 return kLoadWord;
286 case DataType::Type::kInt64:
287 return kLoadWordPair;
288 case DataType::Type::kFloat32:
289 return kLoadSWord;
290 case DataType::Type::kFloat64:
291 return kLoadDWord;
292 default:
293 LOG(FATAL) << "Unreachable type " << type;
294 UNREACHABLE();
295 }
296}
297
298static StoreOperandType GetStoreOperandType(DataType::Type type) {
299 switch (type) {
300 case DataType::Type::kReference:
301 return kStoreWord;
302 case DataType::Type::kBool:
303 case DataType::Type::kUint8:
304 case DataType::Type::kInt8:
305 return kStoreByte;
306 case DataType::Type::kUint16:
307 case DataType::Type::kInt16:
308 return kStoreHalfword;
309 case DataType::Type::kInt32:
310 return kStoreWord;
311 case DataType::Type::kInt64:
312 return kStoreWordPair;
313 case DataType::Type::kFloat32:
314 return kStoreSWord;
315 case DataType::Type::kFloat64:
316 return kStoreDWord;
317 default:
318 LOG(FATAL) << "Unreachable type " << type;
319 UNREACHABLE();
320 }
321}
322
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100323void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
324 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
325 size_t orig_offset = stack_offset;
326
Andreas Gampe3db70682018-12-26 15:12:03 -0800327 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100328 for (uint32_t i : LowToHighBits(core_spills)) {
329 // If the register holds an object, update the stack mask.
330 if (locations->RegisterContainsObject(i)) {
331 locations->SetStackBit(stack_offset / kVRegSize);
332 }
333 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
334 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
335 saved_core_stack_offsets_[i] = stack_offset;
336 stack_offset += kArmWordSize;
337 }
338
339 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
340 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
341
Andreas Gampe3db70682018-12-26 15:12:03 -0800342 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100343 orig_offset = stack_offset;
344 for (uint32_t i : LowToHighBits(fp_spills)) {
345 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
346 saved_fpu_stack_offsets_[i] = stack_offset;
347 stack_offset += kArmWordSize;
348 }
349
350 stack_offset = orig_offset;
351 while (fp_spills != 0u) {
352 uint32_t begin = CTZ(fp_spills);
353 uint32_t tmp = fp_spills + (1u << begin);
354 fp_spills &= tmp; // Clear the contiguous range of 1s.
355 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
356 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
357 }
358 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
359}
360
361void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
362 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
363 size_t orig_offset = stack_offset;
364
Andreas Gampe3db70682018-12-26 15:12:03 -0800365 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100366 for (uint32_t i : LowToHighBits(core_spills)) {
367 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
368 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
369 stack_offset += kArmWordSize;
370 }
371
372 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
373 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
374 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
375
Andreas Gampe3db70682018-12-26 15:12:03 -0800376 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100377 while (fp_spills != 0u) {
378 uint32_t begin = CTZ(fp_spills);
379 uint32_t tmp = fp_spills + (1u << begin);
380 fp_spills &= tmp; // Clear the contiguous range of 1s.
381 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
382 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
383 }
384 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
385}
386
387class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
388 public:
389 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
390
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100391 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100392 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
393 __ Bind(GetEntryLabel());
394 if (instruction_->CanThrowIntoCatchBlock()) {
395 // Live registers will be restored in the catch block if caught.
396 SaveLiveRegisters(codegen, instruction_->GetLocations());
397 }
398 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
399 instruction_,
400 instruction_->GetDexPc(),
401 this);
402 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
403 }
404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100405 bool IsFatal() const override { return true; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100407 const char* GetDescription() const override { return "NullCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100408
409 private:
410 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
411};
412
Scott Wakelingfe885462016-09-22 10:24:38 +0100413class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
414 public:
415 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
416 : SlowPathCodeARMVIXL(instruction) {}
417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100418 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100419 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100420 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100421 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100422 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
423 }
424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100425 bool IsFatal() const override { return true; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100427 const char* GetDescription() const override { return "DivZeroCheckSlowPathARMVIXL"; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100428
429 private:
430 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
431};
432
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100433class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
434 public:
435 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
436 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100438 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100439 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
440 __ Bind(GetEntryLabel());
441 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
442 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
443 if (successor_ == nullptr) {
444 __ B(GetReturnLabel());
445 } else {
446 __ B(arm_codegen->GetLabelOf(successor_));
447 }
448 }
449
450 vixl32::Label* GetReturnLabel() {
451 DCHECK(successor_ == nullptr);
452 return &return_label_;
453 }
454
455 HBasicBlock* GetSuccessor() const {
456 return successor_;
457 }
458
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100459 const char* GetDescription() const override { return "SuspendCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100460
461 private:
462 // If not null, the block to branch to after the suspend check.
463 HBasicBlock* const successor_;
464
465 // If `successor_` is null, the label to branch to after the suspend check.
466 vixl32::Label return_label_;
467
468 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
469};
470
Scott Wakelingc34dba72016-10-03 10:14:44 +0100471class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
472 public:
473 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
474 : SlowPathCodeARMVIXL(instruction) {}
475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100476 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100477 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
478 LocationSummary* locations = instruction_->GetLocations();
479
480 __ Bind(GetEntryLabel());
481 if (instruction_->CanThrowIntoCatchBlock()) {
482 // Live registers will be restored in the catch block if caught.
483 SaveLiveRegisters(codegen, instruction_->GetLocations());
484 }
485 // We're moving two locations to locations that could overlap, so we need a parallel
486 // move resolver.
487 InvokeRuntimeCallingConventionARMVIXL calling_convention;
488 codegen->EmitParallelMoves(
489 locations->InAt(0),
490 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100491 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100492 locations->InAt(1),
493 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100494 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100495 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
496 ? kQuickThrowStringBounds
497 : kQuickThrowArrayBounds;
498 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
499 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
500 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
501 }
502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100503 bool IsFatal() const override { return true; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100505 const char* GetDescription() const override { return "BoundsCheckSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100506
507 private:
508 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
509};
510
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100511class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
512 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100513 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at)
514 : SlowPathCodeARMVIXL(at), cls_(cls) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100515 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100516 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100517 }
518
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100519 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000520 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000521 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100522 const uint32_t dex_pc = instruction_->GetDexPc();
523 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
524 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100525
526 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
527 __ Bind(GetEntryLabel());
528 SaveLiveRegisters(codegen, locations);
529
530 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100531 if (must_resolve_type) {
532 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm_codegen->GetGraph()->GetDexFile()));
533 dex::TypeIndex type_index = cls_->GetTypeIndex();
534 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100535 if (cls_->NeedsAccessCheck()) {
536 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
537 arm_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
538 } else {
539 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
540 arm_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
541 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100542 // If we also must_do_clinit, the resolved type is now in the correct register.
543 } else {
544 DCHECK(must_do_clinit);
545 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
546 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), source);
547 }
548 if (must_do_clinit) {
549 arm_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
550 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100551 }
552
553 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100554 if (out.IsValid()) {
555 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
556 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
557 }
558 RestoreLiveRegisters(codegen, locations);
559 __ B(GetExitLabel());
560 }
561
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100562 const char* GetDescription() const override { return "LoadClassSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100563
564 private:
565 // The class this slow path will load.
566 HLoadClass* const cls_;
567
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100568 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
569};
570
Artem Serovd4cc5b22016-11-04 11:19:09 +0000571class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
572 public:
573 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
574 : SlowPathCodeARMVIXL(instruction) {}
575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100576 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000577 DCHECK(instruction_->IsLoadString());
578 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000579 LocationSummary* locations = instruction_->GetLocations();
580 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000581 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000582
583 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
584 __ Bind(GetEntryLabel());
585 SaveLiveRegisters(codegen, locations);
586
587 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000588 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000589 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
590 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
591
Artem Serovd4cc5b22016-11-04 11:19:09 +0000592 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
593 RestoreLiveRegisters(codegen, locations);
594
595 __ B(GetExitLabel());
596 }
597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100598 const char* GetDescription() const override { return "LoadStringSlowPathARMVIXL"; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000599
600 private:
601 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
602};
603
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100604class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
605 public:
606 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
607 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100609 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100610 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100611 DCHECK(instruction_->IsCheckCast()
612 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
613
614 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
615 __ Bind(GetEntryLabel());
616
Vladimir Marko87584542017-12-12 17:47:52 +0000617 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100618 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100619 }
620
621 // We're moving two locations to locations that could overlap, so we need a parallel
622 // move resolver.
623 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100624
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800625 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800626 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100627 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800628 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800629 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100630 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100631 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100632 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
633 instruction_,
634 instruction_->GetDexPc(),
635 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800636 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100637 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100638 } else {
639 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800640 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
641 instruction_,
642 instruction_->GetDexPc(),
643 this);
644 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100645 }
646
647 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100648 RestoreLiveRegisters(codegen, locations);
649 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100650 }
651 }
652
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100653 const char* GetDescription() const override { return "TypeCheckSlowPathARMVIXL"; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100655 bool IsFatal() const override { return is_fatal_; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100656
657 private:
658 const bool is_fatal_;
659
660 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
661};
662
Scott Wakelingc34dba72016-10-03 10:14:44 +0100663class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
664 public:
665 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
666 : SlowPathCodeARMVIXL(instruction) {}
667
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100668 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100669 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
670 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100671 LocationSummary* locations = instruction_->GetLocations();
672 SaveLiveRegisters(codegen, locations);
673 InvokeRuntimeCallingConventionARMVIXL calling_convention;
674 __ Mov(calling_convention.GetRegisterAt(0),
675 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
676
Scott Wakelingc34dba72016-10-03 10:14:44 +0100677 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100678 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100679 }
680
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100681 const char* GetDescription() const override { return "DeoptimizationSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100682
683 private:
684 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
685};
686
687class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
688 public:
689 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
690
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100691 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100692 LocationSummary* locations = instruction_->GetLocations();
693 __ Bind(GetEntryLabel());
694 SaveLiveRegisters(codegen, locations);
695
696 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100697 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100698 parallel_move.AddMove(
699 locations->InAt(0),
700 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100701 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100702 nullptr);
703 parallel_move.AddMove(
704 locations->InAt(1),
705 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100706 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100707 nullptr);
708 parallel_move.AddMove(
709 locations->InAt(2),
710 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100711 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100712 nullptr);
713 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
714
715 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
716 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
717 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
718 RestoreLiveRegisters(codegen, locations);
719 __ B(GetExitLabel());
720 }
721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100722 const char* GetDescription() const override { return "ArraySetSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100723
724 private:
725 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
726};
727
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000728// Slow path generating a read barrier for a heap reference.
729class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
730 public:
731 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
732 Location out,
733 Location ref,
734 Location obj,
735 uint32_t offset,
736 Location index)
737 : SlowPathCodeARMVIXL(instruction),
738 out_(out),
739 ref_(ref),
740 obj_(obj),
741 offset_(offset),
742 index_(index) {
743 DCHECK(kEmitCompilerReadBarrier);
744 // If `obj` is equal to `out` or `ref`, it means the initial object
745 // has been overwritten by (or after) the heap object reference load
746 // to be instrumented, e.g.:
747 //
748 // __ LoadFromOffset(kLoadWord, out, out, offset);
749 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
750 //
751 // In that case, we have lost the information about the original
752 // object, and the emitted read barrier cannot work properly.
753 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
754 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
755 }
756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100757 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000758 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
759 LocationSummary* locations = instruction_->GetLocations();
760 vixl32::Register reg_out = RegisterFrom(out_);
761 DCHECK(locations->CanCall());
762 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
763 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000764 instruction_->IsPredicatedInstanceFieldGet() ||
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000765 instruction_->IsStaticFieldGet() ||
766 instruction_->IsArrayGet() ||
767 instruction_->IsInstanceOf() ||
768 instruction_->IsCheckCast() ||
Vladimir Marko94d2c812020-11-05 10:04:45 +0000769 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000770 << "Unexpected instruction in read barrier for heap reference slow path: "
771 << instruction_->DebugName();
772 // The read barrier instrumentation of object ArrayGet
773 // instructions does not support the HIntermediateAddress
774 // instruction.
775 DCHECK(!(instruction_->IsArrayGet() &&
776 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
777
778 __ Bind(GetEntryLabel());
779 SaveLiveRegisters(codegen, locations);
780
781 // We may have to change the index's value, but as `index_` is a
782 // constant member (like other "inputs" of this slow path),
783 // introduce a copy of it, `index`.
784 Location index = index_;
785 if (index_.IsValid()) {
786 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
787 if (instruction_->IsArrayGet()) {
788 // Compute the actual memory offset and store it in `index`.
789 vixl32::Register index_reg = RegisterFrom(index_);
790 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
791 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
792 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +0100793 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
794 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000795 // not been saved by the previous call to
796 // art::SlowPathCode::SaveLiveRegisters, as it is a
797 // callee-save register --
798 // art::SlowPathCode::SaveLiveRegisters does not consider
799 // callee-save registers, as it has been designed with the
800 // assumption that callee-save registers are supposed to be
801 // handled by the called function. So, as a callee-save
802 // register, `index_reg` _would_ eventually be saved onto
803 // the stack, but it would be too late: we would have
804 // changed its value earlier. Therefore, we manually save
805 // it here into another freely available register,
806 // `free_reg`, chosen of course among the caller-save
807 // registers (as a callee-save `free_reg` register would
808 // exhibit the same problem).
809 //
810 // Note we could have requested a temporary register from
811 // the register allocator instead; but we prefer not to, as
812 // this is a slow path, and we know we can find a
813 // caller-save register that is available.
814 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
815 __ Mov(free_reg, index_reg);
816 index_reg = free_reg;
817 index = LocationFrom(index_reg);
818 } else {
819 // The initial register stored in `index_` has already been
820 // saved in the call to art::SlowPathCode::SaveLiveRegisters
821 // (as it is not a callee-save register), so we can freely
822 // use it.
823 }
824 // Shifting the index value contained in `index_reg` by the scale
825 // factor (2) cannot overflow in practice, as the runtime is
826 // unable to allocate object arrays with a size larger than
827 // 2^26 - 1 (that is, 2^28 - 4 bytes).
828 __ Lsl(index_reg, index_reg, TIMES_4);
829 static_assert(
830 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
831 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
832 __ Add(index_reg, index_reg, offset_);
833 } else {
834 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
835 // intrinsics, `index_` is not shifted by a scale factor of 2
836 // (as in the case of ArrayGet), as it is actually an offset
837 // to an object field within an object.
838 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
839 DCHECK(instruction_->GetLocations()->Intrinsified());
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000840 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
841 DCHECK(intrinsic == Intrinsics::kUnsafeGetObject ||
842 intrinsic == Intrinsics::kUnsafeGetObjectVolatile ||
Sorin Basca2f01e8e2021-06-18 06:44:07 +0000843 intrinsic == Intrinsics::kJdkUnsafeGetObject ||
844 intrinsic == Intrinsics::kJdkUnsafeGetObjectVolatile ||
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000845 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko3d350a82020-11-18 14:14:27 +0000846 mirror::VarHandle::AccessModeTemplate::kGet ||
847 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
848 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
849 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko526569a2020-11-30 15:48:38 +0000850 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange ||
851 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
852 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate)
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000853 << instruction_->AsInvoke()->GetIntrinsic();
854 DCHECK_EQ(offset_, 0U);
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000855 // Though UnsafeGet's offset location is a register pair, we only pass the low
856 // part (high part is irrelevant for 32-bit addresses) to the slow path.
857 // For VarHandle intrinsics, the index is always just a register.
858 DCHECK(index_.IsRegister());
859 index = index_;
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000860 }
861 }
862
863 // We're moving two or three locations to locations that could
864 // overlap, so we need a parallel move resolver.
865 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100866 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000867 parallel_move.AddMove(ref_,
868 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100869 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000870 nullptr);
871 parallel_move.AddMove(obj_,
872 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100873 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000874 nullptr);
875 if (index.IsValid()) {
876 parallel_move.AddMove(index,
877 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100878 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000879 nullptr);
880 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
881 } else {
882 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
883 __ Mov(calling_convention.GetRegisterAt(2), offset_);
884 }
885 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
886 CheckEntrypointTypes<
887 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
888 arm_codegen->Move32(out_, LocationFrom(r0));
889
890 RestoreLiveRegisters(codegen, locations);
891 __ B(GetExitLabel());
892 }
893
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100894 const char* GetDescription() const override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000895 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
896 }
897
898 private:
899 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
900 uint32_t ref = RegisterFrom(ref_).GetCode();
901 uint32_t obj = RegisterFrom(obj_).GetCode();
902 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
903 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
904 return vixl32::Register(i);
905 }
906 }
907 // We shall never fail to find a free caller-save register, as
908 // there are more than two core caller-save registers on ARM
909 // (meaning it is possible to find one which is different from
910 // `ref` and `obj`).
911 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
912 LOG(FATAL) << "Could not find a free caller-save register";
913 UNREACHABLE();
914 }
915
916 const Location out_;
917 const Location ref_;
918 const Location obj_;
919 const uint32_t offset_;
920 // An additional location containing an index to an array.
921 // Only used for HArrayGet and the UnsafeGetObject &
922 // UnsafeGetObjectVolatile intrinsics.
923 const Location index_;
924
925 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
926};
927
928// Slow path generating a read barrier for a GC root.
929class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
930 public:
931 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
932 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
933 DCHECK(kEmitCompilerReadBarrier);
934 }
935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100936 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000937 LocationSummary* locations = instruction_->GetLocations();
938 vixl32::Register reg_out = RegisterFrom(out_);
939 DCHECK(locations->CanCall());
940 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000941 DCHECK(instruction_->IsLoadClass() ||
942 instruction_->IsLoadString() ||
943 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000944 << "Unexpected instruction in read barrier for GC root slow path: "
945 << instruction_->DebugName();
946
947 __ Bind(GetEntryLabel());
948 SaveLiveRegisters(codegen, locations);
949
950 InvokeRuntimeCallingConventionARMVIXL calling_convention;
951 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
952 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
953 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
954 instruction_,
955 instruction_->GetDexPc(),
956 this);
957 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
958 arm_codegen->Move32(out_, LocationFrom(r0));
959
960 RestoreLiveRegisters(codegen, locations);
961 __ B(GetExitLabel());
962 }
963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100964 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARMVIXL"; }
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000965
966 private:
967 const Location out_;
968 const Location root_;
969
970 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
971};
Scott Wakelingc34dba72016-10-03 10:14:44 +0100972
Scott Wakelingfe885462016-09-22 10:24:38 +0100973inline vixl32::Condition ARMCondition(IfCondition cond) {
974 switch (cond) {
975 case kCondEQ: return eq;
976 case kCondNE: return ne;
977 case kCondLT: return lt;
978 case kCondLE: return le;
979 case kCondGT: return gt;
980 case kCondGE: return ge;
981 case kCondB: return lo;
982 case kCondBE: return ls;
983 case kCondA: return hi;
984 case kCondAE: return hs;
985 }
986 LOG(FATAL) << "Unreachable";
987 UNREACHABLE();
988}
989
990// Maps signed condition to unsigned condition.
991inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
992 switch (cond) {
993 case kCondEQ: return eq;
994 case kCondNE: return ne;
995 // Signed to unsigned.
996 case kCondLT: return lo;
997 case kCondLE: return ls;
998 case kCondGT: return hi;
999 case kCondGE: return hs;
1000 // Unsigned remain unchanged.
1001 case kCondB: return lo;
1002 case kCondBE: return ls;
1003 case kCondA: return hi;
1004 case kCondAE: return hs;
1005 }
1006 LOG(FATAL) << "Unreachable";
1007 UNREACHABLE();
1008}
1009
1010inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1011 // The ARM condition codes can express all the necessary branches, see the
1012 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1013 // There is no dex instruction or HIR that would need the missing conditions
1014 // "equal or unordered" or "not equal".
1015 switch (cond) {
1016 case kCondEQ: return eq;
1017 case kCondNE: return ne /* unordered */;
1018 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1019 case kCondLE: return gt_bias ? ls : le /* unordered */;
1020 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1021 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1022 default:
1023 LOG(FATAL) << "UNREACHABLE";
1024 UNREACHABLE();
1025 }
1026}
1027
Anton Kirilov74234da2017-01-13 14:42:47 +00001028inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1029 switch (op_kind) {
1030 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1031 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1032 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1033 default:
1034 LOG(FATAL) << "Unexpected op kind " << op_kind;
1035 UNREACHABLE();
1036 }
1037}
1038
Scott Wakelingfe885462016-09-22 10:24:38 +01001039void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1040 stream << vixl32::Register(reg);
1041}
1042
1043void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1044 stream << vixl32::SRegister(reg);
1045}
1046
Vladimir Markoa0431112018-06-25 09:32:54 +01001047const ArmInstructionSetFeatures& CodeGeneratorARMVIXL::GetInstructionSetFeatures() const {
1048 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArmInstructionSetFeatures();
1049}
1050
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001051static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001052 uint32_t mask = 0;
1053 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1054 i <= regs.GetLastSRegister().GetCode();
1055 ++i) {
1056 mask |= (1 << i);
1057 }
1058 return mask;
1059}
1060
Artem Serovd4cc5b22016-11-04 11:19:09 +00001061// Saves the register in the stack. Returns the size taken on stack.
1062size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1063 uint32_t reg_id ATTRIBUTE_UNUSED) {
1064 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001065 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001066}
1067
1068// Restores the register from the stack. Returns the size taken on stack.
1069size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1070 uint32_t reg_id ATTRIBUTE_UNUSED) {
1071 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001072 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001073}
1074
1075size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1076 uint32_t reg_id ATTRIBUTE_UNUSED) {
1077 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001078 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001079}
1080
1081size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1082 uint32_t reg_id ATTRIBUTE_UNUSED) {
1083 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001084 UNREACHABLE();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001085}
1086
Anton Kirilov74234da2017-01-13 14:42:47 +00001087static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1088 vixl32::Register out,
1089 vixl32::Register first,
1090 const Operand& second,
1091 CodeGeneratorARMVIXL* codegen) {
1092 if (second.IsImmediate() && second.GetImmediate() == 0) {
1093 const Operand in = kind == HInstruction::kAnd
1094 ? Operand(0)
1095 : Operand(first);
1096
1097 __ Mov(out, in);
1098 } else {
1099 switch (kind) {
1100 case HInstruction::kAdd:
1101 __ Add(out, first, second);
1102 break;
1103 case HInstruction::kAnd:
1104 __ And(out, first, second);
1105 break;
1106 case HInstruction::kOr:
1107 __ Orr(out, first, second);
1108 break;
1109 case HInstruction::kSub:
1110 __ Sub(out, first, second);
1111 break;
1112 case HInstruction::kXor:
1113 __ Eor(out, first, second);
1114 break;
1115 default:
1116 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1117 UNREACHABLE();
1118 }
1119 }
1120}
1121
1122static void GenerateDataProc(HInstruction::InstructionKind kind,
1123 const Location& out,
1124 const Location& first,
1125 const Operand& second_lo,
1126 const Operand& second_hi,
1127 CodeGeneratorARMVIXL* codegen) {
1128 const vixl32::Register first_hi = HighRegisterFrom(first);
1129 const vixl32::Register first_lo = LowRegisterFrom(first);
1130 const vixl32::Register out_hi = HighRegisterFrom(out);
1131 const vixl32::Register out_lo = LowRegisterFrom(out);
1132
1133 if (kind == HInstruction::kAdd) {
1134 __ Adds(out_lo, first_lo, second_lo);
1135 __ Adc(out_hi, first_hi, second_hi);
1136 } else if (kind == HInstruction::kSub) {
1137 __ Subs(out_lo, first_lo, second_lo);
1138 __ Sbc(out_hi, first_hi, second_hi);
1139 } else {
1140 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1141 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1142 }
1143}
1144
1145static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1146 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1147}
1148
1149static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1150 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001152 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1153
1154 const LocationSummary* const locations = instruction->GetLocations();
1155 const uint32_t shift_value = instruction->GetShiftAmount();
1156 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1157 const Location first = locations->InAt(0);
1158 const Location second = locations->InAt(1);
1159 const Location out = locations->Out();
1160 const vixl32::Register first_hi = HighRegisterFrom(first);
1161 const vixl32::Register first_lo = LowRegisterFrom(first);
1162 const vixl32::Register out_hi = HighRegisterFrom(out);
1163 const vixl32::Register out_lo = LowRegisterFrom(out);
1164 const vixl32::Register second_hi = HighRegisterFrom(second);
1165 const vixl32::Register second_lo = LowRegisterFrom(second);
1166 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1167
1168 if (shift_value >= 32) {
1169 if (shift == ShiftType::LSL) {
1170 GenerateDataProcInstruction(kind,
1171 out_hi,
1172 first_hi,
1173 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1174 codegen);
1175 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1176 } else if (shift == ShiftType::ASR) {
1177 GenerateDataProc(kind,
1178 out,
1179 first,
1180 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1181 Operand(second_hi, ShiftType::ASR, 31),
1182 codegen);
1183 } else {
1184 DCHECK_EQ(shift, ShiftType::LSR);
1185 GenerateDataProc(kind,
1186 out,
1187 first,
1188 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1189 0,
1190 codegen);
1191 }
1192 } else {
1193 DCHECK_GT(shift_value, 1U);
1194 DCHECK_LT(shift_value, 32U);
1195
1196 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1197
1198 if (shift == ShiftType::LSL) {
1199 // We are not doing this for HInstruction::kAdd because the output will require
1200 // Location::kOutputOverlap; not applicable to other cases.
1201 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1202 GenerateDataProcInstruction(kind,
1203 out_hi,
1204 first_hi,
1205 Operand(second_hi, ShiftType::LSL, shift_value),
1206 codegen);
1207 GenerateDataProcInstruction(kind,
1208 out_hi,
1209 out_hi,
1210 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1211 codegen);
1212 GenerateDataProcInstruction(kind,
1213 out_lo,
1214 first_lo,
1215 Operand(second_lo, ShiftType::LSL, shift_value),
1216 codegen);
1217 } else {
1218 const vixl32::Register temp = temps.Acquire();
1219
1220 __ Lsl(temp, second_hi, shift_value);
1221 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1222 GenerateDataProc(kind,
1223 out,
1224 first,
1225 Operand(second_lo, ShiftType::LSL, shift_value),
1226 temp,
1227 codegen);
1228 }
1229 } else {
1230 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1231
1232 // We are not doing this for HInstruction::kAdd because the output will require
1233 // Location::kOutputOverlap; not applicable to other cases.
1234 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1235 GenerateDataProcInstruction(kind,
1236 out_lo,
1237 first_lo,
1238 Operand(second_lo, ShiftType::LSR, shift_value),
1239 codegen);
1240 GenerateDataProcInstruction(kind,
1241 out_lo,
1242 out_lo,
1243 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1244 codegen);
1245 GenerateDataProcInstruction(kind,
1246 out_hi,
1247 first_hi,
1248 Operand(second_hi, shift, shift_value),
1249 codegen);
1250 } else {
1251 const vixl32::Register temp = temps.Acquire();
1252
1253 __ Lsr(temp, second_lo, shift_value);
1254 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1255 GenerateDataProc(kind,
1256 out,
1257 first,
1258 temp,
1259 Operand(second_hi, shift, shift_value),
1260 codegen);
1261 }
1262 }
1263 }
1264}
1265
Donghui Bai426b49c2016-11-08 14:55:38 +08001266static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1267 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1268 if (rhs_loc.IsConstant()) {
1269 // 0.0 is the only immediate that can be encoded directly in
1270 // a VCMP instruction.
1271 //
1272 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1273 // specify that in a floating-point comparison, positive zero
1274 // and negative zero are considered equal, so we can use the
1275 // literal 0.0 for both cases here.
1276 //
1277 // Note however that some methods (Float.equal, Float.compare,
1278 // Float.compareTo, Double.equal, Double.compare,
1279 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1280 // StrictMath.min) consider 0.0 to be (strictly) greater than
1281 // -0.0. So if we ever translate calls to these methods into a
1282 // HCompare instruction, we must handle the -0.0 case with
1283 // care here.
1284 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001286 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001288 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001289 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1290 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001291 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001292 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1293 }
1294 } else {
1295 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1296 }
1297}
1298
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001299static int64_t AdjustConstantForCondition(int64_t value,
1300 IfCondition* condition,
1301 IfCondition* opposite) {
1302 if (value == 1) {
1303 if (*condition == kCondB) {
1304 value = 0;
1305 *condition = kCondEQ;
1306 *opposite = kCondNE;
1307 } else if (*condition == kCondAE) {
1308 value = 0;
1309 *condition = kCondNE;
1310 *opposite = kCondEQ;
1311 }
1312 } else if (value == -1) {
1313 if (*condition == kCondGT) {
1314 value = 0;
1315 *condition = kCondGE;
1316 *opposite = kCondLT;
1317 } else if (*condition == kCondLE) {
1318 value = 0;
1319 *condition = kCondLT;
1320 *opposite = kCondGE;
1321 }
1322 }
1323
1324 return value;
1325}
1326
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001327static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1328 HCondition* condition,
1329 bool invert,
1330 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001331 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001332
1333 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001334 IfCondition cond = condition->GetCondition();
1335 IfCondition opposite = condition->GetOppositeCondition();
1336
1337 if (invert) {
1338 std::swap(cond, opposite);
1339 }
1340
1341 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001342 const Location left = locations->InAt(0);
1343 const Location right = locations->InAt(1);
1344
1345 DCHECK(right.IsConstant());
1346
1347 const vixl32::Register left_high = HighRegisterFrom(left);
1348 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001349 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1350 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1351
1352 // Comparisons against 0 are common enough to deserve special attention.
1353 if (value == 0) {
1354 switch (cond) {
1355 case kCondNE:
1356 // x > 0 iff x != 0 when the comparison is unsigned.
1357 case kCondA:
1358 ret = std::make_pair(ne, eq);
1359 FALLTHROUGH_INTENDED;
1360 case kCondEQ:
1361 // x <= 0 iff x == 0 when the comparison is unsigned.
1362 case kCondBE:
1363 __ Orrs(temps.Acquire(), left_low, left_high);
1364 return ret;
1365 case kCondLT:
1366 case kCondGE:
1367 __ Cmp(left_high, 0);
1368 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1369 // Trivially true or false.
1370 case kCondB:
1371 ret = std::make_pair(ne, eq);
1372 FALLTHROUGH_INTENDED;
1373 case kCondAE:
1374 __ Cmp(left_low, left_low);
1375 return ret;
1376 default:
1377 break;
1378 }
1379 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001380
1381 switch (cond) {
1382 case kCondEQ:
1383 case kCondNE:
1384 case kCondB:
1385 case kCondBE:
1386 case kCondA:
1387 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001388 const uint32_t value_low = Low32Bits(value);
1389 Operand operand_low(value_low);
1390
Donghui Bai426b49c2016-11-08 14:55:38 +08001391 __ Cmp(left_high, High32Bits(value));
1392
Anton Kirilov23b752b2017-07-20 14:40:44 +01001393 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1394 // we must ensure that the operands corresponding to the least significant
1395 // halves of the inputs fit into a 16-bit CMP encoding.
1396 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1397 operand_low = Operand(temps.Acquire());
1398 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1399 }
1400
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001401 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001402 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1403 2 * vixl32::k16BitT32InstructionSizeInBytes,
1404 CodeBufferCheckScope::kExactSize);
1405
1406 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001407 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001408 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001409 break;
1410 }
1411 case kCondLE:
1412 case kCondGT:
1413 // Trivially true or false.
1414 if (value == std::numeric_limits<int64_t>::max()) {
1415 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001416 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001417 break;
1418 }
1419
1420 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001421 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001422 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001423 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001424 } else {
1425 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001426 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001427 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001428 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001429 }
1430
1431 value++;
1432 FALLTHROUGH_INTENDED;
1433 case kCondGE:
1434 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001435 __ Cmp(left_low, Low32Bits(value));
1436 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001437 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001438 break;
1439 }
1440 default:
1441 LOG(FATAL) << "Unreachable";
1442 UNREACHABLE();
1443 }
1444
1445 return ret;
1446}
1447
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001448static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1449 HCondition* condition,
1450 bool invert,
1451 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001452 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001453
1454 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001455 IfCondition cond = condition->GetCondition();
1456 IfCondition opposite = condition->GetOppositeCondition();
1457
1458 if (invert) {
1459 std::swap(cond, opposite);
1460 }
1461
1462 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001463 Location left = locations->InAt(0);
1464 Location right = locations->InAt(1);
1465
1466 DCHECK(right.IsRegisterPair());
1467
1468 switch (cond) {
1469 case kCondEQ:
1470 case kCondNE:
1471 case kCondB:
1472 case kCondBE:
1473 case kCondA:
1474 case kCondAE: {
1475 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1476
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001477 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001478 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1479 2 * vixl32::k16BitT32InstructionSizeInBytes,
1480 CodeBufferCheckScope::kExactSize);
1481
1482 __ it(eq);
1483 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001484 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001485 break;
1486 }
1487 case kCondLE:
1488 case kCondGT:
1489 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001490 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001491 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001492 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001493 } else {
1494 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001495 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001496 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001497 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001498 }
1499
1500 std::swap(left, right);
1501 FALLTHROUGH_INTENDED;
1502 case kCondGE:
1503 case kCondLT: {
1504 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1505
1506 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1507 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001508 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001509 break;
1510 }
1511 default:
1512 LOG(FATAL) << "Unreachable";
1513 UNREACHABLE();
1514 }
1515
1516 return ret;
1517}
1518
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001519static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1520 bool invert,
1521 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001522 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001523 IfCondition cond = condition->GetCondition();
1524 IfCondition opposite = condition->GetOppositeCondition();
1525 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001526
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001527 if (invert) {
1528 std::swap(cond, opposite);
1529 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001531 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001532 ret = condition->GetLocations()->InAt(1).IsConstant()
1533 ? GenerateLongTestConstant(condition, invert, codegen)
1534 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001535 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001536 GenerateVcmp(condition, codegen);
1537 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1538 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1539 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001540 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001541 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001542 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1543 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001544 }
1545
1546 return ret;
1547}
1548
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001549static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001550 const vixl32::Register out = OutputRegister(cond);
1551 const auto condition = GenerateTest(cond, false, codegen);
1552
1553 __ Mov(LeaveFlags, out, 0);
1554
1555 if (out.IsLow()) {
1556 // We use the scope because of the IT block that follows.
1557 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1558 2 * vixl32::k16BitT32InstructionSizeInBytes,
1559 CodeBufferCheckScope::kExactSize);
1560
1561 __ it(condition.first);
1562 __ mov(condition.first, out, 1);
1563 } else {
1564 vixl32::Label done_label;
1565 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1566
Andreas Gampe3db70682018-12-26 15:12:03 -08001567 __ B(condition.second, final_label, /* is_far_target= */ false);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001568 __ Mov(out, 1);
1569
1570 if (done_label.IsReferenced()) {
1571 __ Bind(&done_label);
1572 }
1573 }
1574}
1575
1576static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001578
1579 const LocationSummary* const locations = cond->GetLocations();
1580 IfCondition condition = cond->GetCondition();
1581 const vixl32::Register out = OutputRegister(cond);
1582 const Location left = locations->InAt(0);
1583 const Location right = locations->InAt(1);
1584 vixl32::Register left_high = HighRegisterFrom(left);
1585 vixl32::Register left_low = LowRegisterFrom(left);
1586 vixl32::Register temp;
1587 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1588
1589 if (right.IsConstant()) {
1590 IfCondition opposite = cond->GetOppositeCondition();
1591 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
1592 &condition,
1593 &opposite);
1594 Operand right_high = High32Bits(value);
1595 Operand right_low = Low32Bits(value);
1596
1597 // The output uses Location::kNoOutputOverlap.
1598 if (out.Is(left_high)) {
1599 std::swap(left_low, left_high);
1600 std::swap(right_low, right_high);
1601 }
1602
1603 __ Sub(out, left_low, right_low);
1604 temp = temps.Acquire();
1605 __ Sub(temp, left_high, right_high);
1606 } else {
1607 DCHECK(right.IsRegisterPair());
1608 temp = temps.Acquire();
1609 __ Sub(temp, left_high, HighRegisterFrom(right));
1610 __ Sub(out, left_low, LowRegisterFrom(right));
1611 }
1612
1613 // Need to check after calling AdjustConstantForCondition().
1614 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
1615
1616 if (condition == kCondNE && out.IsLow()) {
1617 __ Orrs(out, out, temp);
1618
1619 // We use the scope because of the IT block that follows.
1620 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1621 2 * vixl32::k16BitT32InstructionSizeInBytes,
1622 CodeBufferCheckScope::kExactSize);
1623
1624 __ it(ne);
1625 __ mov(ne, out, 1);
1626 } else {
1627 __ Orr(out, out, temp);
1628 codegen->GenerateConditionWithZero(condition, out, out, temp);
1629 }
1630}
1631
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001632static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001633 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001634
1635 const LocationSummary* const locations = cond->GetLocations();
1636 IfCondition condition = cond->GetCondition();
1637 const vixl32::Register out = OutputRegister(cond);
1638 const Location left = locations->InAt(0);
1639 const Location right = locations->InAt(1);
1640
1641 if (right.IsConstant()) {
1642 IfCondition opposite = cond->GetOppositeCondition();
1643
1644 // Comparisons against 0 are common enough to deserve special attention.
1645 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
1646 switch (condition) {
1647 case kCondNE:
1648 case kCondA:
1649 if (out.IsLow()) {
1650 // We only care if both input registers are 0 or not.
1651 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
1652
1653 // We use the scope because of the IT block that follows.
1654 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1655 2 * vixl32::k16BitT32InstructionSizeInBytes,
1656 CodeBufferCheckScope::kExactSize);
1657
1658 __ it(ne);
1659 __ mov(ne, out, 1);
1660 return;
1661 }
1662
1663 FALLTHROUGH_INTENDED;
1664 case kCondEQ:
1665 case kCondBE:
1666 // We only care if both input registers are 0 or not.
1667 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
1668 codegen->GenerateConditionWithZero(condition, out, out);
1669 return;
1670 case kCondLT:
1671 case kCondGE:
1672 // We only care about the sign bit.
1673 FALLTHROUGH_INTENDED;
1674 case kCondAE:
1675 case kCondB:
1676 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
1677 return;
1678 case kCondLE:
1679 case kCondGT:
1680 default:
1681 break;
1682 }
1683 }
1684 }
1685
Anton Kirilov23b752b2017-07-20 14:40:44 +01001686 // If `out` is a low register, then the GenerateConditionGeneric()
1687 // function generates a shorter code sequence that is still branchless.
1688 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001689 GenerateEqualLong(cond, codegen);
1690 return;
1691 }
1692
Anton Kirilov23b752b2017-07-20 14:40:44 +01001693 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001694}
1695
Roland Levillain6d729a72017-06-30 18:34:01 +01001696static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
1697 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001698 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001700 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001703 GenerateConditionLong(cond, codegen);
1704 return;
1705 }
1706
1707 IfCondition condition = cond->GetCondition();
1708 vixl32::Register in = InputRegisterAt(cond, 0);
1709 const vixl32::Register out = OutputRegister(cond);
1710 const Location right = cond->GetLocations()->InAt(1);
1711 int64_t value;
1712
1713 if (right.IsConstant()) {
1714 IfCondition opposite = cond->GetOppositeCondition();
1715
1716 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
1717
1718 // Comparisons against 0 are common enough to deserve special attention.
1719 if (value == 0) {
1720 switch (condition) {
1721 case kCondNE:
1722 case kCondA:
1723 if (out.IsLow() && out.Is(in)) {
1724 __ Cmp(out, 0);
1725
1726 // We use the scope because of the IT block that follows.
1727 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1728 2 * vixl32::k16BitT32InstructionSizeInBytes,
1729 CodeBufferCheckScope::kExactSize);
1730
1731 __ it(ne);
1732 __ mov(ne, out, 1);
1733 return;
1734 }
1735
1736 FALLTHROUGH_INTENDED;
1737 case kCondEQ:
1738 case kCondBE:
1739 case kCondLT:
1740 case kCondGE:
1741 case kCondAE:
1742 case kCondB:
1743 codegen->GenerateConditionWithZero(condition, out, in);
1744 return;
1745 case kCondLE:
1746 case kCondGT:
1747 default:
1748 break;
1749 }
1750 }
1751 }
1752
1753 if (condition == kCondEQ || condition == kCondNE) {
1754 Operand operand(0);
1755
1756 if (right.IsConstant()) {
1757 operand = Operand::From(value);
1758 } else if (out.Is(RegisterFrom(right))) {
1759 // Avoid 32-bit instructions if possible.
1760 operand = InputOperandAt(cond, 0);
1761 in = RegisterFrom(right);
1762 } else {
1763 operand = InputOperandAt(cond, 1);
1764 }
1765
1766 if (condition == kCondNE && out.IsLow()) {
1767 __ Subs(out, in, operand);
1768
1769 // We use the scope because of the IT block that follows.
1770 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1771 2 * vixl32::k16BitT32InstructionSizeInBytes,
1772 CodeBufferCheckScope::kExactSize);
1773
1774 __ it(ne);
1775 __ mov(ne, out, 1);
1776 } else {
1777 __ Sub(out, in, operand);
1778 codegen->GenerateConditionWithZero(condition, out, out);
1779 }
1780
1781 return;
1782 }
1783
1784 GenerateConditionGeneric(cond, codegen);
1785}
1786
Donghui Bai426b49c2016-11-08 14:55:38 +08001787static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001788 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001789 bool ret = false;
1790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001791 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001793 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001794 const uint64_t value = Uint64ConstantFrom(constant);
1795
1796 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1797 } else {
1798 ret = IsUint<8>(Int32ConstantFrom(constant));
1799 }
1800
1801 return ret;
1802}
1803
1804static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001805 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001806
1807 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1808 return Location::ConstantLocation(constant->AsConstant());
1809 }
1810
1811 return Location::RequiresRegister();
1812}
1813
1814static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1815 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1816 // we check that we are not dealing with floating-point output (there is no
1817 // 16-bit VMOV encoding).
1818 if (!out.IsRegister() && !out.IsRegisterPair()) {
1819 return false;
1820 }
1821
1822 // For constants, we also check that the output is in one or two low registers,
1823 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1824 // MOV encoding can be used.
1825 if (src.IsConstant()) {
1826 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1827 return false;
1828 }
1829
1830 if (out.IsRegister()) {
1831 if (!RegisterFrom(out).IsLow()) {
1832 return false;
1833 }
1834 } else {
1835 DCHECK(out.IsRegisterPair());
1836
1837 if (!HighRegisterFrom(out).IsLow()) {
1838 return false;
1839 }
1840 }
1841 }
1842
1843 return true;
1844}
1845
Scott Wakelingfe885462016-09-22 10:24:38 +01001846#undef __
1847
Donghui Bai426b49c2016-11-08 14:55:38 +08001848vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1849 vixl32::Label* final_label) {
1850 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001851 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001852
1853 const HBasicBlock* const block = instruction->GetBlock();
1854 const HLoopInformation* const info = block->GetLoopInformation();
1855 HInstruction* const next = instruction->GetNext();
1856
1857 // Avoid a branch to a branch.
1858 if (next->IsGoto() && (info == nullptr ||
1859 !info->IsBackEdge(*block) ||
1860 !info->HasSuspendCheck())) {
1861 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1862 }
1863
1864 return final_label;
1865}
1866
Scott Wakelingfe885462016-09-22 10:24:38 +01001867CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +01001868 const CompilerOptions& compiler_options,
1869 OptimizingCompilerStats* stats)
1870 : CodeGenerator(graph,
1871 kNumberOfCoreRegisters,
1872 kNumberOfSRegisters,
1873 kNumberOfRegisterPairs,
1874 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001875 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001876 compiler_options,
1877 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001878 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1879 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001880 location_builder_(graph, this),
1881 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001882 move_resolver_(graph->GetAllocator(), this),
1883 assembler_(graph->GetAllocator()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001884 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001885 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001886 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001887 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001888 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1889 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001890 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001891 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001892 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001893 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001894 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001895 uint32_literals_(std::less<uint32_t>(),
1896 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001897 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001898 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001899 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001900 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1901 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1902 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001903 // Always save the LR register to mimic Quick.
1904 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00001905 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
1906 // S0-S31, which alias to D0-D15.
1907 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
1908 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01001909}
1910
Artem Serov551b28f2016-10-18 19:11:30 +01001911void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1912 uint32_t num_entries = switch_instr_->GetNumEntries();
1913 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1914
1915 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001916 // underlying code buffer and we have generated a jump table of the right size, using
1917 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001918 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1919 num_entries * sizeof(int32_t),
1920 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001921 // TODO(VIXL): Check that using lower case bind is fine here.
1922 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001923 for (uint32_t i = 0; i < num_entries; i++) {
1924 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1925 }
1926}
1927
1928void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1929 uint32_t num_entries = switch_instr_->GetNumEntries();
1930 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1931
Artem Serov551b28f2016-10-18 19:11:30 +01001932 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1933 for (uint32_t i = 0; i < num_entries; i++) {
1934 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1935 DCHECK(target_label->IsBound());
1936 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1937 // When doing BX to address we need to have lower bit set to 1 in T32.
1938 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1939 jump_offset++;
1940 }
1941 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1942 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001943
Scott Wakelingb77051e2016-11-21 19:46:00 +00001944 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001945 }
1946}
1947
Artem Serov09a940d2016-11-11 16:15:11 +00001948void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001949 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001950 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001951 }
1952}
1953
Andreas Gampeca620d72016-11-08 08:09:33 -08001954#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001955
1956void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001957 FixJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001958
1959 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001960 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001961 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1962 uint32_t encoded_data = entry.first;
1963 vixl::aarch32::Label* slow_path_entry = &entry.second.label;
1964 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001965 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001966 }
1967
Scott Wakelingfe885462016-09-22 10:24:38 +01001968 GetAssembler()->FinalizeCode();
1969 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001970
1971 // Verify Baker read barrier linker patches.
1972 if (kIsDebugBuild) {
1973 ArrayRef<const uint8_t> code = allocator->GetMemory();
1974 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1975 DCHECK(info.label.IsBound());
1976 uint32_t literal_offset = info.label.GetLocation();
1977 DCHECK_ALIGNED(literal_offset, 2u);
1978
1979 auto GetInsn16 = [&code](uint32_t offset) {
1980 DCHECK_ALIGNED(offset, 2u);
1981 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
1982 (static_cast<uint32_t>(code[offset + 1]) << 8);
1983 };
1984 auto GetInsn32 = [=](uint32_t offset) {
1985 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
1986 };
1987
1988 uint32_t encoded_data = info.custom_data;
1989 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1990 // Check that the next instruction matches the expected LDR.
1991 switch (kind) {
1992 case BakerReadBarrierKind::kField: {
1993 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1994 if (width == BakerReadBarrierWidth::kWide) {
1995 DCHECK_GE(code.size() - literal_offset, 8u);
1996 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1997 // LDR (immediate), encoding T3, with correct base_reg.
1998 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1999 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2000 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
2001 } else {
2002 DCHECK_GE(code.size() - literal_offset, 6u);
2003 uint32_t next_insn = GetInsn16(literal_offset + 4u);
2004 // LDR (immediate), encoding T1, with correct base_reg.
2005 CheckValidReg(next_insn & 0x7u); // Check destination register.
2006 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2007 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
2008 }
2009 break;
2010 }
2011 case BakerReadBarrierKind::kArray: {
2012 DCHECK_GE(code.size() - literal_offset, 8u);
2013 uint32_t next_insn = GetInsn32(literal_offset + 4u);
2014 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
2015 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
2016 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2017 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
2018 CheckValidReg(next_insn & 0xf); // Check index register
2019 break;
2020 }
2021 case BakerReadBarrierKind::kGcRoot: {
2022 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
2023 if (width == BakerReadBarrierWidth::kWide) {
2024 DCHECK_GE(literal_offset, 4u);
2025 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Markof28be432018-08-14 12:20:51 +00002026 // LDR (immediate), encoding T3, with correct root_reg.
Vladimir Markoca1e0382018-04-11 09:58:41 +00002027 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markof28be432018-08-14 12:20:51 +00002028 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
Vladimir Markoca1e0382018-04-11 09:58:41 +00002029 } else {
2030 DCHECK_GE(literal_offset, 2u);
2031 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00002032 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko3d350a82020-11-18 14:14:27 +00002033 // Usually LDR (immediate), encoding T1, with correct root_reg but we may have
2034 // a `MOV marked, old_value` for intrinsic CAS where `marked` is a low register.
2035 if ((prev_insn & 0xff87u) != (0x4600 | root_reg)) {
2036 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2037 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002038 }
2039 break;
2040 }
Vladimir Marko3d350a82020-11-18 14:14:27 +00002041 case BakerReadBarrierKind::kIntrinsicCas: {
Vladimir Markod887ed82018-08-14 13:52:12 +00002042 DCHECK_GE(literal_offset, 4u);
2043 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Marko3d350a82020-11-18 14:14:27 +00002044 // MOV (register), encoding T3, with correct root_reg.
Vladimir Markod887ed82018-08-14 13:52:12 +00002045 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko3d350a82020-11-18 14:14:27 +00002046 DCHECK_GE(root_reg, 8u); // Used only for high registers.
2047 CHECK_EQ(prev_insn & 0xfffffff0u, 0xea4f0000u | (root_reg << 8));
Vladimir Markod887ed82018-08-14 13:52:12 +00002048 break;
2049 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002050 default:
2051 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2052 UNREACHABLE();
2053 }
2054 }
2055 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002056}
2057
2058void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002059 // Stack register, LR and PC are always reserved.
2060 blocked_core_registers_[SP] = true;
2061 blocked_core_registers_[LR] = true;
2062 blocked_core_registers_[PC] = true;
2063
Roland Levillain6d729a72017-06-30 18:34:01 +01002064 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2065 // Reserve marking register.
2066 blocked_core_registers_[MR] = true;
2067 }
2068
Scott Wakelingfe885462016-09-22 10:24:38 +01002069 // Reserve thread register.
2070 blocked_core_registers_[TR] = true;
2071
2072 // Reserve temp register.
2073 blocked_core_registers_[IP] = true;
2074
2075 if (GetGraph()->IsDebuggable()) {
2076 // Stubs do not save callee-save floating point registers. If the graph
2077 // is debuggable, we need to deal with these registers differently. For
2078 // now, just block them.
2079 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2080 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2081 ++i) {
2082 blocked_fpu_registers_[i] = true;
2083 }
2084 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002085}
2086
Scott Wakelingfe885462016-09-22 10:24:38 +01002087InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2088 CodeGeneratorARMVIXL* codegen)
2089 : InstructionCodeGenerator(graph, codegen),
2090 assembler_(codegen->GetAssembler()),
2091 codegen_(codegen) {}
2092
2093void CodeGeneratorARMVIXL::ComputeSpillMask() {
2094 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Vladimir Marko460f0542019-07-04 14:02:08 +01002095 DCHECK_NE(core_spill_mask_ & (1u << kLrCode), 0u)
2096 << "At least the return address register must be saved";
2097 // 16-bit PUSH/POP (T1) can save/restore just the LR/PC.
2098 DCHECK(GetVIXLAssembler()->IsUsingT32());
Scott Wakelingfe885462016-09-22 10:24:38 +01002099 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2100 // We use vpush and vpop for saving and restoring floating point registers, which take
2101 // a SRegister and the number of registers to save/restore after that SRegister. We
2102 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2103 // but in the range.
2104 if (fpu_spill_mask_ != 0) {
2105 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2106 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2107 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2108 fpu_spill_mask_ |= (1 << i);
2109 }
2110 }
2111}
2112
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002113void CodeGeneratorARMVIXL::MaybeIncrementHotness(bool is_frame_entry) {
2114 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2115 UseScratchRegisterScope temps(GetVIXLAssembler());
2116 vixl32::Register temp = temps.Acquire();
2117 static_assert(ArtMethod::MaxCounter() == 0xFFFF, "asm is probably wrong");
2118 if (!is_frame_entry) {
2119 __ Push(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002120 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002121 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2122 }
2123 // Load with zero extend to clear the high bits for integer overflow check.
2124 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2125 __ Add(temp, temp, 1);
2126 // Subtract one if the counter would overflow.
2127 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
2128 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2129 if (!is_frame_entry) {
2130 __ Pop(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002131 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002132 }
2133 }
2134
2135 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002136 ScopedProfilingInfoUse spiu(
2137 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2138 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002139 if (info != nullptr) {
2140 uint32_t address = reinterpret_cast32<uint32_t>(info);
2141 vixl::aarch32::Label done;
2142 UseScratchRegisterScope temps(GetVIXLAssembler());
2143 temps.Exclude(ip);
2144 if (!is_frame_entry) {
2145 __ Push(r4); // Will be used as temporary. For frame entry, r4 is always available.
Vladimir Markodec78172020-06-19 15:31:23 +01002146 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002147 }
2148 __ Mov(r4, address);
2149 __ Ldrh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2150 __ Add(ip, ip, 1);
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01002151 instruction_visitor_.GenerateAndConst(ip, ip, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002152 __ Strh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2153 if (!is_frame_entry) {
2154 __ Pop(r4);
Vladimir Markodec78172020-06-19 15:31:23 +01002155 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002156 }
2157 __ Lsls(ip, ip, 16);
2158 __ B(ne, &done);
2159 uint32_t entry_point_offset =
2160 GetThreadOffset<kArmPointerSize>(kQuickCompileOptimized).Int32Value();
2161 if (HasEmptyFrame()) {
2162 CHECK(is_frame_entry);
2163 // For leaf methods, we need to spill lr and r0. Also spill r1 and r2 for
2164 // alignment.
2165 uint32_t core_spill_mask =
2166 (1 << lr.GetCode()) | (1 << r0.GetCode()) | (1 << r1.GetCode()) | (1 << r2.GetCode());
2167 __ Push(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002168 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002169 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2170 __ Blx(lr);
2171 __ Pop(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002172 GetAssembler()->cfi().AdjustCFAOffset(
2173 -static_cast<int>(kArmWordSize) * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002174 } else {
2175 if (!RequiresCurrentMethod()) {
2176 CHECK(is_frame_entry);
2177 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2178 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002179 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2180 __ Blx(lr);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002181 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002182 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002183 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002184 }
2185}
2186
Scott Wakelingfe885462016-09-22 10:24:38 +01002187void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2188 bool skip_overflow_check =
2189 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2190 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2191 __ Bind(&frame_entry_label_);
2192
2193 if (HasEmptyFrame()) {
David Srbecky30021842019-02-13 14:19:36 +00002194 // Ensure that the CFI opcode list is not empty.
2195 GetAssembler()->cfi().Nop();
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002196 MaybeIncrementHotness(/* is_frame_entry= */ true);
Scott Wakelingfe885462016-09-22 10:24:38 +01002197 return;
2198 }
2199
Scott Wakelingfe885462016-09-22 10:24:38 +01002200 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002201 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002202 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002203 vixl32::Register temp;
2204 // TODO: Remove this check when R4 is made a callee-save register
2205 // in ART compiled code (b/72801708). Currently we need to make
2206 // sure r4 is not blocked, e.g. in special purpose
2207 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2208 // here.
2209 if (!blocked_core_registers_[R4]) {
2210 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2211 DCHECK(!reg.Is(r4));
2212 }
2213 DCHECK(!kCoreCalleeSaves.Includes(r4));
2214 temp = r4;
2215 } else {
2216 temp = temps.Acquire();
2217 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002218 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002219 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002220 ExactAssemblyScope aas(GetVIXLAssembler(),
2221 vixl32::kMaxInstructionSizeInBytes,
2222 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002223 __ ldr(temp, MemOperand(temp));
2224 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002225 }
2226
Vladimir Marko1a225a72019-07-05 13:37:42 +01002227 uint32_t frame_size = GetFrameSize();
2228 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2229 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2230 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2231 core_spills_offset <= 3u * kArmWordSize) {
2232 // Do a single PUSH for core registers including the method and up to two
2233 // filler registers. Then store the single FP spill if any.
2234 // (The worst case is when the method is not required and we actually
2235 // store 3 extra registers but they are stored in the same properly
2236 // aligned 16-byte chunk where we're already writing anyway.)
2237 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2238 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize);
2239 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(core_spill_mask_));
2240 __ Push(RegisterList(core_spill_mask_ | extra_regs));
2241 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
2242 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2243 core_spills_offset,
2244 core_spill_mask_,
2245 kArmWordSize);
2246 if (fpu_spill_mask_ != 0u) {
2247 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2248 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2249 GetAssembler()->StoreSToOffset(sreg, sp, fp_spills_offset);
2250 GetAssembler()->cfi().RelOffset(DWARFReg(sreg), /*offset=*/ fp_spills_offset);
2251 }
2252 } else {
2253 __ Push(RegisterList(core_spill_mask_));
2254 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2255 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2256 /*offset=*/ 0,
2257 core_spill_mask_,
2258 kArmWordSize);
2259 if (fpu_spill_mask_ != 0) {
2260 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002261
Vladimir Marko1a225a72019-07-05 13:37:42 +01002262 // Check that list is contiguous.
2263 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002264
Vladimir Marko1a225a72019-07-05 13:37:42 +01002265 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2266 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
2267 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0),
2268 /*offset=*/ 0,
2269 fpu_spill_mask_,
2270 kArmWordSize);
2271 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002272
Vladimir Marko1a225a72019-07-05 13:37:42 +01002273 // Adjust SP and save the current method if we need it. Note that we do
2274 // not save the method in HCurrentMethod, as the instruction might have
2275 // been removed in the SSA graph.
2276 if (RequiresCurrentMethod() && fp_spills_offset <= 3 * kArmWordSize) {
2277 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2278 __ Push(RegisterList(MaxInt<uint32_t>(fp_spills_offset / kArmWordSize)));
2279 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2280 } else {
Vladimir Markodec78172020-06-19 15:31:23 +01002281 IncreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002282 if (RequiresCurrentMethod()) {
2283 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2284 }
2285 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002286 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002287
2288 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2289 UseScratchRegisterScope temps(GetVIXLAssembler());
2290 vixl32::Register temp = temps.Acquire();
2291 // Initialize should_deoptimize flag to 0.
2292 __ Mov(temp, 0);
2293 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2294 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002295
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002296 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08002297 MaybeGenerateMarkingRegisterCheck(/* code= */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002298}
2299
2300void CodeGeneratorARMVIXL::GenerateFrameExit() {
2301 if (HasEmptyFrame()) {
2302 __ Bx(lr);
2303 return;
2304 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002305
Scott Wakelingfe885462016-09-22 10:24:38 +01002306 // Pop LR into PC to return.
2307 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2308 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
Vladimir Marko1a225a72019-07-05 13:37:42 +01002309
2310 uint32_t frame_size = GetFrameSize();
2311 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2312 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2313 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2314 // r4 is blocked by TestCodeGeneratorARMVIXL used by some tests.
2315 core_spills_offset <= (blocked_core_registers_[r4.GetCode()] ? 2u : 3u) * kArmWordSize) {
2316 // Load the FP spill if any and then do a single POP including the method
2317 // and up to two filler registers. If we have no FP spills, this also has
2318 // the advantage that we do not need to emit CFI directives.
2319 if (fpu_spill_mask_ != 0u) {
2320 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2321 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2322 GetAssembler()->cfi().RememberState();
2323 GetAssembler()->LoadSFromOffset(sreg, sp, fp_spills_offset);
2324 GetAssembler()->cfi().Restore(DWARFReg(sreg));
2325 }
2326 // Clobber registers r2-r4 as they are caller-save in ART managed ABI and
2327 // never hold the return value.
2328 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize) << r2.GetCode();
2329 DCHECK_EQ(extra_regs & kCoreCalleeSaves.GetList(), 0u);
2330 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(pop_mask));
2331 __ Pop(RegisterList(pop_mask | extra_regs));
2332 if (fpu_spill_mask_ != 0u) {
2333 GetAssembler()->cfi().RestoreState();
2334 }
2335 } else {
2336 GetAssembler()->cfi().RememberState();
Vladimir Markodec78172020-06-19 15:31:23 +01002337 DecreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002338 if (fpu_spill_mask_ != 0) {
2339 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2340
2341 // Check that list is contiguous.
2342 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2343
2344 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2345 GetAssembler()->cfi().AdjustCFAOffset(
2346 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
2347 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
2348 }
2349 __ Pop(RegisterList(pop_mask));
2350 GetAssembler()->cfi().RestoreState();
2351 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2352 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002353}
2354
2355void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2356 __ Bind(GetLabelOf(block));
2357}
2358
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002359Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002360 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002361 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002363 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002364 case DataType::Type::kInt8:
2365 case DataType::Type::kUint16:
2366 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002367 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002368 uint32_t index = gp_index_++;
2369 uint32_t stack_index = stack_index_++;
2370 if (index < calling_convention.GetNumberOfRegisters()) {
2371 return LocationFrom(calling_convention.GetRegisterAt(index));
2372 } else {
2373 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2374 }
2375 }
2376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002378 uint32_t index = gp_index_;
2379 uint32_t stack_index = stack_index_;
2380 gp_index_ += 2;
2381 stack_index_ += 2;
2382 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2383 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2384 // Skip R1, and use R2_R3 instead.
2385 gp_index_++;
2386 index++;
2387 }
2388 }
2389 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2390 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2391 calling_convention.GetRegisterAt(index + 1).GetCode());
2392
2393 return LocationFrom(calling_convention.GetRegisterAt(index),
2394 calling_convention.GetRegisterAt(index + 1));
2395 } else {
2396 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2397 }
2398 }
2399
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002400 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002401 uint32_t stack_index = stack_index_++;
2402 if (float_index_ % 2 == 0) {
2403 float_index_ = std::max(double_index_, float_index_);
2404 }
2405 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2406 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2407 } else {
2408 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2409 }
2410 }
2411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002413 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2414 uint32_t stack_index = stack_index_;
2415 stack_index_ += 2;
2416 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2417 uint32_t index = double_index_;
2418 double_index_ += 2;
2419 Location result = LocationFrom(
2420 calling_convention.GetFpuRegisterAt(index),
2421 calling_convention.GetFpuRegisterAt(index + 1));
2422 DCHECK(ExpectedPairLayout(result));
2423 return result;
2424 } else {
2425 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2426 }
2427 }
2428
Aart Bik66c158e2018-01-31 12:55:04 -08002429 case DataType::Type::kUint32:
2430 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002432 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002433 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002434 }
2435 return Location::NoLocation();
2436}
2437
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002439 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002440 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002442 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 case DataType::Type::kInt8:
2444 case DataType::Type::kUint16:
2445 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002446 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002447 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002448 return LocationFrom(r0);
2449 }
2450
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002451 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002452 return LocationFrom(s0);
2453 }
2454
Aart Bik66c158e2018-01-31 12:55:04 -08002455 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002457 return LocationFrom(r0, r1);
2458 }
2459
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002460 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002461 return LocationFrom(s0, s1);
2462 }
2463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002464 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002465 return Location::NoLocation();
2466 }
2467
2468 UNREACHABLE();
2469}
2470
2471Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2472 return LocationFrom(kMethodRegister);
2473}
2474
Vladimir Marko86c87522020-05-11 16:55:55 +01002475Location CriticalNativeCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
2476 DCHECK_NE(type, DataType::Type::kReference);
2477
2478 // Native ABI uses the same registers as managed, except that the method register r0
2479 // is a normal argument.
2480 Location location = Location::NoLocation();
2481 if (DataType::Is64BitType(type)) {
2482 gpr_index_ = RoundUp(gpr_index_, 2u);
2483 stack_offset_ = RoundUp(stack_offset_, 2 * kFramePointerSize);
2484 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2485 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u],
2486 kParameterCoreRegistersVIXL[gpr_index_]);
2487 gpr_index_ += 2u;
2488 }
2489 } else {
2490 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2491 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u]);
2492 ++gpr_index_;
2493 }
2494 }
2495 if (location.IsInvalid()) {
2496 if (DataType::Is64BitType(type)) {
2497 location = Location::DoubleStackSlot(stack_offset_);
2498 stack_offset_ += 2 * kFramePointerSize;
2499 } else {
2500 location = Location::StackSlot(stack_offset_);
2501 stack_offset_ += kFramePointerSize;
2502 }
2503
2504 if (for_register_allocation_) {
2505 location = Location::Any();
2506 }
2507 }
2508 return location;
2509}
2510
2511Location CriticalNativeCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type)
2512 const {
2513 // We perform conversion to the managed ABI return register after the call if needed.
2514 InvokeDexCallingConventionVisitorARMVIXL dex_calling_convention;
2515 return dex_calling_convention.GetReturnLocation(type);
2516}
2517
2518Location CriticalNativeCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2519 // Pass the method in the hidden argument R4.
2520 return Location::RegisterLocation(R4);
2521}
2522
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002523void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2524 if (source.Equals(destination)) {
2525 return;
2526 }
2527 if (destination.IsRegister()) {
2528 if (source.IsRegister()) {
2529 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2530 } else if (source.IsFpuRegister()) {
2531 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2532 } else {
2533 GetAssembler()->LoadFromOffset(kLoadWord,
2534 RegisterFrom(destination),
2535 sp,
2536 source.GetStackIndex());
2537 }
2538 } else if (destination.IsFpuRegister()) {
2539 if (source.IsRegister()) {
2540 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2541 } else if (source.IsFpuRegister()) {
2542 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2543 } else {
2544 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2545 }
2546 } else {
2547 DCHECK(destination.IsStackSlot()) << destination;
2548 if (source.IsRegister()) {
2549 GetAssembler()->StoreToOffset(kStoreWord,
2550 RegisterFrom(source),
2551 sp,
2552 destination.GetStackIndex());
2553 } else if (source.IsFpuRegister()) {
2554 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2555 } else {
2556 DCHECK(source.IsStackSlot()) << source;
2557 UseScratchRegisterScope temps(GetVIXLAssembler());
2558 vixl32::Register temp = temps.Acquire();
2559 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2560 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2561 }
2562 }
2563}
2564
Artem Serovcfbe9132016-10-14 15:58:56 +01002565void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2566 DCHECK(location.IsRegister());
2567 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002568}
2569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002571 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2572 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002573 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002574 move.AddMove(src, dst, dst_type, nullptr);
2575 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002576}
2577
Artem Serovcfbe9132016-10-14 15:58:56 +01002578void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2579 if (location.IsRegister()) {
2580 locations->AddTemp(location);
2581 } else if (location.IsRegisterPair()) {
2582 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2583 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2584 } else {
2585 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2586 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002587}
2588
2589void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2590 HInstruction* instruction,
2591 uint32_t dex_pc,
2592 SlowPathCode* slow_path) {
2593 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Vladimir Markof6675082019-05-17 12:05:28 +01002594
2595 ThreadOffset32 entrypoint_offset = GetThreadOffset<kArmPointerSize>(entrypoint);
2596 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
2597 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
2598 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01002599 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01002600 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
2601 // Ensure the pc position is recorded immediately after the `blx` instruction.
2602 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
2603 ExactAssemblyScope aas(GetVIXLAssembler(),
2604 vixl32::k16BitT32InstructionSizeInBytes,
2605 CodeBufferCheckScope::kExactSize);
2606 __ blx(lr);
2607 if (EntrypointRequiresStackMap(entrypoint)) {
2608 RecordPcInfo(instruction, dex_pc, slow_path);
2609 }
2610 } else {
2611 // Ensure the pc position is recorded immediately after the `bl` instruction.
2612 ExactAssemblyScope aas(GetVIXLAssembler(),
2613 vixl32::k32BitT32InstructionSizeInBytes,
2614 CodeBufferCheckScope::kExactSize);
2615 EmitEntrypointThunkCall(entrypoint_offset);
2616 if (EntrypointRequiresStackMap(entrypoint)) {
2617 RecordPcInfo(instruction, dex_pc, slow_path);
2618 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002619 }
2620}
2621
2622void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2623 HInstruction* instruction,
2624 SlowPathCode* slow_path) {
2625 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002626 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002627 __ Blx(lr);
2628}
2629
Scott Wakelingfe885462016-09-22 10:24:38 +01002630void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002631 if (successor->IsExitBlock()) {
2632 DCHECK(got->GetPrevious()->AlwaysThrows());
2633 return; // no code needed
2634 }
2635
Scott Wakelingfe885462016-09-22 10:24:38 +01002636 HBasicBlock* block = got->GetBlock();
2637 HInstruction* previous = got->GetPrevious();
2638 HLoopInformation* info = block->GetLoopInformation();
2639
2640 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002641 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Scott Wakelingfe885462016-09-22 10:24:38 +01002642 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2643 return;
2644 }
2645 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2646 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08002647 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002648 }
2649 if (!codegen_->GoesToNextBlock(block, successor)) {
2650 __ B(codegen_->GetLabelOf(successor));
2651 }
2652}
2653
2654void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2655 got->SetLocations(nullptr);
2656}
2657
2658void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2659 HandleGoto(got, got->GetSuccessor());
2660}
2661
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002662void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2663 try_boundary->SetLocations(nullptr);
2664}
2665
2666void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2667 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2668 if (!successor->IsExitBlock()) {
2669 HandleGoto(try_boundary, successor);
2670 }
2671}
2672
Scott Wakelingfe885462016-09-22 10:24:38 +01002673void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2674 exit->SetLocations(nullptr);
2675}
2676
2677void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2678}
2679
Scott Wakelingfe885462016-09-22 10:24:38 +01002680void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002681 vixl32::Label* true_target,
2682 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002683 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002684 if (true_target == false_target) {
2685 DCHECK(true_target != nullptr);
2686 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002687 return;
2688 }
2689
Anton Kirilov23b752b2017-07-20 14:40:44 +01002690 vixl32::Label* non_fallthrough_target;
2691 bool invert;
2692 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002693
Anton Kirilov23b752b2017-07-20 14:40:44 +01002694 if (true_target == nullptr) {
2695 // The true target is fallthrough.
2696 DCHECK(false_target != nullptr);
2697 non_fallthrough_target = false_target;
2698 invert = true;
2699 emit_both_branches = false;
2700 } else {
2701 non_fallthrough_target = true_target;
2702 invert = false;
2703 // Either the false target is fallthrough, or there is no fallthrough
2704 // and both branches must be emitted.
2705 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002706 }
2707
Anton Kirilov23b752b2017-07-20 14:40:44 +01002708 const auto cond = GenerateTest(condition, invert, codegen_);
2709
2710 __ B(cond.first, non_fallthrough_target, is_far_target);
2711
2712 if (emit_both_branches) {
2713 // No target falls through, we need to branch.
2714 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002715 }
2716}
2717
2718void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2719 size_t condition_input_index,
2720 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002721 vixl32::Label* false_target,
2722 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002723 HInstruction* cond = instruction->InputAt(condition_input_index);
2724
2725 if (true_target == nullptr && false_target == nullptr) {
2726 // Nothing to do. The code always falls through.
2727 return;
2728 } else if (cond->IsIntConstant()) {
2729 // Constant condition, statically compared against "true" (integer value 1).
2730 if (cond->AsIntConstant()->IsTrue()) {
2731 if (true_target != nullptr) {
2732 __ B(true_target);
2733 }
2734 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002735 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002736 if (false_target != nullptr) {
2737 __ B(false_target);
2738 }
2739 }
2740 return;
2741 }
2742
2743 // The following code generates these patterns:
2744 // (1) true_target == nullptr && false_target != nullptr
2745 // - opposite condition true => branch to false_target
2746 // (2) true_target != nullptr && false_target == nullptr
2747 // - condition true => branch to true_target
2748 // (3) true_target != nullptr && false_target != nullptr
2749 // - condition true => branch to true_target
2750 // - branch to false_target
2751 if (IsBooleanValueOrMaterializedCondition(cond)) {
2752 // Condition has been materialized, compare the output to 0.
2753 if (kIsDebugBuild) {
2754 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2755 DCHECK(cond_val.IsRegister());
2756 }
2757 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002758 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2759 false_target,
2760 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002761 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002762 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2763 true_target,
2764 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002765 }
2766 } else {
2767 // Condition has not been materialized. Use its inputs as the comparison and
2768 // its condition as the branch condition.
2769 HCondition* condition = cond->AsCondition();
2770
2771 // If this is a long or FP comparison that has been folded into
2772 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 DataType::Type type = condition->InputAt(0)->GetType();
2774 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002775 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002776 return;
2777 }
2778
Donghui Bai426b49c2016-11-08 14:55:38 +08002779 vixl32::Label* non_fallthrough_target;
2780 vixl32::Condition arm_cond = vixl32::Condition::None();
2781 const vixl32::Register left = InputRegisterAt(cond, 0);
2782 const Operand right = InputOperandAt(cond, 1);
2783
Scott Wakelingfe885462016-09-22 10:24:38 +01002784 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002785 arm_cond = ARMCondition(condition->GetOppositeCondition());
2786 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002787 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002788 arm_cond = ARMCondition(condition->GetCondition());
2789 non_fallthrough_target = true_target;
2790 }
2791
2792 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2793 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002794 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002795 } else {
2796 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002797 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002798 }
2799 } else {
2800 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002801 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002802 }
2803 }
2804
2805 // If neither branch falls through (case 3), the conditional branch to `true_target`
2806 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2807 if (true_target != nullptr && false_target != nullptr) {
2808 __ B(false_target);
2809 }
2810}
2811
2812void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002813 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002814 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2815 locations->SetInAt(0, Location::RequiresRegister());
2816 }
2817}
2818
2819void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2820 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2821 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002822 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2823 nullptr : codegen_->GetLabelOf(true_successor);
2824 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2825 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002826 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002827}
2828
Scott Wakelingc34dba72016-10-03 10:14:44 +01002829void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002830 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002831 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002832 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2833 RegisterSet caller_saves = RegisterSet::Empty();
2834 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2835 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002836 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2837 locations->SetInAt(0, Location::RequiresRegister());
2838 }
2839}
2840
2841void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2842 SlowPathCodeARMVIXL* slow_path =
2843 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2844 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002845 /* condition_input_index= */ 0,
Scott Wakelingc34dba72016-10-03 10:14:44 +01002846 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002847 /* false_target= */ nullptr);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002848}
2849
Artem Serovd4cc5b22016-11-04 11:19:09 +00002850void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002851 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002852 LocationSummary(flag, LocationSummary::kNoCall);
2853 locations->SetOut(Location::RequiresRegister());
2854}
2855
2856void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2857 GetAssembler()->LoadFromOffset(kLoadWord,
2858 OutputRegister(flag),
2859 sp,
2860 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2861}
2862
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002863void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002864 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002866
2867 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002868 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002869 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002870 } else {
2871 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002872 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002873 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002874
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002875 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002876 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2877 // The code generator handles overlap with the values, but not with the condition.
2878 locations->SetOut(Location::SameAsFirstInput());
2879 } else if (is_floating_point) {
2880 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2881 } else {
2882 if (!locations->InAt(1).IsConstant()) {
2883 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2884 }
2885
2886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002887 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002888}
2889
2890void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002891 HInstruction* const condition = select->GetCondition();
2892 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002894 const Location first = locations->InAt(0);
2895 const Location out = locations->Out();
2896 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002897
2898 // In the unlucky case the output of this instruction overlaps
2899 // with an input of an "emitted-at-use-site" condition, and
2900 // the output of this instruction is not one of its inputs, we'll
2901 // need to fallback to branches instead of conditional ARM instructions.
2902 bool output_overlaps_with_condition_inputs =
2903 !IsBooleanValueOrMaterializedCondition(condition) &&
2904 !out.Equals(first) &&
2905 !out.Equals(second) &&
2906 (condition->GetLocations()->InAt(0).Equals(out) ||
2907 condition->GetLocations()->InAt(1).Equals(out));
2908 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002909 Location src;
2910
2911 if (condition->IsIntConstant()) {
2912 if (condition->AsIntConstant()->IsFalse()) {
2913 src = first;
2914 } else {
2915 src = second;
2916 }
2917
2918 codegen_->MoveLocation(out, src, type);
2919 return;
2920 }
2921
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002922 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002923 bool invert = false;
2924
2925 if (out.Equals(second)) {
2926 src = first;
2927 invert = true;
2928 } else if (out.Equals(first)) {
2929 src = second;
2930 } else if (second.IsConstant()) {
2931 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2932 src = second;
2933 } else if (first.IsConstant()) {
2934 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2935 src = first;
2936 invert = true;
2937 } else {
2938 src = second;
2939 }
2940
2941 if (CanGenerateConditionalMove(out, src)) {
2942 if (!out.Equals(first) && !out.Equals(second)) {
2943 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2944 }
2945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002946 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2947
2948 if (IsBooleanValueOrMaterializedCondition(condition)) {
2949 __ Cmp(InputRegisterAt(select, 2), 0);
2950 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2951 } else {
2952 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2953 }
2954
Donghui Bai426b49c2016-11-08 14:55:38 +08002955 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002956 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002957 ExactAssemblyScope guard(GetVIXLAssembler(),
2958 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2959 CodeBufferCheckScope::kExactSize);
2960
2961 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002962 __ it(cond.first);
2963 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002964 } else {
2965 DCHECK(out.IsRegisterPair());
2966
2967 Operand operand_high(0);
2968 Operand operand_low(0);
2969
2970 if (src.IsConstant()) {
2971 const int64_t value = Int64ConstantFrom(src);
2972
2973 operand_high = High32Bits(value);
2974 operand_low = Low32Bits(value);
2975 } else {
2976 DCHECK(src.IsRegisterPair());
2977 operand_high = HighRegisterFrom(src);
2978 operand_low = LowRegisterFrom(src);
2979 }
2980
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002981 __ it(cond.first);
2982 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2983 __ it(cond.first);
2984 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002985 }
2986
2987 return;
2988 }
2989 }
2990
2991 vixl32::Label* false_target = nullptr;
2992 vixl32::Label* true_target = nullptr;
2993 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002994 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08002995 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2996
2997 if (out.Equals(second)) {
2998 true_target = target;
2999 src = first;
3000 } else {
3001 false_target = target;
3002 src = second;
3003
3004 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003005 if (output_overlaps_with_condition_inputs) {
3006 false_target = &other_case;
3007 } else {
3008 codegen_->MoveLocation(out, first, type);
3009 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003010 }
3011 }
3012
Andreas Gampe3db70682018-12-26 15:12:03 -08003013 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target= */ false);
Donghui Bai426b49c2016-11-08 14:55:38 +08003014 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003015 if (output_overlaps_with_condition_inputs) {
3016 __ B(target);
3017 __ Bind(&other_case);
3018 codegen_->MoveLocation(out, first, type);
3019 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003020
3021 if (select_end.IsReferenced()) {
3022 __ Bind(&select_end);
3023 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003024}
3025
Artem Serov551b28f2016-10-18 19:11:30 +01003026void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003027 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003028}
3029
3030void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3031 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3032}
3033
Vladimir Markodec78172020-06-19 15:31:23 +01003034void CodeGeneratorARMVIXL::IncreaseFrame(size_t adjustment) {
3035 __ Claim(adjustment);
3036 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3037}
3038
3039void CodeGeneratorARMVIXL::DecreaseFrame(size_t adjustment) {
3040 __ Drop(adjustment);
3041 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3042}
3043
Scott Wakelingfe885462016-09-22 10:24:38 +01003044void CodeGeneratorARMVIXL::GenerateNop() {
3045 __ Nop();
3046}
3047
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003048// `temp` is an extra temporary register that is used for some conditions;
3049// callers may not specify it, in which case the method will use a scratch
3050// register instead.
3051void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3052 vixl32::Register out,
3053 vixl32::Register in,
3054 vixl32::Register temp) {
3055 switch (condition) {
3056 case kCondEQ:
3057 // x <= 0 iff x == 0 when the comparison is unsigned.
3058 case kCondBE:
3059 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3060 temp = out;
3061 }
3062
3063 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3064 // different as well.
3065 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3066 // temp = - in; only 0 sets the carry flag.
3067 __ Rsbs(temp, in, 0);
3068
3069 if (out.Is(in)) {
3070 std::swap(in, temp);
3071 }
3072
3073 // out = - in + in + carry = carry
3074 __ Adc(out, temp, in);
3075 } else {
3076 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3077 __ Clz(out, in);
3078 // Any number less than 32 logically shifted right by 5 bits results in 0;
3079 // the same operation on 32 yields 1.
3080 __ Lsr(out, out, 5);
3081 }
3082
3083 break;
3084 case kCondNE:
3085 // x > 0 iff x != 0 when the comparison is unsigned.
3086 case kCondA: {
3087 UseScratchRegisterScope temps(GetVIXLAssembler());
3088
3089 if (out.Is(in)) {
3090 if (!temp.IsValid() || in.Is(temp)) {
3091 temp = temps.Acquire();
3092 }
3093 } else if (!temp.IsValid() || !temp.IsLow()) {
3094 temp = out;
3095 }
3096
3097 // temp = in - 1; only 0 does not set the carry flag.
3098 __ Subs(temp, in, 1);
3099 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3100 __ Sbc(out, in, temp);
3101 break;
3102 }
3103 case kCondGE:
3104 __ Mvn(out, in);
3105 in = out;
3106 FALLTHROUGH_INTENDED;
3107 case kCondLT:
3108 // We only care about the sign bit.
3109 __ Lsr(out, in, 31);
3110 break;
3111 case kCondAE:
3112 // Trivially true.
3113 __ Mov(out, 1);
3114 break;
3115 case kCondB:
3116 // Trivially false.
3117 __ Mov(out, 0);
3118 break;
3119 default:
3120 LOG(FATAL) << "Unexpected condition " << condition;
3121 UNREACHABLE();
3122 }
3123}
3124
Scott Wakelingfe885462016-09-22 10:24:38 +01003125void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3126 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003127 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003128 const DataType::Type type = cond->InputAt(0)->GetType();
3129 if (DataType::IsFloatingPointType(type)) {
3130 locations->SetInAt(0, Location::RequiresFpuRegister());
3131 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
3132 } else {
3133 locations->SetInAt(0, Location::RequiresRegister());
3134 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3135 }
3136 if (!cond->IsEmittedAtUseSite()) {
3137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003138 }
3139}
3140
3141void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3142 if (cond->IsEmittedAtUseSite()) {
3143 return;
3144 }
3145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003149 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003150 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003151 }
3152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003154
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003155 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003156
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003157 // A condition with only one boolean input, or two boolean inputs without being equality or
3158 // inequality results from transformations done by the instruction simplifier, and is handled
3159 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 if (type == DataType::Type::kBool &&
3161 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003162 (condition == kCondEQ || condition == kCondNE)) {
3163 vixl32::Register left = InputRegisterAt(cond, 0);
3164 const vixl32::Register out = OutputRegister(cond);
3165 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003166
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003167 // The constant case is handled by the instruction simplifier.
3168 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003169
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003170 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003171
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003172 // Avoid 32-bit instructions if possible.
3173 if (out.Is(right)) {
3174 std::swap(left, right);
3175 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003176
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003177 __ Eor(out, left, right);
3178
3179 if (condition == kCondEQ) {
3180 __ Eor(out, out, 1);
3181 }
3182
3183 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003184 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003185
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003186 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003187}
3188
3189void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3190 HandleCondition(comp);
3191}
3192
3193void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3194 HandleCondition(comp);
3195}
3196
3197void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3198 HandleCondition(comp);
3199}
3200
3201void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3202 HandleCondition(comp);
3203}
3204
3205void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3206 HandleCondition(comp);
3207}
3208
3209void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3210 HandleCondition(comp);
3211}
3212
3213void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3214 HandleCondition(comp);
3215}
3216
3217void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3218 HandleCondition(comp);
3219}
3220
3221void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3222 HandleCondition(comp);
3223}
3224
3225void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3226 HandleCondition(comp);
3227}
3228
3229void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3230 HandleCondition(comp);
3231}
3232
3233void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3234 HandleCondition(comp);
3235}
3236
3237void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3238 HandleCondition(comp);
3239}
3240
3241void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3242 HandleCondition(comp);
3243}
3244
3245void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3246 HandleCondition(comp);
3247}
3248
3249void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3250 HandleCondition(comp);
3251}
3252
3253void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3254 HandleCondition(comp);
3255}
3256
3257void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3258 HandleCondition(comp);
3259}
3260
3261void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3262 HandleCondition(comp);
3263}
3264
3265void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3266 HandleCondition(comp);
3267}
3268
3269void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3270 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003271 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003272 locations->SetOut(Location::ConstantLocation(constant));
3273}
3274
3275void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3276 // Will be generated at use site.
3277}
3278
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003279void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3280 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003281 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003282 locations->SetOut(Location::ConstantLocation(constant));
3283}
3284
3285void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3286 // Will be generated at use site.
3287}
3288
Scott Wakelingfe885462016-09-22 10:24:38 +01003289void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3290 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003291 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003292 locations->SetOut(Location::ConstantLocation(constant));
3293}
3294
3295void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3296 // Will be generated at use site.
3297}
3298
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003299void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3300 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003301 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003302 locations->SetOut(Location::ConstantLocation(constant));
3303}
3304
Scott Wakelingc34dba72016-10-03 10:14:44 +01003305void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3306 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003307 // Will be generated at use site.
3308}
3309
3310void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3311 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003312 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003313 locations->SetOut(Location::ConstantLocation(constant));
3314}
3315
Scott Wakelingc34dba72016-10-03 10:14:44 +01003316void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3317 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003318 // Will be generated at use site.
3319}
3320
Igor Murashkind01745e2017-04-05 16:40:31 -07003321void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3322 constructor_fence->SetLocations(nullptr);
3323}
3324
3325void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3326 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3327 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3328}
3329
Scott Wakelingfe885462016-09-22 10:24:38 +01003330void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3331 memory_barrier->SetLocations(nullptr);
3332}
3333
3334void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3335 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3336}
3337
3338void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3339 ret->SetLocations(nullptr);
3340}
3341
3342void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3343 codegen_->GenerateFrameExit();
3344}
3345
3346void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3347 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003348 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003349 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3350}
3351
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00003352void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret) {
3353 if (GetGraph()->IsCompilingOsr()) {
3354 // To simplify callers of an OSR method, we put the return value in both
3355 // floating point and core registers.
3356 switch (ret->InputAt(0)->GetType()) {
3357 case DataType::Type::kFloat32:
3358 __ Vmov(r0, s0);
3359 break;
3360 case DataType::Type::kFloat64:
3361 __ Vmov(r0, r1, d0);
3362 break;
3363 default:
3364 break;
3365 }
3366 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003367 codegen_->GenerateFrameExit();
3368}
3369
Artem Serovcfbe9132016-10-14 15:58:56 +01003370void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3371 // The trampoline uses the same calling convention as dex calling conventions,
3372 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3373 // the method_idx.
3374 HandleInvoke(invoke);
3375}
3376
3377void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3378 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003379 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003380}
3381
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003382void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3383 // Explicit clinit checks triggered by static invokes must have been pruned by
3384 // art::PrepareForRegisterAllocation.
3385 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3386
Anton Kirilov5ec62182016-10-13 20:16:02 +01003387 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3388 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003389 return;
3390 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003391
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01003392 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01003393 CriticalNativeCallingConventionVisitorARMVIXL calling_convention_visitor(
3394 /*for_register_allocation=*/ true);
3395 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3396 } else {
3397 HandleInvoke(invoke);
3398 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003399}
3400
Anton Kirilov5ec62182016-10-13 20:16:02 +01003401static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3402 if (invoke->GetLocations()->Intrinsified()) {
3403 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3404 intrinsic.Dispatch(invoke);
3405 return true;
3406 }
3407 return false;
3408}
3409
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003410void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3411 // Explicit clinit checks triggered by static invokes must have been pruned by
3412 // art::PrepareForRegisterAllocation.
3413 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3414
Anton Kirilov5ec62182016-10-13 20:16:02 +01003415 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003416 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003417 return;
3418 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003419
3420 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003421 codegen_->GenerateStaticOrDirectCall(
3422 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003423
Andreas Gampe3db70682018-12-26 15:12:03 -08003424 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003425}
3426
3427void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003428 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003429 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3430}
3431
3432void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003433 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3434 if (intrinsic.TryDispatch(invoke)) {
3435 return;
3436 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003437
3438 HandleInvoke(invoke);
3439}
3440
3441void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003442 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003443 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003444 return;
3445 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003446
3447 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003448 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003449
Andreas Gampe3db70682018-12-26 15:12:03 -08003450 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003451}
3452
Artem Serovcfbe9132016-10-14 15:58:56 +01003453void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3454 HandleInvoke(invoke);
3455 // Add the hidden argument.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003456 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
3457 // We cannot request r12 as it's blocked by the register allocator.
3458 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
3459 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003460}
3461
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003462void CodeGeneratorARMVIXL::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
3463 vixl32::Register klass) {
3464 DCHECK_EQ(r0.GetCode(), klass.GetCode());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003465 // We know the destination of an intrinsic, so no need to record inline
3466 // caches.
3467 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00003468 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003469 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003470 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01003471 ScopedProfilingInfoUse spiu(
3472 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
3473 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00003474 if (info != nullptr) {
3475 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
3476 uint32_t address = reinterpret_cast32<uint32_t>(cache);
3477 vixl32::Label done;
3478 UseScratchRegisterScope temps(GetVIXLAssembler());
3479 temps.Exclude(ip);
3480 __ Mov(r4, address);
3481 __ Ldr(ip, MemOperand(r4, InlineCache::ClassesOffset().Int32Value()));
3482 // Fast path for a monomorphic cache.
3483 __ Cmp(klass, ip);
3484 __ B(eq, &done, /* is_far_target= */ false);
3485 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
3486 __ Bind(&done);
3487 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003488 }
3489}
3490
Artem Serovcfbe9132016-10-14 15:58:56 +01003491void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3492 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3493 LocationSummary* locations = invoke->GetLocations();
3494 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Artem Serovcfbe9132016-10-14 15:58:56 +01003495 Location receiver = locations->InAt(0);
3496 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3497
3498 DCHECK(!receiver.IsStackSlot());
3499
Alexandre Rames374ddf32016-11-04 10:40:49 +00003500 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3501 {
Artem Serov0fb37192016-12-06 18:13:40 +00003502 ExactAssemblyScope aas(GetVIXLAssembler(),
3503 vixl32::kMaxInstructionSizeInBytes,
3504 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003505 // /* HeapReference<Class> */ temp = receiver->klass_
3506 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3507 codegen_->MaybeRecordImplicitNullCheck(invoke);
3508 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003509 // Instead of simply (possibly) unpoisoning `temp` here, we should
3510 // emit a read barrier for the previous class reference load.
3511 // However this is not required in practice, as this is an
3512 // intermediate/temporary reference and because the current
3513 // concurrent copying collector keeps the from-space memory
3514 // intact/accessible until the end of the marking phase (the
3515 // concurrent copying collector may not in the future).
3516 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003517
3518 // If we're compiling baseline, update the inline cache.
3519 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
3520
Artem Serovcfbe9132016-10-14 15:58:56 +01003521 GetAssembler()->LoadFromOffset(kLoadWord,
3522 temp,
3523 temp,
3524 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003525
Artem Serovcfbe9132016-10-14 15:58:56 +01003526 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3527 invoke->GetImtIndex(), kArmPointerSize));
3528 // temp = temp->GetImtEntryAt(method_offset);
3529 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3530 uint32_t entry_point =
3531 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3532 // LR = temp->GetEntryPoint();
3533 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3534
Scott Wakelingb77051e2016-11-21 19:46:00 +00003535 {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003536 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3537 // instruction from clobbering it as they might use r12 as a scratch register.
3538 Location hidden_reg = Location::RegisterLocation(r12.GetCode());
Scott Wakelingb77051e2016-11-21 19:46:00 +00003539 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3540 // so it checks if the application is using them (by passing them to the macro assembler
3541 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3542 // what is available, and is the opposite of the standard usage: Instead of requesting a
3543 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3544 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3545 // (to materialize the constant), since the destination register becomes available for such use
3546 // internally for the duration of the macro instruction.
3547 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003548 temps.Exclude(RegisterFrom(hidden_reg));
3549 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
3550 Location current_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
3551 if (current_method.IsStackSlot()) {
3552 GetAssembler()->LoadFromOffset(
3553 kLoadWord, RegisterFrom(hidden_reg), sp, current_method.GetStackIndex());
3554 } else {
3555 __ Mov(RegisterFrom(hidden_reg), RegisterFrom(current_method));
3556 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00003557 } else if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
3558 // We pass the method from the IMT in case of a conflict. This will ensure
3559 // we go into the runtime to resolve the actual method.
3560 CHECK_NE(temp.GetCode(), lr.GetCode());
3561 __ Mov(RegisterFrom(hidden_reg), temp);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003562 } else {
3563 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), hidden_reg, invoke);
3564 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00003565 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003566 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003567 // Ensure the pc position is recorded immediately after the `blx` instruction.
3568 // 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 +00003569 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003570 vixl32::k16BitT32InstructionSizeInBytes,
3571 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003572 // LR();
3573 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003574 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003575 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003576 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003577
Andreas Gampe3db70682018-12-26 15:12:03 -08003578 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003579}
3580
Orion Hodsonac141392017-01-13 11:53:47 +00003581void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003582 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3583 if (intrinsic.TryDispatch(invoke)) {
3584 return;
3585 }
Orion Hodsonac141392017-01-13 11:53:47 +00003586 HandleInvoke(invoke);
3587}
3588
3589void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003590 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3591 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 9);
3592 return;
3593 }
Orion Hodsonac141392017-01-13 11:53:47 +00003594 codegen_->GenerateInvokePolymorphicCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003595 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 10);
Orion Hodsonac141392017-01-13 11:53:47 +00003596}
3597
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003598void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3599 HandleInvoke(invoke);
3600}
3601
3602void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3603 codegen_->GenerateInvokeCustomCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003604 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 11);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003605}
3606
Artem Serov02109dd2016-09-23 17:17:54 +01003607void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3608 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003609 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003610 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003611 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003612 locations->SetInAt(0, Location::RequiresRegister());
3613 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3614 break;
3615 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003616 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003617 locations->SetInAt(0, Location::RequiresRegister());
3618 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3619 break;
3620 }
3621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003622 case DataType::Type::kFloat32:
3623 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003624 locations->SetInAt(0, Location::RequiresFpuRegister());
3625 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3626 break;
3627
3628 default:
3629 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3630 }
3631}
3632
3633void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3634 LocationSummary* locations = neg->GetLocations();
3635 Location out = locations->Out();
3636 Location in = locations->InAt(0);
3637 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003638 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003639 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3640 break;
3641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003642 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003643 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3644 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3645 // We cannot emit an RSC (Reverse Subtract with Carry)
3646 // instruction here, as it does not exist in the Thumb-2
3647 // instruction set. We use the following approach
3648 // using SBC and SUB instead.
3649 //
3650 // out.hi = -C
3651 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3652 // out.hi = out.hi - in.hi
3653 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3654 break;
3655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 case DataType::Type::kFloat32:
3657 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003658 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003659 break;
3660
3661 default:
3662 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3663 }
3664}
3665
Scott Wakelingfe885462016-09-22 10:24:38 +01003666void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 DataType::Type result_type = conversion->GetResultType();
3668 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003669 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3670 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003671
3672 // The float-to-long, double-to-long and long-to-float type conversions
3673 // rely on a call to the runtime.
3674 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003675 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3676 && result_type == DataType::Type::kInt64)
3677 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003678 ? LocationSummary::kCallOnMainOnly
3679 : LocationSummary::kNoCall;
3680 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003681 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003682
Scott Wakelingfe885462016-09-22 10:24:38 +01003683 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003684 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003685 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003686 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003688 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3689 locations->SetInAt(0, Location::RequiresRegister());
3690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003691 break;
3692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003694 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003695 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003696 locations->SetInAt(0, Location::Any());
3697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3698 break;
3699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003700 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003701 locations->SetInAt(0, Location::RequiresFpuRegister());
3702 locations->SetOut(Location::RequiresRegister());
3703 locations->AddTemp(Location::RequiresFpuRegister());
3704 break;
3705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003706 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003707 locations->SetInAt(0, Location::RequiresFpuRegister());
3708 locations->SetOut(Location::RequiresRegister());
3709 locations->AddTemp(Location::RequiresFpuRegister());
3710 break;
3711
3712 default:
3713 LOG(FATAL) << "Unexpected type conversion from " << input_type
3714 << " to " << result_type;
3715 }
3716 break;
3717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003719 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003720 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003721 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003723 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003724 case DataType::Type::kInt16:
3725 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003726 locations->SetInAt(0, Location::RequiresRegister());
3727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3728 break;
3729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003731 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3732 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3733 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003734 break;
3735 }
3736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003738 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3739 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3740 calling_convention.GetFpuRegisterAt(1)));
3741 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003742 break;
3743 }
3744
3745 default:
3746 LOG(FATAL) << "Unexpected type conversion from " << input_type
3747 << " to " << result_type;
3748 }
3749 break;
3750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003751 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003752 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003753 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003754 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003756 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 case DataType::Type::kInt16:
3758 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003759 locations->SetInAt(0, Location::RequiresRegister());
3760 locations->SetOut(Location::RequiresFpuRegister());
3761 break;
3762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003763 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003764 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3765 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3766 calling_convention.GetRegisterAt(1)));
3767 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003768 break;
3769 }
3770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003772 locations->SetInAt(0, Location::RequiresFpuRegister());
3773 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3774 break;
3775
3776 default:
3777 LOG(FATAL) << "Unexpected type conversion from " << input_type
3778 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003779 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003780 break;
3781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003782 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003783 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003784 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003785 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003787 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003788 case DataType::Type::kInt16:
3789 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003790 locations->SetInAt(0, Location::RequiresRegister());
3791 locations->SetOut(Location::RequiresFpuRegister());
3792 break;
3793
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003795 locations->SetInAt(0, Location::RequiresRegister());
3796 locations->SetOut(Location::RequiresFpuRegister());
3797 locations->AddTemp(Location::RequiresFpuRegister());
3798 locations->AddTemp(Location::RequiresFpuRegister());
3799 break;
3800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003801 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003802 locations->SetInAt(0, Location::RequiresFpuRegister());
3803 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3804 break;
3805
3806 default:
3807 LOG(FATAL) << "Unexpected type conversion from " << input_type
3808 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003809 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003810 break;
3811
3812 default:
3813 LOG(FATAL) << "Unexpected type conversion from " << input_type
3814 << " to " << result_type;
3815 }
3816}
3817
3818void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3819 LocationSummary* locations = conversion->GetLocations();
3820 Location out = locations->Out();
3821 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003822 DataType::Type result_type = conversion->GetResultType();
3823 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003824 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3825 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003826 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003827 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003828 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003829 case DataType::Type::kInt8:
3830 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003831 case DataType::Type::kInt16:
3832 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003833 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3834 break;
3835 case DataType::Type::kInt64:
3836 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3837 break;
3838
3839 default:
3840 LOG(FATAL) << "Unexpected type conversion from " << input_type
3841 << " to " << result_type;
3842 }
3843 break;
3844
3845 case DataType::Type::kInt8:
3846 switch (input_type) {
3847 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003848 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003849 case DataType::Type::kInt16:
3850 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003851 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3852 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003853 case DataType::Type::kInt64:
3854 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3855 break;
3856
3857 default:
3858 LOG(FATAL) << "Unexpected type conversion from " << input_type
3859 << " to " << result_type;
3860 }
3861 break;
3862
3863 case DataType::Type::kUint16:
3864 switch (input_type) {
3865 case DataType::Type::kInt8:
3866 case DataType::Type::kInt16:
3867 case DataType::Type::kInt32:
3868 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3869 break;
3870 case DataType::Type::kInt64:
3871 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3872 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003873
3874 default:
3875 LOG(FATAL) << "Unexpected type conversion from " << input_type
3876 << " to " << result_type;
3877 }
3878 break;
3879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003880 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003881 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003882 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003883 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003884 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3885 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003886 case DataType::Type::kInt64:
3887 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3888 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003889
3890 default:
3891 LOG(FATAL) << "Unexpected type conversion from " << input_type
3892 << " to " << result_type;
3893 }
3894 break;
3895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003897 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003898 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003899 DCHECK(out.IsRegister());
3900 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003901 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003902 } else if (in.IsDoubleStackSlot()) {
3903 GetAssembler()->LoadFromOffset(kLoadWord,
3904 OutputRegister(conversion),
3905 sp,
3906 in.GetStackIndex());
3907 } else {
3908 DCHECK(in.IsConstant());
3909 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003910 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3911 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003912 }
3913 break;
3914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003915 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003916 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003917 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003918 __ Vmov(OutputRegister(conversion), temp);
3919 break;
3920 }
3921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003922 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003923 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003924 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003925 __ Vmov(OutputRegister(conversion), temp_s);
3926 break;
3927 }
3928
3929 default:
3930 LOG(FATAL) << "Unexpected type conversion from " << input_type
3931 << " to " << result_type;
3932 }
3933 break;
3934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003935 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003936 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003937 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003938 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003939 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003940 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003941 case DataType::Type::kInt16:
3942 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003943 DCHECK(out.IsRegisterPair());
3944 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003945 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003946 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003947 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003948 break;
3949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003950 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003951 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3952 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3953 break;
3954
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003955 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003956 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3957 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3958 break;
3959
3960 default:
3961 LOG(FATAL) << "Unexpected type conversion from " << input_type
3962 << " to " << result_type;
3963 }
3964 break;
3965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003966 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003967 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003968 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003969 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003970 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003971 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003972 case DataType::Type::kInt16:
3973 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003974 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003975 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003976 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003977
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003979 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3980 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3981 break;
3982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003983 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003984 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003985 break;
3986
3987 default:
3988 LOG(FATAL) << "Unexpected type conversion from " << input_type
3989 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003990 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003991 break;
3992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003993 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003994 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003995 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003996 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003997 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003998 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003999 case DataType::Type::kInt16:
4000 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004001 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004002 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004003 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004004
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004005 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004006 vixl32::Register low = LowRegisterFrom(in);
4007 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004008 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004009 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004010 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004011 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004012 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004013
4014 // temp_d = int-to-double(high)
4015 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004016 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004017 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004018 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004019 // out_d = unsigned-to-double(low)
4020 __ Vmov(out_s, low);
4021 __ Vcvt(F64, U32, out_d, out_s);
4022 // out_d += temp_d * constant_d
4023 __ Vmla(F64, out_d, temp_d, constant_d);
4024 break;
4025 }
4026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004027 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004028 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004029 break;
4030
4031 default:
4032 LOG(FATAL) << "Unexpected type conversion from " << input_type
4033 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004034 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004035 break;
4036
4037 default:
4038 LOG(FATAL) << "Unexpected type conversion from " << input_type
4039 << " to " << result_type;
4040 }
4041}
4042
4043void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4044 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004045 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004046 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004047 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004048 locations->SetInAt(0, Location::RequiresRegister());
4049 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4050 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4051 break;
4052 }
4053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004055 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004056 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4058 break;
4059 }
4060
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004061 case DataType::Type::kFloat32:
4062 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004063 locations->SetInAt(0, Location::RequiresFpuRegister());
4064 locations->SetInAt(1, Location::RequiresFpuRegister());
4065 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4066 break;
4067 }
4068
4069 default:
4070 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4071 }
4072}
4073
4074void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4075 LocationSummary* locations = add->GetLocations();
4076 Location out = locations->Out();
4077 Location first = locations->InAt(0);
4078 Location second = locations->InAt(1);
4079
4080 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004081 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004082 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4083 }
4084 break;
4085
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004086 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004087 if (second.IsConstant()) {
4088 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4089 GenerateAddLongConst(out, first, value);
4090 } else {
4091 DCHECK(second.IsRegisterPair());
4092 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4093 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4094 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004095 break;
4096 }
4097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004098 case DataType::Type::kFloat32:
4099 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004100 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004101 break;
4102
4103 default:
4104 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4105 }
4106}
4107
4108void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4109 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004110 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004111 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004112 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004113 locations->SetInAt(0, Location::RequiresRegister());
4114 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4116 break;
4117 }
4118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004120 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004121 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004122 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4123 break;
4124 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004125 case DataType::Type::kFloat32:
4126 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004127 locations->SetInAt(0, Location::RequiresFpuRegister());
4128 locations->SetInAt(1, Location::RequiresFpuRegister());
4129 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4130 break;
4131 }
4132 default:
4133 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4134 }
4135}
4136
4137void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4138 LocationSummary* locations = sub->GetLocations();
4139 Location out = locations->Out();
4140 Location first = locations->InAt(0);
4141 Location second = locations->InAt(1);
4142 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004143 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004144 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004145 break;
4146 }
4147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004148 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004149 if (second.IsConstant()) {
4150 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4151 GenerateAddLongConst(out, first, -value);
4152 } else {
4153 DCHECK(second.IsRegisterPair());
4154 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4155 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4156 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004157 break;
4158 }
4159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004160 case DataType::Type::kFloat32:
4161 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004162 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004163 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004164
4165 default:
4166 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4167 }
4168}
4169
4170void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4171 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004172 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004173 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004174 case DataType::Type::kInt32:
4175 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004176 locations->SetInAt(0, Location::RequiresRegister());
4177 locations->SetInAt(1, Location::RequiresRegister());
4178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4179 break;
4180 }
4181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004182 case DataType::Type::kFloat32:
4183 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004184 locations->SetInAt(0, Location::RequiresFpuRegister());
4185 locations->SetInAt(1, Location::RequiresFpuRegister());
4186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4187 break;
4188 }
4189
4190 default:
4191 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4192 }
4193}
4194
4195void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4196 LocationSummary* locations = mul->GetLocations();
4197 Location out = locations->Out();
4198 Location first = locations->InAt(0);
4199 Location second = locations->InAt(1);
4200 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004201 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004202 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4203 break;
4204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004206 vixl32::Register out_hi = HighRegisterFrom(out);
4207 vixl32::Register out_lo = LowRegisterFrom(out);
4208 vixl32::Register in1_hi = HighRegisterFrom(first);
4209 vixl32::Register in1_lo = LowRegisterFrom(first);
4210 vixl32::Register in2_hi = HighRegisterFrom(second);
4211 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004212
4213 // Extra checks to protect caused by the existence of R1_R2.
4214 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4215 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004216 DCHECK(!out_hi.Is(in1_lo));
4217 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004218
4219 // input: in1 - 64 bits, in2 - 64 bits
4220 // output: out
4221 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4222 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4223 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4224
4225 UseScratchRegisterScope temps(GetVIXLAssembler());
4226 vixl32::Register temp = temps.Acquire();
4227 // temp <- in1.lo * in2.hi
4228 __ Mul(temp, in1_lo, in2_hi);
4229 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4230 __ Mla(out_hi, in1_hi, in2_lo, temp);
4231 // out.lo <- (in1.lo * in2.lo)[31:0];
4232 __ Umull(out_lo, temp, in1_lo, in2_lo);
4233 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004234 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004235 break;
4236 }
4237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004238 case DataType::Type::kFloat32:
4239 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004240 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004241 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004242
4243 default:
4244 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4245 }
4246}
4247
Scott Wakelingfe885462016-09-22 10:24:38 +01004248void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4249 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004250 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004251
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004252 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004253 DCHECK(second.IsConstant());
4254
4255 vixl32::Register out = OutputRegister(instruction);
4256 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004257 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004258 DCHECK(imm == 1 || imm == -1);
4259
4260 if (instruction->IsRem()) {
4261 __ Mov(out, 0);
4262 } else {
4263 if (imm == 1) {
4264 __ Mov(out, dividend);
4265 } else {
4266 __ Rsb(out, dividend, 0);
4267 }
4268 }
4269}
4270
4271void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4272 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004273 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004274
4275 LocationSummary* locations = instruction->GetLocations();
4276 Location second = locations->InAt(1);
4277 DCHECK(second.IsConstant());
4278
4279 vixl32::Register out = OutputRegister(instruction);
4280 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004281 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004282 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4283 int ctz_imm = CTZ(abs_imm);
4284
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004285 auto generate_div_code = [this, imm, ctz_imm](vixl32::Register out, vixl32::Register in) {
4286 __ Asr(out, in, ctz_imm);
Scott Wakelingfe885462016-09-22 10:24:38 +01004287 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004288 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004289 }
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004290 };
4291
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004292 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004293 // No need to adjust the result for non-negative dividends or the INT32_MIN dividend.
4294 // NOTE: The generated code for HDiv/HRem correctly works for the INT32_MIN dividend:
4295 // imm == 2
4296 // HDiv
4297 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
4298 // asr out, out(0x80000001), #1 => out = 0xc0000000
4299 // This is the same as 'asr out, dividend(0x80000000), #1'
4300 //
4301 // imm > 2
4302 // HDiv
4303 // asr out, dividend(0x80000000), #31 => out = -1
4304 // add out, dividend(0x80000000), out(-1), lsr #(32 - ctz_imm) => out = 0b10..01..1,
4305 // where the number of the rightmost 1s is ctz_imm.
4306 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
4307 // leftmost 1s is ctz_imm + 1.
4308 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
4309 //
4310 // imm == INT32_MIN
4311 // HDiv
4312 // asr out, dividend(0x80000000), #31 => out = -1
4313 // add out, dividend(0x80000000), out(-1), lsr #1 => out = 0xc0000000
4314 // asr out, out(0xc0000000), #31 => out = -1
4315 // rsb out, out(-1), #0 => out = 1
4316 // This is the same as
4317 // asr out, dividend(0x80000000), #31
4318 // rsb out, out, #0
4319 //
4320 //
4321 // INT_MIN % imm must be 0 for any imm of power 2. 'and' and 'ubfx' work only with bits
4322 // 0..30 of a dividend. For INT32_MIN those bits are zeros. So 'and' and 'ubfx' always
4323 // produce zero.
4324 if (instruction->IsDiv()) {
4325 generate_div_code(out, dividend);
4326 } else {
4327 if (GetVIXLAssembler()->IsModifiedImmediate(abs_imm - 1)) {
4328 __ And(out, dividend, abs_imm - 1);
4329 } else {
4330 __ Ubfx(out, dividend, 0, ctz_imm);
4331 }
4332 return;
4333 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004334 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004335 vixl32::Register add_right_input = dividend;
4336 if (ctz_imm > 1) {
4337 __ Asr(out, dividend, 31);
4338 add_right_input = out;
4339 }
4340 __ Add(out, dividend, Operand(add_right_input, vixl32::LSR, 32 - ctz_imm));
4341
4342 if (instruction->IsDiv()) {
4343 generate_div_code(out, out);
4344 } else {
4345 __ Bfc(out, 0, ctz_imm);
4346 __ Sub(out, dividend, out);
4347 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004348 }
4349}
4350
4351void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4352 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004354
4355 LocationSummary* locations = instruction->GetLocations();
4356 Location second = locations->InAt(1);
4357 DCHECK(second.IsConstant());
4358
4359 vixl32::Register out = OutputRegister(instruction);
4360 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004361 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4362 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004363 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004364
4365 int64_t magic;
4366 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004367 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Scott Wakelingfe885462016-09-22 10:24:38 +01004368
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004369 auto generate_unsigned_div_code =[this, magic, shift](vixl32::Register out,
4370 vixl32::Register dividend,
4371 vixl32::Register temp1,
4372 vixl32::Register temp2) {
4373 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4374 __ Mov(temp1, static_cast<int32_t>(magic));
4375 if (magic > 0 && shift == 0) {
4376 __ Smull(temp2, out, dividend, temp1);
4377 } else {
4378 __ Smull(temp2, temp1, dividend, temp1);
4379 if (magic < 0) {
4380 // The negative magic M = static_cast<int>(m) means that the multiplier m is greater
4381 // than INT32_MAX. In such a case shift is never 0.
4382 // Proof:
4383 // m = (2^p + d - 2^p % d) / d, where p = 32 + shift, d > 2
4384 //
4385 // If shift == 0, m = (2^32 + d - 2^32 % d) / d =
4386 // = (2^32 + d - (2^32 - (2^32 / d) * d)) / d =
4387 // = (d + (2^32 / d) * d) / d = 1 + (2^32 / d), here '/' is the integer division.
4388 //
4389 // 1 + (2^32 / d) is decreasing when d is increasing.
4390 // The maximum is 1 431 655 766, when d == 3. This value is less than INT32_MAX.
4391 // the minimum is 3, when d = 2^31 -1.
4392 // So for all values of d in [3, INT32_MAX] m with p == 32 is in [3, INT32_MAX) and
4393 // is never less than 0.
4394 __ Add(temp1, temp1, dividend);
4395 }
4396 DCHECK_NE(shift, 0);
4397 __ Lsr(out, temp1, shift);
4398 }
4399 };
Scott Wakelingfe885462016-09-22 10:24:38 +01004400
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004401 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004402 // No need to adjust the result for a non-negative dividend and a positive divisor.
4403 if (instruction->IsDiv()) {
4404 generate_unsigned_div_code(out, dividend, temp1, temp2);
4405 } else {
4406 generate_unsigned_div_code(temp1, dividend, temp1, temp2);
4407 __ Mov(temp2, imm);
4408 __ Mls(out, temp1, temp2, dividend);
4409 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004410 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004411 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4412 __ Mov(temp1, static_cast<int32_t>(magic));
4413 __ Smull(temp2, temp1, dividend, temp1);
4414
4415 if (imm > 0 && magic < 0) {
4416 __ Add(temp1, temp1, dividend);
4417 } else if (imm < 0 && magic > 0) {
4418 __ Sub(temp1, temp1, dividend);
4419 }
4420
4421 if (shift != 0) {
4422 __ Asr(temp1, temp1, shift);
4423 }
4424
4425 if (instruction->IsDiv()) {
4426 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4427 } else {
4428 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4429 // TODO: Strength reduction for mls.
4430 __ Mov(temp2, imm);
4431 __ Mls(out, temp1, temp2, dividend);
4432 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004433 }
4434}
4435
4436void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4437 HBinaryOperation* instruction) {
4438 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004439 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004440
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004441 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004442 DCHECK(second.IsConstant());
4443
Anton Kirilov644032c2016-12-06 17:51:43 +00004444 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004445 if (imm == 0) {
4446 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4447 } else if (imm == 1 || imm == -1) {
4448 DivRemOneOrMinusOne(instruction);
4449 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4450 DivRemByPowerOfTwo(instruction);
4451 } else {
4452 DCHECK(imm <= -2 || imm >= 2);
4453 GenerateDivRemWithAnyConstant(instruction);
4454 }
4455}
4456
4457void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4458 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004459 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004460 // pLdiv runtime call.
4461 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004462 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004463 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004464 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004465 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4466 // pIdivmod runtime call.
4467 call_kind = LocationSummary::kCallOnMainOnly;
4468 }
4469
Vladimir Markoca6fff82017-10-03 14:49:14 +01004470 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004471
4472 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004473 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004474 if (div->InputAt(1)->IsConstant()) {
4475 locations->SetInAt(0, Location::RequiresRegister());
4476 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004477 int32_t value = Int32ConstantFrom(div->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004478 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004479 if (value == 1 || value == 0 || value == -1) {
4480 // No temp register required.
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004481 } else if (IsPowerOfTwo(AbsOrMin(value)) &&
4482 value != 2 &&
4483 value != -2 &&
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004484 !HasNonNegativeOrMinIntInputAt(div, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004485 // The "out" register is used as a temporary, so it overlaps with the inputs.
4486 out_overlaps = Location::kOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004487 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004488 locations->AddRegisterTemps(2);
Scott Wakelingfe885462016-09-22 10:24:38 +01004489 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004490 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Scott Wakelingfe885462016-09-22 10:24:38 +01004491 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4492 locations->SetInAt(0, Location::RequiresRegister());
4493 locations->SetInAt(1, Location::RequiresRegister());
4494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4495 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004496 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4497 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4498 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004499 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004500 // we only need the former.
4501 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004502 }
4503 break;
4504 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004505 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004506 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4507 locations->SetInAt(0, LocationFrom(
4508 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4509 locations->SetInAt(1, LocationFrom(
4510 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4511 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004512 break;
4513 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004514 case DataType::Type::kFloat32:
4515 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004516 locations->SetInAt(0, Location::RequiresFpuRegister());
4517 locations->SetInAt(1, Location::RequiresFpuRegister());
4518 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4519 break;
4520 }
4521
4522 default:
4523 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4524 }
4525}
4526
4527void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004528 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004529 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004530
4531 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004532 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004533 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004534 GenerateDivRemConstantIntegral(div);
4535 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4536 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4537 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004538 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4539 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4540 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4541 DCHECK(r0.Is(OutputRegister(div)));
4542
4543 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4544 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004545 }
4546 break;
4547 }
4548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004549 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004550 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4551 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4552 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4553 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4554 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4555 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4556 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4557
4558 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4559 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004560 break;
4561 }
4562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004563 case DataType::Type::kFloat32:
4564 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004565 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004566 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004567
4568 default:
4569 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4570 }
4571}
4572
Artem Serov551b28f2016-10-18 19:11:30 +01004573void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004574 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004575
4576 // Most remainders are implemented in the runtime.
4577 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004578 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004579 // sdiv will be replaced by other instruction sequence.
4580 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004581 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004582 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4583 // Have hardware divide instruction for int, do it with three instructions.
4584 call_kind = LocationSummary::kNoCall;
4585 }
4586
Vladimir Markoca6fff82017-10-03 14:49:14 +01004587 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004588
4589 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004590 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004591 if (rem->InputAt(1)->IsConstant()) {
4592 locations->SetInAt(0, Location::RequiresRegister());
4593 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004594 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004595 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004596 if (value == 1 || value == 0 || value == -1) {
4597 // No temp register required.
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004598 } else if (IsPowerOfTwo(AbsOrMin(value)) && !HasNonNegativeOrMinIntInputAt(rem, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004599 // The "out" register is used as a temporary, so it overlaps with the inputs.
4600 out_overlaps = Location::kOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004601 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004602 locations->AddRegisterTemps(2);
Artem Serov551b28f2016-10-18 19:11:30 +01004603 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004604 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Artem Serov551b28f2016-10-18 19:11:30 +01004605 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4606 locations->SetInAt(0, Location::RequiresRegister());
4607 locations->SetInAt(1, Location::RequiresRegister());
4608 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4609 locations->AddTemp(Location::RequiresRegister());
4610 } else {
4611 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4612 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4613 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004614 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004615 // we only need the latter.
4616 locations->SetOut(LocationFrom(r1));
4617 }
4618 break;
4619 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004620 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004621 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4622 locations->SetInAt(0, LocationFrom(
4623 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4624 locations->SetInAt(1, LocationFrom(
4625 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4626 // The runtime helper puts the output in R2,R3.
4627 locations->SetOut(LocationFrom(r2, r3));
4628 break;
4629 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004630 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004631 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4632 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4633 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4634 locations->SetOut(LocationFrom(s0));
4635 break;
4636 }
4637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004638 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004639 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4640 locations->SetInAt(0, LocationFrom(
4641 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4642 locations->SetInAt(1, LocationFrom(
4643 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4644 locations->SetOut(LocationFrom(s0, s1));
4645 break;
4646 }
4647
4648 default:
4649 LOG(FATAL) << "Unexpected rem type " << type;
4650 }
4651}
4652
4653void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4654 LocationSummary* locations = rem->GetLocations();
4655 Location second = locations->InAt(1);
4656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004657 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004658 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004659 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004660 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4661 vixl32::Register out_reg = OutputRegister(rem);
4662 if (second.IsConstant()) {
4663 GenerateDivRemConstantIntegral(rem);
4664 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4665 vixl32::Register reg2 = RegisterFrom(second);
4666 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4667
4668 // temp = reg1 / reg2 (integer division)
4669 // dest = reg1 - temp * reg2
4670 __ Sdiv(temp, reg1, reg2);
4671 __ Mls(out_reg, temp, reg2, reg1);
4672 } else {
4673 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4674 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4675 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4676 DCHECK(out_reg.Is(r1));
4677
4678 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4679 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4680 }
4681 break;
4682 }
4683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004684 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004685 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4686 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4687 break;
4688 }
4689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004690 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004691 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4692 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4693 break;
4694 }
4695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004696 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004697 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4698 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4699 break;
4700 }
4701
4702 default:
4703 LOG(FATAL) << "Unexpected rem type " << type;
4704 }
4705}
4706
Aart Bik1f8d51b2018-02-15 10:42:37 -08004707static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4708 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4709 switch (minmax->GetResultType()) {
4710 case DataType::Type::kInt32:
4711 locations->SetInAt(0, Location::RequiresRegister());
4712 locations->SetInAt(1, Location::RequiresRegister());
4713 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4714 break;
4715 case DataType::Type::kInt64:
4716 locations->SetInAt(0, Location::RequiresRegister());
4717 locations->SetInAt(1, Location::RequiresRegister());
4718 locations->SetOut(Location::SameAsFirstInput());
4719 break;
4720 case DataType::Type::kFloat32:
4721 locations->SetInAt(0, Location::RequiresFpuRegister());
4722 locations->SetInAt(1, Location::RequiresFpuRegister());
4723 locations->SetOut(Location::SameAsFirstInput());
4724 locations->AddTemp(Location::RequiresRegister());
4725 break;
4726 case DataType::Type::kFloat64:
4727 locations->SetInAt(0, Location::RequiresFpuRegister());
4728 locations->SetInAt(1, Location::RequiresFpuRegister());
4729 locations->SetOut(Location::SameAsFirstInput());
4730 break;
4731 default:
4732 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4733 }
4734}
4735
Aart Bik351df3e2018-03-07 11:54:57 -08004736void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004737 Location op1_loc = locations->InAt(0);
4738 Location op2_loc = locations->InAt(1);
4739 Location out_loc = locations->Out();
4740
4741 vixl32::Register op1 = RegisterFrom(op1_loc);
4742 vixl32::Register op2 = RegisterFrom(op2_loc);
4743 vixl32::Register out = RegisterFrom(out_loc);
4744
4745 __ Cmp(op1, op2);
4746
4747 {
4748 ExactAssemblyScope aas(GetVIXLAssembler(),
4749 3 * kMaxInstructionSizeInBytes,
4750 CodeBufferCheckScope::kMaximumSize);
4751
4752 __ ite(is_min ? lt : gt);
4753 __ mov(is_min ? lt : gt, out, op1);
4754 __ mov(is_min ? ge : le, out, op2);
4755 }
4756}
4757
4758void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4759 Location op1_loc = locations->InAt(0);
4760 Location op2_loc = locations->InAt(1);
4761 Location out_loc = locations->Out();
4762
4763 // Optimization: don't generate any code if inputs are the same.
4764 if (op1_loc.Equals(op2_loc)) {
4765 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4766 return;
4767 }
4768
4769 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4770 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4771 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4772 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4773 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4774 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4775 UseScratchRegisterScope temps(GetVIXLAssembler());
4776 const vixl32::Register temp = temps.Acquire();
4777
4778 DCHECK(op1_lo.Is(out_lo));
4779 DCHECK(op1_hi.Is(out_hi));
4780
4781 // Compare op1 >= op2, or op1 < op2.
4782 __ Cmp(out_lo, op2_lo);
4783 __ Sbcs(temp, out_hi, op2_hi);
4784
4785 // Now GE/LT condition code is correct for the long comparison.
4786 {
4787 vixl32::ConditionType cond = is_min ? ge : lt;
4788 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4789 3 * kMaxInstructionSizeInBytes,
4790 CodeBufferCheckScope::kMaximumSize);
4791 __ itt(cond);
4792 __ mov(cond, out_lo, op2_lo);
4793 __ mov(cond, out_hi, op2_hi);
4794 }
4795}
4796
Aart Bik351df3e2018-03-07 11:54:57 -08004797void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4798 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004799 Location op1_loc = locations->InAt(0);
4800 Location op2_loc = locations->InAt(1);
4801 Location out_loc = locations->Out();
4802
4803 // Optimization: don't generate any code if inputs are the same.
4804 if (op1_loc.Equals(op2_loc)) {
4805 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4806 return;
4807 }
4808
4809 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4810 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4811 vixl32::SRegister out = SRegisterFrom(out_loc);
4812
4813 UseScratchRegisterScope temps(GetVIXLAssembler());
4814 const vixl32::Register temp1 = temps.Acquire();
4815 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4816 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004817 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004818
4819 DCHECK(op1.Is(out));
4820
4821 __ Vcmp(op1, op2);
4822 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004823 __ B(vs, &nan, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004824
4825 // op1 <> op2
4826 vixl32::ConditionType cond = is_min ? gt : lt;
4827 {
4828 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4829 2 * kMaxInstructionSizeInBytes,
4830 CodeBufferCheckScope::kMaximumSize);
4831 __ it(cond);
4832 __ vmov(cond, F32, out, op2);
4833 }
4834 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004835 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004836
4837 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4838 __ Vmov(temp1, op1);
4839 __ Vmov(temp2, op2);
4840 if (is_min) {
4841 __ Orr(temp1, temp1, temp2);
4842 } else {
4843 __ And(temp1, temp1, temp2);
4844 }
4845 __ Vmov(out, temp1);
4846 __ B(final_label);
4847
4848 // handle NaN input.
4849 __ Bind(&nan);
4850 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4851 __ Vmov(out, temp1);
4852
4853 if (done.IsReferenced()) {
4854 __ Bind(&done);
4855 }
4856}
4857
Aart Bik351df3e2018-03-07 11:54:57 -08004858void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4859 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004860 Location op1_loc = locations->InAt(0);
4861 Location op2_loc = locations->InAt(1);
4862 Location out_loc = locations->Out();
4863
4864 // Optimization: don't generate any code if inputs are the same.
4865 if (op1_loc.Equals(op2_loc)) {
4866 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4867 return;
4868 }
4869
4870 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4871 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4872 vixl32::DRegister out = DRegisterFrom(out_loc);
4873 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004874 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004875
4876 DCHECK(op1.Is(out));
4877
4878 __ Vcmp(op1, op2);
4879 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004880 __ B(vs, &handle_nan_eq, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004881
4882 // op1 <> op2
4883 vixl32::ConditionType cond = is_min ? gt : lt;
4884 {
4885 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4886 2 * kMaxInstructionSizeInBytes,
4887 CodeBufferCheckScope::kMaximumSize);
4888 __ it(cond);
4889 __ vmov(cond, F64, out, op2);
4890 }
4891 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004892 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004893
4894 // handle op1 == op2, max(+0.0,-0.0).
4895 if (!is_min) {
4896 __ Vand(F64, out, op1, op2);
4897 __ B(final_label);
4898 }
4899
4900 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4901 __ Bind(&handle_nan_eq);
4902 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4903
4904 if (done.IsReferenced()) {
4905 __ Bind(&done);
4906 }
4907}
4908
Aart Bik351df3e2018-03-07 11:54:57 -08004909void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4910 DataType::Type type = minmax->GetResultType();
4911 switch (type) {
4912 case DataType::Type::kInt32:
4913 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4914 break;
4915 case DataType::Type::kInt64:
4916 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4917 break;
4918 case DataType::Type::kFloat32:
4919 GenerateMinMaxFloat(minmax, is_min);
4920 break;
4921 case DataType::Type::kFloat64:
4922 GenerateMinMaxDouble(minmax, is_min);
4923 break;
4924 default:
4925 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4926 }
4927}
4928
Aart Bik1f8d51b2018-02-15 10:42:37 -08004929void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4930 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4931}
4932
4933void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004934 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004935}
4936
4937void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4938 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4939}
4940
4941void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004942 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004943}
4944
Aart Bik3dad3412018-02-28 12:01:46 -08004945void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4946 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4947 switch (abs->GetResultType()) {
4948 case DataType::Type::kInt32:
4949 case DataType::Type::kInt64:
4950 locations->SetInAt(0, Location::RequiresRegister());
4951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4952 locations->AddTemp(Location::RequiresRegister());
4953 break;
4954 case DataType::Type::kFloat32:
4955 case DataType::Type::kFloat64:
4956 locations->SetInAt(0, Location::RequiresFpuRegister());
4957 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4958 break;
4959 default:
4960 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4961 }
4962}
4963
4964void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4965 LocationSummary* locations = abs->GetLocations();
4966 switch (abs->GetResultType()) {
4967 case DataType::Type::kInt32: {
4968 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4969 vixl32::Register out_reg = RegisterFrom(locations->Out());
4970 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4971 __ Asr(mask, in_reg, 31);
4972 __ Add(out_reg, in_reg, mask);
4973 __ Eor(out_reg, out_reg, mask);
4974 break;
4975 }
4976 case DataType::Type::kInt64: {
4977 Location in = locations->InAt(0);
4978 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4979 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4980 Location output = locations->Out();
4981 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4982 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4983 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4984 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4985 __ Asr(mask, in_reg_hi, 31);
4986 __ Adds(out_reg_lo, in_reg_lo, mask);
4987 __ Adc(out_reg_hi, in_reg_hi, mask);
4988 __ Eor(out_reg_lo, out_reg_lo, mask);
4989 __ Eor(out_reg_hi, out_reg_hi, mask);
4990 break;
4991 }
4992 case DataType::Type::kFloat32:
4993 case DataType::Type::kFloat64:
4994 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4995 break;
4996 default:
4997 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4998 }
4999}
Artem Serov551b28f2016-10-18 19:11:30 +01005000
Scott Wakelingfe885462016-09-22 10:24:38 +01005001void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005002 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01005003 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01005004}
5005
5006void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
5007 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005008 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01005009 codegen_->AddSlowPath(slow_path);
5010
5011 LocationSummary* locations = instruction->GetLocations();
5012 Location value = locations->InAt(0);
5013
5014 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005015 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005016 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 case DataType::Type::kInt8:
5018 case DataType::Type::kUint16:
5019 case DataType::Type::kInt16:
5020 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005021 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005022 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01005023 } else {
5024 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005025 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005026 __ B(slow_path->GetEntryLabel());
5027 }
5028 }
5029 break;
5030 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005031 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005032 if (value.IsRegisterPair()) {
5033 UseScratchRegisterScope temps(GetVIXLAssembler());
5034 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005035 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005036 __ B(eq, slow_path->GetEntryLabel());
5037 } else {
5038 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005039 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005040 __ B(slow_path->GetEntryLabel());
5041 }
5042 }
5043 break;
5044 }
5045 default:
5046 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5047 }
5048}
5049
Artem Serov02109dd2016-09-23 17:17:54 +01005050void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5051 LocationSummary* locations = ror->GetLocations();
5052 vixl32::Register in = InputRegisterAt(ror, 0);
5053 Location rhs = locations->InAt(1);
5054 vixl32::Register out = OutputRegister(ror);
5055
5056 if (rhs.IsConstant()) {
5057 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5058 // so map all rotations to a +ve. equivalent in that range.
5059 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5060 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5061 if (rot) {
5062 // Rotate, mapping left rotations to right equivalents if necessary.
5063 // (e.g. left by 2 bits == right by 30.)
5064 __ Ror(out, in, rot);
5065 } else if (!out.Is(in)) {
5066 __ Mov(out, in);
5067 }
5068 } else {
5069 __ Ror(out, in, RegisterFrom(rhs));
5070 }
5071}
5072
5073// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5074// rotates by swapping input regs (effectively rotating by the first 32-bits of
5075// a larger rotation) or flipping direction (thus treating larger right/left
5076// rotations as sub-word sized rotations in the other direction) as appropriate.
5077void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5078 LocationSummary* locations = ror->GetLocations();
5079 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5080 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5081 Location rhs = locations->InAt(1);
5082 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5083 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5084
5085 if (rhs.IsConstant()) {
5086 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5087 // Map all rotations to +ve. equivalents on the interval [0,63].
5088 rot &= kMaxLongShiftDistance;
5089 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5090 // logic below to a simple pair of binary orr.
5091 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5092 if (rot >= kArmBitsPerWord) {
5093 rot -= kArmBitsPerWord;
5094 std::swap(in_reg_hi, in_reg_lo);
5095 }
5096 // Rotate, or mov to out for zero or word size rotations.
5097 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005098 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005099 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005100 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005101 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5102 } else {
5103 __ Mov(out_reg_lo, in_reg_lo);
5104 __ Mov(out_reg_hi, in_reg_hi);
5105 }
5106 } else {
5107 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5108 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5109 vixl32::Label end;
5110 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005111 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005112
5113 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5114 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005115 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Andreas Gampe3db70682018-12-26 15:12:03 -08005116 __ B(cc, &shift_by_32_plus_shift_right, /* is_far_target= */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005117
5118 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5119 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5120 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5121 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5122 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5123 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5124 __ Lsr(shift_left, in_reg_hi, shift_right);
5125 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005126 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005127
5128 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5129 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5130 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5131 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5132 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5133 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5134 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5135 __ Lsl(shift_right, in_reg_hi, shift_left);
5136 __ Add(out_reg_lo, out_reg_lo, shift_right);
5137
Anton Kirilov6f644202017-02-27 18:29:45 +00005138 if (end.IsReferenced()) {
5139 __ Bind(&end);
5140 }
Artem Serov02109dd2016-09-23 17:17:54 +01005141 }
5142}
5143
5144void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5145 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005146 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005147 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005148 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005149 locations->SetInAt(0, Location::RequiresRegister());
5150 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5151 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5152 break;
5153 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005154 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005155 locations->SetInAt(0, Location::RequiresRegister());
5156 if (ror->InputAt(1)->IsConstant()) {
5157 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5158 } else {
5159 locations->SetInAt(1, Location::RequiresRegister());
5160 locations->AddTemp(Location::RequiresRegister());
5161 locations->AddTemp(Location::RequiresRegister());
5162 }
5163 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5164 break;
5165 }
5166 default:
5167 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5168 }
5169}
5170
5171void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005172 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005173 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005174 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005175 HandleIntegerRotate(ror);
5176 break;
5177 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005179 HandleLongRotate(ror);
5180 break;
5181 }
5182 default:
5183 LOG(FATAL) << "Unexpected operation type " << type;
5184 UNREACHABLE();
5185 }
5186}
5187
Artem Serov02d37832016-10-25 15:25:33 +01005188void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5189 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5190
5191 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005192 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005193
5194 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005195 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005196 locations->SetInAt(0, Location::RequiresRegister());
5197 if (op->InputAt(1)->IsConstant()) {
5198 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5200 } else {
5201 locations->SetInAt(1, Location::RequiresRegister());
5202 // Make the output overlap, as it will be used to hold the masked
5203 // second input.
5204 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5205 }
5206 break;
5207 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005208 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005209 locations->SetInAt(0, Location::RequiresRegister());
5210 if (op->InputAt(1)->IsConstant()) {
5211 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5212 // For simplicity, use kOutputOverlap even though we only require that low registers
5213 // don't clash with high registers which the register allocator currently guarantees.
5214 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5215 } else {
5216 locations->SetInAt(1, Location::RequiresRegister());
5217 locations->AddTemp(Location::RequiresRegister());
5218 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5219 }
5220 break;
5221 }
5222 default:
5223 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5224 }
5225}
5226
5227void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5228 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5229
5230 LocationSummary* locations = op->GetLocations();
5231 Location out = locations->Out();
5232 Location first = locations->InAt(0);
5233 Location second = locations->InAt(1);
5234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005235 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005236 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005238 vixl32::Register out_reg = OutputRegister(op);
5239 vixl32::Register first_reg = InputRegisterAt(op, 0);
5240 if (second.IsRegister()) {
5241 vixl32::Register second_reg = RegisterFrom(second);
5242 // ARM doesn't mask the shift count so we need to do it ourselves.
5243 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5244 if (op->IsShl()) {
5245 __ Lsl(out_reg, first_reg, out_reg);
5246 } else if (op->IsShr()) {
5247 __ Asr(out_reg, first_reg, out_reg);
5248 } else {
5249 __ Lsr(out_reg, first_reg, out_reg);
5250 }
5251 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005252 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005253 uint32_t shift_value = cst & kMaxIntShiftDistance;
5254 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5255 __ Mov(out_reg, first_reg);
5256 } else if (op->IsShl()) {
5257 __ Lsl(out_reg, first_reg, shift_value);
5258 } else if (op->IsShr()) {
5259 __ Asr(out_reg, first_reg, shift_value);
5260 } else {
5261 __ Lsr(out_reg, first_reg, shift_value);
5262 }
5263 }
5264 break;
5265 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005267 vixl32::Register o_h = HighRegisterFrom(out);
5268 vixl32::Register o_l = LowRegisterFrom(out);
5269
5270 vixl32::Register high = HighRegisterFrom(first);
5271 vixl32::Register low = LowRegisterFrom(first);
5272
5273 if (second.IsRegister()) {
5274 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5275
5276 vixl32::Register second_reg = RegisterFrom(second);
5277
5278 if (op->IsShl()) {
5279 __ And(o_l, second_reg, kMaxLongShiftDistance);
5280 // Shift the high part
5281 __ Lsl(o_h, high, o_l);
5282 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005283 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005284 __ Lsr(temp, low, temp);
5285 __ Orr(o_h, o_h, temp);
5286 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005287 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005288 {
Artem Serov0fb37192016-12-06 18:13:40 +00005289 ExactAssemblyScope guard(GetVIXLAssembler(),
5290 2 * vixl32::kMaxInstructionSizeInBytes,
5291 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005292 __ it(pl);
5293 __ lsl(pl, o_h, low, temp);
5294 }
5295 // Shift the low part
5296 __ Lsl(o_l, low, o_l);
5297 } else if (op->IsShr()) {
5298 __ And(o_h, second_reg, kMaxLongShiftDistance);
5299 // Shift the low part
5300 __ Lsr(o_l, low, o_h);
5301 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005302 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005303 __ Lsl(temp, high, temp);
5304 __ Orr(o_l, o_l, temp);
5305 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005306 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005307 {
Artem Serov0fb37192016-12-06 18:13:40 +00005308 ExactAssemblyScope guard(GetVIXLAssembler(),
5309 2 * vixl32::kMaxInstructionSizeInBytes,
5310 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005311 __ it(pl);
5312 __ asr(pl, o_l, high, temp);
5313 }
5314 // Shift the high part
5315 __ Asr(o_h, high, o_h);
5316 } else {
5317 __ And(o_h, second_reg, kMaxLongShiftDistance);
5318 // same as Shr except we use `Lsr`s and not `Asr`s
5319 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005320 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005321 __ Lsl(temp, high, temp);
5322 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005323 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005324 {
Artem Serov0fb37192016-12-06 18:13:40 +00005325 ExactAssemblyScope guard(GetVIXLAssembler(),
5326 2 * vixl32::kMaxInstructionSizeInBytes,
5327 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005328 __ it(pl);
5329 __ lsr(pl, o_l, high, temp);
5330 }
5331 __ Lsr(o_h, high, o_h);
5332 }
5333 } else {
5334 // Register allocator doesn't create partial overlap.
5335 DCHECK(!o_l.Is(high));
5336 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005337 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005338 uint32_t shift_value = cst & kMaxLongShiftDistance;
5339 if (shift_value > 32) {
5340 if (op->IsShl()) {
5341 __ Lsl(o_h, low, shift_value - 32);
5342 __ Mov(o_l, 0);
5343 } else if (op->IsShr()) {
5344 __ Asr(o_l, high, shift_value - 32);
5345 __ Asr(o_h, high, 31);
5346 } else {
5347 __ Lsr(o_l, high, shift_value - 32);
5348 __ Mov(o_h, 0);
5349 }
5350 } else if (shift_value == 32) {
5351 if (op->IsShl()) {
5352 __ Mov(o_h, low);
5353 __ Mov(o_l, 0);
5354 } else if (op->IsShr()) {
5355 __ Mov(o_l, high);
5356 __ Asr(o_h, high, 31);
5357 } else {
5358 __ Mov(o_l, high);
5359 __ Mov(o_h, 0);
5360 }
5361 } else if (shift_value == 1) {
5362 if (op->IsShl()) {
5363 __ Lsls(o_l, low, 1);
5364 __ Adc(o_h, high, high);
5365 } else if (op->IsShr()) {
5366 __ Asrs(o_h, high, 1);
5367 __ Rrx(o_l, low);
5368 } else {
5369 __ Lsrs(o_h, high, 1);
5370 __ Rrx(o_l, low);
5371 }
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005372 } else if (shift_value == 0) {
5373 __ Mov(o_l, low);
5374 __ Mov(o_h, high);
Artem Serov02d37832016-10-25 15:25:33 +01005375 } else {
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005376 DCHECK(0 < shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01005377 if (op->IsShl()) {
5378 __ Lsl(o_h, high, shift_value);
5379 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5380 __ Lsl(o_l, low, shift_value);
5381 } else if (op->IsShr()) {
5382 __ Lsr(o_l, low, shift_value);
5383 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5384 __ Asr(o_h, high, shift_value);
5385 } else {
5386 __ Lsr(o_l, low, shift_value);
5387 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5388 __ Lsr(o_h, high, shift_value);
5389 }
5390 }
5391 }
5392 break;
5393 }
5394 default:
5395 LOG(FATAL) << "Unexpected operation type " << type;
5396 UNREACHABLE();
5397 }
5398}
5399
5400void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5401 HandleShift(shl);
5402}
5403
5404void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5405 HandleShift(shl);
5406}
5407
5408void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5409 HandleShift(shr);
5410}
5411
5412void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5413 HandleShift(shr);
5414}
5415
5416void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5417 HandleShift(ushr);
5418}
5419
5420void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5421 HandleShift(ushr);
5422}
5423
5424void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005425 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5426 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005427 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5428 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005429 locations->SetOut(LocationFrom(r0));
5430}
5431
5432void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005433 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5434 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andra Danciua0130e82020-07-23 12:34:56 +00005435 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005436}
5437
5438void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005439 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5440 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005441 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005442 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005443 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5444 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005445}
5446
5447void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005448 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5449 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005450 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005451 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005452 DCHECK(!codegen_->IsLeafMethod());
Andra Danciua0130e82020-07-23 12:34:56 +00005453 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 13);
Artem Serov02d37832016-10-25 15:25:33 +01005454}
5455
5456void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5457 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005458 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005459 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5460 if (location.IsStackSlot()) {
5461 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5462 } else if (location.IsDoubleStackSlot()) {
5463 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5464 }
5465 locations->SetOut(location);
5466}
5467
5468void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5469 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5470 // Nothing to do, the parameter is already at its location.
5471}
5472
5473void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5474 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005475 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005476 locations->SetOut(LocationFrom(kMethodRegister));
5477}
5478
5479void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5480 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5481 // Nothing to do, the method is already at its location.
5482}
5483
5484void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5485 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005486 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005487 locations->SetInAt(0, Location::RequiresRegister());
5488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5489}
5490
5491void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5492 LocationSummary* locations = not_->GetLocations();
5493 Location out = locations->Out();
5494 Location in = locations->InAt(0);
5495 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005496 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005497 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5498 break;
5499
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005500 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005501 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5502 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5503 break;
5504
5505 default:
5506 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5507 }
5508}
5509
Scott Wakelingc34dba72016-10-03 10:14:44 +01005510void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5511 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005512 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005513 locations->SetInAt(0, Location::RequiresRegister());
5514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5515}
5516
5517void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5518 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5519}
5520
Artem Serov02d37832016-10-25 15:25:33 +01005521void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5522 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005523 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005524 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005525 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005526 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005528 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005529 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 case DataType::Type::kInt32:
5531 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005532 locations->SetInAt(0, Location::RequiresRegister());
5533 locations->SetInAt(1, Location::RequiresRegister());
5534 // Output overlaps because it is written before doing the low comparison.
5535 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5536 break;
5537 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 case DataType::Type::kFloat32:
5539 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005540 locations->SetInAt(0, Location::RequiresFpuRegister());
5541 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5542 locations->SetOut(Location::RequiresRegister());
5543 break;
5544 }
5545 default:
5546 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5547 }
5548}
5549
5550void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5551 LocationSummary* locations = compare->GetLocations();
5552 vixl32::Register out = OutputRegister(compare);
5553 Location left = locations->InAt(0);
5554 Location right = locations->InAt(1);
5555
5556 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005557 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005558 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005559 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005560 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005561 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005562 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005565 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005567 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5568 __ Mov(out, 0);
5569 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5570 less_cond = lt;
5571 break;
5572 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005574 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Andreas Gampe3db70682018-12-26 15:12:03 -08005575 __ B(lt, &less, /* is_far_target= */ false);
5576 __ B(gt, &greater, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005577 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5578 __ Mov(out, 0);
5579 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5580 less_cond = lo;
5581 break;
5582 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005583 case DataType::Type::kFloat32:
5584 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005585 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005586 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005587 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5588 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5589 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5590 break;
5591 }
5592 default:
5593 LOG(FATAL) << "Unexpected compare type " << type;
5594 UNREACHABLE();
5595 }
5596
Andreas Gampe3db70682018-12-26 15:12:03 -08005597 __ B(eq, final_label, /* is_far_target= */ false);
5598 __ B(less_cond, &less, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005599
5600 __ Bind(&greater);
5601 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005602 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005603
5604 __ Bind(&less);
5605 __ Mov(out, -1);
5606
Anton Kirilov6f644202017-02-27 18:29:45 +00005607 if (done.IsReferenced()) {
5608 __ Bind(&done);
5609 }
Artem Serov02d37832016-10-25 15:25:33 +01005610}
5611
5612void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5613 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005614 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005615 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5616 locations->SetInAt(i, Location::Any());
5617 }
5618 locations->SetOut(Location::Any());
5619}
5620
5621void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5622 LOG(FATAL) << "Unreachable";
5623}
5624
5625void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5626 // TODO (ported from quick): revisit ARM barrier kinds.
5627 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5628 switch (kind) {
5629 case MemBarrierKind::kAnyStore:
5630 case MemBarrierKind::kLoadAny:
5631 case MemBarrierKind::kAnyAny: {
5632 flavor = DmbOptions::ISH;
5633 break;
5634 }
5635 case MemBarrierKind::kStoreStore: {
5636 flavor = DmbOptions::ISHST;
5637 break;
5638 }
5639 default:
5640 LOG(FATAL) << "Unexpected memory barrier " << kind;
5641 }
5642 __ Dmb(flavor);
5643}
5644
5645void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5646 uint32_t offset,
5647 vixl32::Register out_lo,
5648 vixl32::Register out_hi) {
5649 UseScratchRegisterScope temps(GetVIXLAssembler());
5650 if (offset != 0) {
5651 vixl32::Register temp = temps.Acquire();
5652 __ Add(temp, addr, offset);
5653 addr = temp;
5654 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005655 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005656}
5657
5658void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5659 uint32_t offset,
5660 vixl32::Register value_lo,
5661 vixl32::Register value_hi,
5662 vixl32::Register temp1,
5663 vixl32::Register temp2,
5664 HInstruction* instruction) {
5665 UseScratchRegisterScope temps(GetVIXLAssembler());
5666 vixl32::Label fail;
5667 if (offset != 0) {
5668 vixl32::Register temp = temps.Acquire();
5669 __ Add(temp, addr, offset);
5670 addr = temp;
5671 }
5672 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005673 {
5674 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005675 ExactAssemblyScope aas(GetVIXLAssembler(),
5676 vixl32::kMaxInstructionSizeInBytes,
5677 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005678 // We need a load followed by store. (The address used in a STREX instruction must
5679 // be the same as the address in the most recently executed LDREX instruction.)
5680 __ ldrexd(temp1, temp2, MemOperand(addr));
5681 codegen_->MaybeRecordImplicitNullCheck(instruction);
5682 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005683 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005684 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005685}
Artem Serov02109dd2016-09-23 17:17:54 +01005686
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005687void LocationsBuilderARMVIXL::HandleFieldSet(
5688 HInstruction* instruction, const FieldInfo& field_info) {
5689 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5690
5691 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005692 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005693 locations->SetInAt(0, Location::RequiresRegister());
5694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005695 DataType::Type field_type = field_info.GetFieldType();
5696 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005697 locations->SetInAt(1, Location::RequiresFpuRegister());
5698 } else {
5699 locations->SetInAt(1, Location::RequiresRegister());
5700 }
5701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005702 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005703 bool generate_volatile = field_info.IsVolatile()
5704 && is_wide
5705 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5706 bool needs_write_barrier =
5707 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5708 // Temporary registers for the write barrier.
5709 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5710 if (needs_write_barrier) {
5711 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5712 locations->AddTemp(Location::RequiresRegister());
5713 } else if (generate_volatile) {
5714 // ARM encoding have some additional constraints for ldrexd/strexd:
5715 // - registers need to be consecutive
5716 // - the first register should be even but not R14.
5717 // We don't test for ARM yet, and the assertion makes sure that we
5718 // revisit this if we ever enable ARM encoding.
5719 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5720
5721 locations->AddTemp(Location::RequiresRegister());
5722 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005723 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005724 // For doubles we need two more registers to copy the value.
5725 locations->AddTemp(LocationFrom(r2));
5726 locations->AddTemp(LocationFrom(r3));
5727 }
5728 }
5729}
5730
5731void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5732 const FieldInfo& field_info,
5733 bool value_can_be_null) {
5734 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5735
5736 LocationSummary* locations = instruction->GetLocations();
5737 vixl32::Register base = InputRegisterAt(instruction, 0);
5738 Location value = locations->InAt(1);
Alex Light3a73ffb2021-01-25 14:11:05 +00005739 std::optional<vixl::aarch32::Label> pred_is_null;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005740
Alex Light3a73ffb2021-01-25 14:11:05 +00005741 bool is_predicated =
5742 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005743 bool is_volatile = field_info.IsVolatile();
5744 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005745 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5747 bool needs_write_barrier =
5748 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5749
Alex Light3a73ffb2021-01-25 14:11:05 +00005750 if (is_predicated) {
5751 pred_is_null.emplace();
5752 __ CompareAndBranchIfZero(base, &*pred_is_null, /* is_far_target= */ false);
5753 }
5754
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005755 if (is_volatile) {
5756 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5757 }
5758
5759 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005760 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005761 case DataType::Type::kUint8:
5762 case DataType::Type::kInt8:
5763 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005764 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005765 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005766 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5767 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005768 StoreOperandType operand_type = GetStoreOperandType(field_type);
5769 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005770 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005771 break;
5772 }
5773
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 case DataType::Type::kReference: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005775 vixl32::Register value_reg = RegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005776 if (kPoisonHeapReferences && needs_write_barrier) {
5777 // Note that in the case where `value` is a null reference,
5778 // we do not enter this block, as a null reference does not
5779 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005780 DCHECK_EQ(field_type, DataType::Type::kReference);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005781 value_reg = RegisterFrom(locations->GetTemp(0));
5782 __ Mov(value_reg, RegisterFrom(value));
5783 GetAssembler()->PoisonHeapReference(value_reg);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005784 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005785 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5786 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5787 GetAssembler()->StoreToOffset(kStoreWord, value_reg, base, offset);
5788 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005789 break;
5790 }
5791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005792 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005793 if (is_volatile && !atomic_ldrd_strd) {
5794 GenerateWideAtomicStore(base,
5795 offset,
5796 LowRegisterFrom(value),
5797 HighRegisterFrom(value),
5798 RegisterFrom(locations->GetTemp(0)),
5799 RegisterFrom(locations->GetTemp(1)),
5800 instruction);
5801 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005802 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5803 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005804 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5805 codegen_->MaybeRecordImplicitNullCheck(instruction);
5806 }
5807 break;
5808 }
5809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005811 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5812 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005813 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005814 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005815 break;
5816 }
5817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005818 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005819 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005820 if (is_volatile && !atomic_ldrd_strd) {
5821 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5822 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5823
5824 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5825
5826 GenerateWideAtomicStore(base,
5827 offset,
5828 value_reg_lo,
5829 value_reg_hi,
5830 RegisterFrom(locations->GetTemp(2)),
5831 RegisterFrom(locations->GetTemp(3)),
5832 instruction);
5833 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005834 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5835 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005836 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5837 codegen_->MaybeRecordImplicitNullCheck(instruction);
5838 }
5839 break;
5840 }
5841
Aart Bik66c158e2018-01-31 12:55:04 -08005842 case DataType::Type::kUint32:
5843 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005844 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005845 LOG(FATAL) << "Unreachable type " << field_type;
5846 UNREACHABLE();
5847 }
5848
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005849 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5850 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5851 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5852 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5853 }
5854
5855 if (is_volatile) {
5856 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5857 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005858
5859 if (is_predicated) {
5860 __ Bind(&*pred_is_null);
5861 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005862}
5863
Artem Serov02d37832016-10-25 15:25:33 +01005864void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5865 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005866 DCHECK(instruction->IsInstanceFieldGet() ||
5867 instruction->IsStaticFieldGet() ||
5868 instruction->IsPredicatedInstanceFieldGet());
Artem Serov02d37832016-10-25 15:25:33 +01005869
5870 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005871 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00005872 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Artem Serov02d37832016-10-25 15:25:33 +01005873 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005874 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5875 object_field_get_with_read_barrier
5876 ? LocationSummary::kCallOnSlowPath
5877 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005878 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5879 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5880 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005881 // Input for object receiver.
5882 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
Artem Serov02d37832016-10-25 15:25:33 +01005883
5884 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005886 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5887 // The output overlaps in case of volatile long: we don't want the
5888 // code generated by GenerateWideAtomicLoad to overwrite the
5889 // object's location. Likewise, in the case of an object field get
5890 // with read barriers enabled, we do not want the load to overwrite
5891 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005892 bool overlap =
5893 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005894 object_field_get_with_read_barrier;
5895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005896 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005897 if (is_predicated) {
5898 locations->SetInAt(0, Location::RequiresFpuRegister());
5899 locations->SetOut(Location::SameAsFirstInput());
5900 } else {
5901 locations->SetOut(Location::RequiresFpuRegister());
5902 }
Artem Serov02d37832016-10-25 15:25:33 +01005903 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00005904 if (is_predicated) {
5905 locations->SetInAt(0, Location::RequiresRegister());
5906 locations->SetOut(Location::SameAsFirstInput());
5907 } else {
5908 locations->SetOut(Location::RequiresRegister(),
5909 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5910 }
Artem Serov02d37832016-10-25 15:25:33 +01005911 }
5912 if (volatile_for_double) {
5913 // ARM encoding have some additional constraints for ldrexd/strexd:
5914 // - registers need to be consecutive
5915 // - the first register should be even but not R14.
5916 // We don't test for ARM yet, and the assertion makes sure that we
5917 // revisit this if we ever enable ARM encoding.
5918 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5919 locations->AddTemp(Location::RequiresRegister());
5920 locations->AddTemp(Location::RequiresRegister());
5921 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005922 // We need a temporary register for the read barrier load in
5923 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5924 // only if the offset is too big.
5925 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005926 locations->AddTemp(Location::RequiresRegister());
5927 }
Artem Serov02d37832016-10-25 15:25:33 +01005928 }
5929}
5930
5931Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005932 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005933 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5934 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5935 return Location::ConstantLocation(input->AsConstant());
5936 } else {
5937 return Location::RequiresFpuRegister();
5938 }
5939}
5940
Artem Serov02109dd2016-09-23 17:17:54 +01005941Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5942 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005944 if (constant->IsConstant() &&
5945 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5946 return Location::ConstantLocation(constant->AsConstant());
5947 }
5948 return Location::RequiresRegister();
5949}
5950
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005951static bool CanEncode32BitConstantAsImmediate(
5952 CodeGeneratorARMVIXL* codegen,
5953 uint32_t value,
5954 Opcode opcode,
5955 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5956 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5957 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005958 return true;
5959 }
5960 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005961 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005962 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005963 case AND: neg_opcode = BIC; neg_value = ~value; break;
5964 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5965 case ADD: neg_opcode = SUB; neg_value = -value; break;
5966 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5967 case SUB: neg_opcode = ADD; neg_value = -value; break;
5968 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5969 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005970 default:
5971 return false;
5972 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005973
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005974 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005975 return true;
5976 }
5977
5978 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005979}
5980
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005981bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5982 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5983 if (DataType::Is64BitType(input_cst->GetType())) {
5984 Opcode high_opcode = opcode;
5985 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5986 switch (opcode) {
5987 case SUB:
5988 // Flip the operation to an ADD.
5989 value = -value;
5990 opcode = ADD;
5991 FALLTHROUGH_INTENDED;
5992 case ADD:
5993 if (Low32Bits(value) == 0u) {
5994 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5995 }
5996 high_opcode = ADC;
5997 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5998 break;
5999 default:
6000 break;
6001 }
6002 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
6003 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
6004 } else {
6005 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
6006 }
6007}
6008
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006009void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
6010 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00006011 DCHECK(instruction->IsInstanceFieldGet() ||
6012 instruction->IsStaticFieldGet() ||
6013 instruction->IsPredicatedInstanceFieldGet());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006014
6015 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00006016 uint32_t receiver_input = instruction->IsPredicatedInstanceFieldGet() ? 1 : 0;
6017 vixl32::Register base = InputRegisterAt(instruction, receiver_input);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006018 Location out = locations->Out();
6019 bool is_volatile = field_info.IsVolatile();
6020 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01006021 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6022 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006023 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
6024
Vladimir Marko61b92282017-10-11 13:23:17 +01006025 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006026 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006027 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006028 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006030 case DataType::Type::kInt16:
6031 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006032 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6033 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01006034 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006035 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006036 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006037 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006038 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006040 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006041 // /* HeapReference<Object> */ out = *(base + offset)
6042 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006043 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006044 // Note that a potential implicit null check is handled in this
6045 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
6046 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006047 instruction, out, base, offset, maybe_temp, /* needs_null_check= */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006048 if (is_volatile) {
6049 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6050 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006051 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006052 {
6053 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6054 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6055 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
6056 codegen_->MaybeRecordImplicitNullCheck(instruction);
6057 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006058 if (is_volatile) {
6059 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6060 }
6061 // If read barriers are enabled, emit read barriers other than
6062 // Baker's using a slow path (and also unpoison the loaded
6063 // reference, if heap poisoning is enabled).
Alex Light3a73ffb2021-01-25 14:11:05 +00006064 codegen_->MaybeGenerateReadBarrierSlow(
6065 instruction, out, out, locations->InAt(receiver_input), offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006066 }
6067 break;
6068 }
6069
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006070 case DataType::Type::kInt64: {
6071 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6072 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006073 if (is_volatile && !atomic_ldrd_strd) {
6074 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6075 } else {
6076 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6077 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006078 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006079 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006080 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006081
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006082 case DataType::Type::kFloat32: {
6083 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6084 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006085 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006086 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006087 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006088 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006090 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006091 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6092 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006093 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006094 if (is_volatile && !atomic_ldrd_strd) {
6095 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6096 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6097 GenerateWideAtomicLoad(base, offset, lo, hi);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006098 codegen_->MaybeRecordImplicitNullCheck(instruction);
6099 __ Vmov(out_dreg, lo, hi);
6100 } else {
6101 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006102 codegen_->MaybeRecordImplicitNullCheck(instruction);
6103 }
6104 break;
6105 }
6106
Aart Bik66c158e2018-01-31 12:55:04 -08006107 case DataType::Type::kUint32:
6108 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006109 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006110 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006111 UNREACHABLE();
6112 }
6113
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006114 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006115 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006116 // Memory barriers, in the case of references, are also handled
6117 // in the previous switch statement.
6118 } else {
6119 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6120 }
6121 }
6122}
6123
6124void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6125 HandleFieldSet(instruction, instruction->GetFieldInfo());
6126}
6127
6128void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6129 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6130}
6131
6132void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6133 HandleFieldGet(instruction, instruction->GetFieldInfo());
6134}
6135
Alex Light3a73ffb2021-01-25 14:11:05 +00006136void LocationsBuilderARMVIXL::VisitPredicatedInstanceFieldGet(
6137 HPredicatedInstanceFieldGet* instruction) {
6138 HandleFieldGet(instruction, instruction->GetFieldInfo());
6139}
6140
6141void InstructionCodeGeneratorARMVIXL::VisitPredicatedInstanceFieldGet(
6142 HPredicatedInstanceFieldGet* instruction) {
6143 vixl::aarch32::Label finish;
6144 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 1), &finish, false);
6145 HandleFieldGet(instruction, instruction->GetFieldInfo());
6146 __ Bind(&finish);
6147}
6148
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006149void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6150 HandleFieldGet(instruction, instruction->GetFieldInfo());
6151}
6152
6153void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6154 HandleFieldGet(instruction, instruction->GetFieldInfo());
6155}
6156
6157void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6158 HandleFieldGet(instruction, instruction->GetFieldInfo());
6159}
6160
Scott Wakelingc34dba72016-10-03 10:14:44 +01006161void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6162 HandleFieldSet(instruction, instruction->GetFieldInfo());
6163}
6164
6165void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6166 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6167}
6168
Vladimir Marko552a1342017-10-31 10:56:47 +00006169void LocationsBuilderARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6170 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(r0));
6171}
6172
6173void InstructionCodeGeneratorARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6174 __ Mov(r0, instruction->GetFormat()->GetValue());
6175 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6176}
6177
Artem Serovcfbe9132016-10-14 15:58:56 +01006178void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6179 HUnresolvedInstanceFieldGet* instruction) {
6180 FieldAccessCallingConventionARMVIXL calling_convention;
6181 codegen_->CreateUnresolvedFieldLocationSummary(
6182 instruction, instruction->GetFieldType(), calling_convention);
6183}
6184
6185void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6186 HUnresolvedInstanceFieldGet* instruction) {
6187 FieldAccessCallingConventionARMVIXL calling_convention;
6188 codegen_->GenerateUnresolvedFieldAccess(instruction,
6189 instruction->GetFieldType(),
6190 instruction->GetFieldIndex(),
6191 instruction->GetDexPc(),
6192 calling_convention);
6193}
6194
6195void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6196 HUnresolvedInstanceFieldSet* instruction) {
6197 FieldAccessCallingConventionARMVIXL calling_convention;
6198 codegen_->CreateUnresolvedFieldLocationSummary(
6199 instruction, instruction->GetFieldType(), calling_convention);
6200}
6201
6202void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6203 HUnresolvedInstanceFieldSet* instruction) {
6204 FieldAccessCallingConventionARMVIXL calling_convention;
6205 codegen_->GenerateUnresolvedFieldAccess(instruction,
6206 instruction->GetFieldType(),
6207 instruction->GetFieldIndex(),
6208 instruction->GetDexPc(),
6209 calling_convention);
6210}
6211
6212void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6213 HUnresolvedStaticFieldGet* instruction) {
6214 FieldAccessCallingConventionARMVIXL calling_convention;
6215 codegen_->CreateUnresolvedFieldLocationSummary(
6216 instruction, instruction->GetFieldType(), calling_convention);
6217}
6218
6219void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6220 HUnresolvedStaticFieldGet* instruction) {
6221 FieldAccessCallingConventionARMVIXL calling_convention;
6222 codegen_->GenerateUnresolvedFieldAccess(instruction,
6223 instruction->GetFieldType(),
6224 instruction->GetFieldIndex(),
6225 instruction->GetDexPc(),
6226 calling_convention);
6227}
6228
6229void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6230 HUnresolvedStaticFieldSet* instruction) {
6231 FieldAccessCallingConventionARMVIXL calling_convention;
6232 codegen_->CreateUnresolvedFieldLocationSummary(
6233 instruction, instruction->GetFieldType(), calling_convention);
6234}
6235
6236void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6237 HUnresolvedStaticFieldSet* instruction) {
6238 FieldAccessCallingConventionARMVIXL calling_convention;
6239 codegen_->GenerateUnresolvedFieldAccess(instruction,
6240 instruction->GetFieldType(),
6241 instruction->GetFieldIndex(),
6242 instruction->GetDexPc(),
6243 calling_convention);
6244}
6245
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006246void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006247 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006248 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006249}
6250
6251void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6252 if (CanMoveNullCheckToUser(instruction)) {
6253 return;
6254 }
6255
6256 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006257 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006258 ExactAssemblyScope aas(GetVIXLAssembler(),
6259 vixl32::kMaxInstructionSizeInBytes,
6260 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006261 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6262 RecordPcInfo(instruction, instruction->GetDexPc());
6263}
6264
6265void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6266 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006267 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006268 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006269 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006270}
6271
6272void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6273 codegen_->GenerateNullCheck(instruction);
6274}
6275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006276void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006277 Location out_loc,
6278 vixl32::Register base,
6279 vixl32::Register reg_index,
6280 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006281 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006282 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6283
6284 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006285 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006286 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006287 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6288 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006289 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006290 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6291 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006293 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6294 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006295 case DataType::Type::kInt16:
6296 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006298 case DataType::Type::kReference:
6299 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006300 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6301 break;
6302 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006303 case DataType::Type::kInt64:
6304 case DataType::Type::kFloat32:
6305 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006306 default:
6307 LOG(FATAL) << "Unreachable type " << type;
6308 UNREACHABLE();
6309 }
6310}
6311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006312void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006313 Location loc,
6314 vixl32::Register base,
6315 vixl32::Register reg_index,
6316 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006318 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6319
6320 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006322 case DataType::Type::kUint8:
6323 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006324 __ Strb(cond, RegisterFrom(loc), mem_address);
6325 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006326 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006327 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006328 __ Strh(cond, RegisterFrom(loc), mem_address);
6329 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006330 case DataType::Type::kReference:
6331 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006332 __ Str(cond, RegisterFrom(loc), mem_address);
6333 break;
6334 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 case DataType::Type::kInt64:
6336 case DataType::Type::kFloat32:
6337 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 default:
6339 LOG(FATAL) << "Unreachable type " << type;
6340 UNREACHABLE();
6341 }
6342}
6343
6344void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6345 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006347 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006348 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6349 object_array_get_with_read_barrier
6350 ? LocationSummary::kCallOnSlowPath
6351 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006352 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006353 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006354 }
6355 locations->SetInAt(0, Location::RequiresRegister());
6356 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006357 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006358 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6359 } else {
6360 // The output overlaps in the case of an object array get with
6361 // read barriers enabled: we do not want the move to overwrite the
6362 // array's location, as we need it to emit the read barrier.
6363 locations->SetOut(
6364 Location::RequiresRegister(),
6365 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6366 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006367 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006368 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006369 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01006370 // We need a temporary register for the read barrier load in
6371 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
6372 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006373 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6374 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006376 if (offset >= kReferenceLoadMinFarOffset) {
6377 locations->AddTemp(Location::RequiresRegister());
6378 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006379 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006380 // We need a non-scratch temporary for the array data pointer in
6381 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006382 locations->AddTemp(Location::RequiresRegister());
6383 }
6384 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6385 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006386 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006387 }
6388}
6389
6390void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006391 LocationSummary* locations = instruction->GetLocations();
6392 Location obj_loc = locations->InAt(0);
6393 vixl32::Register obj = InputRegisterAt(instruction, 0);
6394 Location index = locations->InAt(1);
6395 Location out_loc = locations->Out();
6396 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006398 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6399 instruction->IsStringCharAt();
6400 HInstruction* array_instr = instruction->GetArray();
6401 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006402
6403 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006404 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006405 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006406 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006407 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006408 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006410 vixl32::Register length;
6411 if (maybe_compressed_char_at) {
6412 length = RegisterFrom(locations->GetTemp(0));
6413 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006414 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6415 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006416 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6417 codegen_->MaybeRecordImplicitNullCheck(instruction);
6418 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006419 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006420 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006421 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006422 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006423 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006424 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6425 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6426 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006427 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006428 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6429 RegisterFrom(out_loc),
6430 obj,
6431 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006432 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006433 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006434 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006435 RegisterFrom(out_loc),
6436 obj,
6437 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006438 if (done.IsReferenced()) {
6439 __ Bind(&done);
6440 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006441 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006443
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006444 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6445 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006446 LoadOperandType load_type = GetLoadOperandType(type);
6447 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006448 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006449 }
6450 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006451 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006452 vixl32::Register temp = temps.Acquire();
6453
6454 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006455 // We do not need to compute the intermediate address from the array: the
6456 // input instruction has done it already. See the comment in
6457 // `TryExtractArrayAccessAddress()`.
6458 if (kIsDebugBuild) {
6459 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006460 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006461 }
6462 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006463 } else {
6464 __ Add(temp, obj, data_offset);
6465 }
6466 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006467 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006468 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006469 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6470 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6471 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006472 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006473 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006474 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006475 __ Bind(&uncompressed_load);
6476 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006477 if (done.IsReferenced()) {
6478 __ Bind(&done);
6479 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006480 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006481 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6482 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006483 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006484 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006485 }
6486 }
6487 break;
6488 }
6489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006490 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006491 // The read barrier instrumentation of object ArrayGet
6492 // instructions does not support the HIntermediateAddress
6493 // instruction.
6494 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6495
Scott Wakelingc34dba72016-10-03 10:14:44 +01006496 static_assert(
6497 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6498 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6499 // /* HeapReference<Object> */ out =
6500 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6501 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006502 // Note that a potential implicit null check is handled in this
6503 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006504 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6505 if (index.IsConstant()) {
6506 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006507 Location maybe_temp =
6508 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006510 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6511 out_loc,
6512 obj,
6513 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006514 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006515 /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006516 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006517 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006518 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006519 out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006520 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006521 } else {
6522 vixl32::Register out = OutputRegister(instruction);
6523 if (index.IsConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006524 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6525 {
6526 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6527 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6528 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
6529 codegen_->MaybeRecordImplicitNullCheck(instruction);
6530 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006531 // If read barriers are enabled, emit read barriers other than
6532 // Baker's using a slow path (and also unpoison the loaded
6533 // reference, if heap poisoning is enabled).
6534 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6535 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006536 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006537 vixl32::Register temp = temps.Acquire();
6538
6539 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006540 // We do not need to compute the intermediate address from the array: the
6541 // input instruction has done it already. See the comment in
6542 // `TryExtractArrayAccessAddress()`.
6543 if (kIsDebugBuild) {
6544 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006545 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006546 }
6547 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006548 } else {
6549 __ Add(temp, obj, data_offset);
6550 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006551 {
6552 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6553 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6554 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6555 temps.Close();
6556 codegen_->MaybeRecordImplicitNullCheck(instruction);
6557 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006558 // If read barriers are enabled, emit read barriers other than
6559 // Baker's using a slow path (and also unpoison the loaded
6560 // reference, if heap poisoning is enabled).
6561 codegen_->MaybeGenerateReadBarrierSlow(
6562 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6563 }
6564 }
6565 break;
6566 }
6567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006568 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006569 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6570 // As two macro instructions can be emitted the max size is doubled.
6571 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006572 if (index.IsConstant()) {
6573 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006574 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006575 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6576 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006577 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006578 vixl32::Register temp = temps.Acquire();
6579 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6580 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6581 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006582 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006583 break;
6584 }
6585
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006586 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006587 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6588 // As two macro instructions can be emitted the max size is doubled.
6589 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006590 vixl32::SRegister out = SRegisterFrom(out_loc);
6591 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006592 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006593 GetAssembler()->LoadSFromOffset(out, obj, offset);
6594 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006595 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006596 vixl32::Register temp = temps.Acquire();
6597 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6598 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6599 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006600 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006601 break;
6602 }
6603
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006604 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006605 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6606 // As two macro instructions can be emitted the max size is doubled.
6607 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006608 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006609 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006610 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6611 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006612 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006613 vixl32::Register temp = temps.Acquire();
6614 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6615 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6616 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006617 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006618 break;
6619 }
6620
Aart Bik66c158e2018-01-31 12:55:04 -08006621 case DataType::Type::kUint32:
6622 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006623 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006624 LOG(FATAL) << "Unreachable type " << type;
6625 UNREACHABLE();
6626 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006627}
6628
6629void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006630 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006631
6632 bool needs_write_barrier =
6633 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006634 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006635
Vladimir Markoca6fff82017-10-03 14:49:14 +01006636 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006637 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006638 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006639
6640 locations->SetInAt(0, Location::RequiresRegister());
6641 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006642 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006643 locations->SetInAt(2, Location::RequiresFpuRegister());
6644 } else {
6645 locations->SetInAt(2, Location::RequiresRegister());
6646 }
6647 if (needs_write_barrier) {
6648 // Temporary registers for the write barrier.
6649 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6650 locations->AddTemp(Location::RequiresRegister());
6651 }
6652}
6653
6654void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006655 LocationSummary* locations = instruction->GetLocations();
6656 vixl32::Register array = InputRegisterAt(instruction, 0);
6657 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006658 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006659 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006660 bool needs_write_barrier =
6661 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6662 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006663 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006664 Location value_loc = locations->InAt(2);
6665 HInstruction* array_instr = instruction->GetArray();
6666 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006667
6668 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006669 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006670 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006671 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006672 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006673 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006674 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006675 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006676 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006677 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006678 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006679 StoreOperandType store_type = GetStoreOperandType(value_type);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006680 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6681 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006682 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006683 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006684 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006685 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006686 vixl32::Register temp = temps.Acquire();
6687
6688 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006689 // We do not need to compute the intermediate address from the array: the
6690 // input instruction has done it already. See the comment in
6691 // `TryExtractArrayAccessAddress()`.
6692 if (kIsDebugBuild) {
6693 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006694 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006695 }
6696 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006697 } else {
6698 __ Add(temp, array, data_offset);
6699 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006700 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6701 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006702 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006703 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006704 }
6705 break;
6706 }
6707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006708 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006709 vixl32::Register value = RegisterFrom(value_loc);
6710 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6711 // See the comment in instruction_simplifier_shared.cc.
6712 DCHECK(!has_intermediate_address);
6713
6714 if (instruction->InputAt(2)->IsNullConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006715 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6716 // As two macro instructions can be emitted the max size is doubled.
6717 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006718 // Just setting null.
6719 if (index.IsConstant()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006720 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006721 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6722 } else {
6723 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006724 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006725 vixl32::Register temp = temps.Acquire();
6726 __ Add(temp, array, data_offset);
6727 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6728 }
6729 codegen_->MaybeRecordImplicitNullCheck(instruction);
6730 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006731 DCHECK(!needs_type_check);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006732 break;
6733 }
6734
6735 DCHECK(needs_write_barrier);
6736 Location temp1_loc = locations->GetTemp(0);
6737 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6738 Location temp2_loc = locations->GetTemp(1);
6739 vixl32::Register temp2 = RegisterFrom(temp2_loc);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006740
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006741 bool can_value_be_null = instruction->GetValueCanBeNull();
6742 vixl32::Label do_store;
6743 if (can_value_be_null) {
6744 __ CompareAndBranchIfZero(value, &do_store, /* is_far_target= */ false);
6745 }
6746
6747 SlowPathCodeARMVIXL* slow_path = nullptr;
6748 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006749 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006750 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006751
6752 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6753 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6754 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006755
6756 // Note that when read barriers are enabled, the type checks
6757 // are performed without read barriers. This is fine, even in
6758 // the case where a class object is in the from-space after
6759 // the flip, as a comparison involving such a type would not
6760 // produce a false positive; it may of course produce a false
6761 // negative, in which case we would take the ArraySet slow
6762 // path.
6763
Alexandre Rames374ddf32016-11-04 10:40:49 +00006764 {
6765 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006766 ExactAssemblyScope aas(GetVIXLAssembler(),
6767 vixl32::kMaxInstructionSizeInBytes,
6768 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006769 // /* HeapReference<Class> */ temp1 = array->klass_
6770 __ ldr(temp1, MemOperand(array, class_offset));
6771 codegen_->MaybeRecordImplicitNullCheck(instruction);
6772 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006773 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6774
6775 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6776 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6777 // /* HeapReference<Class> */ temp2 = value->klass_
6778 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6779 // If heap poisoning is enabled, no need to unpoison `temp1`
6780 // nor `temp2`, as we are comparing two poisoned references.
6781 __ Cmp(temp1, temp2);
6782
6783 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006784 vixl32::Label do_put;
6785 __ B(eq, &do_put, /* is_far_target= */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006786 // If heap poisoning is enabled, the `temp1` reference has
6787 // not been unpoisoned yet; unpoison it now.
6788 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6789
6790 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6791 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6792 // If heap poisoning is enabled, no need to unpoison
6793 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006794 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006795 __ Bind(&do_put);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006796 } else {
6797 __ B(ne, slow_path->GetEntryLabel());
6798 }
6799 }
6800
Vladimir Markoac3fcff2020-11-17 12:17:58 +00006801 codegen_->MarkGCCard(temp1, temp2, array, value, /* value_can_be_null= */ false);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006802
6803 if (can_value_be_null) {
6804 DCHECK(do_store.IsReferenced());
6805 __ Bind(&do_store);
6806 }
6807
Scott Wakelingc34dba72016-10-03 10:14:44 +01006808 vixl32::Register source = value;
6809 if (kPoisonHeapReferences) {
6810 // Note that in the case where `value` is a null reference,
6811 // we do not enter this block, as a null reference does not
6812 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006813 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006814 __ Mov(temp1, value);
6815 GetAssembler()->PoisonHeapReference(temp1);
6816 source = temp1;
6817 }
6818
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006819 {
6820 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6821 // As two macro instructions can be emitted the max size is doubled.
6822 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
6823 if (index.IsConstant()) {
6824 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6825 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6826 } else {
6827 DCHECK(index.IsRegister()) << index;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006828
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006829 UseScratchRegisterScope temps(GetVIXLAssembler());
6830 vixl32::Register temp = temps.Acquire();
6831 __ Add(temp, array, data_offset);
6832 codegen_->StoreToShiftedRegOffset(value_type,
6833 LocationFrom(source),
6834 temp,
6835 RegisterFrom(index));
6836 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006837
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006838 if (can_value_be_null || !needs_type_check) {
6839 codegen_->MaybeRecordImplicitNullCheck(instruction);
6840 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006841 }
6842
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006843 if (slow_path != nullptr) {
6844 __ Bind(slow_path->GetExitLabel());
6845 }
6846
Scott Wakelingc34dba72016-10-03 10:14:44 +01006847 break;
6848 }
6849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006850 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006851 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6852 // As two macro instructions can be emitted the max size is doubled.
6853 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006854 Location value = locations->InAt(2);
6855 if (index.IsConstant()) {
6856 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006857 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006858 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6859 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006860 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006861 vixl32::Register temp = temps.Acquire();
6862 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6863 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6864 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006865 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006866 break;
6867 }
6868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006869 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006870 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6871 // As two macro instructions can be emitted the max size is doubled.
6872 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006873 Location value = locations->InAt(2);
6874 DCHECK(value.IsFpuRegister());
6875 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006876 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006877 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6878 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006879 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006880 vixl32::Register temp = temps.Acquire();
6881 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6882 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6883 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006884 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006885 break;
6886 }
6887
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006888 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006889 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6890 // As two macro instructions can be emitted the max size is doubled.
6891 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006892 Location value = locations->InAt(2);
6893 DCHECK(value.IsFpuRegisterPair());
6894 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006895 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006896 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6897 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006898 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006899 vixl32::Register temp = temps.Acquire();
6900 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6901 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6902 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006903 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006904 break;
6905 }
6906
Aart Bik66c158e2018-01-31 12:55:04 -08006907 case DataType::Type::kUint32:
6908 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006909 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006910 LOG(FATAL) << "Unreachable type " << value_type;
6911 UNREACHABLE();
6912 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006913}
6914
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006915void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6916 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006917 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006918 locations->SetInAt(0, Location::RequiresRegister());
6919 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6920}
6921
6922void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6923 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6924 vixl32::Register obj = InputRegisterAt(instruction, 0);
6925 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006926 {
Artem Serov0fb37192016-12-06 18:13:40 +00006927 ExactAssemblyScope aas(GetVIXLAssembler(),
6928 vixl32::kMaxInstructionSizeInBytes,
6929 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006930 __ ldr(out, MemOperand(obj, offset));
6931 codegen_->MaybeRecordImplicitNullCheck(instruction);
6932 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006933 // Mask out compression flag from String's array length.
6934 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006935 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006936 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006937}
6938
Artem Serov2bbc9532016-10-21 11:51:50 +01006939void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006940 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006941 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006942
6943 locations->SetInAt(0, Location::RequiresRegister());
6944 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6946}
6947
6948void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6949 vixl32::Register out = OutputRegister(instruction);
6950 vixl32::Register first = InputRegisterAt(instruction, 0);
6951 Location second = instruction->GetLocations()->InAt(1);
6952
Artem Serov2bbc9532016-10-21 11:51:50 +01006953 if (second.IsRegister()) {
6954 __ Add(out, first, RegisterFrom(second));
6955 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006956 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006957 }
6958}
6959
Artem Serove1811ed2017-04-27 16:50:47 +01006960void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6961 HIntermediateAddressIndex* instruction) {
6962 LOG(FATAL) << "Unreachable " << instruction->GetId();
6963}
6964
6965void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6966 HIntermediateAddressIndex* instruction) {
6967 LOG(FATAL) << "Unreachable " << instruction->GetId();
6968}
6969
Scott Wakelingc34dba72016-10-03 10:14:44 +01006970void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6971 RegisterSet caller_saves = RegisterSet::Empty();
6972 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6973 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6974 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6975 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006976
6977 HInstruction* index = instruction->InputAt(0);
6978 HInstruction* length = instruction->InputAt(1);
6979 // If both index and length are constants we can statically check the bounds. But if at least one
6980 // of them is not encodable ArmEncodableConstantOrRegister will create
6981 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6982 // locations.
6983 bool both_const = index->IsConstant() && length->IsConstant();
6984 locations->SetInAt(0, both_const
6985 ? Location::ConstantLocation(index->AsConstant())
6986 : ArmEncodableConstantOrRegister(index, CMP));
6987 locations->SetInAt(1, both_const
6988 ? Location::ConstantLocation(length->AsConstant())
6989 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006990}
6991
6992void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006993 LocationSummary* locations = instruction->GetLocations();
6994 Location index_loc = locations->InAt(0);
6995 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006996
Artem Serov2dd053d2017-03-08 14:54:06 +00006997 if (length_loc.IsConstant()) {
6998 int32_t length = Int32ConstantFrom(length_loc);
6999 if (index_loc.IsConstant()) {
7000 // BCE will remove the bounds check if we are guaranteed to pass.
7001 int32_t index = Int32ConstantFrom(index_loc);
7002 if (index < 0 || index >= length) {
7003 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007004 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007005 codegen_->AddSlowPath(slow_path);
7006 __ B(slow_path->GetEntryLabel());
7007 } else {
7008 // Some optimization after BCE may have generated this, and we should not
7009 // generate a bounds check if it is a valid range.
7010 }
7011 return;
7012 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01007013
Artem Serov2dd053d2017-03-08 14:54:06 +00007014 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007015 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007016 __ Cmp(RegisterFrom(index_loc), length);
7017 codegen_->AddSlowPath(slow_path);
7018 __ B(hs, slow_path->GetEntryLabel());
7019 } else {
7020 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007021 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007022 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
7023 codegen_->AddSlowPath(slow_path);
7024 __ B(ls, slow_path->GetEntryLabel());
7025 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01007026}
7027
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007028void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
7029 vixl32::Register card,
7030 vixl32::Register object,
7031 vixl32::Register value,
Vladimir Markoac3fcff2020-11-17 12:17:58 +00007032 bool value_can_be_null) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007033 vixl32::Label is_null;
Vladimir Markoac3fcff2020-11-17 12:17:58 +00007034 if (value_can_be_null) {
7035 __ CompareAndBranchIfZero(value, &is_null, /* is_far_target=*/ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007036 }
Roland Levillainc73f0522018-08-14 15:16:50 +01007037 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007038 GetAssembler()->LoadFromOffset(
7039 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01007040 // Calculate the offset (in the card table) of the card corresponding to
7041 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00007042 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01007043 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
7044 // `object`'s card.
7045 //
7046 // Register `card` contains the address of the card table. Note that the card
7047 // table's base is biased during its creation so that it always starts at an
7048 // address whose least-significant byte is equal to `kCardDirty` (see
7049 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
7050 // below writes the `kCardDirty` (byte) value into the `object`'s card
7051 // (located at `card + object >> kCardShift`).
7052 //
7053 // This dual use of the value in register `card` (1. to calculate the location
7054 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
7055 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007056 __ Strb(card, MemOperand(card, temp));
Vladimir Markoac3fcff2020-11-17 12:17:58 +00007057 if (value_can_be_null) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007058 __ Bind(&is_null);
7059 }
7060}
7061
Scott Wakelingfe885462016-09-22 10:24:38 +01007062void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7063 LOG(FATAL) << "Unreachable";
7064}
7065
7066void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01007067 if (instruction->GetNext()->IsSuspendCheck() &&
7068 instruction->GetBlock()->GetLoopInformation() != nullptr) {
7069 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
7070 // The back edge will generate the suspend check.
7071 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
7072 }
7073
Scott Wakelingfe885462016-09-22 10:24:38 +01007074 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7075}
7076
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007077void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007078 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7079 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00007080 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007081}
7082
7083void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
7084 HBasicBlock* block = instruction->GetBlock();
7085 if (block->GetLoopInformation() != nullptr) {
7086 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7087 // The back edge will generate the suspend check.
7088 return;
7089 }
7090 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7091 // The goto will generate the suspend check.
7092 return;
7093 }
7094 GenerateSuspendCheck(instruction, nullptr);
Andra Danciua0130e82020-07-23 12:34:56 +00007095 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007096}
7097
7098void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7099 HBasicBlock* successor) {
7100 SuspendCheckSlowPathARMVIXL* slow_path =
7101 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7102 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007103 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007104 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007105 instruction->SetSlowPath(slow_path);
7106 codegen_->AddSlowPath(slow_path);
7107 if (successor != nullptr) {
7108 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007109 }
7110 } else {
7111 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7112 }
7113
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007114 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007115 vixl32::Register temp = temps.Acquire();
7116 GetAssembler()->LoadFromOffset(
7117 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7118 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007119 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007120 __ Bind(slow_path->GetReturnLabel());
7121 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007122 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007123 __ B(slow_path->GetEntryLabel());
7124 }
7125}
7126
Scott Wakelingfe885462016-09-22 10:24:38 +01007127ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7128 return codegen_->GetAssembler();
7129}
7130
7131void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007132 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007133 MoveOperands* move = moves_[index];
7134 Location source = move->GetSource();
7135 Location destination = move->GetDestination();
7136
7137 if (source.IsRegister()) {
7138 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007139 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007140 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007141 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007142 } else {
7143 DCHECK(destination.IsStackSlot());
7144 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007145 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007146 sp,
7147 destination.GetStackIndex());
7148 }
7149 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007150 if (destination.IsRegister()) {
7151 GetAssembler()->LoadFromOffset(kLoadWord,
7152 RegisterFrom(destination),
7153 sp,
7154 source.GetStackIndex());
7155 } else if (destination.IsFpuRegister()) {
7156 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7157 } else {
7158 DCHECK(destination.IsStackSlot());
7159 vixl32::Register temp = temps.Acquire();
7160 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7161 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7162 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007163 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007164 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007165 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007166 } else if (destination.IsFpuRegister()) {
7167 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7168 } else {
7169 DCHECK(destination.IsStackSlot());
7170 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7171 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007172 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007173 if (destination.IsDoubleStackSlot()) {
7174 vixl32::DRegister temp = temps.AcquireD();
7175 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7176 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7177 } else if (destination.IsRegisterPair()) {
7178 DCHECK(ExpectedPairLayout(destination));
7179 GetAssembler()->LoadFromOffset(
7180 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7181 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007182 DCHECK(destination.IsFpuRegisterPair()) << destination;
7183 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007184 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007185 } else if (source.IsRegisterPair()) {
7186 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007187 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7188 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007189 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007190 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007191 } else {
7192 DCHECK(destination.IsDoubleStackSlot()) << destination;
7193 DCHECK(ExpectedPairLayout(source));
7194 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007195 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007196 sp,
7197 destination.GetStackIndex());
7198 }
7199 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007200 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007201 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007202 } else if (destination.IsFpuRegisterPair()) {
7203 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7204 } else {
7205 DCHECK(destination.IsDoubleStackSlot()) << destination;
7206 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7207 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007208 } else {
7209 DCHECK(source.IsConstant()) << source;
7210 HConstant* constant = source.GetConstant();
7211 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7212 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7213 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007214 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007215 } else {
7216 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007217 vixl32::Register temp = temps.Acquire();
7218 __ Mov(temp, value);
7219 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7220 }
7221 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007222 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007223 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007224 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7225 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007226 } else {
7227 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007228 vixl32::Register temp = temps.Acquire();
7229 __ Mov(temp, Low32Bits(value));
7230 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7231 __ Mov(temp, High32Bits(value));
7232 GetAssembler()->StoreToOffset(kStoreWord,
7233 temp,
7234 sp,
7235 destination.GetHighStackIndex(kArmWordSize));
7236 }
7237 } else if (constant->IsDoubleConstant()) {
7238 double value = constant->AsDoubleConstant()->GetValue();
7239 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007240 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007241 } else {
7242 DCHECK(destination.IsDoubleStackSlot()) << destination;
7243 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007244 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007245 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007246 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007247 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007248 GetAssembler()->StoreToOffset(kStoreWord,
7249 temp,
7250 sp,
7251 destination.GetHighStackIndex(kArmWordSize));
7252 }
7253 } else {
7254 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7255 float value = constant->AsFloatConstant()->GetValue();
7256 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007257 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007258 } else {
7259 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007260 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007261 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007262 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7263 }
7264 }
7265 }
7266}
7267
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007268void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7269 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7270 vixl32::Register temp = temps.Acquire();
7271 __ Mov(temp, reg);
7272 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7273 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007274}
7275
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007276void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7277 // TODO(VIXL32): Double check the performance of this implementation.
7278 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007279 vixl32::Register temp1 = temps.Acquire();
7280 ScratchRegisterScope ensure_scratch(
7281 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7282 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007283
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007284 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7285 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7286 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7287 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7288 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007289}
7290
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007291void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7292 MoveOperands* move = moves_[index];
7293 Location source = move->GetSource();
7294 Location destination = move->GetDestination();
7295 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7296
7297 if (source.IsRegister() && destination.IsRegister()) {
7298 vixl32::Register temp = temps.Acquire();
7299 DCHECK(!RegisterFrom(source).Is(temp));
7300 DCHECK(!RegisterFrom(destination).Is(temp));
7301 __ Mov(temp, RegisterFrom(destination));
7302 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7303 __ Mov(RegisterFrom(source), temp);
7304 } else if (source.IsRegister() && destination.IsStackSlot()) {
7305 Exchange(RegisterFrom(source), destination.GetStackIndex());
7306 } else if (source.IsStackSlot() && destination.IsRegister()) {
7307 Exchange(RegisterFrom(destination), source.GetStackIndex());
7308 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007309 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007310 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007311 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007312 __ Vmov(temp, SRegisterFrom(source));
7313 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7314 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007315 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7316 vixl32::DRegister temp = temps.AcquireD();
7317 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7318 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7319 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7320 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7321 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7322 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7323 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7324 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7325 vixl32::DRegister temp = temps.AcquireD();
7326 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7327 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7328 GetAssembler()->StoreDToOffset(temp, sp, mem);
7329 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007330 vixl32::DRegister first = DRegisterFrom(source);
7331 vixl32::DRegister second = DRegisterFrom(destination);
7332 vixl32::DRegister temp = temps.AcquireD();
7333 __ Vmov(temp, first);
7334 __ Vmov(first, second);
7335 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007336 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007337 vixl32::DRegister reg = source.IsFpuRegisterPair()
7338 ? DRegisterFrom(source)
7339 : DRegisterFrom(destination);
7340 int mem = source.IsFpuRegisterPair()
7341 ? destination.GetStackIndex()
7342 : source.GetStackIndex();
7343 vixl32::DRegister temp = temps.AcquireD();
7344 __ Vmov(temp, reg);
7345 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7346 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007347 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007348 vixl32::SRegister reg = source.IsFpuRegister()
7349 ? SRegisterFrom(source)
7350 : SRegisterFrom(destination);
7351 int mem = source.IsFpuRegister()
7352 ? destination.GetStackIndex()
7353 : source.GetStackIndex();
7354 vixl32::Register temp = temps.Acquire();
7355 __ Vmov(temp, reg);
7356 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7357 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007358 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7359 vixl32::DRegister temp1 = temps.AcquireD();
7360 vixl32::DRegister temp2 = temps.AcquireD();
7361 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7362 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7363 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7364 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7365 } else {
7366 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7367 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007368}
7369
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007370void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7371 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007372}
7373
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007374void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7375 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007376}
7377
Artem Serov02d37832016-10-25 15:25:33 +01007378HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007379 HLoadClass::LoadKind desired_class_load_kind) {
7380 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007381 case HLoadClass::LoadKind::kInvalid:
7382 LOG(FATAL) << "UNREACHABLE";
7383 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007384 case HLoadClass::LoadKind::kReferrersClass:
7385 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007386 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007387 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007388 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007389 case HLoadClass::LoadKind::kBssEntryPublic:
7390 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007391 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007392 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007393 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007394 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007395 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007396 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007397 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007398 break;
7399 }
7400 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007401}
7402
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007403void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007404 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007405 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007406 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007407 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007408 cls,
7409 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007410 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007411 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007412 return;
7413 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007414 DCHECK_EQ(cls->NeedsAccessCheck(),
7415 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7416 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelingfe885462016-09-22 10:24:38 +01007417
Artem Serovd4cc5b22016-11-04 11:19:09 +00007418 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7419 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007420 ? LocationSummary::kCallOnSlowPath
7421 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007422 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007423 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007424 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007425 }
7426
Vladimir Marko41559982017-01-06 14:04:23 +00007427 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007428 locations->SetInAt(0, Location::RequiresRegister());
7429 }
7430 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007431 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7432 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7433 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007434 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007435 } else {
7436 // For non-Baker read barrier we have a temp-clobbering call.
7437 }
7438 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007439}
7440
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007441// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7442// move.
7443void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007444 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007445 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007446 codegen_->GenerateLoadClassRuntimeCall(cls);
Andra Danciua0130e82020-07-23 12:34:56 +00007447 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007448 return;
7449 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007450 DCHECK_EQ(cls->NeedsAccessCheck(),
7451 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7452 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007453
Vladimir Marko41559982017-01-06 14:04:23 +00007454 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007455 Location out_loc = locations->Out();
7456 vixl32::Register out = OutputRegister(cls);
7457
Artem Serovd4cc5b22016-11-04 11:19:09 +00007458 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7459 ? kWithoutReadBarrier
7460 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007461 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007462 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007463 case HLoadClass::LoadKind::kReferrersClass: {
7464 DCHECK(!cls->CanCallRuntime());
7465 DCHECK(!cls->MustGenerateClinitCheck());
7466 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7467 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007468 codegen_->GenerateGcRootFieldLoad(cls,
7469 out_loc,
7470 current_method,
7471 ArtMethod::DeclaringClassOffset().Int32Value(),
7472 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007473 break;
7474 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007475 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007476 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7477 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007478 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7479 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007480 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007481 codegen_->EmitMovwMovtPlaceholder(labels, out);
7482 break;
7483 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007484 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007485 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7486 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markode91ca92020-10-27 13:41:40 +00007487 codegen_->NewBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007488 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007489 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007490 break;
7491 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007492 case HLoadClass::LoadKind::kBssEntry:
7493 case HLoadClass::LoadKind::kBssEntryPublic:
7494 case HLoadClass::LoadKind::kBssEntryPackage: {
7495 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels = codegen_->NewTypeBssEntryPatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00007496 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007497 // All aligned loads are implicitly atomic consume operations on ARM.
Andreas Gampe3db70682018-12-26 15:12:03 -08007498 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007499 generate_null_check = true;
7500 break;
7501 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007502 case HLoadClass::LoadKind::kJitBootImageAddress: {
7503 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7504 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7505 DCHECK_NE(address, 0u);
7506 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7507 break;
7508 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007509 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007510 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7511 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007512 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007513 // /* GcRoot<mirror::Class> */ out = *out
Andreas Gampe3db70682018-12-26 15:12:03 -08007514 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007515 break;
7516 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007517 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007518 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007519 LOG(FATAL) << "UNREACHABLE";
7520 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007521 }
7522
7523 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7524 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007525 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007526 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007527 codegen_->AddSlowPath(slow_path);
7528 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007529 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007530 }
7531 if (cls->MustGenerateClinitCheck()) {
7532 GenerateClassInitializationCheck(slow_path, out);
7533 } else {
7534 __ Bind(slow_path->GetExitLabel());
7535 }
Andra Danciua0130e82020-07-23 12:34:56 +00007536 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 16);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007537 }
7538}
7539
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007540void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7541 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7542 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7543 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7544}
7545
7546void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7547 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7548}
7549
Orion Hodson18259d72018-04-12 11:18:23 +01007550void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7551 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7552 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7553 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7554}
7555
7556void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7557 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7558}
7559
Artem Serov02d37832016-10-25 15:25:33 +01007560void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7561 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007562 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007563 locations->SetInAt(0, Location::RequiresRegister());
7564 if (check->HasUses()) {
7565 locations->SetOut(Location::SameAsFirstInput());
7566 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007567 // Rely on the type initialization to save everything we need.
7568 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007569}
7570
7571void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7572 // We assume the class is not null.
7573 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007574 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007575 codegen_->AddSlowPath(slow_path);
7576 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7577}
7578
7579void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7580 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7581 UseScratchRegisterScope temps(GetVIXLAssembler());
7582 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007583 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Markobf121912019-06-04 13:49:05 +01007584 constexpr uint32_t shifted_visibly_initialized_value =
7585 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << status_lsb_position;
Vladimir Markodc682aa2018-01-04 18:42:57 +00007586
Vladimir Markobf121912019-06-04 13:49:05 +01007587 const size_t status_offset = mirror::Class::StatusOffset().SizeValue();
7588 GetAssembler()->LoadFromOffset(kLoadWord, temp, class_reg, status_offset);
7589 __ Cmp(temp, shifted_visibly_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007590 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007591 __ Bind(slow_path->GetExitLabel());
7592}
7593
Vladimir Marko175e7862018-03-27 09:03:13 +00007594void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7595 HTypeCheckInstruction* check,
7596 vixl32::Register temp,
7597 vixl32::FlagsUpdate flags_update) {
7598 uint32_t path_to_root = check->GetBitstringPathToRoot();
7599 uint32_t mask = check->GetBitstringMask();
7600 DCHECK(IsPowerOfTwo(mask + 1));
7601 size_t mask_bits = WhichPowerOf2(mask + 1);
7602
7603 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7604 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7605 if (mask_bits == 16u) {
7606 // Load only the bitstring part of the status word.
7607 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7608 // Check if the bitstring bits are equal to `path_to_root`.
7609 if (flags_update == SetFlags) {
7610 __ Cmp(temp, path_to_root);
7611 } else {
7612 __ Sub(temp, temp, path_to_root);
7613 }
7614 } else {
7615 // /* uint32_t */ temp = temp->status_
7616 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7617 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7618 // Compare the bitstring bits using SUB.
7619 __ Sub(temp, temp, path_to_root);
7620 // Shift out bits that do not contribute to the comparison.
7621 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7622 } else if (IsUint<16>(path_to_root)) {
7623 if (temp.IsLow()) {
7624 // Note: Optimized for size but contains one more dependent instruction than necessary.
7625 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7626 // macro assembler would use the high reg IP for the constant by default.
7627 // Compare the bitstring bits using SUB.
7628 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7629 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7630 // Shift out bits that do not contribute to the comparison.
7631 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7632 } else {
7633 // Extract the bitstring bits.
7634 __ Ubfx(temp, temp, 0, mask_bits);
7635 // Check if the bitstring bits are equal to `path_to_root`.
7636 if (flags_update == SetFlags) {
7637 __ Cmp(temp, path_to_root);
7638 } else {
7639 __ Sub(temp, temp, path_to_root);
7640 }
7641 }
7642 } else {
7643 // Shift out bits that do not contribute to the comparison.
7644 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7645 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7646 if (flags_update == SetFlags) {
7647 __ Cmp(temp, path_to_root << (32u - mask_bits));
7648 } else {
7649 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7650 }
7651 }
7652 }
7653}
7654
Artem Serov02d37832016-10-25 15:25:33 +01007655HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007656 HLoadString::LoadKind desired_string_load_kind) {
7657 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007658 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007659 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007660 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007661 DCHECK(!GetCompilerOptions().IsJitCompiler());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007662 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007663 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007664 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007665 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007666 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007667 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007668 break;
7669 }
7670 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007671}
7672
7673void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007674 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007675 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007676 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007677 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007678 locations->SetOut(LocationFrom(r0));
7679 } else {
7680 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007681 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7682 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007683 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007684 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007685 } else {
7686 // For non-Baker read barrier we have a temp-clobbering call.
7687 }
7688 }
Artem Serov02d37832016-10-25 15:25:33 +01007689 }
7690}
7691
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007692// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7693// move.
7694void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007695 LocationSummary* locations = load->GetLocations();
7696 Location out_loc = locations->Out();
7697 vixl32::Register out = OutputRegister(load);
7698 HLoadString::LoadKind load_kind = load->GetLoadKind();
7699
7700 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007701 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007702 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7703 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007704 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007705 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007706 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007707 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007708 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007709 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007710 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7711 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markode91ca92020-10-27 13:41:40 +00007712 codegen_->NewBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007713 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007714 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007715 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007716 }
7717 case HLoadString::LoadKind::kBssEntry: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007718 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007719 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007720 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007721 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007722 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007723 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007724 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007725 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007726 codegen_->AddSlowPath(slow_path);
7727 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7728 __ Bind(slow_path->GetExitLabel());
Andra Danciua0130e82020-07-23 12:34:56 +00007729 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 17);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007730 return;
7731 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007732 case HLoadString::LoadKind::kJitBootImageAddress: {
7733 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7734 DCHECK_NE(address, 0u);
7735 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7736 return;
7737 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007738 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007739 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007740 load->GetStringIndex(),
7741 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007742 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007743 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007744 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007745 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007746 }
7747 default:
7748 break;
7749 }
Artem Serov02d37832016-10-25 15:25:33 +01007750
7751 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007752 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007753 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007754 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007755 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7756 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andra Danciua0130e82020-07-23 12:34:56 +00007757 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 18);
Artem Serov02d37832016-10-25 15:25:33 +01007758}
7759
7760static int32_t GetExceptionTlsOffset() {
7761 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7762}
7763
7764void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7765 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007766 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007767 locations->SetOut(Location::RequiresRegister());
7768}
7769
7770void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7771 vixl32::Register out = OutputRegister(load);
7772 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7773}
7774
7775
7776void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007777 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007778}
7779
7780void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7781 UseScratchRegisterScope temps(GetVIXLAssembler());
7782 vixl32::Register temp = temps.Acquire();
7783 __ Mov(temp, 0);
7784 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7785}
7786
7787void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007788 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7789 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007790 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7791 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7792}
7793
7794void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7795 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7796 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7797}
7798
Artem Serov657022c2016-11-23 14:19:38 +00007799// Temp is used for read barrier.
7800static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7801 if (kEmitCompilerReadBarrier &&
7802 (kUseBakerReadBarrier ||
7803 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7804 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7805 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7806 return 1;
7807 }
7808 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007809}
7810
Artem Serov657022c2016-11-23 14:19:38 +00007811// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7812// interface pointer, one for loading the current interface.
7813// The other checks have one temp for loading the object's class.
7814static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7815 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7816 return 3;
7817 }
7818 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7819}
Artem Serovcfbe9132016-10-14 15:58:56 +01007820
7821void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7822 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7823 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7824 bool baker_read_barrier_slow_path = false;
7825 switch (type_check_kind) {
7826 case TypeCheckKind::kExactCheck:
7827 case TypeCheckKind::kAbstractClassCheck:
7828 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007829 case TypeCheckKind::kArrayObjectCheck: {
7830 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7831 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7832 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007833 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007834 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007835 case TypeCheckKind::kArrayCheck:
7836 case TypeCheckKind::kUnresolvedCheck:
7837 case TypeCheckKind::kInterfaceCheck:
7838 call_kind = LocationSummary::kCallOnSlowPath;
7839 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007840 case TypeCheckKind::kBitstringCheck:
7841 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007842 }
7843
Vladimir Markoca6fff82017-10-03 14:49:14 +01007844 LocationSummary* locations =
7845 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007846 if (baker_read_barrier_slow_path) {
7847 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7848 }
7849 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007850 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7851 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7852 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7853 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7854 } else {
7855 locations->SetInAt(1, Location::RequiresRegister());
7856 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007857 // The "out" register is used as a temporary, so it overlaps with the inputs.
7858 // Note that TypeCheckSlowPathARM uses this register too.
7859 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007860 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007861}
7862
7863void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7864 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7865 LocationSummary* locations = instruction->GetLocations();
7866 Location obj_loc = locations->InAt(0);
7867 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007868 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7869 ? vixl32::Register()
7870 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007871 Location out_loc = locations->Out();
7872 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007873 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7874 DCHECK_LE(num_temps, 1u);
7875 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007876 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7877 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7878 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7879 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007880 vixl32::Label done;
7881 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007882 SlowPathCodeARMVIXL* slow_path = nullptr;
7883
7884 // Return 0 if `obj` is null.
7885 // avoid null check if we know obj is not null.
7886 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007887 DCHECK(!out.Is(obj));
7888 __ Mov(out, 0);
Andreas Gampe3db70682018-12-26 15:12:03 -08007889 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007890 }
7891
Artem Serovcfbe9132016-10-14 15:58:56 +01007892 switch (type_check_kind) {
7893 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007894 ReadBarrierOption read_barrier_option =
7895 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007896 // /* HeapReference<Class> */ out = obj->klass_
7897 GenerateReferenceLoadTwoRegisters(instruction,
7898 out_loc,
7899 obj_loc,
7900 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007901 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007902 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007903 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007904 __ Cmp(out, cls);
7905 // We speculatively set the result to false without changing the condition
7906 // flags, which allows us to avoid some branching later.
7907 __ Mov(LeaveFlags, out, 0);
7908
7909 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7910 // we check that the output is in a low register, so that a 16-bit MOV
7911 // encoding can be used.
7912 if (out.IsLow()) {
7913 // We use the scope because of the IT block that follows.
7914 ExactAssemblyScope guard(GetVIXLAssembler(),
7915 2 * vixl32::k16BitT32InstructionSizeInBytes,
7916 CodeBufferCheckScope::kExactSize);
7917
7918 __ it(eq);
7919 __ mov(eq, out, 1);
7920 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007921 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007922 __ Mov(out, 1);
7923 }
7924
Artem Serovcfbe9132016-10-14 15:58:56 +01007925 break;
7926 }
7927
7928 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007929 ReadBarrierOption read_barrier_option =
7930 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007931 // /* HeapReference<Class> */ out = obj->klass_
7932 GenerateReferenceLoadTwoRegisters(instruction,
7933 out_loc,
7934 obj_loc,
7935 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007936 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007937 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007938 // If the class is abstract, we eagerly fetch the super class of the
7939 // object to avoid doing a comparison we know will fail.
7940 vixl32::Label loop;
7941 __ Bind(&loop);
7942 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007943 GenerateReferenceLoadOneRegister(instruction,
7944 out_loc,
7945 super_offset,
7946 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007947 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007948 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007949 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007950 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007951 __ B(ne, &loop, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007952 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007953 break;
7954 }
7955
7956 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007957 ReadBarrierOption read_barrier_option =
7958 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007959 // /* HeapReference<Class> */ out = obj->klass_
7960 GenerateReferenceLoadTwoRegisters(instruction,
7961 out_loc,
7962 obj_loc,
7963 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007964 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007965 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007966 // Walk over the class hierarchy to find a match.
7967 vixl32::Label loop, success;
7968 __ Bind(&loop);
7969 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007970 __ B(eq, &success, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007971 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007972 GenerateReferenceLoadOneRegister(instruction,
7973 out_loc,
7974 super_offset,
7975 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007976 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007977 // This is essentially a null check, but it sets the condition flags to the
7978 // proper value for the code that follows the loop, i.e. not `eq`.
7979 __ Cmp(out, 1);
Andreas Gampe3db70682018-12-26 15:12:03 -08007980 __ B(hs, &loop, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007981
7982 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7983 // we check that the output is in a low register, so that a 16-bit MOV
7984 // encoding can be used.
7985 if (out.IsLow()) {
7986 // If `out` is null, we use it for the result, and the condition flags
7987 // have already been set to `ne`, so the IT block that comes afterwards
7988 // (and which handles the successful case) turns into a NOP (instead of
7989 // overwriting `out`).
7990 __ Bind(&success);
7991
7992 // We use the scope because of the IT block that follows.
7993 ExactAssemblyScope guard(GetVIXLAssembler(),
7994 2 * vixl32::k16BitT32InstructionSizeInBytes,
7995 CodeBufferCheckScope::kExactSize);
7996
7997 // There is only one branch to the `success` label (which is bound to this
7998 // IT block), and it has the same condition, `eq`, so in that case the MOV
7999 // is executed.
8000 __ it(eq);
8001 __ mov(eq, out, 1);
8002 } else {
8003 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00008004 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008005 __ Bind(&success);
8006 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008007 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008008
Artem Serovcfbe9132016-10-14 15:58:56 +01008009 break;
8010 }
8011
8012 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00008013 ReadBarrierOption read_barrier_option =
8014 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08008015 // /* HeapReference<Class> */ out = obj->klass_
8016 GenerateReferenceLoadTwoRegisters(instruction,
8017 out_loc,
8018 obj_loc,
8019 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008020 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008021 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01008022 // Do an exact check.
8023 vixl32::Label exact_check;
8024 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008025 __ B(eq, &exact_check, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008026 // Otherwise, we need to check that the object's class is a non-primitive array.
8027 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008028 GenerateReferenceLoadOneRegister(instruction,
8029 out_loc,
8030 component_offset,
8031 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008032 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008033 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08008034 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008035 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
8036 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008037 __ Cmp(out, 0);
8038 // We speculatively set the result to false without changing the condition
8039 // flags, which allows us to avoid some branching later.
8040 __ Mov(LeaveFlags, out, 0);
8041
8042 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
8043 // we check that the output is in a low register, so that a 16-bit MOV
8044 // encoding can be used.
8045 if (out.IsLow()) {
8046 __ Bind(&exact_check);
8047
8048 // We use the scope because of the IT block that follows.
8049 ExactAssemblyScope guard(GetVIXLAssembler(),
8050 2 * vixl32::k16BitT32InstructionSizeInBytes,
8051 CodeBufferCheckScope::kExactSize);
8052
8053 __ it(eq);
8054 __ mov(eq, out, 1);
8055 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08008056 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008057 __ Bind(&exact_check);
8058 __ Mov(out, 1);
8059 }
8060
Artem Serovcfbe9132016-10-14 15:58:56 +01008061 break;
8062 }
8063
8064 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008065 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08008066 // /* HeapReference<Class> */ out = obj->klass_
8067 GenerateReferenceLoadTwoRegisters(instruction,
8068 out_loc,
8069 obj_loc,
8070 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008071 maybe_temp_loc,
8072 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008073 __ Cmp(out, cls);
8074 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008075 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008076 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008077 codegen_->AddSlowPath(slow_path);
8078 __ B(ne, slow_path->GetEntryLabel());
8079 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008080 break;
8081 }
8082
8083 case TypeCheckKind::kUnresolvedCheck:
8084 case TypeCheckKind::kInterfaceCheck: {
8085 // Note that we indeed only call on slow path, but we always go
8086 // into the slow path for the unresolved and interface check
8087 // cases.
8088 //
8089 // We cannot directly call the InstanceofNonTrivial runtime
8090 // entry point without resorting to a type checking slow path
8091 // here (i.e. by calling InvokeRuntime directly), as it would
8092 // require to assign fixed registers for the inputs of this
8093 // HInstanceOf instruction (following the runtime calling
8094 // convention), which might be cluttered by the potential first
8095 // read barrier emission at the beginning of this method.
8096 //
8097 // TODO: Introduce a new runtime entry point taking the object
8098 // to test (instead of its class) as argument, and let it deal
8099 // with the read barrier issues. This will let us refactor this
8100 // case of the `switch` code as it was previously (with a direct
8101 // call to the runtime not using a type checking slow path).
8102 // This should also be beneficial for the other cases above.
8103 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008104 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008105 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008106 codegen_->AddSlowPath(slow_path);
8107 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008108 break;
8109 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008110
8111 case TypeCheckKind::kBitstringCheck: {
8112 // /* HeapReference<Class> */ temp = obj->klass_
8113 GenerateReferenceLoadTwoRegisters(instruction,
8114 out_loc,
8115 obj_loc,
8116 class_offset,
8117 maybe_temp_loc,
8118 kWithoutReadBarrier);
8119
8120 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
8121 // If `out` is a low reg and we would have another low reg temp, we could
8122 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
8123 //
8124 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
8125 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
8126 // would be the same and we would have fewer direct data dependencies.
8127 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
8128 break;
8129 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008130 }
8131
Artem Serovcfbe9132016-10-14 15:58:56 +01008132 if (done.IsReferenced()) {
8133 __ Bind(&done);
8134 }
8135
8136 if (slow_path != nullptr) {
8137 __ Bind(slow_path->GetExitLabel());
8138 }
8139}
8140
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008141void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008142 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008143 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008144 LocationSummary* locations =
8145 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008146 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00008147 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
8148 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
8149 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
8150 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
8151 } else {
8152 locations->SetInAt(1, Location::RequiresRegister());
8153 }
Artem Serov657022c2016-11-23 14:19:38 +00008154 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008155}
8156
8157void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8158 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8159 LocationSummary* locations = instruction->GetLocations();
8160 Location obj_loc = locations->InAt(0);
8161 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00008162 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
8163 ? vixl32::Register()
8164 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008165 Location temp_loc = locations->GetTemp(0);
8166 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008167 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8168 DCHECK_LE(num_temps, 3u);
8169 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8170 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8171 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8172 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8173 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8174 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8175 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8176 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8177 const uint32_t object_array_data_offset =
8178 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008179
Vladimir Marko87584542017-12-12 17:47:52 +00008180 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008181 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008182 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8183 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008184 codegen_->AddSlowPath(type_check_slow_path);
8185
8186 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008187 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008188 // Avoid null check if we know obj is not null.
8189 if (instruction->MustDoNullCheck()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08008190 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008191 }
8192
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008193 switch (type_check_kind) {
8194 case TypeCheckKind::kExactCheck:
8195 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008196 // /* HeapReference<Class> */ temp = obj->klass_
8197 GenerateReferenceLoadTwoRegisters(instruction,
8198 temp_loc,
8199 obj_loc,
8200 class_offset,
8201 maybe_temp2_loc,
8202 kWithoutReadBarrier);
8203
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008204 __ Cmp(temp, cls);
8205 // Jump to slow path for throwing the exception or doing a
8206 // more involved array check.
8207 __ B(ne, type_check_slow_path->GetEntryLabel());
8208 break;
8209 }
8210
8211 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008212 // /* HeapReference<Class> */ temp = obj->klass_
8213 GenerateReferenceLoadTwoRegisters(instruction,
8214 temp_loc,
8215 obj_loc,
8216 class_offset,
8217 maybe_temp2_loc,
8218 kWithoutReadBarrier);
8219
Artem Serovcfbe9132016-10-14 15:58:56 +01008220 // If the class is abstract, we eagerly fetch the super class of the
8221 // object to avoid doing a comparison we know will fail.
8222 vixl32::Label loop;
8223 __ Bind(&loop);
8224 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008225 GenerateReferenceLoadOneRegister(instruction,
8226 temp_loc,
8227 super_offset,
8228 maybe_temp2_loc,
8229 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008230
8231 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8232 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008233 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008234
8235 // Otherwise, compare the classes.
8236 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008237 __ B(ne, &loop, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008238 break;
8239 }
8240
8241 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008242 // /* HeapReference<Class> */ temp = obj->klass_
8243 GenerateReferenceLoadTwoRegisters(instruction,
8244 temp_loc,
8245 obj_loc,
8246 class_offset,
8247 maybe_temp2_loc,
8248 kWithoutReadBarrier);
8249
Artem Serovcfbe9132016-10-14 15:58:56 +01008250 // Walk over the class hierarchy to find a match.
8251 vixl32::Label loop;
8252 __ Bind(&loop);
8253 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008254 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008255
8256 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008257 GenerateReferenceLoadOneRegister(instruction,
8258 temp_loc,
8259 super_offset,
8260 maybe_temp2_loc,
8261 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008262
8263 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8264 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008265 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008266 // Otherwise, jump to the beginning of the loop.
8267 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008268 break;
8269 }
8270
Artem Serovcfbe9132016-10-14 15:58:56 +01008271 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008272 // /* HeapReference<Class> */ temp = obj->klass_
8273 GenerateReferenceLoadTwoRegisters(instruction,
8274 temp_loc,
8275 obj_loc,
8276 class_offset,
8277 maybe_temp2_loc,
8278 kWithoutReadBarrier);
8279
Artem Serovcfbe9132016-10-14 15:58:56 +01008280 // Do an exact check.
8281 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008282 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008283
8284 // Otherwise, we need to check that the object's class is a non-primitive array.
8285 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008286 GenerateReferenceLoadOneRegister(instruction,
8287 temp_loc,
8288 component_offset,
8289 maybe_temp2_loc,
8290 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008291 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008292 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008293 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8294 // to further check that this component type is not a primitive type.
8295 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008296 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008297 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008298 break;
8299 }
8300
8301 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008302 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008303 // We cannot directly call the CheckCast runtime entry point
8304 // without resorting to a type checking slow path here (i.e. by
8305 // calling InvokeRuntime directly), as it would require to
8306 // assign fixed registers for the inputs of this HInstanceOf
8307 // instruction (following the runtime calling convention), which
8308 // might be cluttered by the potential first read barrier
8309 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008310
Artem Serovcfbe9132016-10-14 15:58:56 +01008311 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008312 break;
Artem Serov657022c2016-11-23 14:19:38 +00008313
8314 case TypeCheckKind::kInterfaceCheck: {
8315 // Avoid read barriers to improve performance of the fast path. We can not get false
8316 // positives by doing this.
8317 // /* HeapReference<Class> */ temp = obj->klass_
8318 GenerateReferenceLoadTwoRegisters(instruction,
8319 temp_loc,
8320 obj_loc,
8321 class_offset,
8322 maybe_temp2_loc,
8323 kWithoutReadBarrier);
8324
8325 // /* HeapReference<Class> */ temp = temp->iftable_
8326 GenerateReferenceLoadTwoRegisters(instruction,
8327 temp_loc,
8328 temp_loc,
8329 iftable_offset,
8330 maybe_temp2_loc,
8331 kWithoutReadBarrier);
8332 // Iftable is never null.
8333 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8334 // Loop through the iftable and check if any class matches.
8335 vixl32::Label start_loop;
8336 __ Bind(&start_loop);
8337 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8338 type_check_slow_path->GetEntryLabel());
8339 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8340 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8341 // Go to next interface.
8342 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8343 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8344 // Compare the classes and continue the loop if they do not match.
8345 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Andreas Gampe3db70682018-12-26 15:12:03 -08008346 __ B(ne, &start_loop, /* is_far_target= */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008347 break;
8348 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008349
8350 case TypeCheckKind::kBitstringCheck: {
8351 // /* HeapReference<Class> */ temp = obj->klass_
8352 GenerateReferenceLoadTwoRegisters(instruction,
8353 temp_loc,
8354 obj_loc,
8355 class_offset,
8356 maybe_temp2_loc,
8357 kWithoutReadBarrier);
8358
8359 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8360 __ B(ne, type_check_slow_path->GetEntryLabel());
8361 break;
8362 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008363 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008364 if (done.IsReferenced()) {
8365 __ Bind(&done);
8366 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008367
8368 __ Bind(type_check_slow_path->GetExitLabel());
8369}
8370
Artem Serov551b28f2016-10-18 19:11:30 +01008371void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008372 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8373 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008374 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8375 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8376}
8377
8378void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8379 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8380 instruction,
8381 instruction->GetDexPc());
8382 if (instruction->IsEnter()) {
8383 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8384 } else {
8385 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8386 }
Andra Danciua0130e82020-07-23 12:34:56 +00008387 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 19);
Artem Serov551b28f2016-10-18 19:11:30 +01008388}
8389
Artem Serov02109dd2016-09-23 17:17:54 +01008390void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8391 HandleBitwiseOperation(instruction, AND);
8392}
8393
8394void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8395 HandleBitwiseOperation(instruction, ORR);
8396}
8397
8398void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8399 HandleBitwiseOperation(instruction, EOR);
8400}
8401
8402void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8403 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008404 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008405 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8406 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008407 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8408 locations->SetInAt(0, Location::RequiresRegister());
8409 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8411}
8412
8413void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8414 HandleBitwiseOperation(instruction);
8415}
8416
8417void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8418 HandleBitwiseOperation(instruction);
8419}
8420
8421void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8422 HandleBitwiseOperation(instruction);
8423}
8424
Artem Serov2bbc9532016-10-21 11:51:50 +01008425void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8426 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008427 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008428 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8429 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008430
8431 locations->SetInAt(0, Location::RequiresRegister());
8432 locations->SetInAt(1, Location::RequiresRegister());
8433 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8434}
8435
8436void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8437 LocationSummary* locations = instruction->GetLocations();
8438 Location first = locations->InAt(0);
8439 Location second = locations->InAt(1);
8440 Location out = locations->Out();
8441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008442 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008443 vixl32::Register first_reg = RegisterFrom(first);
8444 vixl32::Register second_reg = RegisterFrom(second);
8445 vixl32::Register out_reg = RegisterFrom(out);
8446
8447 switch (instruction->GetOpKind()) {
8448 case HInstruction::kAnd:
8449 __ Bic(out_reg, first_reg, second_reg);
8450 break;
8451 case HInstruction::kOr:
8452 __ Orn(out_reg, first_reg, second_reg);
8453 break;
8454 // There is no EON on arm.
8455 case HInstruction::kXor:
8456 default:
8457 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8458 UNREACHABLE();
8459 }
8460 return;
8461
8462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008463 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008464 vixl32::Register first_low = LowRegisterFrom(first);
8465 vixl32::Register first_high = HighRegisterFrom(first);
8466 vixl32::Register second_low = LowRegisterFrom(second);
8467 vixl32::Register second_high = HighRegisterFrom(second);
8468 vixl32::Register out_low = LowRegisterFrom(out);
8469 vixl32::Register out_high = HighRegisterFrom(out);
8470
8471 switch (instruction->GetOpKind()) {
8472 case HInstruction::kAnd:
8473 __ Bic(out_low, first_low, second_low);
8474 __ Bic(out_high, first_high, second_high);
8475 break;
8476 case HInstruction::kOr:
8477 __ Orn(out_low, first_low, second_low);
8478 __ Orn(out_high, first_high, second_high);
8479 break;
8480 // There is no EON on arm.
8481 case HInstruction::kXor:
8482 default:
8483 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8484 UNREACHABLE();
8485 }
8486 }
8487}
8488
Anton Kirilov74234da2017-01-13 14:42:47 +00008489void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8490 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008491 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8492 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008493 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008494 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008495 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008496 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8497
8498 locations->SetInAt(0, Location::RequiresRegister());
8499 locations->SetInAt(1, Location::RequiresRegister());
8500 locations->SetOut(Location::RequiresRegister(),
8501 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8502}
8503
8504void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8505 HDataProcWithShifterOp* instruction) {
8506 const LocationSummary* const locations = instruction->GetLocations();
8507 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8508 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008510 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008511 const vixl32::Register first = InputRegisterAt(instruction, 0);
8512 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008513 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008514 ? LowRegisterFrom(locations->InAt(1))
8515 : InputRegisterAt(instruction, 1);
8516
Anton Kirilov420ee302017-02-21 18:10:26 +00008517 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8518 DCHECK_EQ(kind, HInstruction::kAdd);
8519
8520 switch (op_kind) {
8521 case HDataProcWithShifterOp::kUXTB:
8522 __ Uxtab(output, first, second);
8523 break;
8524 case HDataProcWithShifterOp::kUXTH:
8525 __ Uxtah(output, first, second);
8526 break;
8527 case HDataProcWithShifterOp::kSXTB:
8528 __ Sxtab(output, first, second);
8529 break;
8530 case HDataProcWithShifterOp::kSXTH:
8531 __ Sxtah(output, first, second);
8532 break;
8533 default:
8534 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8535 UNREACHABLE();
8536 }
8537 } else {
8538 GenerateDataProcInstruction(kind,
8539 output,
8540 first,
8541 Operand(second,
8542 ShiftFromOpKind(op_kind),
8543 instruction->GetShiftAmount()),
8544 codegen_);
8545 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008546 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008547 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008548
8549 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8550 const vixl32::Register second = InputRegisterAt(instruction, 1);
8551
8552 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8553 GenerateDataProc(kind,
8554 locations->Out(),
8555 locations->InAt(0),
8556 second,
8557 Operand(second, ShiftType::ASR, 31),
8558 codegen_);
8559 } else {
8560 GenerateLongDataProc(instruction, codegen_);
8561 }
8562 }
8563}
8564
Artem Serov02109dd2016-09-23 17:17:54 +01008565// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8566void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8567 vixl32::Register first,
8568 uint32_t value) {
8569 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8570 if (value == 0xffffffffu) {
8571 if (!out.Is(first)) {
8572 __ Mov(out, first);
8573 }
8574 return;
8575 }
8576 if (value == 0u) {
8577 __ Mov(out, 0);
8578 return;
8579 }
8580 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008581 __ And(out, first, value);
8582 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8583 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008584 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008585 DCHECK(IsPowerOfTwo(value + 1));
8586 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008587 }
8588}
8589
8590// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8591void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8592 vixl32::Register first,
8593 uint32_t value) {
8594 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8595 if (value == 0u) {
8596 if (!out.Is(first)) {
8597 __ Mov(out, first);
8598 }
8599 return;
8600 }
8601 if (value == 0xffffffffu) {
8602 __ Mvn(out, 0);
8603 return;
8604 }
8605 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8606 __ Orr(out, first, value);
8607 } else {
8608 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8609 __ Orn(out, first, ~value);
8610 }
8611}
8612
8613// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8614void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8615 vixl32::Register first,
8616 uint32_t value) {
8617 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8618 if (value == 0u) {
8619 if (!out.Is(first)) {
8620 __ Mov(out, first);
8621 }
8622 return;
8623 }
8624 __ Eor(out, first, value);
8625}
8626
Anton Kirilovdda43962016-11-21 19:55:20 +00008627void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8628 Location first,
8629 uint64_t value) {
8630 vixl32::Register out_low = LowRegisterFrom(out);
8631 vixl32::Register out_high = HighRegisterFrom(out);
8632 vixl32::Register first_low = LowRegisterFrom(first);
8633 vixl32::Register first_high = HighRegisterFrom(first);
8634 uint32_t value_low = Low32Bits(value);
8635 uint32_t value_high = High32Bits(value);
8636 if (value_low == 0u) {
8637 if (!out_low.Is(first_low)) {
8638 __ Mov(out_low, first_low);
8639 }
8640 __ Add(out_high, first_high, value_high);
8641 return;
8642 }
8643 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008644 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008645 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008646 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008647 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8648 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008649 }
8650}
8651
Artem Serov02109dd2016-09-23 17:17:54 +01008652void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8653 LocationSummary* locations = instruction->GetLocations();
8654 Location first = locations->InAt(0);
8655 Location second = locations->InAt(1);
8656 Location out = locations->Out();
8657
8658 if (second.IsConstant()) {
8659 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8660 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008661 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008662 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8663 vixl32::Register out_reg = OutputRegister(instruction);
8664 if (instruction->IsAnd()) {
8665 GenerateAndConst(out_reg, first_reg, value_low);
8666 } else if (instruction->IsOr()) {
8667 GenerateOrrConst(out_reg, first_reg, value_low);
8668 } else {
8669 DCHECK(instruction->IsXor());
8670 GenerateEorConst(out_reg, first_reg, value_low);
8671 }
8672 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008673 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008674 uint32_t value_high = High32Bits(value);
8675 vixl32::Register first_low = LowRegisterFrom(first);
8676 vixl32::Register first_high = HighRegisterFrom(first);
8677 vixl32::Register out_low = LowRegisterFrom(out);
8678 vixl32::Register out_high = HighRegisterFrom(out);
8679 if (instruction->IsAnd()) {
8680 GenerateAndConst(out_low, first_low, value_low);
8681 GenerateAndConst(out_high, first_high, value_high);
8682 } else if (instruction->IsOr()) {
8683 GenerateOrrConst(out_low, first_low, value_low);
8684 GenerateOrrConst(out_high, first_high, value_high);
8685 } else {
8686 DCHECK(instruction->IsXor());
8687 GenerateEorConst(out_low, first_low, value_low);
8688 GenerateEorConst(out_high, first_high, value_high);
8689 }
8690 }
8691 return;
8692 }
8693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008694 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008695 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8696 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8697 vixl32::Register out_reg = OutputRegister(instruction);
8698 if (instruction->IsAnd()) {
8699 __ And(out_reg, first_reg, second_reg);
8700 } else if (instruction->IsOr()) {
8701 __ Orr(out_reg, first_reg, second_reg);
8702 } else {
8703 DCHECK(instruction->IsXor());
8704 __ Eor(out_reg, first_reg, second_reg);
8705 }
8706 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008707 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008708 vixl32::Register first_low = LowRegisterFrom(first);
8709 vixl32::Register first_high = HighRegisterFrom(first);
8710 vixl32::Register second_low = LowRegisterFrom(second);
8711 vixl32::Register second_high = HighRegisterFrom(second);
8712 vixl32::Register out_low = LowRegisterFrom(out);
8713 vixl32::Register out_high = HighRegisterFrom(out);
8714 if (instruction->IsAnd()) {
8715 __ And(out_low, first_low, second_low);
8716 __ And(out_high, first_high, second_high);
8717 } else if (instruction->IsOr()) {
8718 __ Orr(out_low, first_low, second_low);
8719 __ Orr(out_high, first_high, second_high);
8720 } else {
8721 DCHECK(instruction->IsXor());
8722 __ Eor(out_low, first_low, second_low);
8723 __ Eor(out_high, first_high, second_high);
8724 }
8725 }
8726}
8727
Artem Serovcfbe9132016-10-14 15:58:56 +01008728void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008729 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008730 Location out,
8731 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008732 Location maybe_temp,
8733 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008734 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008735 if (read_barrier_option == kWithReadBarrier) {
8736 CHECK(kEmitCompilerReadBarrier);
8737 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8738 if (kUseBakerReadBarrier) {
8739 // Load with fast path based Baker's read barrier.
8740 // /* HeapReference<Object> */ out = *(out + offset)
8741 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008742 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008743 } else {
8744 // Load with slow path based read barrier.
8745 // Save the value of `out` into `maybe_temp` before overwriting it
8746 // in the following move operation, as we will need it for the
8747 // read barrier below.
8748 __ Mov(RegisterFrom(maybe_temp), out_reg);
8749 // /* HeapReference<Object> */ out = *(out + offset)
8750 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8751 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8752 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008753 } else {
8754 // Plain load with no read barrier.
8755 // /* HeapReference<Object> */ out = *(out + offset)
8756 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8757 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8758 }
8759}
8760
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008761void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008762 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008763 Location out,
8764 Location obj,
8765 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008766 Location maybe_temp,
8767 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008768 vixl32::Register out_reg = RegisterFrom(out);
8769 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008770 if (read_barrier_option == kWithReadBarrier) {
8771 CHECK(kEmitCompilerReadBarrier);
8772 if (kUseBakerReadBarrier) {
8773 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8774 // Load with fast path based Baker's read barrier.
8775 // /* HeapReference<Object> */ out = *(obj + offset)
8776 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008777 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008778 } else {
8779 // Load with slow path based read barrier.
8780 // /* HeapReference<Object> */ out = *(obj + offset)
8781 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8782 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8783 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008784 } else {
8785 // Plain load with no read barrier.
8786 // /* HeapReference<Object> */ out = *(obj + offset)
8787 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8788 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8789 }
8790}
8791
Vladimir Markoca1e0382018-04-11 09:58:41 +00008792void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008793 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008794 Location root,
8795 vixl32::Register obj,
8796 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008797 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008798 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008799 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008800 DCHECK(kEmitCompilerReadBarrier);
8801 if (kUseBakerReadBarrier) {
8802 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008803 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008804
Vladimir Marko008e09f32018-08-06 15:42:43 +01008805 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8806 // the Marking Register) to decide whether we need to enter
8807 // the slow path to mark the GC root.
8808 //
8809 // We use shared thunks for the slow path; shared within the method
8810 // for JIT, across methods for AOT. That thunk checks the reference
8811 // and jumps to the entrypoint if needed.
8812 //
8813 // lr = &return_address;
8814 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8815 // if (mr) { // Thread::Current()->GetIsGcMarking()
8816 // goto gc_root_thunk<root_reg>(lr)
8817 // }
8818 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008819
Vladimir Marko008e09f32018-08-06 15:42:43 +01008820 UseScratchRegisterScope temps(GetVIXLAssembler());
8821 temps.Exclude(ip);
8822 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8823 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008824
Vladimir Markod887ed82018-08-14 13:52:12 +00008825 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8826 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8827 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8828 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8829 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008830 vixl32::Label return_address;
8831 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8832 __ cmp(mr, Operand(0));
8833 // Currently the offset is always within range. If that changes,
8834 // we shall have to split the load the same way as for fields.
8835 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8836 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8837 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8838 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008839 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008840 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8841 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8842 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008843 } else {
8844 // GC root loaded through a slow path for read barriers other
8845 // than Baker's.
8846 // /* GcRoot<mirror::Object>* */ root = obj + offset
8847 __ Add(root_reg, obj, offset);
8848 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008849 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008850 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008851 } else {
8852 // Plain GC root load with no read barrier.
8853 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8854 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8855 // Note that GC roots are not affected by heap poisoning, thus we
8856 // do not have to unpoison `root_reg` here.
8857 }
Andra Danciua0130e82020-07-23 12:34:56 +00008858 MaybeGenerateMarkingRegisterCheck(/* code= */ 20);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008859}
8860
Vladimir Marko3d350a82020-11-18 14:14:27 +00008861void CodeGeneratorARMVIXL::GenerateIntrinsicCasMoveWithBakerReadBarrier(
8862 vixl::aarch32::Register marked_old_value,
8863 vixl::aarch32::Register old_value) {
Vladimir Markod887ed82018-08-14 13:52:12 +00008864 DCHECK(kEmitCompilerReadBarrier);
8865 DCHECK(kUseBakerReadBarrier);
8866
Vladimir Marko3d350a82020-11-18 14:14:27 +00008867 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
8868 // For low registers, we can reuse the GC root narrow entrypoint, for high registers
8869 // we use a specialized entrypoint because the register bits are 8-11 instead of 12-15.
8870 bool narrow_mov = marked_old_value.IsLow();
8871 uint32_t custom_data = narrow_mov
8872 ? EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode(), /*narrow=*/ true)
8873 : EncodeBakerReadBarrierIntrinsicCasData(marked_old_value.GetCode());
Vladimir Markod887ed82018-08-14 13:52:12 +00008874
Vladimir Marko3d350a82020-11-18 14:14:27 +00008875 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* MOV */ (narrow_mov ? 1u : 0u);
8876 size_t wide_instructions = /* ADR+CMP+MOV+BNE */ 4u - narrow_instructions;
Vladimir Markod887ed82018-08-14 13:52:12 +00008877 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8878 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8879 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8880 vixl32::Label return_address;
8881 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8882 __ cmp(mr, Operand(0));
8883 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko3d350a82020-11-18 14:14:27 +00008884 __ mov(EncodingSize(narrow_mov ? Narrow : Wide), marked_old_value, old_value);
Vladimir Markod887ed82018-08-14 13:52:12 +00008885 EmitBakerReadBarrierBne(custom_data);
8886 __ bind(&return_address);
8887 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
Vladimir Marko3d350a82020-11-18 14:14:27 +00008888 narrow_mov
8889 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8890 : BAKER_MARK_INTROSPECTION_INTRINSIC_CAS_MOV_OFFSET);
Vladimir Markod887ed82018-08-14 13:52:12 +00008891}
8892
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008893void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8894 Location ref,
8895 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008896 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008897 bool needs_null_check) {
8898 DCHECK(kEmitCompilerReadBarrier);
8899 DCHECK(kUseBakerReadBarrier);
8900
Vladimir Marko008e09f32018-08-06 15:42:43 +01008901 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8902 // Marking Register) to decide whether we need to enter the slow
8903 // path to mark the reference. Then, in the slow path, check the
8904 // gray bit in the lock word of the reference's holder (`obj`) to
8905 // decide whether to mark `ref` or not.
8906 //
8907 // We use shared thunks for the slow path; shared within the method
8908 // for JIT, across methods for AOT. That thunk checks the holder
8909 // and jumps to the entrypoint if needed. If the holder is not gray,
8910 // it creates a fake dependency and returns to the LDR instruction.
8911 //
8912 // lr = &gray_return_address;
8913 // if (mr) { // Thread::Current()->GetIsGcMarking()
8914 // goto field_thunk<holder_reg, base_reg>(lr)
8915 // }
8916 // not_gray_return_address:
8917 // // Original reference load. If the offset is too large to fit
8918 // // into LDR, we use an adjusted base register here.
8919 // HeapReference<mirror::Object> reference = *(obj+offset);
8920 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008921
Vladimir Marko248141f2018-08-10 10:40:07 +01008922 DCHECK(src.GetAddrMode() == vixl32::Offset);
8923 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008924 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008925 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8926
Vladimir Marko008e09f32018-08-06 15:42:43 +01008927 UseScratchRegisterScope temps(GetVIXLAssembler());
8928 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008929 uint32_t custom_data =
8930 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008931
Vladimir Marko008e09f32018-08-06 15:42:43 +01008932 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008933 size_t narrow_instructions =
8934 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8935 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
8936 size_t wide_instructions =
8937 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8938 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8939 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8940 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008941 vixl32::Label return_address;
8942 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8943 __ cmp(mr, Operand(0));
8944 EmitBakerReadBarrierBne(custom_data);
8945 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01008946 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008947 if (needs_null_check) {
8948 MaybeRecordImplicitNullCheck(instruction);
8949 }
8950 // Note: We need a specific width for the unpoisoning NEG.
8951 if (kPoisonHeapReferences) {
8952 if (narrow) {
8953 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8954 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8955 } else {
8956 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8957 }
8958 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008959 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008960 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8961 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8962 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
8963 }
Andra Danciua0130e82020-07-23 12:34:56 +00008964 MaybeGenerateMarkingRegisterCheck(/* code= */ 21, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008965}
8966
Vladimir Marko248141f2018-08-10 10:40:07 +01008967void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8968 Location ref,
8969 vixl32::Register obj,
8970 uint32_t offset,
Vladimir Marko01b65522020-10-28 15:43:54 +00008971 Location maybe_temp,
Vladimir Marko248141f2018-08-10 10:40:07 +01008972 bool needs_null_check) {
8973 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8974 vixl32::Register base = obj;
8975 if (offset >= kReferenceLoadMinFarOffset) {
Vladimir Marko01b65522020-10-28 15:43:54 +00008976 base = RegisterFrom(maybe_temp);
Vladimir Marko248141f2018-08-10 10:40:07 +01008977 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8978 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8979 offset &= (kReferenceLoadMinFarOffset - 1u);
8980 }
8981 GenerateFieldLoadWithBakerReadBarrier(
8982 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
8983}
8984
Vladimir Marko008e09f32018-08-06 15:42:43 +01008985void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008986 vixl32::Register obj,
8987 uint32_t data_offset,
8988 Location index,
8989 Location temp,
8990 bool needs_null_check) {
8991 DCHECK(kEmitCompilerReadBarrier);
8992 DCHECK(kUseBakerReadBarrier);
8993
8994 static_assert(
8995 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8996 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008997 ScaleFactor scale_factor = TIMES_4;
8998
Vladimir Marko008e09f32018-08-06 15:42:43 +01008999 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
9000 // Marking Register) to decide whether we need to enter the slow
9001 // path to mark the reference. Then, in the slow path, check the
9002 // gray bit in the lock word of the reference's holder (`obj`) to
9003 // decide whether to mark `ref` or not.
9004 //
9005 // We use shared thunks for the slow path; shared within the method
9006 // for JIT, across methods for AOT. That thunk checks the holder
9007 // and jumps to the entrypoint if needed. If the holder is not gray,
9008 // it creates a fake dependency and returns to the LDR instruction.
9009 //
9010 // lr = &gray_return_address;
9011 // if (mr) { // Thread::Current()->GetIsGcMarking()
9012 // goto array_thunk<base_reg>(lr)
9013 // }
9014 // not_gray_return_address:
9015 // // Original reference load. If the offset is too large to fit
9016 // // into LDR, we use an adjusted base register here.
9017 // HeapReference<mirror::Object> reference = data[index];
9018 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009019
Vladimir Marko008e09f32018-08-06 15:42:43 +01009020 DCHECK(index.IsValid());
9021 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
9022 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
9023 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009024
Vladimir Marko008e09f32018-08-06 15:42:43 +01009025 UseScratchRegisterScope temps(GetVIXLAssembler());
9026 temps.Exclude(ip);
9027 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009028
Vladimir Marko008e09f32018-08-06 15:42:43 +01009029 __ Add(data_reg, obj, Operand(data_offset));
9030 {
Vladimir Markod887ed82018-08-14 13:52:12 +00009031 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
9032 size_t wide_instructions =
9033 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
9034 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
9035 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
9036 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01009037 vixl32::Label return_address;
9038 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
9039 __ cmp(mr, Operand(0));
9040 EmitBakerReadBarrierBne(custom_data);
9041 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
9042 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
9043 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
9044 // Note: We need a Wide NEG for the unpoisoning.
9045 if (kPoisonHeapReferences) {
9046 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009047 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009048 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01009049 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
9050 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009051 }
Andra Danciua0130e82020-07-23 12:34:56 +00009052 MaybeGenerateMarkingRegisterCheck(/* code= */ 22, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00009053}
9054
Roland Levillain5daa4952017-07-03 17:23:56 +01009055void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
9056 // The following condition is a compile-time one, so it does not have a run-time cost.
9057 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
9058 // The following condition is a run-time one; it is executed after the
9059 // previous compile-time test, to avoid penalizing non-debug builds.
9060 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
9061 UseScratchRegisterScope temps(GetVIXLAssembler());
9062 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
9063 GetAssembler()->GenerateMarkingRegisterCheck(temp,
9064 kMarkingRegisterCheckBreakCodeBaseCode + code);
9065 }
9066 }
9067}
9068
Vladimir Marko3d350a82020-11-18 14:14:27 +00009069SlowPathCodeARMVIXL* CodeGeneratorARMVIXL::AddReadBarrierSlowPath(HInstruction* instruction,
9070 Location out,
9071 Location ref,
9072 Location obj,
9073 uint32_t offset,
9074 Location index) {
9075 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
9076 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9077 AddSlowPath(slow_path);
9078 return slow_path;
9079}
9080
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009081void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
9082 Location out,
9083 Location ref,
9084 Location obj,
9085 uint32_t offset,
9086 Location index) {
9087 DCHECK(kEmitCompilerReadBarrier);
9088
9089 // Insert a slow path based read barrier *after* the reference load.
9090 //
9091 // If heap poisoning is enabled, the unpoisoning of the loaded
9092 // reference will be carried out by the runtime within the slow
9093 // path.
9094 //
9095 // Note that `ref` currently does not get unpoisoned (when heap
9096 // poisoning is enabled), which is alright as the `ref` argument is
9097 // not used by the artReadBarrierSlow entry point.
9098 //
9099 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko3d350a82020-11-18 14:14:27 +00009100 SlowPathCodeARMVIXL* slow_path =
9101 AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009102
9103 __ B(slow_path->GetEntryLabel());
9104 __ Bind(slow_path->GetExitLabel());
9105}
9106
9107void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009108 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009109 Location ref,
9110 Location obj,
9111 uint32_t offset,
9112 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009113 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009114 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009115 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009116 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009117 // If heap poisoning is enabled, unpoisoning will be taken care of
9118 // by the runtime within the slow path.
9119 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009120 } else if (kPoisonHeapReferences) {
9121 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9122 }
9123}
9124
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009125void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9126 Location out,
9127 Location root) {
9128 DCHECK(kEmitCompilerReadBarrier);
9129
9130 // Insert a slow path based read barrier *after* the GC root load.
9131 //
9132 // Note that GC roots are not affected by heap poisoning, so we do
9133 // not need to do anything special for this here.
9134 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009135 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009136 AddSlowPath(slow_path);
9137
9138 __ B(slow_path->GetEntryLabel());
9139 __ Bind(slow_path->GetExitLabel());
9140}
9141
Artem Serov02d37832016-10-25 15:25:33 +01009142// Check if the desired_dispatch_info is supported. If it is, return it,
9143// otherwise return a fall-back info that should be used instead.
9144HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009145 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Vladimir Marko86c87522020-05-11 16:55:55 +01009146 ArtMethod* method) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009147 if (method->IsIntrinsic() &&
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009148 desired_dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009149 // As a work-around for soft-float native ABI interfering with type checks, we are
9150 // inserting fake calls to Float.floatToRawIntBits() or Double.doubleToRawLongBits()
9151 // when a float or double argument is passed in core registers but we cannot do that
9152 // for actual intrinsic implementations that expect them in FP registers. Therefore
9153 // we do not use `kCallCriticalNative` for intrinsics with FP arguments; if they are
9154 // properly intrinsified, the dispatch type does not matter anyway.
Vladimir Marko86c87522020-05-11 16:55:55 +01009155 ScopedObjectAccess soa(Thread::Current());
9156 uint32_t shorty_len;
9157 const char* shorty = method->GetShorty(&shorty_len);
Vladimir Marko86c87522020-05-11 16:55:55 +01009158 for (uint32_t i = 1; i != shorty_len; ++i) {
Vladimir Marko86c87522020-05-11 16:55:55 +01009159 if (shorty[i] == 'D' || shorty[i] == 'F') {
9160 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009161 dispatch_info.code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Marko86c87522020-05-11 16:55:55 +01009162 return dispatch_info;
9163 }
Vladimir Marko86c87522020-05-11 16:55:55 +01009164 }
9165 }
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009166 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009167}
9168
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009169
9170void CodeGeneratorARMVIXL::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
9171 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009172 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01009173 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009174 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01009175 vixl32::Register temp_reg = RegisterFrom(temp);
9176 EmitMovwMovtPlaceholder(labels, temp_reg);
9177 break;
9178 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009179 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009180 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00009181 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
9182 vixl32::Register temp_reg = RegisterFrom(temp);
9183 EmitMovwMovtPlaceholder(labels, temp_reg);
9184 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
9185 break;
9186 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009187 case MethodLoadKind::kBssEntry: {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009188 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009189 vixl32::Register temp_reg = RegisterFrom(temp);
9190 EmitMovwMovtPlaceholder(labels, temp_reg);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01009191 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009192 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009193 break;
9194 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009195 case MethodLoadKind::kJitDirectAddress: {
9196 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetResolvedMethod()));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009197 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009198 }
9199 case MethodLoadKind::kRuntimeCall: {
9200 // Test situation, don't do anything.
9201 break;
9202 }
9203 default: {
9204 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
9205 UNREACHABLE();
9206 }
9207 }
9208}
9209
9210void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
9211 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
9212 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
9213 switch (invoke->GetMethodLoadKind()) {
9214 case MethodLoadKind::kStringInit: {
9215 uint32_t offset =
9216 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9217 // temp = thread->string_init_entrypoint
9218 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9219 break;
9220 }
9221 case MethodLoadKind::kRecursive: {
9222 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
9223 break;
9224 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009225 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009226 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9227 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009228 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01009229 case MethodLoadKind::kBootImageLinkTimePcRelative:
9230 // Note: Unlike arm64, x86 and x86-64, we do not avoid the materialization of method
9231 // pointer for kCallCriticalNative because it would not save us an instruction from
9232 // the current sequence MOVW+MOVT+ADD(pc)+LDR+BL. The ADD(pc) separates the patched
9233 // offset instructions MOVW+MOVT from the entrypoint load, so they cannot be fused.
9234 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009235 default: {
9236 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
9237 break;
9238 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009239 }
9240
Vladimir Marko86c87522020-05-11 16:55:55 +01009241 auto call_code_pointer_member = [&](MemberOffset offset) {
9242 // LR = callee_method->member;
9243 GetAssembler()->LoadFromOffset(kLoadWord, lr, RegisterFrom(callee_method), offset.Int32Value());
9244 {
9245 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9246 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9247 ExactAssemblyScope aas(GetVIXLAssembler(),
9248 vixl32::k16BitT32InstructionSizeInBytes,
9249 CodeBufferCheckScope::kExactSize);
9250 // LR()
9251 __ blx(lr);
9252 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9253 }
9254 };
Artem Serovd4cc5b22016-11-04 11:19:09 +00009255 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009256 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009257 {
9258 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9259 ExactAssemblyScope aas(GetVIXLAssembler(),
9260 vixl32::k32BitT32InstructionSizeInBytes,
9261 CodeBufferCheckScope::kMaximumSize);
9262 __ bl(GetFrameEntryLabel());
9263 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9264 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009265 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009266 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01009267 size_t out_frame_size =
9268 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARMVIXL,
9269 kAapcsStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01009270 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01009271 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArmPointerSize));
9272 // Move the result when needed due to native and managed ABI mismatch.
9273 switch (invoke->GetType()) {
9274 case DataType::Type::kFloat32:
9275 __ Vmov(s0, r0);
9276 break;
9277 case DataType::Type::kFloat64:
9278 __ Vmov(d0, r0, r1);
9279 break;
9280 case DataType::Type::kBool:
9281 case DataType::Type::kInt8:
9282 case DataType::Type::kUint16:
9283 case DataType::Type::kInt16:
9284 case DataType::Type::kInt32:
9285 case DataType::Type::kInt64:
9286 case DataType::Type::kVoid:
9287 break;
9288 default:
9289 DCHECK(false) << invoke->GetType();
9290 break;
9291 }
9292 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01009293 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01009294 }
9295 break;
9296 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009297 case CodePtrLocation::kCallArtMethod:
Vladimir Marko86c87522020-05-11 16:55:55 +01009298 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009299 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009300 }
9301
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009302 DCHECK(!IsLeafMethod());
9303}
9304
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009305void CodeGeneratorARMVIXL::GenerateVirtualCall(
9306 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009307 vixl32::Register temp = RegisterFrom(temp_location);
9308 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9309 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9310
9311 // Use the calling convention instead of the location of the receiver, as
9312 // intrinsics may have put the receiver in a different register. In the intrinsics
9313 // slow path, the arguments have been moved to the right place, so here we are
9314 // guaranteed that the receiver is the first register of the calling convention.
9315 InvokeDexCallingConventionARMVIXL calling_convention;
9316 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9317 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009318 {
9319 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009320 ExactAssemblyScope aas(GetVIXLAssembler(),
9321 vixl32::kMaxInstructionSizeInBytes,
9322 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009323 // /* HeapReference<Class> */ temp = receiver->klass_
9324 __ ldr(temp, MemOperand(receiver, class_offset));
9325 MaybeRecordImplicitNullCheck(invoke);
9326 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009327 // Instead of simply (possibly) unpoisoning `temp` here, we should
9328 // emit a read barrier for the previous class reference load.
9329 // However this is not required in practice, as this is an
9330 // intermediate/temporary reference and because the current
9331 // concurrent copying collector keeps the from-space memory
9332 // intact/accessible until the end of the marking phase (the
9333 // concurrent copying collector may not in the future).
9334 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9335
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00009336 // If we're compiling baseline, update the inline cache.
9337 MaybeGenerateInlineCacheCheck(invoke, temp);
9338
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009339 // temp = temp->GetMethodAt(method_offset);
9340 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9341 kArmPointerSize).Int32Value();
9342 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9343 // LR = temp->GetEntryPoint();
9344 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009345 {
9346 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9347 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9348 ExactAssemblyScope aas(GetVIXLAssembler(),
9349 vixl32::k16BitT32InstructionSizeInBytes,
9350 CodeBufferCheckScope::kExactSize);
9351 // LR();
9352 __ blx(lr);
9353 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9354 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009355}
9356
Vladimir Marko6fd16062018-06-26 11:02:04 +01009357CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
9358 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009359 return NewPcRelativePatch(/* dex_file= */ nullptr, intrinsic_data, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009360}
9361
Vladimir Markob066d432018-01-03 13:14:37 +00009362CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9363 uint32_t boot_image_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08009364 return NewPcRelativePatch(/* dex_file= */ nullptr,
Vladimir Markob066d432018-01-03 13:14:37 +00009365 boot_image_offset,
Vladimir Marko2d06e022019-07-08 15:45:19 +01009366 &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00009367}
9368
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009369CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009370 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009371 return NewPcRelativePatch(
9372 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009373}
9374
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009375CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9376 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009377 return NewPcRelativePatch(
9378 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009379}
9380
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009381CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009382 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009383 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009384}
9385
Vladimir Marko1998cd02017-01-13 13:02:58 +00009386CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01009387 HLoadClass* load_class) {
9388 const DexFile& dex_file = load_class->GetDexFile();
9389 dex::TypeIndex type_index = load_class->GetTypeIndex();
9390 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
9391 switch (load_class->GetLoadKind()) {
9392 case HLoadClass::LoadKind::kBssEntry:
9393 patches = &type_bss_entry_patches_;
9394 break;
9395 case HLoadClass::LoadKind::kBssEntryPublic:
9396 patches = &public_type_bss_entry_patches_;
9397 break;
9398 case HLoadClass::LoadKind::kBssEntryPackage:
9399 patches = &package_type_bss_entry_patches_;
9400 break;
9401 default:
9402 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
9403 UNREACHABLE();
9404 }
Vladimir Markobaade402020-09-30 14:45:39 +00009405 return NewPcRelativePatch(&dex_file, type_index.index_, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009406}
9407
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009408CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009409 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009410 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009411}
9412
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009413CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9414 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009415 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009416}
9417
Artem Serovd4cc5b22016-11-04 11:19:09 +00009418CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009419 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009420 patches->emplace_back(dex_file, offset_or_index);
9421 return &patches->back();
9422}
9423
Vladimir Markof6675082019-05-17 12:05:28 +01009424void CodeGeneratorARMVIXL::EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset) {
9425 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009426 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01009427 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
9428 vixl::aarch32::Label* bl_label = &call_entrypoint_patches_.back().label;
9429 __ bind(bl_label);
9430 vixl32::Label placeholder_label;
9431 __ bl(&placeholder_label); // Placeholder, patched at link-time.
9432 __ bind(&placeholder_label);
9433}
9434
Vladimir Marko966b46f2018-08-03 10:20:19 +00009435void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00009436 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009437 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00009438 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
9439 vixl::aarch32::Label* slow_path_entry = &it->second.label;
9440 __ b(ne, EncodingSize(Wide), slow_path_entry);
9441 } else {
9442 baker_read_barrier_patches_.emplace_back(custom_data);
9443 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
9444 __ bind(patch_label);
9445 vixl32::Label placeholder_label;
9446 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
9447 __ bind(&placeholder_label);
9448 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009449}
9450
Artem Serovc5fcb442016-12-02 19:19:58 +00009451VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009452 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009453}
9454
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009455VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9456 const DexFile& dex_file,
9457 dex::StringIndex string_index,
9458 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009459 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009460 return jit_string_patches_.GetOrCreate(
9461 StringReference(&dex_file, string_index),
9462 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009463 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009464 });
9465}
9466
9467VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9468 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009469 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009470 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009471 return jit_class_patches_.GetOrCreate(
9472 TypeReference(&dex_file, type_index),
9473 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009474 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009475 });
9476}
9477
Vladimir Marko6fd16062018-06-26 11:02:04 +01009478void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
9479 uint32_t boot_image_reference) {
9480 if (GetCompilerOptions().IsBootImage()) {
9481 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9482 NewBootImageIntrinsicPatch(boot_image_reference);
9483 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01009484 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009485 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9486 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009487 EmitMovwMovtPlaceholder(labels, reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08009488 __ Ldr(reg, MemOperand(reg, /* offset= */ 0));
Vladimir Markoeebb8212018-06-05 14:57:24 +01009489 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01009490 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01009491 gc::Heap* heap = Runtime::Current()->GetHeap();
9492 DCHECK(!heap->GetBootImageSpaces().empty());
9493 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01009494 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009495 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
9496 }
9497}
9498
Vladimir Marko7968cae2021-01-19 12:02:35 +00009499void CodeGeneratorARMVIXL::LoadTypeForBootImageIntrinsic(vixl::aarch32::Register reg,
9500 TypeReference target_type) {
9501 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
9502 DCHECK(GetCompilerOptions().IsBootImage());
9503 PcRelativePatchInfo* labels =
9504 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
9505 EmitMovwMovtPlaceholder(labels, reg);
9506}
9507
Vladimir Markode91ca92020-10-27 13:41:40 +00009508void CodeGeneratorARMVIXL::LoadIntrinsicDeclaringClass(vixl32::Register reg, HInvoke* invoke) {
9509 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009510 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009511 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01009512 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko7968cae2021-01-19 12:02:35 +00009513 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01009514 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00009515 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
9516 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009517 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01009518}
9519
Vladimir Marko7968cae2021-01-19 12:02:35 +00009520void CodeGeneratorARMVIXL::LoadClassRootForIntrinsic(vixl::aarch32::Register reg,
9521 ClassRoot class_root) {
9522 if (GetCompilerOptions().IsBootImage()) {
9523 ScopedObjectAccess soa(Thread::Current());
9524 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
9525 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
9526 LoadTypeForBootImageIntrinsic(reg, target_type);
9527 } else {
9528 uint32_t boot_image_offset = GetBootImageOffset(class_root);
9529 LoadBootImageAddress(reg, boot_image_offset);
9530 }
9531}
9532
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009533template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009534inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9535 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009536 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009537 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009538 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009539 size_t offset_or_index = info.offset_or_index;
9540 DCHECK(info.add_pc_label.IsBound());
9541 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9542 // Add MOVW patch.
9543 DCHECK(info.movw_label.IsBound());
9544 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009545 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009546 // Add MOVT patch.
9547 DCHECK(info.movt_label.IsBound());
9548 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009549 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009550 }
9551}
9552
Vladimir Marko6fd16062018-06-26 11:02:04 +01009553template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
9554linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
9555 const DexFile* target_dex_file,
9556 uint32_t pc_insn_offset,
9557 uint32_t boot_image_offset) {
9558 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
9559 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00009560}
9561
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009562void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009563 DCHECK(linker_patches->empty());
9564 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009565 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009566 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009567 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009568 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01009569 /* MOVW+MOVT for each entry */ 2u * public_type_bss_entry_patches_.size() +
9570 /* MOVW+MOVT for each entry */ 2u * package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009571 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009572 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01009573 /* MOVW+MOVT for each entry */ 2u * boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01009574 call_entrypoint_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009575 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009576 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01009577 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009578 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009579 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009580 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009581 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009582 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009583 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009584 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009585 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009586 DCHECK(boot_image_type_patches_.empty());
9587 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01009588 }
9589 if (GetCompilerOptions().IsBootImage()) {
9590 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
9591 boot_image_other_patches_, linker_patches);
9592 } else {
9593 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
9594 boot_image_other_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009595 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009596 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9597 method_bss_entry_patches_, linker_patches);
9598 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9599 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01009600 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
9601 public_type_bss_entry_patches_, linker_patches);
9602 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
9603 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009604 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9605 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01009606 for (const PatchInfo<vixl32::Label>& info : call_entrypoint_patches_) {
9607 DCHECK(info.target_dex_file == nullptr);
9608 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
9609 info.label.GetLocation(), info.offset_or_index));
9610 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009611 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009612 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9613 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009614 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009615 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009616}
9617
Vladimir Markoca1e0382018-04-11 09:58:41 +00009618bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01009619 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
9620 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00009621 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9622}
9623
9624void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9625 /*out*/ ArenaVector<uint8_t>* code,
9626 /*out*/ std::string* debug_name) {
9627 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9628 switch (patch.GetType()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009629 case linker::LinkerPatch::Type::kCallRelative: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009630 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9631 // to the generic JNI and interpreter trampolines.
Vladimir Markof6675082019-05-17 12:05:28 +01009632 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
9633 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, vixl32::r0, offset.Int32Value());
Vladimir Markoca1e0382018-04-11 09:58:41 +00009634 assembler.GetVIXLAssembler()->Bkpt(0);
David Srbecky889da942021-04-30 13:03:14 +01009635 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009636 *debug_name = "MethodCallThunk";
9637 }
9638 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009639 }
9640 case linker::LinkerPatch::Type::kCallEntrypoint: {
9641 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, tr, patch.EntrypointOffset());
9642 assembler.GetVIXLAssembler()->Bkpt(0);
David Srbecky889da942021-04-30 13:03:14 +01009643 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009644 *debug_name = "EntrypointCallThunk_" + std::to_string(patch.EntrypointOffset());
9645 }
9646 break;
9647 }
9648 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009649 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9650 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9651 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009652 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009653 default:
9654 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9655 UNREACHABLE();
9656 }
9657
9658 // Ensure we emit the literal pool if any.
9659 assembler.FinalizeCode();
9660 code->resize(assembler.CodeSize());
9661 MemoryRegion code_region(code->data(), code->size());
9662 assembler.FinalizeInstructions(code_region);
9663}
9664
Artem Serovc5fcb442016-12-02 19:19:58 +00009665VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9666 uint32_t value,
9667 Uint32ToLiteralMap* map) {
9668 return map->GetOrCreate(
9669 value,
9670 [this, value]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009671 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ value);
Artem Serovc5fcb442016-12-02 19:19:58 +00009672 });
9673}
9674
Artem Serov2bbc9532016-10-21 11:51:50 +01009675void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9676 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009677 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009678 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9679 Location::RequiresRegister());
9680 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9681 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9683}
9684
9685void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9686 vixl32::Register res = OutputRegister(instr);
9687 vixl32::Register accumulator =
9688 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9689 vixl32::Register mul_left =
9690 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9691 vixl32::Register mul_right =
9692 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9693
9694 if (instr->GetOpKind() == HInstruction::kAdd) {
9695 __ Mla(res, mul_left, mul_right, accumulator);
9696 } else {
9697 __ Mls(res, mul_left, mul_right, accumulator);
9698 }
9699}
9700
Artem Serov551b28f2016-10-18 19:11:30 +01009701void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9702 // Nothing to do, this should be removed during prepare for register allocator.
9703 LOG(FATAL) << "Unreachable";
9704}
9705
9706void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9707 // Nothing to do, this should be removed during prepare for register allocator.
9708 LOG(FATAL) << "Unreachable";
9709}
9710
9711// Simple implementation of packed switch - generate cascaded compare/jumps.
9712void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9713 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009714 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009715 locations->SetInAt(0, Location::RequiresRegister());
9716 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9717 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9718 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9719 if (switch_instr->GetStartValue() != 0) {
9720 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9721 }
9722 }
9723}
9724
9725// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9726void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9727 int32_t lower_bound = switch_instr->GetStartValue();
9728 uint32_t num_entries = switch_instr->GetNumEntries();
9729 LocationSummary* locations = switch_instr->GetLocations();
9730 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9731 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9732
9733 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9734 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9735 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009736 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009737 vixl32::Register temp_reg = temps.Acquire();
9738 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9739 // the immediate, because IP is used as the destination register. For the other
9740 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9741 // and they can be encoded in the instruction without making use of IP register.
9742 __ Adds(temp_reg, value_reg, -lower_bound);
9743
9744 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9745 // Jump to successors[0] if value == lower_bound.
9746 __ B(eq, codegen_->GetLabelOf(successors[0]));
9747 int32_t last_index = 0;
9748 for (; num_entries - last_index > 2; last_index += 2) {
9749 __ Adds(temp_reg, temp_reg, -2);
9750 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9751 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9752 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9753 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9754 }
9755 if (num_entries - last_index == 2) {
9756 // The last missing case_value.
9757 __ Cmp(temp_reg, 1);
9758 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9759 }
9760
9761 // And the default for any other value.
9762 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9763 __ B(codegen_->GetLabelOf(default_block));
9764 }
9765 } else {
9766 // Create a table lookup.
9767 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9768
9769 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9770
9771 // Remove the bias.
9772 vixl32::Register key_reg;
9773 if (lower_bound != 0) {
9774 key_reg = RegisterFrom(locations->GetTemp(1));
9775 __ Sub(key_reg, value_reg, lower_bound);
9776 } else {
9777 key_reg = value_reg;
9778 }
9779
9780 // Check whether the value is in the table, jump to default block if not.
9781 __ Cmp(key_reg, num_entries - 1);
9782 __ B(hi, codegen_->GetLabelOf(default_block));
9783
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009784 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009785 vixl32::Register jump_offset = temps.Acquire();
9786
9787 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009788 {
9789 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9790 ExactAssemblyScope aas(GetVIXLAssembler(),
9791 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9792 CodeBufferCheckScope::kMaximumSize);
9793 __ adr(table_base, jump_table->GetTableStartLabel());
9794 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009795
Scott Wakeling86e9d262017-01-18 15:59:24 +00009796 // Jump to target block by branching to table_base(pc related) + offset.
9797 vixl32::Register target_address = table_base;
9798 __ add(target_address, table_base, jump_offset);
9799 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009800
Scott Wakeling86e9d262017-01-18 15:59:24 +00009801 jump_table->EmitTable(codegen_);
9802 }
Artem Serov551b28f2016-10-18 19:11:30 +01009803 }
9804}
9805
Artem Serov02d37832016-10-25 15:25:33 +01009806// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009807void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009808 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009809 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009810 return;
9811 }
9812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009813 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009814
Artem Serovd4cc5b22016-11-04 11:19:09 +00009815 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009816 if (return_loc.Equals(trg)) {
9817 return;
9818 }
9819
Vladimir Markoac3fcff2020-11-17 12:17:58 +00009820 // Let the parallel move resolver take care of all of this.
9821 HParallelMove parallel_move(GetGraph()->GetAllocator());
9822 parallel_move.AddMove(return_loc, trg, type, nullptr);
9823 GetMoveResolver()->EmitNativeCode(&parallel_move);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009824}
Scott Wakelingfe885462016-09-22 10:24:38 +01009825
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009826void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9827 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009828 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009829 locations->SetInAt(0, Location::RequiresRegister());
9830 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009831}
9832
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009833void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9834 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9835 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9836 instruction->GetIndex(), kArmPointerSize).SizeValue();
9837 GetAssembler()->LoadFromOffset(kLoadWord,
9838 OutputRegister(instruction),
9839 InputRegisterAt(instruction, 0),
9840 method_offset);
9841 } else {
9842 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9843 instruction->GetIndex(), kArmPointerSize));
9844 GetAssembler()->LoadFromOffset(kLoadWord,
9845 OutputRegister(instruction),
9846 InputRegisterAt(instruction, 0),
9847 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9848 GetAssembler()->LoadFromOffset(kLoadWord,
9849 OutputRegister(instruction),
9850 OutputRegister(instruction),
9851 method_offset);
9852 }
Artem Serov551b28f2016-10-18 19:11:30 +01009853}
9854
Artem Serovc5fcb442016-12-02 19:19:58 +00009855static void PatchJitRootUse(uint8_t* code,
9856 const uint8_t* roots_data,
9857 VIXLUInt32Literal* literal,
9858 uint64_t index_in_table) {
9859 DCHECK(literal->IsBound());
9860 uint32_t literal_offset = literal->GetLocation();
9861 uintptr_t address =
9862 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9863 uint8_t* data = code + literal_offset;
9864 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9865}
9866
9867void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9868 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009869 const StringReference& string_reference = entry.first;
9870 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009871 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009872 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009873 }
9874 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009875 const TypeReference& type_reference = entry.first;
9876 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009877 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009878 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009879 }
9880}
9881
Artem Serovd4cc5b22016-11-04 11:19:09 +00009882void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9883 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9884 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009885 ExactAssemblyScope aas(GetVIXLAssembler(),
9886 3 * vixl32::kMaxInstructionSizeInBytes,
9887 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009888 // TODO(VIXL): Think about using mov instead of movw.
9889 __ bind(&labels->movw_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009890 __ movw(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009891 __ bind(&labels->movt_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009892 __ movt(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009893 __ bind(&labels->add_pc_label);
9894 __ add(out, out, pc);
9895}
9896
Scott Wakelingfe885462016-09-22 10:24:38 +01009897#undef __
9898#undef QUICK_ENTRY_POINT
9899#undef TODO_VIXL32
9900
Vladimir Markoca1e0382018-04-11 09:58:41 +00009901#define __ assembler.GetVIXLAssembler()->
9902
9903static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9904 vixl32::Register base_reg,
9905 vixl32::MemOperand& lock_word,
9906 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009907 int32_t raw_ldr_offset,
9908 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009909 // Load the lock word containing the rb_state.
9910 __ Ldr(ip, lock_word);
9911 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009912 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009913 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9914 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
Andreas Gampe3db70682018-12-26 15:12:03 -08009915 __ B(ne, slow_path, /* is_far_target= */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009916 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9917 if (throw_npe != nullptr) {
9918 __ Bind(throw_npe);
9919 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009920 __ Add(lr, lr, raw_ldr_offset);
9921 // Introduce a dependency on the lock_word including rb_state,
9922 // to prevent load-load reordering, and without using
9923 // a memory barrier (which would be more expensive).
9924 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9925 __ Bx(lr); // And return back to the function.
9926 // Note: The fake dependency is unnecessary for the slow path.
9927}
9928
9929// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009930static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009931 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009932 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9933 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009934 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9935 DCHECK_EQ(ip.GetCode(), 12u);
9936 const int32_t entry_point_offset =
9937 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9938 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +01009939 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009940}
9941
9942void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9943 uint32_t encoded_data,
9944 /*out*/ std::string* debug_name) {
9945 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9946 switch (kind) {
9947 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009948 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9949 CheckValidReg(base_reg.GetCode());
9950 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9951 CheckValidReg(holder_reg.GetCode());
9952 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9953 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9954 temps.Exclude(ip);
Roland Levillain988c3912019-09-25 19:33:35 +01009955 // In the case of a field load, if `base_reg` differs from
9956 // `holder_reg`, the offset was too large and we must have emitted (during the construction
9957 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
9958 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
9959 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
9960 // not necessarily do that check before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01009961 vixl32::Label throw_npe_label;
9962 vixl32::Label* throw_npe = nullptr;
9963 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9964 throw_npe = &throw_npe_label;
Andreas Gampe3db70682018-12-26 15:12:03 -08009965 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009966 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009967 // Check if the holder is gray and, if not, add fake dependency to the base register
9968 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9969 // to load the reference and call the entrypoint that performs further checks on the
9970 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009971 vixl32::Label slow_path;
9972 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9973 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9974 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9975 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009976 EmitGrayCheckAndFastPath(
9977 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009978 __ Bind(&slow_path);
9979 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9980 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +01009981 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009982 if (width == BakerReadBarrierWidth::kWide) {
9983 MemOperand ldr_half_address(lr, ldr_offset + 2);
9984 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9985 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9986 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9987 } else {
9988 MemOperand ldr_address(lr, ldr_offset);
9989 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9990 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9991 ep_reg, // for narrow LDR.
9992 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9993 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9994 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9995 }
9996 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9997 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009998 break;
9999 }
10000 case BakerReadBarrierKind::kArray: {
10001 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
10002 CheckValidReg(base_reg.GetCode());
10003 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10004 BakerReadBarrierSecondRegField::Decode(encoded_data));
10005 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10006 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
10007 temps.Exclude(ip);
10008 vixl32::Label slow_path;
10009 int32_t data_offset =
10010 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
10011 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
10012 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
10013 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
10014 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
10015 __ Bind(&slow_path);
10016 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
10017 raw_ldr_offset;
10018 MemOperand ldr_address(lr, ldr_offset + 2);
10019 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
10020 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +010010021 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +000010022 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
10023 // a switch case target based on the index register.
10024 __ Mov(ip, base_reg); // Move the base register to ip0.
10025 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
10026 break;
10027 }
Vladimir Markod887ed82018-08-14 13:52:12 +000010028 case BakerReadBarrierKind::kGcRoot:
Vladimir Marko3d350a82020-11-18 14:14:27 +000010029 case BakerReadBarrierKind::kIntrinsicCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +000010030 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
10031 // and it does not have a forwarding address), call the correct introspection entrypoint;
10032 // otherwise return the reference (or the extracted forwarding address).
10033 // There is no gray bit check for GC roots.
10034 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
10035 CheckValidReg(root_reg.GetCode());
10036 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10037 BakerReadBarrierSecondRegField::Decode(encoded_data));
10038 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
10039 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
10040 temps.Exclude(ip);
10041 vixl32::Label return_label, not_marked, forwarding_address;
Andreas Gampe3db70682018-12-26 15:12:03 -080010042 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +000010043 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
10044 __ Ldr(ip, lock_word);
10045 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
10046 __ B(eq, &not_marked);
10047 __ Bind(&return_label);
10048 __ Bx(lr);
10049 __ Bind(&not_marked);
10050 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
10051 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
10052 " the highest bits and the 'forwarding address' state to have all bits set");
10053 __ Cmp(ip, Operand(0xc0000000));
10054 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +010010055 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Marko3d350a82020-11-18 14:14:27 +000010056 // Adjust the art_quick_read_barrier_mark_introspection address
10057 // in kBakerCcEntrypointRegister to one of
10058 // art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},intrinsic_cas}.
10059 if (kind == BakerReadBarrierKind::kIntrinsicCas) {
10060 DCHECK(width == BakerReadBarrierWidth::kWide);
10061 DCHECK(!root_reg.IsLow());
10062 }
Vladimir Markod887ed82018-08-14 13:52:12 +000010063 int32_t entrypoint_offset =
10064 (kind == BakerReadBarrierKind::kGcRoot)
10065 ? (width == BakerReadBarrierWidth::kWide)
10066 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
10067 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
Vladimir Marko3d350a82020-11-18 14:14:27 +000010068 : BAKER_MARK_INTROSPECTION_INTRINSIC_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +000010069 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
10070 __ Mov(ip, root_reg);
10071 __ Bx(ep_reg);
10072 __ Bind(&forwarding_address);
10073 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
10074 __ Bx(lr);
10075 break;
10076 }
10077 default:
10078 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
10079 UNREACHABLE();
10080 }
10081
Vladimir Marko966b46f2018-08-03 10:20:19 +000010082 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +010010083 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +010010084 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +000010085 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +000010086 std::ostringstream oss;
10087 oss << "BakerReadBarrierThunk";
10088 switch (kind) {
10089 case BakerReadBarrierKind::kField:
10090 oss << "Field";
10091 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
10092 oss << "Wide";
10093 }
10094 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
10095 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
10096 break;
10097 case BakerReadBarrierKind::kArray:
10098 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10099 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10100 BakerReadBarrierSecondRegField::Decode(encoded_data));
10101 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10102 break;
10103 case BakerReadBarrierKind::kGcRoot:
10104 oss << "GcRoot";
10105 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
10106 oss << "Wide";
10107 }
10108 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10109 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10110 BakerReadBarrierSecondRegField::Decode(encoded_data));
10111 break;
Vladimir Marko3d350a82020-11-18 14:14:27 +000010112 case BakerReadBarrierKind::kIntrinsicCas:
10113 oss << "IntrinsicCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markod887ed82018-08-14 13:52:12 +000010114 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10115 BakerReadBarrierSecondRegField::Decode(encoded_data));
10116 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10117 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +000010118 }
10119 *debug_name = oss.str();
10120 }
10121}
10122
10123#undef __
10124
Scott Wakelingfe885462016-09-22 10:24:38 +010010125} // namespace arm
10126} // namespace art