blob: bf0c77da57a49a4005ed4c4106753e415f26f6e9 [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 ||
Sorin Basca4a4696a2021-10-09 07:14:40 +0000845 intrinsic == Intrinsics::kJdkUnsafeGetObjectAcquire ||
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000846 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko3d350a82020-11-18 14:14:27 +0000847 mirror::VarHandle::AccessModeTemplate::kGet ||
848 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
849 mirror::VarHandle::AccessModeTemplate::kCompareAndSet ||
850 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
Vladimir Marko526569a2020-11-30 15:48:38 +0000851 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange ||
852 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) ==
853 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate)
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000854 << instruction_->AsInvoke()->GetIntrinsic();
855 DCHECK_EQ(offset_, 0U);
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000856 // Though UnsafeGet's offset location is a register pair, we only pass the low
857 // part (high part is irrelevant for 32-bit addresses) to the slow path.
858 // For VarHandle intrinsics, the index is always just a register.
859 DCHECK(index_.IsRegister());
860 index = index_;
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000861 }
862 }
863
864 // We're moving two or three locations to locations that could
865 // overlap, so we need a parallel move resolver.
866 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100867 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000868 parallel_move.AddMove(ref_,
869 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100870 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000871 nullptr);
872 parallel_move.AddMove(obj_,
873 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100874 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000875 nullptr);
876 if (index.IsValid()) {
877 parallel_move.AddMove(index,
878 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100879 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000880 nullptr);
881 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
882 } else {
883 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
884 __ Mov(calling_convention.GetRegisterAt(2), offset_);
885 }
886 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
887 CheckEntrypointTypes<
888 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
889 arm_codegen->Move32(out_, LocationFrom(r0));
890
891 RestoreLiveRegisters(codegen, locations);
892 __ B(GetExitLabel());
893 }
894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100895 const char* GetDescription() const override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000896 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
897 }
898
899 private:
900 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
901 uint32_t ref = RegisterFrom(ref_).GetCode();
902 uint32_t obj = RegisterFrom(obj_).GetCode();
903 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
904 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
905 return vixl32::Register(i);
906 }
907 }
908 // We shall never fail to find a free caller-save register, as
909 // there are more than two core caller-save registers on ARM
910 // (meaning it is possible to find one which is different from
911 // `ref` and `obj`).
912 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
913 LOG(FATAL) << "Could not find a free caller-save register";
914 UNREACHABLE();
915 }
916
917 const Location out_;
918 const Location ref_;
919 const Location obj_;
920 const uint32_t offset_;
921 // An additional location containing an index to an array.
922 // Only used for HArrayGet and the UnsafeGetObject &
923 // UnsafeGetObjectVolatile intrinsics.
924 const Location index_;
925
926 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
927};
928
929// Slow path generating a read barrier for a GC root.
930class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
931 public:
932 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
933 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
934 DCHECK(kEmitCompilerReadBarrier);
935 }
936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100937 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000938 LocationSummary* locations = instruction_->GetLocations();
939 vixl32::Register reg_out = RegisterFrom(out_);
940 DCHECK(locations->CanCall());
941 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
Vladimir Markoac3fcff2020-11-17 12:17:58 +0000942 DCHECK(instruction_->IsLoadClass() ||
943 instruction_->IsLoadString() ||
944 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000945 << "Unexpected instruction in read barrier for GC root slow path: "
946 << instruction_->DebugName();
947
948 __ Bind(GetEntryLabel());
949 SaveLiveRegisters(codegen, locations);
950
951 InvokeRuntimeCallingConventionARMVIXL calling_convention;
952 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
953 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
954 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
955 instruction_,
956 instruction_->GetDexPc(),
957 this);
958 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
959 arm_codegen->Move32(out_, LocationFrom(r0));
960
961 RestoreLiveRegisters(codegen, locations);
962 __ B(GetExitLabel());
963 }
964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100965 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARMVIXL"; }
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000966
967 private:
968 const Location out_;
969 const Location root_;
970
971 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
972};
Scott Wakelingc34dba72016-10-03 10:14:44 +0100973
Mythri Alle2d4feeb2021-10-13 15:39:37 +0000974class MethodEntryExitHooksSlowPathARMVIXL : public SlowPathCodeARMVIXL {
975 public:
976 explicit MethodEntryExitHooksSlowPathARMVIXL(HInstruction* instruction)
977 : SlowPathCodeARMVIXL(instruction) {}
978
979 void EmitNativeCode(CodeGenerator* codegen) override {
980 LocationSummary* locations = instruction_->GetLocations();
981 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
982 QuickEntrypointEnum entry_point =
983 (instruction_->IsMethodEntryHook()) ? kQuickMethodEntryHook : kQuickMethodExitHook;
984 __ Bind(GetEntryLabel());
985 SaveLiveRegisters(codegen, locations);
986 arm_codegen->InvokeRuntime(entry_point, instruction_, instruction_->GetDexPc(), this);
987 RestoreLiveRegisters(codegen, locations);
988 __ B(GetExitLabel());
989 }
990
991 const char* GetDescription() const override {
992 return "MethodEntryExitHooksSlowPath";
993 }
994
995 private:
996 DISALLOW_COPY_AND_ASSIGN(MethodEntryExitHooksSlowPathARMVIXL);
997};
998
Scott Wakelingfe885462016-09-22 10:24:38 +0100999inline vixl32::Condition ARMCondition(IfCondition cond) {
1000 switch (cond) {
1001 case kCondEQ: return eq;
1002 case kCondNE: return ne;
1003 case kCondLT: return lt;
1004 case kCondLE: return le;
1005 case kCondGT: return gt;
1006 case kCondGE: return ge;
1007 case kCondB: return lo;
1008 case kCondBE: return ls;
1009 case kCondA: return hi;
1010 case kCondAE: return hs;
1011 }
1012 LOG(FATAL) << "Unreachable";
1013 UNREACHABLE();
1014}
1015
1016// Maps signed condition to unsigned condition.
1017inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
1018 switch (cond) {
1019 case kCondEQ: return eq;
1020 case kCondNE: return ne;
1021 // Signed to unsigned.
1022 case kCondLT: return lo;
1023 case kCondLE: return ls;
1024 case kCondGT: return hi;
1025 case kCondGE: return hs;
1026 // Unsigned remain unchanged.
1027 case kCondB: return lo;
1028 case kCondBE: return ls;
1029 case kCondA: return hi;
1030 case kCondAE: return hs;
1031 }
1032 LOG(FATAL) << "Unreachable";
1033 UNREACHABLE();
1034}
1035
1036inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1037 // The ARM condition codes can express all the necessary branches, see the
1038 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1039 // There is no dex instruction or HIR that would need the missing conditions
1040 // "equal or unordered" or "not equal".
1041 switch (cond) {
1042 case kCondEQ: return eq;
1043 case kCondNE: return ne /* unordered */;
1044 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1045 case kCondLE: return gt_bias ? ls : le /* unordered */;
1046 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1047 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1048 default:
1049 LOG(FATAL) << "UNREACHABLE";
1050 UNREACHABLE();
1051 }
1052}
1053
Anton Kirilov74234da2017-01-13 14:42:47 +00001054inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1055 switch (op_kind) {
1056 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1057 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1058 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1059 default:
1060 LOG(FATAL) << "Unexpected op kind " << op_kind;
1061 UNREACHABLE();
1062 }
1063}
1064
Scott Wakelingfe885462016-09-22 10:24:38 +01001065void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1066 stream << vixl32::Register(reg);
1067}
1068
1069void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1070 stream << vixl32::SRegister(reg);
1071}
1072
Vladimir Markoa0431112018-06-25 09:32:54 +01001073const ArmInstructionSetFeatures& CodeGeneratorARMVIXL::GetInstructionSetFeatures() const {
1074 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArmInstructionSetFeatures();
1075}
1076
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001077static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001078 uint32_t mask = 0;
1079 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1080 i <= regs.GetLastSRegister().GetCode();
1081 ++i) {
1082 mask |= (1 << i);
1083 }
1084 return mask;
1085}
1086
Artem Serovd4cc5b22016-11-04 11:19:09 +00001087// Saves the register in the stack. Returns the size taken on stack.
1088size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1089 uint32_t reg_id ATTRIBUTE_UNUSED) {
1090 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001091 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001092}
1093
1094// Restores the register from the stack. Returns the size taken on stack.
1095size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1096 uint32_t reg_id ATTRIBUTE_UNUSED) {
1097 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001098 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001099}
1100
1101size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1102 uint32_t reg_id ATTRIBUTE_UNUSED) {
1103 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001104 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001105}
1106
1107size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1108 uint32_t reg_id ATTRIBUTE_UNUSED) {
1109 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001110 UNREACHABLE();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001111}
1112
Anton Kirilov74234da2017-01-13 14:42:47 +00001113static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1114 vixl32::Register out,
1115 vixl32::Register first,
1116 const Operand& second,
1117 CodeGeneratorARMVIXL* codegen) {
1118 if (second.IsImmediate() && second.GetImmediate() == 0) {
1119 const Operand in = kind == HInstruction::kAnd
1120 ? Operand(0)
1121 : Operand(first);
1122
1123 __ Mov(out, in);
1124 } else {
1125 switch (kind) {
1126 case HInstruction::kAdd:
1127 __ Add(out, first, second);
1128 break;
1129 case HInstruction::kAnd:
1130 __ And(out, first, second);
1131 break;
1132 case HInstruction::kOr:
1133 __ Orr(out, first, second);
1134 break;
1135 case HInstruction::kSub:
1136 __ Sub(out, first, second);
1137 break;
1138 case HInstruction::kXor:
1139 __ Eor(out, first, second);
1140 break;
1141 default:
1142 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1143 UNREACHABLE();
1144 }
1145 }
1146}
1147
1148static void GenerateDataProc(HInstruction::InstructionKind kind,
1149 const Location& out,
1150 const Location& first,
1151 const Operand& second_lo,
1152 const Operand& second_hi,
1153 CodeGeneratorARMVIXL* codegen) {
1154 const vixl32::Register first_hi = HighRegisterFrom(first);
1155 const vixl32::Register first_lo = LowRegisterFrom(first);
1156 const vixl32::Register out_hi = HighRegisterFrom(out);
1157 const vixl32::Register out_lo = LowRegisterFrom(out);
1158
1159 if (kind == HInstruction::kAdd) {
1160 __ Adds(out_lo, first_lo, second_lo);
1161 __ Adc(out_hi, first_hi, second_hi);
1162 } else if (kind == HInstruction::kSub) {
1163 __ Subs(out_lo, first_lo, second_lo);
1164 __ Sbc(out_hi, first_hi, second_hi);
1165 } else {
1166 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1167 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1168 }
1169}
1170
1171static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1172 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1173}
1174
1175static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1176 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001177 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001178 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1179
1180 const LocationSummary* const locations = instruction->GetLocations();
1181 const uint32_t shift_value = instruction->GetShiftAmount();
1182 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1183 const Location first = locations->InAt(0);
1184 const Location second = locations->InAt(1);
1185 const Location out = locations->Out();
1186 const vixl32::Register first_hi = HighRegisterFrom(first);
1187 const vixl32::Register first_lo = LowRegisterFrom(first);
1188 const vixl32::Register out_hi = HighRegisterFrom(out);
1189 const vixl32::Register out_lo = LowRegisterFrom(out);
1190 const vixl32::Register second_hi = HighRegisterFrom(second);
1191 const vixl32::Register second_lo = LowRegisterFrom(second);
1192 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1193
1194 if (shift_value >= 32) {
1195 if (shift == ShiftType::LSL) {
1196 GenerateDataProcInstruction(kind,
1197 out_hi,
1198 first_hi,
1199 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1200 codegen);
1201 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1202 } else if (shift == ShiftType::ASR) {
1203 GenerateDataProc(kind,
1204 out,
1205 first,
1206 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1207 Operand(second_hi, ShiftType::ASR, 31),
1208 codegen);
1209 } else {
1210 DCHECK_EQ(shift, ShiftType::LSR);
1211 GenerateDataProc(kind,
1212 out,
1213 first,
1214 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1215 0,
1216 codegen);
1217 }
1218 } else {
1219 DCHECK_GT(shift_value, 1U);
1220 DCHECK_LT(shift_value, 32U);
1221
1222 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1223
1224 if (shift == ShiftType::LSL) {
1225 // We are not doing this for HInstruction::kAdd because the output will require
1226 // Location::kOutputOverlap; not applicable to other cases.
1227 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1228 GenerateDataProcInstruction(kind,
1229 out_hi,
1230 first_hi,
1231 Operand(second_hi, ShiftType::LSL, shift_value),
1232 codegen);
1233 GenerateDataProcInstruction(kind,
1234 out_hi,
1235 out_hi,
1236 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1237 codegen);
1238 GenerateDataProcInstruction(kind,
1239 out_lo,
1240 first_lo,
1241 Operand(second_lo, ShiftType::LSL, shift_value),
1242 codegen);
1243 } else {
1244 const vixl32::Register temp = temps.Acquire();
1245
1246 __ Lsl(temp, second_hi, shift_value);
1247 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1248 GenerateDataProc(kind,
1249 out,
1250 first,
1251 Operand(second_lo, ShiftType::LSL, shift_value),
1252 temp,
1253 codegen);
1254 }
1255 } else {
1256 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1257
1258 // We are not doing this for HInstruction::kAdd because the output will require
1259 // Location::kOutputOverlap; not applicable to other cases.
1260 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1261 GenerateDataProcInstruction(kind,
1262 out_lo,
1263 first_lo,
1264 Operand(second_lo, ShiftType::LSR, shift_value),
1265 codegen);
1266 GenerateDataProcInstruction(kind,
1267 out_lo,
1268 out_lo,
1269 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1270 codegen);
1271 GenerateDataProcInstruction(kind,
1272 out_hi,
1273 first_hi,
1274 Operand(second_hi, shift, shift_value),
1275 codegen);
1276 } else {
1277 const vixl32::Register temp = temps.Acquire();
1278
1279 __ Lsr(temp, second_lo, shift_value);
1280 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1281 GenerateDataProc(kind,
1282 out,
1283 first,
1284 temp,
1285 Operand(second_hi, shift, shift_value),
1286 codegen);
1287 }
1288 }
1289 }
1290}
1291
Donghui Bai426b49c2016-11-08 14:55:38 +08001292static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1293 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1294 if (rhs_loc.IsConstant()) {
1295 // 0.0 is the only immediate that can be encoded directly in
1296 // a VCMP instruction.
1297 //
1298 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1299 // specify that in a floating-point comparison, positive zero
1300 // and negative zero are considered equal, so we can use the
1301 // literal 0.0 for both cases here.
1302 //
1303 // Note however that some methods (Float.equal, Float.compare,
1304 // Float.compareTo, Double.equal, Double.compare,
1305 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1306 // StrictMath.min) consider 0.0 to be (strictly) greater than
1307 // -0.0. So if we ever translate calls to these methods into a
1308 // HCompare instruction, we must handle the -0.0 case with
1309 // care here.
1310 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001312 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001315 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1316 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001317 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001318 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1319 }
1320 } else {
1321 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1322 }
1323}
1324
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001325static int64_t AdjustConstantForCondition(int64_t value,
1326 IfCondition* condition,
1327 IfCondition* opposite) {
1328 if (value == 1) {
1329 if (*condition == kCondB) {
1330 value = 0;
1331 *condition = kCondEQ;
1332 *opposite = kCondNE;
1333 } else if (*condition == kCondAE) {
1334 value = 0;
1335 *condition = kCondNE;
1336 *opposite = kCondEQ;
1337 }
1338 } else if (value == -1) {
1339 if (*condition == kCondGT) {
1340 value = 0;
1341 *condition = kCondGE;
1342 *opposite = kCondLT;
1343 } else if (*condition == kCondLE) {
1344 value = 0;
1345 *condition = kCondLT;
1346 *opposite = kCondGE;
1347 }
1348 }
1349
1350 return value;
1351}
1352
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001353static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1354 HCondition* condition,
1355 bool invert,
1356 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001357 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001358
1359 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001360 IfCondition cond = condition->GetCondition();
1361 IfCondition opposite = condition->GetOppositeCondition();
1362
1363 if (invert) {
1364 std::swap(cond, opposite);
1365 }
1366
1367 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001368 const Location left = locations->InAt(0);
1369 const Location right = locations->InAt(1);
1370
1371 DCHECK(right.IsConstant());
1372
1373 const vixl32::Register left_high = HighRegisterFrom(left);
1374 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001375 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1376 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1377
1378 // Comparisons against 0 are common enough to deserve special attention.
1379 if (value == 0) {
1380 switch (cond) {
1381 case kCondNE:
1382 // x > 0 iff x != 0 when the comparison is unsigned.
1383 case kCondA:
1384 ret = std::make_pair(ne, eq);
1385 FALLTHROUGH_INTENDED;
1386 case kCondEQ:
1387 // x <= 0 iff x == 0 when the comparison is unsigned.
1388 case kCondBE:
1389 __ Orrs(temps.Acquire(), left_low, left_high);
1390 return ret;
1391 case kCondLT:
1392 case kCondGE:
1393 __ Cmp(left_high, 0);
1394 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1395 // Trivially true or false.
1396 case kCondB:
1397 ret = std::make_pair(ne, eq);
1398 FALLTHROUGH_INTENDED;
1399 case kCondAE:
1400 __ Cmp(left_low, left_low);
1401 return ret;
1402 default:
1403 break;
1404 }
1405 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001406
1407 switch (cond) {
1408 case kCondEQ:
1409 case kCondNE:
1410 case kCondB:
1411 case kCondBE:
1412 case kCondA:
1413 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001414 const uint32_t value_low = Low32Bits(value);
1415 Operand operand_low(value_low);
1416
Donghui Bai426b49c2016-11-08 14:55:38 +08001417 __ Cmp(left_high, High32Bits(value));
1418
Anton Kirilov23b752b2017-07-20 14:40:44 +01001419 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1420 // we must ensure that the operands corresponding to the least significant
1421 // halves of the inputs fit into a 16-bit CMP encoding.
1422 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1423 operand_low = Operand(temps.Acquire());
1424 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1425 }
1426
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001427 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001428 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1429 2 * vixl32::k16BitT32InstructionSizeInBytes,
1430 CodeBufferCheckScope::kExactSize);
1431
1432 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001433 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001434 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001435 break;
1436 }
1437 case kCondLE:
1438 case kCondGT:
1439 // Trivially true or false.
1440 if (value == std::numeric_limits<int64_t>::max()) {
1441 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001442 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001443 break;
1444 }
1445
1446 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001447 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001448 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001449 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001450 } else {
1451 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001452 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001453 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001454 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001455 }
1456
1457 value++;
1458 FALLTHROUGH_INTENDED;
1459 case kCondGE:
1460 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001461 __ Cmp(left_low, Low32Bits(value));
1462 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001463 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001464 break;
1465 }
1466 default:
1467 LOG(FATAL) << "Unreachable";
1468 UNREACHABLE();
1469 }
1470
1471 return ret;
1472}
1473
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001474static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1475 HCondition* condition,
1476 bool invert,
1477 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001478 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001479
1480 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001481 IfCondition cond = condition->GetCondition();
1482 IfCondition opposite = condition->GetOppositeCondition();
1483
1484 if (invert) {
1485 std::swap(cond, opposite);
1486 }
1487
1488 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001489 Location left = locations->InAt(0);
1490 Location right = locations->InAt(1);
1491
1492 DCHECK(right.IsRegisterPair());
1493
1494 switch (cond) {
1495 case kCondEQ:
1496 case kCondNE:
1497 case kCondB:
1498 case kCondBE:
1499 case kCondA:
1500 case kCondAE: {
1501 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1502
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001503 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001504 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1505 2 * vixl32::k16BitT32InstructionSizeInBytes,
1506 CodeBufferCheckScope::kExactSize);
1507
1508 __ it(eq);
1509 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001510 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001511 break;
1512 }
1513 case kCondLE:
1514 case kCondGT:
1515 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001516 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001517 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001518 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001519 } else {
1520 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001521 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001522 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001523 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001524 }
1525
1526 std::swap(left, right);
1527 FALLTHROUGH_INTENDED;
1528 case kCondGE:
1529 case kCondLT: {
1530 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1531
1532 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1533 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001534 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001535 break;
1536 }
1537 default:
1538 LOG(FATAL) << "Unreachable";
1539 UNREACHABLE();
1540 }
1541
1542 return ret;
1543}
1544
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001545static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1546 bool invert,
1547 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001548 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001549 IfCondition cond = condition->GetCondition();
1550 IfCondition opposite = condition->GetOppositeCondition();
1551 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001552
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001553 if (invert) {
1554 std::swap(cond, opposite);
1555 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001557 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001558 ret = condition->GetLocations()->InAt(1).IsConstant()
1559 ? GenerateLongTestConstant(condition, invert, codegen)
1560 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001561 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001562 GenerateVcmp(condition, codegen);
1563 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1564 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1565 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001566 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001567 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001568 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1569 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001570 }
1571
1572 return ret;
1573}
1574
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001575static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001576 const vixl32::Register out = OutputRegister(cond);
1577 const auto condition = GenerateTest(cond, false, codegen);
1578
1579 __ Mov(LeaveFlags, out, 0);
1580
1581 if (out.IsLow()) {
1582 // We use the scope because of the IT block that follows.
1583 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1584 2 * vixl32::k16BitT32InstructionSizeInBytes,
1585 CodeBufferCheckScope::kExactSize);
1586
1587 __ it(condition.first);
1588 __ mov(condition.first, out, 1);
1589 } else {
1590 vixl32::Label done_label;
1591 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1592
Andreas Gampe3db70682018-12-26 15:12:03 -08001593 __ B(condition.second, final_label, /* is_far_target= */ false);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001594 __ Mov(out, 1);
1595
1596 if (done_label.IsReferenced()) {
1597 __ Bind(&done_label);
1598 }
1599 }
1600}
1601
1602static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001603 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001604
1605 const LocationSummary* const locations = cond->GetLocations();
1606 IfCondition condition = cond->GetCondition();
1607 const vixl32::Register out = OutputRegister(cond);
1608 const Location left = locations->InAt(0);
1609 const Location right = locations->InAt(1);
1610 vixl32::Register left_high = HighRegisterFrom(left);
1611 vixl32::Register left_low = LowRegisterFrom(left);
1612 vixl32::Register temp;
1613 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1614
1615 if (right.IsConstant()) {
1616 IfCondition opposite = cond->GetOppositeCondition();
1617 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
1618 &condition,
1619 &opposite);
1620 Operand right_high = High32Bits(value);
1621 Operand right_low = Low32Bits(value);
1622
1623 // The output uses Location::kNoOutputOverlap.
1624 if (out.Is(left_high)) {
1625 std::swap(left_low, left_high);
1626 std::swap(right_low, right_high);
1627 }
1628
1629 __ Sub(out, left_low, right_low);
1630 temp = temps.Acquire();
1631 __ Sub(temp, left_high, right_high);
1632 } else {
1633 DCHECK(right.IsRegisterPair());
1634 temp = temps.Acquire();
1635 __ Sub(temp, left_high, HighRegisterFrom(right));
1636 __ Sub(out, left_low, LowRegisterFrom(right));
1637 }
1638
1639 // Need to check after calling AdjustConstantForCondition().
1640 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
1641
1642 if (condition == kCondNE && out.IsLow()) {
1643 __ Orrs(out, out, temp);
1644
1645 // We use the scope because of the IT block that follows.
1646 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1647 2 * vixl32::k16BitT32InstructionSizeInBytes,
1648 CodeBufferCheckScope::kExactSize);
1649
1650 __ it(ne);
1651 __ mov(ne, out, 1);
1652 } else {
1653 __ Orr(out, out, temp);
1654 codegen->GenerateConditionWithZero(condition, out, out, temp);
1655 }
1656}
1657
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001658static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001659 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001660
1661 const LocationSummary* const locations = cond->GetLocations();
1662 IfCondition condition = cond->GetCondition();
1663 const vixl32::Register out = OutputRegister(cond);
1664 const Location left = locations->InAt(0);
1665 const Location right = locations->InAt(1);
1666
1667 if (right.IsConstant()) {
1668 IfCondition opposite = cond->GetOppositeCondition();
1669
1670 // Comparisons against 0 are common enough to deserve special attention.
1671 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
1672 switch (condition) {
1673 case kCondNE:
1674 case kCondA:
1675 if (out.IsLow()) {
1676 // We only care if both input registers are 0 or not.
1677 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
1678
1679 // We use the scope because of the IT block that follows.
1680 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1681 2 * vixl32::k16BitT32InstructionSizeInBytes,
1682 CodeBufferCheckScope::kExactSize);
1683
1684 __ it(ne);
1685 __ mov(ne, out, 1);
1686 return;
1687 }
1688
1689 FALLTHROUGH_INTENDED;
1690 case kCondEQ:
1691 case kCondBE:
1692 // We only care if both input registers are 0 or not.
1693 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
1694 codegen->GenerateConditionWithZero(condition, out, out);
1695 return;
1696 case kCondLT:
1697 case kCondGE:
1698 // We only care about the sign bit.
1699 FALLTHROUGH_INTENDED;
1700 case kCondAE:
1701 case kCondB:
1702 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
1703 return;
1704 case kCondLE:
1705 case kCondGT:
1706 default:
1707 break;
1708 }
1709 }
1710 }
1711
Anton Kirilov23b752b2017-07-20 14:40:44 +01001712 // If `out` is a low register, then the GenerateConditionGeneric()
1713 // function generates a shorter code sequence that is still branchless.
1714 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001715 GenerateEqualLong(cond, codegen);
1716 return;
1717 }
1718
Anton Kirilov23b752b2017-07-20 14:40:44 +01001719 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001720}
1721
Roland Levillain6d729a72017-06-30 18:34:01 +01001722static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
1723 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001724 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001728 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001729 GenerateConditionLong(cond, codegen);
1730 return;
1731 }
1732
1733 IfCondition condition = cond->GetCondition();
1734 vixl32::Register in = InputRegisterAt(cond, 0);
1735 const vixl32::Register out = OutputRegister(cond);
1736 const Location right = cond->GetLocations()->InAt(1);
1737 int64_t value;
1738
1739 if (right.IsConstant()) {
1740 IfCondition opposite = cond->GetOppositeCondition();
1741
1742 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
1743
1744 // Comparisons against 0 are common enough to deserve special attention.
1745 if (value == 0) {
1746 switch (condition) {
1747 case kCondNE:
1748 case kCondA:
1749 if (out.IsLow() && out.Is(in)) {
1750 __ Cmp(out, 0);
1751
1752 // We use the scope because of the IT block that follows.
1753 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1754 2 * vixl32::k16BitT32InstructionSizeInBytes,
1755 CodeBufferCheckScope::kExactSize);
1756
1757 __ it(ne);
1758 __ mov(ne, out, 1);
1759 return;
1760 }
1761
1762 FALLTHROUGH_INTENDED;
1763 case kCondEQ:
1764 case kCondBE:
1765 case kCondLT:
1766 case kCondGE:
1767 case kCondAE:
1768 case kCondB:
1769 codegen->GenerateConditionWithZero(condition, out, in);
1770 return;
1771 case kCondLE:
1772 case kCondGT:
1773 default:
1774 break;
1775 }
1776 }
1777 }
1778
1779 if (condition == kCondEQ || condition == kCondNE) {
1780 Operand operand(0);
1781
1782 if (right.IsConstant()) {
1783 operand = Operand::From(value);
1784 } else if (out.Is(RegisterFrom(right))) {
1785 // Avoid 32-bit instructions if possible.
1786 operand = InputOperandAt(cond, 0);
1787 in = RegisterFrom(right);
1788 } else {
1789 operand = InputOperandAt(cond, 1);
1790 }
1791
1792 if (condition == kCondNE && out.IsLow()) {
1793 __ Subs(out, in, operand);
1794
1795 // We use the scope because of the IT block that follows.
1796 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1797 2 * vixl32::k16BitT32InstructionSizeInBytes,
1798 CodeBufferCheckScope::kExactSize);
1799
1800 __ it(ne);
1801 __ mov(ne, out, 1);
1802 } else {
1803 __ Sub(out, in, operand);
1804 codegen->GenerateConditionWithZero(condition, out, out);
1805 }
1806
1807 return;
1808 }
1809
1810 GenerateConditionGeneric(cond, codegen);
1811}
1812
Donghui Bai426b49c2016-11-08 14:55:38 +08001813static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001815 bool ret = false;
1816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001820 const uint64_t value = Uint64ConstantFrom(constant);
1821
1822 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1823 } else {
1824 ret = IsUint<8>(Int32ConstantFrom(constant));
1825 }
1826
1827 return ret;
1828}
1829
1830static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001832
1833 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1834 return Location::ConstantLocation(constant->AsConstant());
1835 }
1836
1837 return Location::RequiresRegister();
1838}
1839
1840static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1841 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1842 // we check that we are not dealing with floating-point output (there is no
1843 // 16-bit VMOV encoding).
1844 if (!out.IsRegister() && !out.IsRegisterPair()) {
1845 return false;
1846 }
1847
1848 // For constants, we also check that the output is in one or two low registers,
1849 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1850 // MOV encoding can be used.
1851 if (src.IsConstant()) {
1852 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1853 return false;
1854 }
1855
1856 if (out.IsRegister()) {
1857 if (!RegisterFrom(out).IsLow()) {
1858 return false;
1859 }
1860 } else {
1861 DCHECK(out.IsRegisterPair());
1862
1863 if (!HighRegisterFrom(out).IsLow()) {
1864 return false;
1865 }
1866 }
1867 }
1868
1869 return true;
1870}
1871
Scott Wakelingfe885462016-09-22 10:24:38 +01001872#undef __
1873
Donghui Bai426b49c2016-11-08 14:55:38 +08001874vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1875 vixl32::Label* final_label) {
1876 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001877 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001878
1879 const HBasicBlock* const block = instruction->GetBlock();
1880 const HLoopInformation* const info = block->GetLoopInformation();
1881 HInstruction* const next = instruction->GetNext();
1882
1883 // Avoid a branch to a branch.
1884 if (next->IsGoto() && (info == nullptr ||
1885 !info->IsBackEdge(*block) ||
1886 !info->HasSuspendCheck())) {
1887 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1888 }
1889
1890 return final_label;
1891}
1892
Scott Wakelingfe885462016-09-22 10:24:38 +01001893CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +01001894 const CompilerOptions& compiler_options,
1895 OptimizingCompilerStats* stats)
1896 : CodeGenerator(graph,
1897 kNumberOfCoreRegisters,
1898 kNumberOfSRegisters,
1899 kNumberOfRegisterPairs,
1900 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001901 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001902 compiler_options,
1903 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001904 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1905 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001906 location_builder_(graph, this),
1907 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001908 move_resolver_(graph->GetAllocator(), this),
1909 assembler_(graph->GetAllocator()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001910 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001911 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001912 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001913 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001914 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1915 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001916 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001917 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001918 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001919 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001920 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001921 uint32_literals_(std::less<uint32_t>(),
1922 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001923 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001924 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001925 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001926 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1927 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1928 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001929 // Always save the LR register to mimic Quick.
1930 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00001931 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
1932 // S0-S31, which alias to D0-D15.
1933 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
1934 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01001935}
1936
Artem Serov551b28f2016-10-18 19:11:30 +01001937void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1938 uint32_t num_entries = switch_instr_->GetNumEntries();
1939 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1940
1941 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001942 // underlying code buffer and we have generated a jump table of the right size, using
1943 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001944 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1945 num_entries * sizeof(int32_t),
1946 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001947 // TODO(VIXL): Check that using lower case bind is fine here.
1948 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001949 for (uint32_t i = 0; i < num_entries; i++) {
1950 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1951 }
1952}
1953
1954void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1955 uint32_t num_entries = switch_instr_->GetNumEntries();
1956 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1957
Artem Serov551b28f2016-10-18 19:11:30 +01001958 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1959 for (uint32_t i = 0; i < num_entries; i++) {
1960 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1961 DCHECK(target_label->IsBound());
1962 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1963 // When doing BX to address we need to have lower bit set to 1 in T32.
1964 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1965 jump_offset++;
1966 }
1967 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1968 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001969
Scott Wakelingb77051e2016-11-21 19:46:00 +00001970 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001971 }
1972}
1973
Artem Serov09a940d2016-11-11 16:15:11 +00001974void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001975 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001976 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001977 }
1978}
1979
Andreas Gampeca620d72016-11-08 08:09:33 -08001980#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001981
1982void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001983 FixJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001984
1985 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001986 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001987 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1988 uint32_t encoded_data = entry.first;
1989 vixl::aarch32::Label* slow_path_entry = &entry.second.label;
1990 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001991 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001992 }
1993
Scott Wakelingfe885462016-09-22 10:24:38 +01001994 GetAssembler()->FinalizeCode();
1995 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001996
1997 // Verify Baker read barrier linker patches.
1998 if (kIsDebugBuild) {
1999 ArrayRef<const uint8_t> code = allocator->GetMemory();
2000 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
2001 DCHECK(info.label.IsBound());
2002 uint32_t literal_offset = info.label.GetLocation();
2003 DCHECK_ALIGNED(literal_offset, 2u);
2004
2005 auto GetInsn16 = [&code](uint32_t offset) {
2006 DCHECK_ALIGNED(offset, 2u);
2007 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
2008 (static_cast<uint32_t>(code[offset + 1]) << 8);
2009 };
2010 auto GetInsn32 = [=](uint32_t offset) {
2011 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
2012 };
2013
2014 uint32_t encoded_data = info.custom_data;
2015 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
2016 // Check that the next instruction matches the expected LDR.
2017 switch (kind) {
2018 case BakerReadBarrierKind::kField: {
2019 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
2020 if (width == BakerReadBarrierWidth::kWide) {
2021 DCHECK_GE(code.size() - literal_offset, 8u);
2022 uint32_t next_insn = GetInsn32(literal_offset + 4u);
2023 // LDR (immediate), encoding T3, with correct base_reg.
2024 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
2025 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2026 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
2027 } else {
2028 DCHECK_GE(code.size() - literal_offset, 6u);
2029 uint32_t next_insn = GetInsn16(literal_offset + 4u);
2030 // LDR (immediate), encoding T1, with correct base_reg.
2031 CheckValidReg(next_insn & 0x7u); // Check destination register.
2032 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2033 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
2034 }
2035 break;
2036 }
2037 case BakerReadBarrierKind::kArray: {
2038 DCHECK_GE(code.size() - literal_offset, 8u);
2039 uint32_t next_insn = GetInsn32(literal_offset + 4u);
2040 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
2041 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
2042 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2043 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
2044 CheckValidReg(next_insn & 0xf); // Check index register
2045 break;
2046 }
2047 case BakerReadBarrierKind::kGcRoot: {
2048 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
2049 if (width == BakerReadBarrierWidth::kWide) {
2050 DCHECK_GE(literal_offset, 4u);
2051 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Markof28be432018-08-14 12:20:51 +00002052 // LDR (immediate), encoding T3, with correct root_reg.
Vladimir Markoca1e0382018-04-11 09:58:41 +00002053 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markof28be432018-08-14 12:20:51 +00002054 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
Vladimir Markoca1e0382018-04-11 09:58:41 +00002055 } else {
2056 DCHECK_GE(literal_offset, 2u);
2057 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00002058 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko3d350a82020-11-18 14:14:27 +00002059 // Usually LDR (immediate), encoding T1, with correct root_reg but we may have
2060 // a `MOV marked, old_value` for intrinsic CAS where `marked` is a low register.
2061 if ((prev_insn & 0xff87u) != (0x4600 | root_reg)) {
2062 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2063 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002064 }
2065 break;
2066 }
Vladimir Marko3d350a82020-11-18 14:14:27 +00002067 case BakerReadBarrierKind::kIntrinsicCas: {
Vladimir Markod887ed82018-08-14 13:52:12 +00002068 DCHECK_GE(literal_offset, 4u);
2069 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Marko3d350a82020-11-18 14:14:27 +00002070 // MOV (register), encoding T3, with correct root_reg.
Vladimir Markod887ed82018-08-14 13:52:12 +00002071 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko3d350a82020-11-18 14:14:27 +00002072 DCHECK_GE(root_reg, 8u); // Used only for high registers.
2073 CHECK_EQ(prev_insn & 0xfffffff0u, 0xea4f0000u | (root_reg << 8));
Vladimir Markod887ed82018-08-14 13:52:12 +00002074 break;
2075 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002076 default:
2077 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2078 UNREACHABLE();
2079 }
2080 }
2081 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002082}
2083
2084void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002085 // Stack register, LR and PC are always reserved.
2086 blocked_core_registers_[SP] = true;
2087 blocked_core_registers_[LR] = true;
2088 blocked_core_registers_[PC] = true;
2089
Roland Levillain6d729a72017-06-30 18:34:01 +01002090 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2091 // Reserve marking register.
2092 blocked_core_registers_[MR] = true;
2093 }
2094
Scott Wakelingfe885462016-09-22 10:24:38 +01002095 // Reserve thread register.
2096 blocked_core_registers_[TR] = true;
2097
2098 // Reserve temp register.
2099 blocked_core_registers_[IP] = true;
2100
2101 if (GetGraph()->IsDebuggable()) {
2102 // Stubs do not save callee-save floating point registers. If the graph
2103 // is debuggable, we need to deal with these registers differently. For
2104 // now, just block them.
2105 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2106 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2107 ++i) {
2108 blocked_fpu_registers_[i] = true;
2109 }
2110 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002111}
2112
Scott Wakelingfe885462016-09-22 10:24:38 +01002113InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2114 CodeGeneratorARMVIXL* codegen)
2115 : InstructionCodeGenerator(graph, codegen),
2116 assembler_(codegen->GetAssembler()),
2117 codegen_(codegen) {}
2118
2119void CodeGeneratorARMVIXL::ComputeSpillMask() {
2120 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Vladimir Marko460f0542019-07-04 14:02:08 +01002121 DCHECK_NE(core_spill_mask_ & (1u << kLrCode), 0u)
2122 << "At least the return address register must be saved";
2123 // 16-bit PUSH/POP (T1) can save/restore just the LR/PC.
2124 DCHECK(GetVIXLAssembler()->IsUsingT32());
Scott Wakelingfe885462016-09-22 10:24:38 +01002125 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2126 // We use vpush and vpop for saving and restoring floating point registers, which take
2127 // a SRegister and the number of registers to save/restore after that SRegister. We
2128 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2129 // but in the range.
2130 if (fpu_spill_mask_ != 0) {
2131 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2132 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2133 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2134 fpu_spill_mask_ |= (1 << i);
2135 }
2136 }
2137}
2138
Mythri Alle2d4feeb2021-10-13 15:39:37 +00002139void LocationsBuilderARMVIXL::VisitMethodExitHook(HMethodExitHook* method_hook) {
2140 LocationSummary* locations = new (GetGraph()->GetAllocator())
2141 LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
2142 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(method_hook->InputAt(0)->GetType()));
2143}
2144
2145void InstructionCodeGeneratorARMVIXL::GenerateMethodEntryExitHook(HInstruction* instruction) {
2146 UseScratchRegisterScope temps(GetVIXLAssembler());
2147 vixl32::Register temp = temps.Acquire();
2148
2149 SlowPathCodeARMVIXL* slow_path =
2150 new (codegen_->GetScopedAllocator()) MethodEntryExitHooksSlowPathARMVIXL(instruction);
2151 codegen_->AddSlowPath(slow_path);
2152
2153 int offset = instrumentation::Instrumentation::NeedsEntryExitHooksOffset().Int32Value();
2154 uint32_t address = reinterpret_cast32<uint32_t>(Runtime::Current()->GetInstrumentation());
2155 __ Mov(temp, address + offset);
2156 __ Ldrh(temp, MemOperand(temp, 0));
2157 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
2158 __ Bind(slow_path->GetExitLabel());
2159}
2160
2161void InstructionCodeGeneratorARMVIXL::VisitMethodExitHook(HMethodExitHook* instruction) {
2162 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
2163 DCHECK(codegen_->RequiresCurrentMethod());
2164 GenerateMethodEntryExitHook(instruction);
2165}
2166
2167void LocationsBuilderARMVIXL::VisitMethodEntryHook(HMethodEntryHook* method_hook) {
2168 new (GetGraph()->GetAllocator()) LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
2169}
2170
2171void InstructionCodeGeneratorARMVIXL::VisitMethodEntryHook(HMethodEntryHook* instruction) {
2172 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
2173 DCHECK(codegen_->RequiresCurrentMethod());
2174 GenerateMethodEntryExitHook(instruction);
2175}
2176
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002177void CodeGeneratorARMVIXL::MaybeIncrementHotness(bool is_frame_entry) {
2178 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2179 UseScratchRegisterScope temps(GetVIXLAssembler());
2180 vixl32::Register temp = temps.Acquire();
2181 static_assert(ArtMethod::MaxCounter() == 0xFFFF, "asm is probably wrong");
2182 if (!is_frame_entry) {
2183 __ Push(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002184 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002185 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2186 }
2187 // Load with zero extend to clear the high bits for integer overflow check.
2188 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
Vladimir Markoce131fe2021-10-26 20:03:35 +00002189 __ Add(temp, temp, 1);
2190 // Subtract one if the counter would overflow.
2191 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002192 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2193 if (!is_frame_entry) {
2194 __ Pop(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002195 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002196 }
2197 }
2198
2199 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002200 ScopedProfilingInfoUse spiu(
2201 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2202 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002203 if (info != nullptr) {
2204 uint32_t address = reinterpret_cast32<uint32_t>(info);
2205 vixl::aarch32::Label done;
2206 UseScratchRegisterScope temps(GetVIXLAssembler());
2207 temps.Exclude(ip);
2208 if (!is_frame_entry) {
2209 __ Push(r4); // Will be used as temporary. For frame entry, r4 is always available.
Vladimir Markodec78172020-06-19 15:31:23 +01002210 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002211 }
2212 __ Mov(r4, address);
2213 __ Ldrh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2214 __ Add(ip, ip, 1);
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01002215 instruction_visitor_.GenerateAndConst(ip, ip, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002216 __ Strh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2217 if (!is_frame_entry) {
2218 __ Pop(r4);
Vladimir Markodec78172020-06-19 15:31:23 +01002219 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002220 }
2221 __ Lsls(ip, ip, 16);
2222 __ B(ne, &done);
2223 uint32_t entry_point_offset =
2224 GetThreadOffset<kArmPointerSize>(kQuickCompileOptimized).Int32Value();
2225 if (HasEmptyFrame()) {
2226 CHECK(is_frame_entry);
2227 // For leaf methods, we need to spill lr and r0. Also spill r1 and r2 for
2228 // alignment.
2229 uint32_t core_spill_mask =
2230 (1 << lr.GetCode()) | (1 << r0.GetCode()) | (1 << r1.GetCode()) | (1 << r2.GetCode());
2231 __ Push(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002232 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002233 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2234 __ Blx(lr);
2235 __ Pop(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002236 GetAssembler()->cfi().AdjustCFAOffset(
2237 -static_cast<int>(kArmWordSize) * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002238 } else {
2239 if (!RequiresCurrentMethod()) {
2240 CHECK(is_frame_entry);
2241 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2242 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002243 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2244 __ Blx(lr);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002245 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002246 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002247 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002248 }
2249}
2250
Scott Wakelingfe885462016-09-22 10:24:38 +01002251void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2252 bool skip_overflow_check =
2253 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2254 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2255 __ Bind(&frame_entry_label_);
2256
2257 if (HasEmptyFrame()) {
David Srbecky30021842019-02-13 14:19:36 +00002258 // Ensure that the CFI opcode list is not empty.
2259 GetAssembler()->cfi().Nop();
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002260 MaybeIncrementHotness(/* is_frame_entry= */ true);
Scott Wakelingfe885462016-09-22 10:24:38 +01002261 return;
2262 }
2263
Scott Wakelingfe885462016-09-22 10:24:38 +01002264 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002265 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002266 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002267 vixl32::Register temp;
2268 // TODO: Remove this check when R4 is made a callee-save register
2269 // in ART compiled code (b/72801708). Currently we need to make
2270 // sure r4 is not blocked, e.g. in special purpose
2271 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2272 // here.
2273 if (!blocked_core_registers_[R4]) {
2274 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2275 DCHECK(!reg.Is(r4));
2276 }
2277 DCHECK(!kCoreCalleeSaves.Includes(r4));
2278 temp = r4;
2279 } else {
2280 temp = temps.Acquire();
2281 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002282 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002283 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002284 ExactAssemblyScope aas(GetVIXLAssembler(),
2285 vixl32::kMaxInstructionSizeInBytes,
2286 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002287 __ ldr(temp, MemOperand(temp));
2288 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002289 }
2290
Vladimir Marko1a225a72019-07-05 13:37:42 +01002291 uint32_t frame_size = GetFrameSize();
2292 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2293 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2294 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2295 core_spills_offset <= 3u * kArmWordSize) {
2296 // Do a single PUSH for core registers including the method and up to two
2297 // filler registers. Then store the single FP spill if any.
2298 // (The worst case is when the method is not required and we actually
2299 // store 3 extra registers but they are stored in the same properly
2300 // aligned 16-byte chunk where we're already writing anyway.)
2301 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2302 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize);
2303 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(core_spill_mask_));
2304 __ Push(RegisterList(core_spill_mask_ | extra_regs));
2305 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
2306 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2307 core_spills_offset,
2308 core_spill_mask_,
2309 kArmWordSize);
2310 if (fpu_spill_mask_ != 0u) {
2311 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2312 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2313 GetAssembler()->StoreSToOffset(sreg, sp, fp_spills_offset);
2314 GetAssembler()->cfi().RelOffset(DWARFReg(sreg), /*offset=*/ fp_spills_offset);
2315 }
2316 } else {
2317 __ Push(RegisterList(core_spill_mask_));
2318 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2319 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2320 /*offset=*/ 0,
2321 core_spill_mask_,
2322 kArmWordSize);
2323 if (fpu_spill_mask_ != 0) {
2324 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002325
Vladimir Marko1a225a72019-07-05 13:37:42 +01002326 // Check that list is contiguous.
2327 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002328
Vladimir Marko1a225a72019-07-05 13:37:42 +01002329 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2330 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
2331 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0),
2332 /*offset=*/ 0,
2333 fpu_spill_mask_,
2334 kArmWordSize);
2335 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002336
Vladimir Marko1a225a72019-07-05 13:37:42 +01002337 // Adjust SP and save the current method if we need it. Note that we do
2338 // not save the method in HCurrentMethod, as the instruction might have
2339 // been removed in the SSA graph.
2340 if (RequiresCurrentMethod() && fp_spills_offset <= 3 * kArmWordSize) {
2341 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2342 __ Push(RegisterList(MaxInt<uint32_t>(fp_spills_offset / kArmWordSize)));
2343 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2344 } else {
Vladimir Markodec78172020-06-19 15:31:23 +01002345 IncreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002346 if (RequiresCurrentMethod()) {
2347 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2348 }
2349 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002350 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002351
2352 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2353 UseScratchRegisterScope temps(GetVIXLAssembler());
2354 vixl32::Register temp = temps.Acquire();
2355 // Initialize should_deoptimize flag to 0.
2356 __ Mov(temp, 0);
2357 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2358 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002359
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002360 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08002361 MaybeGenerateMarkingRegisterCheck(/* code= */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002362}
2363
2364void CodeGeneratorARMVIXL::GenerateFrameExit() {
2365 if (HasEmptyFrame()) {
2366 __ Bx(lr);
2367 return;
2368 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002369
Scott Wakelingfe885462016-09-22 10:24:38 +01002370 // Pop LR into PC to return.
2371 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2372 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
Vladimir Marko1a225a72019-07-05 13:37:42 +01002373
2374 uint32_t frame_size = GetFrameSize();
2375 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2376 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2377 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2378 // r4 is blocked by TestCodeGeneratorARMVIXL used by some tests.
2379 core_spills_offset <= (blocked_core_registers_[r4.GetCode()] ? 2u : 3u) * kArmWordSize) {
2380 // Load the FP spill if any and then do a single POP including the method
2381 // and up to two filler registers. If we have no FP spills, this also has
2382 // the advantage that we do not need to emit CFI directives.
2383 if (fpu_spill_mask_ != 0u) {
2384 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2385 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2386 GetAssembler()->cfi().RememberState();
2387 GetAssembler()->LoadSFromOffset(sreg, sp, fp_spills_offset);
2388 GetAssembler()->cfi().Restore(DWARFReg(sreg));
2389 }
2390 // Clobber registers r2-r4 as they are caller-save in ART managed ABI and
2391 // never hold the return value.
2392 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize) << r2.GetCode();
2393 DCHECK_EQ(extra_regs & kCoreCalleeSaves.GetList(), 0u);
2394 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(pop_mask));
2395 __ Pop(RegisterList(pop_mask | extra_regs));
2396 if (fpu_spill_mask_ != 0u) {
2397 GetAssembler()->cfi().RestoreState();
2398 }
2399 } else {
2400 GetAssembler()->cfi().RememberState();
Vladimir Markodec78172020-06-19 15:31:23 +01002401 DecreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002402 if (fpu_spill_mask_ != 0) {
2403 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2404
2405 // Check that list is contiguous.
2406 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2407
2408 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2409 GetAssembler()->cfi().AdjustCFAOffset(
2410 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
2411 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
2412 }
2413 __ Pop(RegisterList(pop_mask));
2414 GetAssembler()->cfi().RestoreState();
2415 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2416 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002417}
2418
2419void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2420 __ Bind(GetLabelOf(block));
2421}
2422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002424 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002425 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002427 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002428 case DataType::Type::kInt8:
2429 case DataType::Type::kUint16:
2430 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002431 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002432 uint32_t index = gp_index_++;
2433 uint32_t stack_index = stack_index_++;
2434 if (index < calling_convention.GetNumberOfRegisters()) {
2435 return LocationFrom(calling_convention.GetRegisterAt(index));
2436 } else {
2437 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2438 }
2439 }
2440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002442 uint32_t index = gp_index_;
2443 uint32_t stack_index = stack_index_;
2444 gp_index_ += 2;
2445 stack_index_ += 2;
2446 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2447 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2448 // Skip R1, and use R2_R3 instead.
2449 gp_index_++;
2450 index++;
2451 }
2452 }
2453 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2454 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2455 calling_convention.GetRegisterAt(index + 1).GetCode());
2456
2457 return LocationFrom(calling_convention.GetRegisterAt(index),
2458 calling_convention.GetRegisterAt(index + 1));
2459 } else {
2460 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2461 }
2462 }
2463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002464 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002465 uint32_t stack_index = stack_index_++;
2466 if (float_index_ % 2 == 0) {
2467 float_index_ = std::max(double_index_, float_index_);
2468 }
2469 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2470 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2471 } else {
2472 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2473 }
2474 }
2475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002477 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2478 uint32_t stack_index = stack_index_;
2479 stack_index_ += 2;
2480 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2481 uint32_t index = double_index_;
2482 double_index_ += 2;
2483 Location result = LocationFrom(
2484 calling_convention.GetFpuRegisterAt(index),
2485 calling_convention.GetFpuRegisterAt(index + 1));
2486 DCHECK(ExpectedPairLayout(result));
2487 return result;
2488 } else {
2489 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2490 }
2491 }
2492
Aart Bik66c158e2018-01-31 12:55:04 -08002493 case DataType::Type::kUint32:
2494 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002496 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002497 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002498 }
2499 return Location::NoLocation();
2500}
2501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002502Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002503 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002504 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002506 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kInt8:
2508 case DataType::Type::kUint16:
2509 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002510 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002511 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002512 return LocationFrom(r0);
2513 }
2514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002516 return LocationFrom(s0);
2517 }
2518
Aart Bik66c158e2018-01-31 12:55:04 -08002519 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002521 return LocationFrom(r0, r1);
2522 }
2523
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002524 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002525 return LocationFrom(s0, s1);
2526 }
2527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002529 return Location::NoLocation();
2530 }
2531
2532 UNREACHABLE();
2533}
2534
2535Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2536 return LocationFrom(kMethodRegister);
2537}
2538
Vladimir Marko86c87522020-05-11 16:55:55 +01002539Location CriticalNativeCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
2540 DCHECK_NE(type, DataType::Type::kReference);
2541
2542 // Native ABI uses the same registers as managed, except that the method register r0
2543 // is a normal argument.
2544 Location location = Location::NoLocation();
2545 if (DataType::Is64BitType(type)) {
2546 gpr_index_ = RoundUp(gpr_index_, 2u);
2547 stack_offset_ = RoundUp(stack_offset_, 2 * kFramePointerSize);
2548 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2549 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u],
2550 kParameterCoreRegistersVIXL[gpr_index_]);
2551 gpr_index_ += 2u;
2552 }
2553 } else {
2554 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2555 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u]);
2556 ++gpr_index_;
2557 }
2558 }
2559 if (location.IsInvalid()) {
2560 if (DataType::Is64BitType(type)) {
2561 location = Location::DoubleStackSlot(stack_offset_);
2562 stack_offset_ += 2 * kFramePointerSize;
2563 } else {
2564 location = Location::StackSlot(stack_offset_);
2565 stack_offset_ += kFramePointerSize;
2566 }
2567
2568 if (for_register_allocation_) {
2569 location = Location::Any();
2570 }
2571 }
2572 return location;
2573}
2574
2575Location CriticalNativeCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type)
2576 const {
2577 // We perform conversion to the managed ABI return register after the call if needed.
2578 InvokeDexCallingConventionVisitorARMVIXL dex_calling_convention;
2579 return dex_calling_convention.GetReturnLocation(type);
2580}
2581
2582Location CriticalNativeCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2583 // Pass the method in the hidden argument R4.
2584 return Location::RegisterLocation(R4);
2585}
2586
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002587void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2588 if (source.Equals(destination)) {
2589 return;
2590 }
2591 if (destination.IsRegister()) {
2592 if (source.IsRegister()) {
2593 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2594 } else if (source.IsFpuRegister()) {
2595 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2596 } else {
2597 GetAssembler()->LoadFromOffset(kLoadWord,
2598 RegisterFrom(destination),
2599 sp,
2600 source.GetStackIndex());
2601 }
2602 } else if (destination.IsFpuRegister()) {
2603 if (source.IsRegister()) {
2604 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2605 } else if (source.IsFpuRegister()) {
2606 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2607 } else {
2608 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2609 }
2610 } else {
2611 DCHECK(destination.IsStackSlot()) << destination;
2612 if (source.IsRegister()) {
2613 GetAssembler()->StoreToOffset(kStoreWord,
2614 RegisterFrom(source),
2615 sp,
2616 destination.GetStackIndex());
2617 } else if (source.IsFpuRegister()) {
2618 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2619 } else {
2620 DCHECK(source.IsStackSlot()) << source;
2621 UseScratchRegisterScope temps(GetVIXLAssembler());
2622 vixl32::Register temp = temps.Acquire();
2623 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2624 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2625 }
2626 }
2627}
2628
Artem Serovcfbe9132016-10-14 15:58:56 +01002629void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2630 DCHECK(location.IsRegister());
2631 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002632}
2633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002635 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2636 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002637 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002638 move.AddMove(src, dst, dst_type, nullptr);
2639 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002640}
2641
Artem Serovcfbe9132016-10-14 15:58:56 +01002642void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2643 if (location.IsRegister()) {
2644 locations->AddTemp(location);
2645 } else if (location.IsRegisterPair()) {
2646 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2647 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2648 } else {
2649 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2650 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002651}
2652
2653void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2654 HInstruction* instruction,
2655 uint32_t dex_pc,
2656 SlowPathCode* slow_path) {
2657 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Vladimir Markof6675082019-05-17 12:05:28 +01002658
2659 ThreadOffset32 entrypoint_offset = GetThreadOffset<kArmPointerSize>(entrypoint);
2660 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
2661 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
2662 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01002663 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01002664 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
2665 // Ensure the pc position is recorded immediately after the `blx` instruction.
2666 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
2667 ExactAssemblyScope aas(GetVIXLAssembler(),
2668 vixl32::k16BitT32InstructionSizeInBytes,
2669 CodeBufferCheckScope::kExactSize);
2670 __ blx(lr);
2671 if (EntrypointRequiresStackMap(entrypoint)) {
2672 RecordPcInfo(instruction, dex_pc, slow_path);
2673 }
2674 } else {
2675 // Ensure the pc position is recorded immediately after the `bl` instruction.
2676 ExactAssemblyScope aas(GetVIXLAssembler(),
2677 vixl32::k32BitT32InstructionSizeInBytes,
2678 CodeBufferCheckScope::kExactSize);
2679 EmitEntrypointThunkCall(entrypoint_offset);
2680 if (EntrypointRequiresStackMap(entrypoint)) {
2681 RecordPcInfo(instruction, dex_pc, slow_path);
2682 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002683 }
2684}
2685
2686void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2687 HInstruction* instruction,
2688 SlowPathCode* slow_path) {
2689 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002690 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002691 __ Blx(lr);
2692}
2693
Scott Wakelingfe885462016-09-22 10:24:38 +01002694void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002695 if (successor->IsExitBlock()) {
2696 DCHECK(got->GetPrevious()->AlwaysThrows());
2697 return; // no code needed
2698 }
2699
Scott Wakelingfe885462016-09-22 10:24:38 +01002700 HBasicBlock* block = got->GetBlock();
2701 HInstruction* previous = got->GetPrevious();
2702 HLoopInformation* info = block->GetLoopInformation();
2703
2704 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002705 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Scott Wakelingfe885462016-09-22 10:24:38 +01002706 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2707 return;
2708 }
2709 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2710 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08002711 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002712 }
2713 if (!codegen_->GoesToNextBlock(block, successor)) {
2714 __ B(codegen_->GetLabelOf(successor));
2715 }
2716}
2717
2718void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2719 got->SetLocations(nullptr);
2720}
2721
2722void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2723 HandleGoto(got, got->GetSuccessor());
2724}
2725
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002726void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2727 try_boundary->SetLocations(nullptr);
2728}
2729
2730void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2731 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2732 if (!successor->IsExitBlock()) {
2733 HandleGoto(try_boundary, successor);
2734 }
2735}
2736
Scott Wakelingfe885462016-09-22 10:24:38 +01002737void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2738 exit->SetLocations(nullptr);
2739}
2740
2741void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2742}
2743
Scott Wakelingfe885462016-09-22 10:24:38 +01002744void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002745 vixl32::Label* true_target,
2746 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002747 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002748 if (true_target == false_target) {
2749 DCHECK(true_target != nullptr);
2750 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002751 return;
2752 }
2753
Anton Kirilov23b752b2017-07-20 14:40:44 +01002754 vixl32::Label* non_fallthrough_target;
2755 bool invert;
2756 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002757
Anton Kirilov23b752b2017-07-20 14:40:44 +01002758 if (true_target == nullptr) {
2759 // The true target is fallthrough.
2760 DCHECK(false_target != nullptr);
2761 non_fallthrough_target = false_target;
2762 invert = true;
2763 emit_both_branches = false;
2764 } else {
2765 non_fallthrough_target = true_target;
2766 invert = false;
2767 // Either the false target is fallthrough, or there is no fallthrough
2768 // and both branches must be emitted.
2769 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002770 }
2771
Anton Kirilov23b752b2017-07-20 14:40:44 +01002772 const auto cond = GenerateTest(condition, invert, codegen_);
2773
2774 __ B(cond.first, non_fallthrough_target, is_far_target);
2775
2776 if (emit_both_branches) {
2777 // No target falls through, we need to branch.
2778 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002779 }
2780}
2781
2782void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2783 size_t condition_input_index,
2784 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002785 vixl32::Label* false_target,
2786 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002787 HInstruction* cond = instruction->InputAt(condition_input_index);
2788
2789 if (true_target == nullptr && false_target == nullptr) {
2790 // Nothing to do. The code always falls through.
2791 return;
2792 } else if (cond->IsIntConstant()) {
2793 // Constant condition, statically compared against "true" (integer value 1).
2794 if (cond->AsIntConstant()->IsTrue()) {
2795 if (true_target != nullptr) {
2796 __ B(true_target);
2797 }
2798 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002799 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002800 if (false_target != nullptr) {
2801 __ B(false_target);
2802 }
2803 }
2804 return;
2805 }
2806
2807 // The following code generates these patterns:
2808 // (1) true_target == nullptr && false_target != nullptr
2809 // - opposite condition true => branch to false_target
2810 // (2) true_target != nullptr && false_target == nullptr
2811 // - condition true => branch to true_target
2812 // (3) true_target != nullptr && false_target != nullptr
2813 // - condition true => branch to true_target
2814 // - branch to false_target
2815 if (IsBooleanValueOrMaterializedCondition(cond)) {
2816 // Condition has been materialized, compare the output to 0.
2817 if (kIsDebugBuild) {
2818 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2819 DCHECK(cond_val.IsRegister());
2820 }
2821 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002822 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2823 false_target,
2824 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002825 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002826 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2827 true_target,
2828 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002829 }
2830 } else {
2831 // Condition has not been materialized. Use its inputs as the comparison and
2832 // its condition as the branch condition.
2833 HCondition* condition = cond->AsCondition();
2834
2835 // If this is a long or FP comparison that has been folded into
2836 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 DataType::Type type = condition->InputAt(0)->GetType();
2838 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002839 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002840 return;
2841 }
2842
Donghui Bai426b49c2016-11-08 14:55:38 +08002843 vixl32::Label* non_fallthrough_target;
2844 vixl32::Condition arm_cond = vixl32::Condition::None();
2845 const vixl32::Register left = InputRegisterAt(cond, 0);
2846 const Operand right = InputOperandAt(cond, 1);
2847
Scott Wakelingfe885462016-09-22 10:24:38 +01002848 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002849 arm_cond = ARMCondition(condition->GetOppositeCondition());
2850 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002851 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002852 arm_cond = ARMCondition(condition->GetCondition());
2853 non_fallthrough_target = true_target;
2854 }
2855
2856 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2857 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002858 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002859 } else {
2860 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002861 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002862 }
2863 } else {
2864 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002865 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002866 }
2867 }
2868
2869 // If neither branch falls through (case 3), the conditional branch to `true_target`
2870 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2871 if (true_target != nullptr && false_target != nullptr) {
2872 __ B(false_target);
2873 }
2874}
2875
2876void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002877 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002878 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2879 locations->SetInAt(0, Location::RequiresRegister());
2880 }
2881}
2882
2883void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2884 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2885 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002886 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2887 nullptr : codegen_->GetLabelOf(true_successor);
2888 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2889 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002890 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002891}
2892
Scott Wakelingc34dba72016-10-03 10:14:44 +01002893void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002894 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002895 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002896 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2897 RegisterSet caller_saves = RegisterSet::Empty();
2898 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2899 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002900 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2901 locations->SetInAt(0, Location::RequiresRegister());
2902 }
2903}
2904
2905void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2906 SlowPathCodeARMVIXL* slow_path =
2907 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2908 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002909 /* condition_input_index= */ 0,
Scott Wakelingc34dba72016-10-03 10:14:44 +01002910 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002911 /* false_target= */ nullptr);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002912}
2913
Artem Serovd4cc5b22016-11-04 11:19:09 +00002914void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002915 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002916 LocationSummary(flag, LocationSummary::kNoCall);
2917 locations->SetOut(Location::RequiresRegister());
2918}
2919
2920void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2921 GetAssembler()->LoadFromOffset(kLoadWord,
2922 OutputRegister(flag),
2923 sp,
2924 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2925}
2926
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002927void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002928 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002930
2931 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002932 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002933 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002934 } else {
2935 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002936 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002937 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002938
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002939 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002940 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2941 // The code generator handles overlap with the values, but not with the condition.
2942 locations->SetOut(Location::SameAsFirstInput());
2943 } else if (is_floating_point) {
2944 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2945 } else {
2946 if (!locations->InAt(1).IsConstant()) {
2947 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2948 }
2949
2950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002951 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002952}
2953
2954void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002955 HInstruction* const condition = select->GetCondition();
2956 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002958 const Location first = locations->InAt(0);
2959 const Location out = locations->Out();
2960 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002961
2962 // In the unlucky case the output of this instruction overlaps
2963 // with an input of an "emitted-at-use-site" condition, and
2964 // the output of this instruction is not one of its inputs, we'll
2965 // need to fallback to branches instead of conditional ARM instructions.
2966 bool output_overlaps_with_condition_inputs =
2967 !IsBooleanValueOrMaterializedCondition(condition) &&
2968 !out.Equals(first) &&
2969 !out.Equals(second) &&
2970 (condition->GetLocations()->InAt(0).Equals(out) ||
2971 condition->GetLocations()->InAt(1).Equals(out));
2972 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002973 Location src;
2974
2975 if (condition->IsIntConstant()) {
2976 if (condition->AsIntConstant()->IsFalse()) {
2977 src = first;
2978 } else {
2979 src = second;
2980 }
2981
2982 codegen_->MoveLocation(out, src, type);
2983 return;
2984 }
2985
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002986 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002987 bool invert = false;
2988
2989 if (out.Equals(second)) {
2990 src = first;
2991 invert = true;
2992 } else if (out.Equals(first)) {
2993 src = second;
2994 } else if (second.IsConstant()) {
2995 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2996 src = second;
2997 } else if (first.IsConstant()) {
2998 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2999 src = first;
3000 invert = true;
3001 } else {
3002 src = second;
3003 }
3004
3005 if (CanGenerateConditionalMove(out, src)) {
3006 if (!out.Equals(first) && !out.Equals(second)) {
3007 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3008 }
3009
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003010 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
3011
3012 if (IsBooleanValueOrMaterializedCondition(condition)) {
3013 __ Cmp(InputRegisterAt(select, 2), 0);
3014 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
3015 } else {
3016 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3017 }
3018
Donghui Bai426b49c2016-11-08 14:55:38 +08003019 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003020 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08003021 ExactAssemblyScope guard(GetVIXLAssembler(),
3022 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
3023 CodeBufferCheckScope::kExactSize);
3024
3025 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003026 __ it(cond.first);
3027 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08003028 } else {
3029 DCHECK(out.IsRegisterPair());
3030
3031 Operand operand_high(0);
3032 Operand operand_low(0);
3033
3034 if (src.IsConstant()) {
3035 const int64_t value = Int64ConstantFrom(src);
3036
3037 operand_high = High32Bits(value);
3038 operand_low = Low32Bits(value);
3039 } else {
3040 DCHECK(src.IsRegisterPair());
3041 operand_high = HighRegisterFrom(src);
3042 operand_low = LowRegisterFrom(src);
3043 }
3044
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003045 __ it(cond.first);
3046 __ mov(cond.first, LowRegisterFrom(out), operand_low);
3047 __ it(cond.first);
3048 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08003049 }
3050
3051 return;
3052 }
3053 }
3054
3055 vixl32::Label* false_target = nullptr;
3056 vixl32::Label* true_target = nullptr;
3057 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003058 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08003059 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
3060
3061 if (out.Equals(second)) {
3062 true_target = target;
3063 src = first;
3064 } else {
3065 false_target = target;
3066 src = second;
3067
3068 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003069 if (output_overlaps_with_condition_inputs) {
3070 false_target = &other_case;
3071 } else {
3072 codegen_->MoveLocation(out, first, type);
3073 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003074 }
3075 }
3076
Andreas Gampe3db70682018-12-26 15:12:03 -08003077 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target= */ false);
Donghui Bai426b49c2016-11-08 14:55:38 +08003078 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003079 if (output_overlaps_with_condition_inputs) {
3080 __ B(target);
3081 __ Bind(&other_case);
3082 codegen_->MoveLocation(out, first, type);
3083 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003084
3085 if (select_end.IsReferenced()) {
3086 __ Bind(&select_end);
3087 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003088}
3089
Artem Serov551b28f2016-10-18 19:11:30 +01003090void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003091 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003092}
3093
3094void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3095 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3096}
3097
Vladimir Markodec78172020-06-19 15:31:23 +01003098void CodeGeneratorARMVIXL::IncreaseFrame(size_t adjustment) {
3099 __ Claim(adjustment);
3100 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3101}
3102
3103void CodeGeneratorARMVIXL::DecreaseFrame(size_t adjustment) {
3104 __ Drop(adjustment);
3105 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3106}
3107
Scott Wakelingfe885462016-09-22 10:24:38 +01003108void CodeGeneratorARMVIXL::GenerateNop() {
3109 __ Nop();
3110}
3111
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003112// `temp` is an extra temporary register that is used for some conditions;
3113// callers may not specify it, in which case the method will use a scratch
3114// register instead.
3115void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3116 vixl32::Register out,
3117 vixl32::Register in,
3118 vixl32::Register temp) {
3119 switch (condition) {
3120 case kCondEQ:
3121 // x <= 0 iff x == 0 when the comparison is unsigned.
3122 case kCondBE:
3123 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3124 temp = out;
3125 }
3126
3127 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3128 // different as well.
3129 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3130 // temp = - in; only 0 sets the carry flag.
3131 __ Rsbs(temp, in, 0);
3132
3133 if (out.Is(in)) {
3134 std::swap(in, temp);
3135 }
3136
3137 // out = - in + in + carry = carry
3138 __ Adc(out, temp, in);
3139 } else {
3140 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3141 __ Clz(out, in);
3142 // Any number less than 32 logically shifted right by 5 bits results in 0;
3143 // the same operation on 32 yields 1.
3144 __ Lsr(out, out, 5);
3145 }
3146
3147 break;
3148 case kCondNE:
3149 // x > 0 iff x != 0 when the comparison is unsigned.
3150 case kCondA: {
3151 UseScratchRegisterScope temps(GetVIXLAssembler());
3152
3153 if (out.Is(in)) {
3154 if (!temp.IsValid() || in.Is(temp)) {
3155 temp = temps.Acquire();
3156 }
3157 } else if (!temp.IsValid() || !temp.IsLow()) {
3158 temp = out;
3159 }
3160
3161 // temp = in - 1; only 0 does not set the carry flag.
3162 __ Subs(temp, in, 1);
3163 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3164 __ Sbc(out, in, temp);
3165 break;
3166 }
3167 case kCondGE:
3168 __ Mvn(out, in);
3169 in = out;
3170 FALLTHROUGH_INTENDED;
3171 case kCondLT:
3172 // We only care about the sign bit.
3173 __ Lsr(out, in, 31);
3174 break;
3175 case kCondAE:
3176 // Trivially true.
3177 __ Mov(out, 1);
3178 break;
3179 case kCondB:
3180 // Trivially false.
3181 __ Mov(out, 0);
3182 break;
3183 default:
3184 LOG(FATAL) << "Unexpected condition " << condition;
3185 UNREACHABLE();
3186 }
3187}
3188
Scott Wakelingfe885462016-09-22 10:24:38 +01003189void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3190 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003191 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003192 const DataType::Type type = cond->InputAt(0)->GetType();
3193 if (DataType::IsFloatingPointType(type)) {
3194 locations->SetInAt(0, Location::RequiresFpuRegister());
3195 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
3196 } else {
3197 locations->SetInAt(0, Location::RequiresRegister());
3198 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3199 }
3200 if (!cond->IsEmittedAtUseSite()) {
3201 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003202 }
3203}
3204
3205void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3206 if (cond->IsEmittedAtUseSite()) {
3207 return;
3208 }
3209
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003212 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003213 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003214 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003215 }
3216
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003218
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003219 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003220
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003221 // A condition with only one boolean input, or two boolean inputs without being equality or
3222 // inequality results from transformations done by the instruction simplifier, and is handled
3223 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003224 if (type == DataType::Type::kBool &&
3225 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003226 (condition == kCondEQ || condition == kCondNE)) {
3227 vixl32::Register left = InputRegisterAt(cond, 0);
3228 const vixl32::Register out = OutputRegister(cond);
3229 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003230
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003231 // The constant case is handled by the instruction simplifier.
3232 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003233
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003234 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003235
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003236 // Avoid 32-bit instructions if possible.
3237 if (out.Is(right)) {
3238 std::swap(left, right);
3239 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003240
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003241 __ Eor(out, left, right);
3242
3243 if (condition == kCondEQ) {
3244 __ Eor(out, out, 1);
3245 }
3246
3247 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003248 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003249
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003250 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003251}
3252
3253void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3254 HandleCondition(comp);
3255}
3256
3257void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3258 HandleCondition(comp);
3259}
3260
3261void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3262 HandleCondition(comp);
3263}
3264
3265void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3266 HandleCondition(comp);
3267}
3268
3269void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3270 HandleCondition(comp);
3271}
3272
3273void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3274 HandleCondition(comp);
3275}
3276
3277void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3278 HandleCondition(comp);
3279}
3280
3281void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3282 HandleCondition(comp);
3283}
3284
3285void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3286 HandleCondition(comp);
3287}
3288
3289void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3290 HandleCondition(comp);
3291}
3292
3293void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3294 HandleCondition(comp);
3295}
3296
3297void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3298 HandleCondition(comp);
3299}
3300
3301void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3302 HandleCondition(comp);
3303}
3304
3305void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3306 HandleCondition(comp);
3307}
3308
3309void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3310 HandleCondition(comp);
3311}
3312
3313void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3314 HandleCondition(comp);
3315}
3316
3317void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3318 HandleCondition(comp);
3319}
3320
3321void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3322 HandleCondition(comp);
3323}
3324
3325void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3326 HandleCondition(comp);
3327}
3328
3329void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3330 HandleCondition(comp);
3331}
3332
3333void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3334 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003335 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003336 locations->SetOut(Location::ConstantLocation(constant));
3337}
3338
3339void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3340 // Will be generated at use site.
3341}
3342
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003343void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3344 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003345 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003346 locations->SetOut(Location::ConstantLocation(constant));
3347}
3348
3349void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3350 // Will be generated at use site.
3351}
3352
Scott Wakelingfe885462016-09-22 10:24:38 +01003353void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3354 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003355 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003356 locations->SetOut(Location::ConstantLocation(constant));
3357}
3358
3359void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3360 // Will be generated at use site.
3361}
3362
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003363void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3364 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003365 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003366 locations->SetOut(Location::ConstantLocation(constant));
3367}
3368
Scott Wakelingc34dba72016-10-03 10:14:44 +01003369void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3370 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003371 // Will be generated at use site.
3372}
3373
3374void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3375 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003376 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003377 locations->SetOut(Location::ConstantLocation(constant));
3378}
3379
Scott Wakelingc34dba72016-10-03 10:14:44 +01003380void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3381 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003382 // Will be generated at use site.
3383}
3384
Igor Murashkind01745e2017-04-05 16:40:31 -07003385void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3386 constructor_fence->SetLocations(nullptr);
3387}
3388
3389void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3390 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3391 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3392}
3393
Scott Wakelingfe885462016-09-22 10:24:38 +01003394void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3395 memory_barrier->SetLocations(nullptr);
3396}
3397
3398void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3399 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3400}
3401
3402void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3403 ret->SetLocations(nullptr);
3404}
3405
3406void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3407 codegen_->GenerateFrameExit();
3408}
3409
3410void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3411 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003412 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003413 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3414}
3415
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00003416void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret) {
3417 if (GetGraph()->IsCompilingOsr()) {
3418 // To simplify callers of an OSR method, we put the return value in both
3419 // floating point and core registers.
3420 switch (ret->InputAt(0)->GetType()) {
3421 case DataType::Type::kFloat32:
3422 __ Vmov(r0, s0);
3423 break;
3424 case DataType::Type::kFloat64:
3425 __ Vmov(r0, r1, d0);
3426 break;
3427 default:
3428 break;
3429 }
3430 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003431 codegen_->GenerateFrameExit();
3432}
3433
Artem Serovcfbe9132016-10-14 15:58:56 +01003434void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3435 // The trampoline uses the same calling convention as dex calling conventions,
3436 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3437 // the method_idx.
3438 HandleInvoke(invoke);
3439}
3440
3441void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3442 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003443 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003444}
3445
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003446void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3447 // Explicit clinit checks triggered by static invokes must have been pruned by
3448 // art::PrepareForRegisterAllocation.
3449 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3450
Anton Kirilov5ec62182016-10-13 20:16:02 +01003451 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3452 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003453 return;
3454 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003455
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01003456 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01003457 CriticalNativeCallingConventionVisitorARMVIXL calling_convention_visitor(
3458 /*for_register_allocation=*/ true);
3459 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3460 } else {
3461 HandleInvoke(invoke);
3462 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003463}
3464
Anton Kirilov5ec62182016-10-13 20:16:02 +01003465static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3466 if (invoke->GetLocations()->Intrinsified()) {
3467 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3468 intrinsic.Dispatch(invoke);
3469 return true;
3470 }
3471 return false;
3472}
3473
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003474void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3475 // Explicit clinit checks triggered by static invokes must have been pruned by
3476 // art::PrepareForRegisterAllocation.
3477 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3478
Anton Kirilov5ec62182016-10-13 20:16:02 +01003479 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003480 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003481 return;
3482 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003483
3484 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003485 codegen_->GenerateStaticOrDirectCall(
3486 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003487
Andreas Gampe3db70682018-12-26 15:12:03 -08003488 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003489}
3490
3491void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003492 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003493 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3494}
3495
3496void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003497 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3498 if (intrinsic.TryDispatch(invoke)) {
3499 return;
3500 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003501
3502 HandleInvoke(invoke);
3503}
3504
3505void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003506 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003507 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003508 return;
3509 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003510
3511 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003512 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003513
Andreas Gampe3db70682018-12-26 15:12:03 -08003514 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003515}
3516
Artem Serovcfbe9132016-10-14 15:58:56 +01003517void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3518 HandleInvoke(invoke);
3519 // Add the hidden argument.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003520 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
3521 // We cannot request r12 as it's blocked by the register allocator.
3522 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
3523 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003524}
3525
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003526void CodeGeneratorARMVIXL::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
3527 vixl32::Register klass) {
3528 DCHECK_EQ(r0.GetCode(), klass.GetCode());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003529 // We know the destination of an intrinsic, so no need to record inline
3530 // caches.
3531 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00003532 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003533 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003534 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01003535 ScopedProfilingInfoUse spiu(
3536 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
3537 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00003538 if (info != nullptr) {
3539 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
3540 uint32_t address = reinterpret_cast32<uint32_t>(cache);
3541 vixl32::Label done;
3542 UseScratchRegisterScope temps(GetVIXLAssembler());
3543 temps.Exclude(ip);
3544 __ Mov(r4, address);
3545 __ Ldr(ip, MemOperand(r4, InlineCache::ClassesOffset().Int32Value()));
3546 // Fast path for a monomorphic cache.
3547 __ Cmp(klass, ip);
3548 __ B(eq, &done, /* is_far_target= */ false);
3549 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
3550 __ Bind(&done);
3551 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003552 }
3553}
3554
Artem Serovcfbe9132016-10-14 15:58:56 +01003555void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3556 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3557 LocationSummary* locations = invoke->GetLocations();
3558 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Artem Serovcfbe9132016-10-14 15:58:56 +01003559 Location receiver = locations->InAt(0);
3560 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3561
3562 DCHECK(!receiver.IsStackSlot());
3563
Alexandre Rames374ddf32016-11-04 10:40:49 +00003564 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3565 {
Artem Serov0fb37192016-12-06 18:13:40 +00003566 ExactAssemblyScope aas(GetVIXLAssembler(),
3567 vixl32::kMaxInstructionSizeInBytes,
3568 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003569 // /* HeapReference<Class> */ temp = receiver->klass_
3570 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3571 codegen_->MaybeRecordImplicitNullCheck(invoke);
3572 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003573 // Instead of simply (possibly) unpoisoning `temp` here, we should
3574 // emit a read barrier for the previous class reference load.
3575 // However this is not required in practice, as this is an
3576 // intermediate/temporary reference and because the current
3577 // concurrent copying collector keeps the from-space memory
3578 // intact/accessible until the end of the marking phase (the
3579 // concurrent copying collector may not in the future).
3580 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003581
3582 // If we're compiling baseline, update the inline cache.
3583 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
3584
Artem Serovcfbe9132016-10-14 15:58:56 +01003585 GetAssembler()->LoadFromOffset(kLoadWord,
3586 temp,
3587 temp,
3588 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003589
Artem Serovcfbe9132016-10-14 15:58:56 +01003590 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3591 invoke->GetImtIndex(), kArmPointerSize));
3592 // temp = temp->GetImtEntryAt(method_offset);
3593 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3594 uint32_t entry_point =
3595 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3596 // LR = temp->GetEntryPoint();
3597 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3598
Scott Wakelingb77051e2016-11-21 19:46:00 +00003599 {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003600 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3601 // instruction from clobbering it as they might use r12 as a scratch register.
3602 Location hidden_reg = Location::RegisterLocation(r12.GetCode());
Scott Wakelingb77051e2016-11-21 19:46:00 +00003603 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3604 // so it checks if the application is using them (by passing them to the macro assembler
3605 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3606 // what is available, and is the opposite of the standard usage: Instead of requesting a
3607 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3608 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3609 // (to materialize the constant), since the destination register becomes available for such use
3610 // internally for the duration of the macro instruction.
3611 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003612 temps.Exclude(RegisterFrom(hidden_reg));
3613 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
3614 Location current_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
3615 if (current_method.IsStackSlot()) {
3616 GetAssembler()->LoadFromOffset(
3617 kLoadWord, RegisterFrom(hidden_reg), sp, current_method.GetStackIndex());
3618 } else {
3619 __ Mov(RegisterFrom(hidden_reg), RegisterFrom(current_method));
3620 }
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00003621 } else if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
3622 // We pass the method from the IMT in case of a conflict. This will ensure
3623 // we go into the runtime to resolve the actual method.
3624 CHECK_NE(temp.GetCode(), lr.GetCode());
3625 __ Mov(RegisterFrom(hidden_reg), temp);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003626 } else {
3627 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), hidden_reg, invoke);
3628 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00003629 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003630 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003631 // Ensure the pc position is recorded immediately after the `blx` instruction.
3632 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
Artem Serov0fb37192016-12-06 18:13:40 +00003633 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003634 vixl32::k16BitT32InstructionSizeInBytes,
3635 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003636 // LR();
3637 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003638 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003639 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003640 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003641
Andreas Gampe3db70682018-12-26 15:12:03 -08003642 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003643}
3644
Orion Hodsonac141392017-01-13 11:53:47 +00003645void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003646 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3647 if (intrinsic.TryDispatch(invoke)) {
3648 return;
3649 }
Orion Hodsonac141392017-01-13 11:53:47 +00003650 HandleInvoke(invoke);
3651}
3652
3653void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003654 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3655 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 9);
3656 return;
3657 }
Orion Hodsonac141392017-01-13 11:53:47 +00003658 codegen_->GenerateInvokePolymorphicCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003659 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 10);
Orion Hodsonac141392017-01-13 11:53:47 +00003660}
3661
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003662void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3663 HandleInvoke(invoke);
3664}
3665
3666void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3667 codegen_->GenerateInvokeCustomCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003668 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 11);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003669}
3670
Artem Serov02109dd2016-09-23 17:17:54 +01003671void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3672 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003673 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003674 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003675 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003676 locations->SetInAt(0, Location::RequiresRegister());
3677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3678 break;
3679 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003681 locations->SetInAt(0, Location::RequiresRegister());
3682 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3683 break;
3684 }
3685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kFloat32:
3687 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003688 locations->SetInAt(0, Location::RequiresFpuRegister());
3689 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3690 break;
3691
3692 default:
3693 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3694 }
3695}
3696
3697void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3698 LocationSummary* locations = neg->GetLocations();
3699 Location out = locations->Out();
3700 Location in = locations->InAt(0);
3701 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003702 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003703 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3704 break;
3705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003706 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003707 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3708 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3709 // We cannot emit an RSC (Reverse Subtract with Carry)
3710 // instruction here, as it does not exist in the Thumb-2
3711 // instruction set. We use the following approach
3712 // using SBC and SUB instead.
3713 //
3714 // out.hi = -C
3715 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3716 // out.hi = out.hi - in.hi
3717 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3718 break;
3719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003720 case DataType::Type::kFloat32:
3721 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003722 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003723 break;
3724
3725 default:
3726 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3727 }
3728}
3729
Scott Wakelingfe885462016-09-22 10:24:38 +01003730void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003731 DataType::Type result_type = conversion->GetResultType();
3732 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003733 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3734 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003735
3736 // The float-to-long, double-to-long and long-to-float type conversions
3737 // rely on a call to the runtime.
3738 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003739 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3740 && result_type == DataType::Type::kInt64)
3741 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003742 ? LocationSummary::kCallOnMainOnly
3743 : LocationSummary::kNoCall;
3744 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003745 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003746
Scott Wakelingfe885462016-09-22 10:24:38 +01003747 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003748 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003749 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003750 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003751 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003752 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3753 locations->SetInAt(0, Location::RequiresRegister());
3754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003755 break;
3756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003759 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003760 locations->SetInAt(0, Location::Any());
3761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3762 break;
3763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003765 locations->SetInAt(0, Location::RequiresFpuRegister());
3766 locations->SetOut(Location::RequiresRegister());
3767 locations->AddTemp(Location::RequiresFpuRegister());
3768 break;
3769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003771 locations->SetInAt(0, Location::RequiresFpuRegister());
3772 locations->SetOut(Location::RequiresRegister());
3773 locations->AddTemp(Location::RequiresFpuRegister());
3774 break;
3775
3776 default:
3777 LOG(FATAL) << "Unexpected type conversion from " << input_type
3778 << " to " << result_type;
3779 }
3780 break;
3781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003782 case DataType::Type::kInt64:
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::RequiresRegister(), Location::kNoOutputOverlap);
3792 break;
3793
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003795 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3796 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3797 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003798 break;
3799 }
3800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003801 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003802 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3803 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3804 calling_convention.GetFpuRegisterAt(1)));
3805 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003806 break;
3807 }
3808
3809 default:
3810 LOG(FATAL) << "Unexpected type conversion from " << input_type
3811 << " to " << result_type;
3812 }
3813 break;
3814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003816 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003817 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003818 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003819 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003820 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003821 case DataType::Type::kInt16:
3822 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003823 locations->SetInAt(0, Location::RequiresRegister());
3824 locations->SetOut(Location::RequiresFpuRegister());
3825 break;
3826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003827 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003828 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3829 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3830 calling_convention.GetRegisterAt(1)));
3831 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003832 break;
3833 }
3834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003836 locations->SetInAt(0, Location::RequiresFpuRegister());
3837 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3838 break;
3839
3840 default:
3841 LOG(FATAL) << "Unexpected type conversion from " << input_type
3842 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003843 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003844 break;
3845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003846 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003847 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003848 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003849 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003851 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 case DataType::Type::kInt16:
3853 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003854 locations->SetInAt(0, Location::RequiresRegister());
3855 locations->SetOut(Location::RequiresFpuRegister());
3856 break;
3857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003858 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003859 locations->SetInAt(0, Location::RequiresRegister());
3860 locations->SetOut(Location::RequiresFpuRegister());
3861 locations->AddTemp(Location::RequiresFpuRegister());
3862 locations->AddTemp(Location::RequiresFpuRegister());
3863 break;
3864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003865 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003866 locations->SetInAt(0, Location::RequiresFpuRegister());
3867 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3868 break;
3869
3870 default:
3871 LOG(FATAL) << "Unexpected type conversion from " << input_type
3872 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003873 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003874 break;
3875
3876 default:
3877 LOG(FATAL) << "Unexpected type conversion from " << input_type
3878 << " to " << result_type;
3879 }
3880}
3881
3882void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3883 LocationSummary* locations = conversion->GetLocations();
3884 Location out = locations->Out();
3885 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003886 DataType::Type result_type = conversion->GetResultType();
3887 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003888 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3889 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003890 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003891 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003892 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003893 case DataType::Type::kInt8:
3894 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003895 case DataType::Type::kInt16:
3896 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003897 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3898 break;
3899 case DataType::Type::kInt64:
3900 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3901 break;
3902
3903 default:
3904 LOG(FATAL) << "Unexpected type conversion from " << input_type
3905 << " to " << result_type;
3906 }
3907 break;
3908
3909 case DataType::Type::kInt8:
3910 switch (input_type) {
3911 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003912 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003913 case DataType::Type::kInt16:
3914 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003915 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3916 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003917 case DataType::Type::kInt64:
3918 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3919 break;
3920
3921 default:
3922 LOG(FATAL) << "Unexpected type conversion from " << input_type
3923 << " to " << result_type;
3924 }
3925 break;
3926
3927 case DataType::Type::kUint16:
3928 switch (input_type) {
3929 case DataType::Type::kInt8:
3930 case DataType::Type::kInt16:
3931 case DataType::Type::kInt32:
3932 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3933 break;
3934 case DataType::Type::kInt64:
3935 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3936 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003937
3938 default:
3939 LOG(FATAL) << "Unexpected type conversion from " << input_type
3940 << " to " << result_type;
3941 }
3942 break;
3943
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003944 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003945 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003946 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003947 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003948 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3949 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003950 case DataType::Type::kInt64:
3951 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3952 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003953
3954 default:
3955 LOG(FATAL) << "Unexpected type conversion from " << input_type
3956 << " to " << result_type;
3957 }
3958 break;
3959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003961 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003963 DCHECK(out.IsRegister());
3964 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003965 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003966 } else if (in.IsDoubleStackSlot()) {
3967 GetAssembler()->LoadFromOffset(kLoadWord,
3968 OutputRegister(conversion),
3969 sp,
3970 in.GetStackIndex());
3971 } else {
3972 DCHECK(in.IsConstant());
3973 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003974 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3975 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003976 }
3977 break;
3978
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003979 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003980 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003981 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003982 __ Vmov(OutputRegister(conversion), temp);
3983 break;
3984 }
3985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003986 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003987 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003988 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003989 __ Vmov(OutputRegister(conversion), temp_s);
3990 break;
3991 }
3992
3993 default:
3994 LOG(FATAL) << "Unexpected type conversion from " << input_type
3995 << " to " << result_type;
3996 }
3997 break;
3998
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003999 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004000 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004001 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004002 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004003 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004004 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004005 case DataType::Type::kInt16:
4006 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004007 DCHECK(out.IsRegisterPair());
4008 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004009 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004010 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004011 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01004012 break;
4013
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004014 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004015 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
4016 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4017 break;
4018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004019 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004020 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
4021 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4022 break;
4023
4024 default:
4025 LOG(FATAL) << "Unexpected type conversion from " << input_type
4026 << " to " << result_type;
4027 }
4028 break;
4029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004030 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004031 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004032 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004033 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004034 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004035 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004036 case DataType::Type::kInt16:
4037 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01004038 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004039 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01004040 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004041
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004042 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004043 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
4044 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4045 break;
4046
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004047 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004048 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01004049 break;
4050
4051 default:
4052 LOG(FATAL) << "Unexpected type conversion from " << input_type
4053 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004054 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004055 break;
4056
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004057 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01004058 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004059 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004060 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004061 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004062 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 case DataType::Type::kInt16:
4064 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004065 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004066 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01004067 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004069 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004070 vixl32::Register low = LowRegisterFrom(in);
4071 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004072 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01004073 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004074 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01004075 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004076 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004077
4078 // temp_d = int-to-double(high)
4079 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01004080 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01004081 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004082 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01004083 // out_d = unsigned-to-double(low)
4084 __ Vmov(out_s, low);
4085 __ Vcvt(F64, U32, out_d, out_s);
4086 // out_d += temp_d * constant_d
4087 __ Vmla(F64, out_d, temp_d, constant_d);
4088 break;
4089 }
4090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004091 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004092 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004093 break;
4094
4095 default:
4096 LOG(FATAL) << "Unexpected type conversion from " << input_type
4097 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004098 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004099 break;
4100
4101 default:
4102 LOG(FATAL) << "Unexpected type conversion from " << input_type
4103 << " to " << result_type;
4104 }
4105}
4106
4107void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4108 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004109 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004110 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004112 locations->SetInAt(0, Location::RequiresRegister());
4113 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4115 break;
4116 }
4117
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004118 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004119 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004120 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4122 break;
4123 }
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
4133 default:
4134 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4135 }
4136}
4137
4138void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4139 LocationSummary* locations = add->GetLocations();
4140 Location out = locations->Out();
4141 Location first = locations->InAt(0);
4142 Location second = locations->InAt(1);
4143
4144 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004145 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004146 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4147 }
4148 break;
4149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004150 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004151 if (second.IsConstant()) {
4152 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4153 GenerateAddLongConst(out, first, value);
4154 } else {
4155 DCHECK(second.IsRegisterPair());
4156 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4157 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4158 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004159 break;
4160 }
4161
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004162 case DataType::Type::kFloat32:
4163 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004164 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004165 break;
4166
4167 default:
4168 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4169 }
4170}
4171
4172void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4173 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004174 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004175 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004176 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004177 locations->SetInAt(0, Location::RequiresRegister());
4178 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4180 break;
4181 }
4182
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004183 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004184 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004185 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4187 break;
4188 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004189 case DataType::Type::kFloat32:
4190 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004191 locations->SetInAt(0, Location::RequiresFpuRegister());
4192 locations->SetInAt(1, Location::RequiresFpuRegister());
4193 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4194 break;
4195 }
4196 default:
4197 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4198 }
4199}
4200
4201void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4202 LocationSummary* locations = sub->GetLocations();
4203 Location out = locations->Out();
4204 Location first = locations->InAt(0);
4205 Location second = locations->InAt(1);
4206 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004207 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004208 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004209 break;
4210 }
4211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004213 if (second.IsConstant()) {
4214 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4215 GenerateAddLongConst(out, first, -value);
4216 } else {
4217 DCHECK(second.IsRegisterPair());
4218 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4219 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4220 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004221 break;
4222 }
4223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004224 case DataType::Type::kFloat32:
4225 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004226 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004227 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004228
4229 default:
4230 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4231 }
4232}
4233
4234void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4235 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004236 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004237 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004238 case DataType::Type::kInt32:
4239 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004240 locations->SetInAt(0, Location::RequiresRegister());
4241 locations->SetInAt(1, Location::RequiresRegister());
4242 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4243 break;
4244 }
4245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004246 case DataType::Type::kFloat32:
4247 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004248 locations->SetInAt(0, Location::RequiresFpuRegister());
4249 locations->SetInAt(1, Location::RequiresFpuRegister());
4250 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4251 break;
4252 }
4253
4254 default:
4255 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4256 }
4257}
4258
4259void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4260 LocationSummary* locations = mul->GetLocations();
4261 Location out = locations->Out();
4262 Location first = locations->InAt(0);
4263 Location second = locations->InAt(1);
4264 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004265 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004266 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4267 break;
4268 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004269 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004270 vixl32::Register out_hi = HighRegisterFrom(out);
4271 vixl32::Register out_lo = LowRegisterFrom(out);
4272 vixl32::Register in1_hi = HighRegisterFrom(first);
4273 vixl32::Register in1_lo = LowRegisterFrom(first);
4274 vixl32::Register in2_hi = HighRegisterFrom(second);
4275 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004276
4277 // Extra checks to protect caused by the existence of R1_R2.
4278 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4279 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004280 DCHECK(!out_hi.Is(in1_lo));
4281 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004282
4283 // input: in1 - 64 bits, in2 - 64 bits
4284 // output: out
4285 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4286 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4287 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4288
4289 UseScratchRegisterScope temps(GetVIXLAssembler());
4290 vixl32::Register temp = temps.Acquire();
4291 // temp <- in1.lo * in2.hi
4292 __ Mul(temp, in1_lo, in2_hi);
4293 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4294 __ Mla(out_hi, in1_hi, in2_lo, temp);
4295 // out.lo <- (in1.lo * in2.lo)[31:0];
4296 __ Umull(out_lo, temp, in1_lo, in2_lo);
4297 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004298 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004299 break;
4300 }
4301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004302 case DataType::Type::kFloat32:
4303 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004304 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004305 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004306
4307 default:
4308 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4309 }
4310}
4311
Scott Wakelingfe885462016-09-22 10:24:38 +01004312void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4313 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004314 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004315
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004316 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004317 DCHECK(second.IsConstant());
4318
4319 vixl32::Register out = OutputRegister(instruction);
4320 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004321 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004322 DCHECK(imm == 1 || imm == -1);
4323
4324 if (instruction->IsRem()) {
4325 __ Mov(out, 0);
4326 } else {
4327 if (imm == 1) {
4328 __ Mov(out, dividend);
4329 } else {
4330 __ Rsb(out, dividend, 0);
4331 }
4332 }
4333}
4334
4335void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4336 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004337 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004338
4339 LocationSummary* locations = instruction->GetLocations();
4340 Location second = locations->InAt(1);
4341 DCHECK(second.IsConstant());
4342
4343 vixl32::Register out = OutputRegister(instruction);
4344 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004345 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004346 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4347 int ctz_imm = CTZ(abs_imm);
4348
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004349 auto generate_div_code = [this, imm, ctz_imm](vixl32::Register out, vixl32::Register in) {
4350 __ Asr(out, in, ctz_imm);
Scott Wakelingfe885462016-09-22 10:24:38 +01004351 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004352 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004353 }
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004354 };
4355
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004356 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004357 // No need to adjust the result for non-negative dividends or the INT32_MIN dividend.
4358 // NOTE: The generated code for HDiv/HRem correctly works for the INT32_MIN dividend:
4359 // imm == 2
4360 // HDiv
4361 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
4362 // asr out, out(0x80000001), #1 => out = 0xc0000000
4363 // This is the same as 'asr out, dividend(0x80000000), #1'
4364 //
4365 // imm > 2
4366 // HDiv
4367 // asr out, dividend(0x80000000), #31 => out = -1
4368 // add out, dividend(0x80000000), out(-1), lsr #(32 - ctz_imm) => out = 0b10..01..1,
4369 // where the number of the rightmost 1s is ctz_imm.
4370 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
4371 // leftmost 1s is ctz_imm + 1.
4372 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
4373 //
4374 // imm == INT32_MIN
4375 // HDiv
4376 // asr out, dividend(0x80000000), #31 => out = -1
4377 // add out, dividend(0x80000000), out(-1), lsr #1 => out = 0xc0000000
4378 // asr out, out(0xc0000000), #31 => out = -1
4379 // rsb out, out(-1), #0 => out = 1
4380 // This is the same as
4381 // asr out, dividend(0x80000000), #31
4382 // rsb out, out, #0
4383 //
4384 //
4385 // INT_MIN % imm must be 0 for any imm of power 2. 'and' and 'ubfx' work only with bits
4386 // 0..30 of a dividend. For INT32_MIN those bits are zeros. So 'and' and 'ubfx' always
4387 // produce zero.
4388 if (instruction->IsDiv()) {
4389 generate_div_code(out, dividend);
4390 } else {
4391 if (GetVIXLAssembler()->IsModifiedImmediate(abs_imm - 1)) {
4392 __ And(out, dividend, abs_imm - 1);
4393 } else {
4394 __ Ubfx(out, dividend, 0, ctz_imm);
4395 }
4396 return;
4397 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004398 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004399 vixl32::Register add_right_input = dividend;
4400 if (ctz_imm > 1) {
4401 __ Asr(out, dividend, 31);
4402 add_right_input = out;
4403 }
4404 __ Add(out, dividend, Operand(add_right_input, vixl32::LSR, 32 - ctz_imm));
4405
4406 if (instruction->IsDiv()) {
4407 generate_div_code(out, out);
4408 } else {
4409 __ Bfc(out, 0, ctz_imm);
4410 __ Sub(out, dividend, out);
4411 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004412 }
4413}
4414
4415void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4416 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004417 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004418
4419 LocationSummary* locations = instruction->GetLocations();
4420 Location second = locations->InAt(1);
4421 DCHECK(second.IsConstant());
4422
4423 vixl32::Register out = OutputRegister(instruction);
4424 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004425 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4426 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004427 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004428
4429 int64_t magic;
4430 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004431 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Scott Wakelingfe885462016-09-22 10:24:38 +01004432
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004433 auto generate_unsigned_div_code =[this, magic, shift](vixl32::Register out,
4434 vixl32::Register dividend,
4435 vixl32::Register temp1,
4436 vixl32::Register temp2) {
4437 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4438 __ Mov(temp1, static_cast<int32_t>(magic));
4439 if (magic > 0 && shift == 0) {
4440 __ Smull(temp2, out, dividend, temp1);
4441 } else {
4442 __ Smull(temp2, temp1, dividend, temp1);
4443 if (magic < 0) {
4444 // The negative magic M = static_cast<int>(m) means that the multiplier m is greater
4445 // than INT32_MAX. In such a case shift is never 0.
4446 // Proof:
4447 // m = (2^p + d - 2^p % d) / d, where p = 32 + shift, d > 2
4448 //
4449 // If shift == 0, m = (2^32 + d - 2^32 % d) / d =
4450 // = (2^32 + d - (2^32 - (2^32 / d) * d)) / d =
4451 // = (d + (2^32 / d) * d) / d = 1 + (2^32 / d), here '/' is the integer division.
4452 //
4453 // 1 + (2^32 / d) is decreasing when d is increasing.
4454 // The maximum is 1 431 655 766, when d == 3. This value is less than INT32_MAX.
4455 // the minimum is 3, when d = 2^31 -1.
4456 // So for all values of d in [3, INT32_MAX] m with p == 32 is in [3, INT32_MAX) and
4457 // is never less than 0.
4458 __ Add(temp1, temp1, dividend);
4459 }
4460 DCHECK_NE(shift, 0);
4461 __ Lsr(out, temp1, shift);
4462 }
4463 };
Scott Wakelingfe885462016-09-22 10:24:38 +01004464
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004465 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004466 // No need to adjust the result for a non-negative dividend and a positive divisor.
4467 if (instruction->IsDiv()) {
4468 generate_unsigned_div_code(out, dividend, temp1, temp2);
4469 } else {
4470 generate_unsigned_div_code(temp1, dividend, temp1, temp2);
4471 __ Mov(temp2, imm);
4472 __ Mls(out, temp1, temp2, dividend);
4473 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004474 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004475 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4476 __ Mov(temp1, static_cast<int32_t>(magic));
4477 __ Smull(temp2, temp1, dividend, temp1);
4478
4479 if (imm > 0 && magic < 0) {
4480 __ Add(temp1, temp1, dividend);
4481 } else if (imm < 0 && magic > 0) {
4482 __ Sub(temp1, temp1, dividend);
4483 }
4484
4485 if (shift != 0) {
4486 __ Asr(temp1, temp1, shift);
4487 }
4488
4489 if (instruction->IsDiv()) {
4490 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4491 } else {
4492 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4493 // TODO: Strength reduction for mls.
4494 __ Mov(temp2, imm);
4495 __ Mls(out, temp1, temp2, dividend);
4496 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004497 }
4498}
4499
4500void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4501 HBinaryOperation* instruction) {
4502 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004503 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004504
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004505 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004506 DCHECK(second.IsConstant());
4507
Anton Kirilov644032c2016-12-06 17:51:43 +00004508 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004509 if (imm == 0) {
4510 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4511 } else if (imm == 1 || imm == -1) {
4512 DivRemOneOrMinusOne(instruction);
4513 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4514 DivRemByPowerOfTwo(instruction);
4515 } else {
4516 DCHECK(imm <= -2 || imm >= 2);
4517 GenerateDivRemWithAnyConstant(instruction);
4518 }
4519}
4520
4521void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4522 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004523 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004524 // pLdiv runtime call.
4525 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004526 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004527 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004529 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4530 // pIdivmod runtime call.
4531 call_kind = LocationSummary::kCallOnMainOnly;
4532 }
4533
Vladimir Markoca6fff82017-10-03 14:49:14 +01004534 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004535
4536 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004537 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004538 if (div->InputAt(1)->IsConstant()) {
4539 locations->SetInAt(0, Location::RequiresRegister());
4540 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004541 int32_t value = Int32ConstantFrom(div->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004542 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004543 if (value == 1 || value == 0 || value == -1) {
4544 // No temp register required.
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004545 } else if (IsPowerOfTwo(AbsOrMin(value)) &&
4546 value != 2 &&
4547 value != -2 &&
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004548 !HasNonNegativeOrMinIntInputAt(div, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004549 // The "out" register is used as a temporary, so it overlaps with the inputs.
4550 out_overlaps = Location::kOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004551 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004552 locations->AddRegisterTemps(2);
Scott Wakelingfe885462016-09-22 10:24:38 +01004553 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004554 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Scott Wakelingfe885462016-09-22 10:24:38 +01004555 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4556 locations->SetInAt(0, Location::RequiresRegister());
4557 locations->SetInAt(1, Location::RequiresRegister());
4558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4559 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004560 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4561 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4562 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004563 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004564 // we only need the former.
4565 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004566 }
4567 break;
4568 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004570 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4571 locations->SetInAt(0, LocationFrom(
4572 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4573 locations->SetInAt(1, LocationFrom(
4574 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4575 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004576 break;
4577 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004578 case DataType::Type::kFloat32:
4579 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004580 locations->SetInAt(0, Location::RequiresFpuRegister());
4581 locations->SetInAt(1, Location::RequiresFpuRegister());
4582 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4583 break;
4584 }
4585
4586 default:
4587 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4588 }
4589}
4590
4591void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004592 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004593 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004594
4595 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004596 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004597 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004598 GenerateDivRemConstantIntegral(div);
4599 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4600 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4601 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004602 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4603 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4604 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4605 DCHECK(r0.Is(OutputRegister(div)));
4606
4607 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4608 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004609 }
4610 break;
4611 }
4612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004613 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004614 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4615 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4616 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4617 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4618 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4619 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4620 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4621
4622 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4623 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004624 break;
4625 }
4626
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004627 case DataType::Type::kFloat32:
4628 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004629 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004630 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004631
4632 default:
4633 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4634 }
4635}
4636
Artem Serov551b28f2016-10-18 19:11:30 +01004637void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004638 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004639
4640 // Most remainders are implemented in the runtime.
4641 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004643 // sdiv will be replaced by other instruction sequence.
4644 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004646 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4647 // Have hardware divide instruction for int, do it with three instructions.
4648 call_kind = LocationSummary::kNoCall;
4649 }
4650
Vladimir Markoca6fff82017-10-03 14:49:14 +01004651 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004652
4653 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004654 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004655 if (rem->InputAt(1)->IsConstant()) {
4656 locations->SetInAt(0, Location::RequiresRegister());
4657 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004658 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004659 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004660 if (value == 1 || value == 0 || value == -1) {
4661 // No temp register required.
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004662 } else if (IsPowerOfTwo(AbsOrMin(value)) && !HasNonNegativeOrMinIntInputAt(rem, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004663 // The "out" register is used as a temporary, so it overlaps with the inputs.
4664 out_overlaps = Location::kOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004665 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004666 locations->AddRegisterTemps(2);
Artem Serov551b28f2016-10-18 19:11:30 +01004667 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004668 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Artem Serov551b28f2016-10-18 19:11:30 +01004669 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4670 locations->SetInAt(0, Location::RequiresRegister());
4671 locations->SetInAt(1, Location::RequiresRegister());
4672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4673 locations->AddTemp(Location::RequiresRegister());
4674 } else {
4675 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4676 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4677 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004678 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004679 // we only need the latter.
4680 locations->SetOut(LocationFrom(r1));
4681 }
4682 break;
4683 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004684 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004685 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4686 locations->SetInAt(0, LocationFrom(
4687 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4688 locations->SetInAt(1, LocationFrom(
4689 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4690 // The runtime helper puts the output in R2,R3.
4691 locations->SetOut(LocationFrom(r2, r3));
4692 break;
4693 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004694 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004695 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4696 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4697 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4698 locations->SetOut(LocationFrom(s0));
4699 break;
4700 }
4701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004702 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004703 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4704 locations->SetInAt(0, LocationFrom(
4705 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4706 locations->SetInAt(1, LocationFrom(
4707 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4708 locations->SetOut(LocationFrom(s0, s1));
4709 break;
4710 }
4711
4712 default:
4713 LOG(FATAL) << "Unexpected rem type " << type;
4714 }
4715}
4716
4717void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4718 LocationSummary* locations = rem->GetLocations();
4719 Location second = locations->InAt(1);
4720
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004721 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004722 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004723 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004724 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4725 vixl32::Register out_reg = OutputRegister(rem);
4726 if (second.IsConstant()) {
4727 GenerateDivRemConstantIntegral(rem);
4728 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4729 vixl32::Register reg2 = RegisterFrom(second);
4730 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4731
4732 // temp = reg1 / reg2 (integer division)
4733 // dest = reg1 - temp * reg2
4734 __ Sdiv(temp, reg1, reg2);
4735 __ Mls(out_reg, temp, reg2, reg1);
4736 } else {
4737 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4738 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4739 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4740 DCHECK(out_reg.Is(r1));
4741
4742 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4743 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4744 }
4745 break;
4746 }
4747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004748 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004749 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4750 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4751 break;
4752 }
4753
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004754 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004755 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4756 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4757 break;
4758 }
4759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004760 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004761 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4762 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4763 break;
4764 }
4765
4766 default:
4767 LOG(FATAL) << "Unexpected rem type " << type;
4768 }
4769}
4770
Aart Bik1f8d51b2018-02-15 10:42:37 -08004771static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4772 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4773 switch (minmax->GetResultType()) {
4774 case DataType::Type::kInt32:
4775 locations->SetInAt(0, Location::RequiresRegister());
4776 locations->SetInAt(1, Location::RequiresRegister());
4777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4778 break;
4779 case DataType::Type::kInt64:
4780 locations->SetInAt(0, Location::RequiresRegister());
4781 locations->SetInAt(1, Location::RequiresRegister());
4782 locations->SetOut(Location::SameAsFirstInput());
4783 break;
4784 case DataType::Type::kFloat32:
4785 locations->SetInAt(0, Location::RequiresFpuRegister());
4786 locations->SetInAt(1, Location::RequiresFpuRegister());
4787 locations->SetOut(Location::SameAsFirstInput());
4788 locations->AddTemp(Location::RequiresRegister());
4789 break;
4790 case DataType::Type::kFloat64:
4791 locations->SetInAt(0, Location::RequiresFpuRegister());
4792 locations->SetInAt(1, Location::RequiresFpuRegister());
4793 locations->SetOut(Location::SameAsFirstInput());
4794 break;
4795 default:
4796 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4797 }
4798}
4799
Aart Bik351df3e2018-03-07 11:54:57 -08004800void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004801 Location op1_loc = locations->InAt(0);
4802 Location op2_loc = locations->InAt(1);
4803 Location out_loc = locations->Out();
4804
4805 vixl32::Register op1 = RegisterFrom(op1_loc);
4806 vixl32::Register op2 = RegisterFrom(op2_loc);
4807 vixl32::Register out = RegisterFrom(out_loc);
4808
4809 __ Cmp(op1, op2);
4810
4811 {
4812 ExactAssemblyScope aas(GetVIXLAssembler(),
4813 3 * kMaxInstructionSizeInBytes,
4814 CodeBufferCheckScope::kMaximumSize);
4815
4816 __ ite(is_min ? lt : gt);
4817 __ mov(is_min ? lt : gt, out, op1);
4818 __ mov(is_min ? ge : le, out, op2);
4819 }
4820}
4821
4822void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4823 Location op1_loc = locations->InAt(0);
4824 Location op2_loc = locations->InAt(1);
4825 Location out_loc = locations->Out();
4826
4827 // Optimization: don't generate any code if inputs are the same.
4828 if (op1_loc.Equals(op2_loc)) {
4829 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4830 return;
4831 }
4832
4833 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4834 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4835 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4836 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4837 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4838 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4839 UseScratchRegisterScope temps(GetVIXLAssembler());
4840 const vixl32::Register temp = temps.Acquire();
4841
4842 DCHECK(op1_lo.Is(out_lo));
4843 DCHECK(op1_hi.Is(out_hi));
4844
4845 // Compare op1 >= op2, or op1 < op2.
4846 __ Cmp(out_lo, op2_lo);
4847 __ Sbcs(temp, out_hi, op2_hi);
4848
4849 // Now GE/LT condition code is correct for the long comparison.
4850 {
4851 vixl32::ConditionType cond = is_min ? ge : lt;
4852 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4853 3 * kMaxInstructionSizeInBytes,
4854 CodeBufferCheckScope::kMaximumSize);
4855 __ itt(cond);
4856 __ mov(cond, out_lo, op2_lo);
4857 __ mov(cond, out_hi, op2_hi);
4858 }
4859}
4860
Aart Bik351df3e2018-03-07 11:54:57 -08004861void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4862 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004863 Location op1_loc = locations->InAt(0);
4864 Location op2_loc = locations->InAt(1);
4865 Location out_loc = locations->Out();
4866
4867 // Optimization: don't generate any code if inputs are the same.
4868 if (op1_loc.Equals(op2_loc)) {
4869 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4870 return;
4871 }
4872
4873 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4874 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4875 vixl32::SRegister out = SRegisterFrom(out_loc);
4876
4877 UseScratchRegisterScope temps(GetVIXLAssembler());
4878 const vixl32::Register temp1 = temps.Acquire();
4879 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4880 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004881 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004882
4883 DCHECK(op1.Is(out));
4884
4885 __ Vcmp(op1, op2);
4886 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004887 __ B(vs, &nan, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004888
4889 // op1 <> op2
4890 vixl32::ConditionType cond = is_min ? gt : lt;
4891 {
4892 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4893 2 * kMaxInstructionSizeInBytes,
4894 CodeBufferCheckScope::kMaximumSize);
4895 __ it(cond);
4896 __ vmov(cond, F32, out, op2);
4897 }
4898 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004899 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004900
4901 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4902 __ Vmov(temp1, op1);
4903 __ Vmov(temp2, op2);
4904 if (is_min) {
4905 __ Orr(temp1, temp1, temp2);
4906 } else {
4907 __ And(temp1, temp1, temp2);
4908 }
4909 __ Vmov(out, temp1);
4910 __ B(final_label);
4911
4912 // handle NaN input.
4913 __ Bind(&nan);
4914 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4915 __ Vmov(out, temp1);
4916
4917 if (done.IsReferenced()) {
4918 __ Bind(&done);
4919 }
4920}
4921
Aart Bik351df3e2018-03-07 11:54:57 -08004922void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4923 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004924 Location op1_loc = locations->InAt(0);
4925 Location op2_loc = locations->InAt(1);
4926 Location out_loc = locations->Out();
4927
4928 // Optimization: don't generate any code if inputs are the same.
4929 if (op1_loc.Equals(op2_loc)) {
4930 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4931 return;
4932 }
4933
4934 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4935 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4936 vixl32::DRegister out = DRegisterFrom(out_loc);
4937 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004938 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004939
4940 DCHECK(op1.Is(out));
4941
4942 __ Vcmp(op1, op2);
4943 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004944 __ B(vs, &handle_nan_eq, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004945
4946 // op1 <> op2
4947 vixl32::ConditionType cond = is_min ? gt : lt;
4948 {
4949 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4950 2 * kMaxInstructionSizeInBytes,
4951 CodeBufferCheckScope::kMaximumSize);
4952 __ it(cond);
4953 __ vmov(cond, F64, out, op2);
4954 }
4955 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004956 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004957
4958 // handle op1 == op2, max(+0.0,-0.0).
4959 if (!is_min) {
4960 __ Vand(F64, out, op1, op2);
4961 __ B(final_label);
4962 }
4963
4964 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4965 __ Bind(&handle_nan_eq);
4966 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4967
4968 if (done.IsReferenced()) {
4969 __ Bind(&done);
4970 }
4971}
4972
Aart Bik351df3e2018-03-07 11:54:57 -08004973void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4974 DataType::Type type = minmax->GetResultType();
4975 switch (type) {
4976 case DataType::Type::kInt32:
4977 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4978 break;
4979 case DataType::Type::kInt64:
4980 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4981 break;
4982 case DataType::Type::kFloat32:
4983 GenerateMinMaxFloat(minmax, is_min);
4984 break;
4985 case DataType::Type::kFloat64:
4986 GenerateMinMaxDouble(minmax, is_min);
4987 break;
4988 default:
4989 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4990 }
4991}
4992
Aart Bik1f8d51b2018-02-15 10:42:37 -08004993void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4994 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4995}
4996
4997void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004998 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004999}
5000
5001void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
5002 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
5003}
5004
5005void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08005006 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005007}
5008
Aart Bik3dad3412018-02-28 12:01:46 -08005009void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
5010 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5011 switch (abs->GetResultType()) {
5012 case DataType::Type::kInt32:
5013 case DataType::Type::kInt64:
5014 locations->SetInAt(0, Location::RequiresRegister());
5015 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5016 locations->AddTemp(Location::RequiresRegister());
5017 break;
5018 case DataType::Type::kFloat32:
5019 case DataType::Type::kFloat64:
5020 locations->SetInAt(0, Location::RequiresFpuRegister());
5021 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5022 break;
5023 default:
5024 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5025 }
5026}
5027
5028void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
5029 LocationSummary* locations = abs->GetLocations();
5030 switch (abs->GetResultType()) {
5031 case DataType::Type::kInt32: {
5032 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
5033 vixl32::Register out_reg = RegisterFrom(locations->Out());
5034 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
5035 __ Asr(mask, in_reg, 31);
5036 __ Add(out_reg, in_reg, mask);
5037 __ Eor(out_reg, out_reg, mask);
5038 break;
5039 }
5040 case DataType::Type::kInt64: {
5041 Location in = locations->InAt(0);
5042 vixl32::Register in_reg_lo = LowRegisterFrom(in);
5043 vixl32::Register in_reg_hi = HighRegisterFrom(in);
5044 Location output = locations->Out();
5045 vixl32::Register out_reg_lo = LowRegisterFrom(output);
5046 vixl32::Register out_reg_hi = HighRegisterFrom(output);
5047 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
5048 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
5049 __ Asr(mask, in_reg_hi, 31);
5050 __ Adds(out_reg_lo, in_reg_lo, mask);
5051 __ Adc(out_reg_hi, in_reg_hi, mask);
5052 __ Eor(out_reg_lo, out_reg_lo, mask);
5053 __ Eor(out_reg_hi, out_reg_hi, mask);
5054 break;
5055 }
5056 case DataType::Type::kFloat32:
5057 case DataType::Type::kFloat64:
5058 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
5059 break;
5060 default:
5061 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5062 }
5063}
Artem Serov551b28f2016-10-18 19:11:30 +01005064
Scott Wakelingfe885462016-09-22 10:24:38 +01005065void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005066 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01005067 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01005068}
5069
5070void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
5071 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005072 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01005073 codegen_->AddSlowPath(slow_path);
5074
5075 LocationSummary* locations = instruction->GetLocations();
5076 Location value = locations->InAt(0);
5077
5078 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005079 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005080 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005081 case DataType::Type::kInt8:
5082 case DataType::Type::kUint16:
5083 case DataType::Type::kInt16:
5084 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005085 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00005086 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01005087 } else {
5088 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005089 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005090 __ B(slow_path->GetEntryLabel());
5091 }
5092 }
5093 break;
5094 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005095 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005096 if (value.IsRegisterPair()) {
5097 UseScratchRegisterScope temps(GetVIXLAssembler());
5098 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005099 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005100 __ B(eq, slow_path->GetEntryLabel());
5101 } else {
5102 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005103 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005104 __ B(slow_path->GetEntryLabel());
5105 }
5106 }
5107 break;
5108 }
5109 default:
5110 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5111 }
5112}
5113
Artem Serov02109dd2016-09-23 17:17:54 +01005114void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5115 LocationSummary* locations = ror->GetLocations();
5116 vixl32::Register in = InputRegisterAt(ror, 0);
5117 Location rhs = locations->InAt(1);
5118 vixl32::Register out = OutputRegister(ror);
5119
5120 if (rhs.IsConstant()) {
5121 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5122 // so map all rotations to a +ve. equivalent in that range.
5123 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5124 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5125 if (rot) {
5126 // Rotate, mapping left rotations to right equivalents if necessary.
5127 // (e.g. left by 2 bits == right by 30.)
5128 __ Ror(out, in, rot);
5129 } else if (!out.Is(in)) {
5130 __ Mov(out, in);
5131 }
5132 } else {
5133 __ Ror(out, in, RegisterFrom(rhs));
5134 }
5135}
5136
5137// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5138// rotates by swapping input regs (effectively rotating by the first 32-bits of
5139// a larger rotation) or flipping direction (thus treating larger right/left
5140// rotations as sub-word sized rotations in the other direction) as appropriate.
5141void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5142 LocationSummary* locations = ror->GetLocations();
5143 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5144 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5145 Location rhs = locations->InAt(1);
5146 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5147 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5148
5149 if (rhs.IsConstant()) {
5150 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5151 // Map all rotations to +ve. equivalents on the interval [0,63].
5152 rot &= kMaxLongShiftDistance;
5153 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5154 // logic below to a simple pair of binary orr.
5155 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5156 if (rot >= kArmBitsPerWord) {
5157 rot -= kArmBitsPerWord;
5158 std::swap(in_reg_hi, in_reg_lo);
5159 }
5160 // Rotate, or mov to out for zero or word size rotations.
5161 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005162 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005163 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005164 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005165 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5166 } else {
5167 __ Mov(out_reg_lo, in_reg_lo);
5168 __ Mov(out_reg_hi, in_reg_hi);
5169 }
5170 } else {
5171 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5172 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5173 vixl32::Label end;
5174 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005175 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005176
5177 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5178 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005179 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Andreas Gampe3db70682018-12-26 15:12:03 -08005180 __ B(cc, &shift_by_32_plus_shift_right, /* is_far_target= */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005181
5182 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5183 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5184 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5185 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5186 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5187 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5188 __ Lsr(shift_left, in_reg_hi, shift_right);
5189 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005190 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005191
5192 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5193 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5194 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5195 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5196 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5197 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5198 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5199 __ Lsl(shift_right, in_reg_hi, shift_left);
5200 __ Add(out_reg_lo, out_reg_lo, shift_right);
5201
Anton Kirilov6f644202017-02-27 18:29:45 +00005202 if (end.IsReferenced()) {
5203 __ Bind(&end);
5204 }
Artem Serov02109dd2016-09-23 17:17:54 +01005205 }
5206}
5207
5208void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5209 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005210 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005211 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005212 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005213 locations->SetInAt(0, Location::RequiresRegister());
5214 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5216 break;
5217 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005218 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005219 locations->SetInAt(0, Location::RequiresRegister());
5220 if (ror->InputAt(1)->IsConstant()) {
5221 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5222 } else {
5223 locations->SetInAt(1, Location::RequiresRegister());
5224 locations->AddTemp(Location::RequiresRegister());
5225 locations->AddTemp(Location::RequiresRegister());
5226 }
5227 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5228 break;
5229 }
5230 default:
5231 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5232 }
5233}
5234
5235void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005237 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005238 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005239 HandleIntegerRotate(ror);
5240 break;
5241 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005243 HandleLongRotate(ror);
5244 break;
5245 }
5246 default:
5247 LOG(FATAL) << "Unexpected operation type " << type;
5248 UNREACHABLE();
5249 }
5250}
5251
Artem Serov02d37832016-10-25 15:25:33 +01005252void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5253 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5254
5255 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005256 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005257
5258 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005259 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005260 locations->SetInAt(0, Location::RequiresRegister());
5261 if (op->InputAt(1)->IsConstant()) {
5262 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5264 } else {
5265 locations->SetInAt(1, Location::RequiresRegister());
5266 // Make the output overlap, as it will be used to hold the masked
5267 // second input.
5268 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5269 }
5270 break;
5271 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005272 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005273 locations->SetInAt(0, Location::RequiresRegister());
5274 if (op->InputAt(1)->IsConstant()) {
5275 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5276 // For simplicity, use kOutputOverlap even though we only require that low registers
5277 // don't clash with high registers which the register allocator currently guarantees.
5278 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5279 } else {
5280 locations->SetInAt(1, Location::RequiresRegister());
5281 locations->AddTemp(Location::RequiresRegister());
5282 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5283 }
5284 break;
5285 }
5286 default:
5287 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5288 }
5289}
5290
5291void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5292 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5293
5294 LocationSummary* locations = op->GetLocations();
5295 Location out = locations->Out();
5296 Location first = locations->InAt(0);
5297 Location second = locations->InAt(1);
5298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005299 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005300 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005301 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005302 vixl32::Register out_reg = OutputRegister(op);
5303 vixl32::Register first_reg = InputRegisterAt(op, 0);
5304 if (second.IsRegister()) {
5305 vixl32::Register second_reg = RegisterFrom(second);
5306 // ARM doesn't mask the shift count so we need to do it ourselves.
5307 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5308 if (op->IsShl()) {
5309 __ Lsl(out_reg, first_reg, out_reg);
5310 } else if (op->IsShr()) {
5311 __ Asr(out_reg, first_reg, out_reg);
5312 } else {
5313 __ Lsr(out_reg, first_reg, out_reg);
5314 }
5315 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005316 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005317 uint32_t shift_value = cst & kMaxIntShiftDistance;
5318 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5319 __ Mov(out_reg, first_reg);
5320 } else if (op->IsShl()) {
5321 __ Lsl(out_reg, first_reg, shift_value);
5322 } else if (op->IsShr()) {
5323 __ Asr(out_reg, first_reg, shift_value);
5324 } else {
5325 __ Lsr(out_reg, first_reg, shift_value);
5326 }
5327 }
5328 break;
5329 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005331 vixl32::Register o_h = HighRegisterFrom(out);
5332 vixl32::Register o_l = LowRegisterFrom(out);
5333
5334 vixl32::Register high = HighRegisterFrom(first);
5335 vixl32::Register low = LowRegisterFrom(first);
5336
5337 if (second.IsRegister()) {
5338 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5339
5340 vixl32::Register second_reg = RegisterFrom(second);
5341
5342 if (op->IsShl()) {
5343 __ And(o_l, second_reg, kMaxLongShiftDistance);
5344 // Shift the high part
5345 __ Lsl(o_h, high, o_l);
5346 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005347 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005348 __ Lsr(temp, low, temp);
5349 __ Orr(o_h, o_h, temp);
5350 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005351 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005352 {
Artem Serov0fb37192016-12-06 18:13:40 +00005353 ExactAssemblyScope guard(GetVIXLAssembler(),
5354 2 * vixl32::kMaxInstructionSizeInBytes,
5355 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005356 __ it(pl);
5357 __ lsl(pl, o_h, low, temp);
5358 }
5359 // Shift the low part
5360 __ Lsl(o_l, low, o_l);
5361 } else if (op->IsShr()) {
5362 __ And(o_h, second_reg, kMaxLongShiftDistance);
5363 // Shift the low part
5364 __ Lsr(o_l, low, o_h);
5365 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005366 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005367 __ Lsl(temp, high, temp);
5368 __ Orr(o_l, o_l, temp);
5369 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005370 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005371 {
Artem Serov0fb37192016-12-06 18:13:40 +00005372 ExactAssemblyScope guard(GetVIXLAssembler(),
5373 2 * vixl32::kMaxInstructionSizeInBytes,
5374 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005375 __ it(pl);
5376 __ asr(pl, o_l, high, temp);
5377 }
5378 // Shift the high part
5379 __ Asr(o_h, high, o_h);
5380 } else {
5381 __ And(o_h, second_reg, kMaxLongShiftDistance);
5382 // same as Shr except we use `Lsr`s and not `Asr`s
5383 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005384 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005385 __ Lsl(temp, high, temp);
5386 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005387 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005388 {
Artem Serov0fb37192016-12-06 18:13:40 +00005389 ExactAssemblyScope guard(GetVIXLAssembler(),
5390 2 * vixl32::kMaxInstructionSizeInBytes,
5391 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005392 __ it(pl);
5393 __ lsr(pl, o_l, high, temp);
5394 }
5395 __ Lsr(o_h, high, o_h);
5396 }
5397 } else {
5398 // Register allocator doesn't create partial overlap.
5399 DCHECK(!o_l.Is(high));
5400 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005401 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005402 uint32_t shift_value = cst & kMaxLongShiftDistance;
5403 if (shift_value > 32) {
5404 if (op->IsShl()) {
5405 __ Lsl(o_h, low, shift_value - 32);
5406 __ Mov(o_l, 0);
5407 } else if (op->IsShr()) {
5408 __ Asr(o_l, high, shift_value - 32);
5409 __ Asr(o_h, high, 31);
5410 } else {
5411 __ Lsr(o_l, high, shift_value - 32);
5412 __ Mov(o_h, 0);
5413 }
5414 } else if (shift_value == 32) {
5415 if (op->IsShl()) {
5416 __ Mov(o_h, low);
5417 __ Mov(o_l, 0);
5418 } else if (op->IsShr()) {
5419 __ Mov(o_l, high);
5420 __ Asr(o_h, high, 31);
5421 } else {
5422 __ Mov(o_l, high);
5423 __ Mov(o_h, 0);
5424 }
5425 } else if (shift_value == 1) {
5426 if (op->IsShl()) {
5427 __ Lsls(o_l, low, 1);
5428 __ Adc(o_h, high, high);
5429 } else if (op->IsShr()) {
5430 __ Asrs(o_h, high, 1);
5431 __ Rrx(o_l, low);
5432 } else {
5433 __ Lsrs(o_h, high, 1);
5434 __ Rrx(o_l, low);
5435 }
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005436 } else if (shift_value == 0) {
5437 __ Mov(o_l, low);
5438 __ Mov(o_h, high);
Artem Serov02d37832016-10-25 15:25:33 +01005439 } else {
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005440 DCHECK(0 < shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01005441 if (op->IsShl()) {
5442 __ Lsl(o_h, high, shift_value);
5443 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5444 __ Lsl(o_l, low, shift_value);
5445 } else if (op->IsShr()) {
5446 __ Lsr(o_l, low, shift_value);
5447 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5448 __ Asr(o_h, high, shift_value);
5449 } else {
5450 __ Lsr(o_l, low, shift_value);
5451 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5452 __ Lsr(o_h, high, shift_value);
5453 }
5454 }
5455 }
5456 break;
5457 }
5458 default:
5459 LOG(FATAL) << "Unexpected operation type " << type;
5460 UNREACHABLE();
5461 }
5462}
5463
5464void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5465 HandleShift(shl);
5466}
5467
5468void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5469 HandleShift(shl);
5470}
5471
5472void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5473 HandleShift(shr);
5474}
5475
5476void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5477 HandleShift(shr);
5478}
5479
5480void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5481 HandleShift(ushr);
5482}
5483
5484void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5485 HandleShift(ushr);
5486}
5487
5488void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005489 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5490 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005491 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5492 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005493 locations->SetOut(LocationFrom(r0));
5494}
5495
5496void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005497 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5498 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andra Danciua0130e82020-07-23 12:34:56 +00005499 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005500}
5501
5502void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005503 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5504 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005505 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005506 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005507 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5508 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005509}
5510
5511void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005512 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5513 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005514 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005515 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005516 DCHECK(!codegen_->IsLeafMethod());
Andra Danciua0130e82020-07-23 12:34:56 +00005517 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 13);
Artem Serov02d37832016-10-25 15:25:33 +01005518}
5519
5520void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5521 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005522 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005523 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5524 if (location.IsStackSlot()) {
5525 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5526 } else if (location.IsDoubleStackSlot()) {
5527 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5528 }
5529 locations->SetOut(location);
5530}
5531
5532void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5533 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5534 // Nothing to do, the parameter is already at its location.
5535}
5536
5537void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5538 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005539 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005540 locations->SetOut(LocationFrom(kMethodRegister));
5541}
5542
5543void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5544 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5545 // Nothing to do, the method is already at its location.
5546}
5547
5548void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5549 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005550 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005551 locations->SetInAt(0, Location::RequiresRegister());
5552 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5553}
5554
5555void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5556 LocationSummary* locations = not_->GetLocations();
5557 Location out = locations->Out();
5558 Location in = locations->InAt(0);
5559 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005560 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005561 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5562 break;
5563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005565 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5566 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5567 break;
5568
5569 default:
5570 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5571 }
5572}
5573
Scott Wakelingc34dba72016-10-03 10:14:44 +01005574void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005576 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005577 locations->SetInAt(0, Location::RequiresRegister());
5578 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5579}
5580
5581void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5582 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5583}
5584
Artem Serov02d37832016-10-25 15:25:33 +01005585void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5586 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005587 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005588 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005589 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005590 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005591 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005593 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005594 case DataType::Type::kInt32:
5595 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005596 locations->SetInAt(0, Location::RequiresRegister());
5597 locations->SetInAt(1, Location::RequiresRegister());
5598 // Output overlaps because it is written before doing the low comparison.
5599 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5600 break;
5601 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005602 case DataType::Type::kFloat32:
5603 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005604 locations->SetInAt(0, Location::RequiresFpuRegister());
5605 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5606 locations->SetOut(Location::RequiresRegister());
5607 break;
5608 }
5609 default:
5610 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5611 }
5612}
5613
5614void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5615 LocationSummary* locations = compare->GetLocations();
5616 vixl32::Register out = OutputRegister(compare);
5617 Location left = locations->InAt(0);
5618 Location right = locations->InAt(1);
5619
5620 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005621 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005622 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005623 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005624 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005626 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005627 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005628 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005629 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005630 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005631 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5632 __ Mov(out, 0);
5633 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5634 less_cond = lt;
5635 break;
5636 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005638 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Andreas Gampe3db70682018-12-26 15:12:03 -08005639 __ B(lt, &less, /* is_far_target= */ false);
5640 __ B(gt, &greater, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005641 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5642 __ Mov(out, 0);
5643 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5644 less_cond = lo;
5645 break;
5646 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005647 case DataType::Type::kFloat32:
5648 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005649 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005650 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005651 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5652 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5653 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5654 break;
5655 }
5656 default:
5657 LOG(FATAL) << "Unexpected compare type " << type;
5658 UNREACHABLE();
5659 }
5660
Andreas Gampe3db70682018-12-26 15:12:03 -08005661 __ B(eq, final_label, /* is_far_target= */ false);
5662 __ B(less_cond, &less, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005663
5664 __ Bind(&greater);
5665 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005666 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005667
5668 __ Bind(&less);
5669 __ Mov(out, -1);
5670
Anton Kirilov6f644202017-02-27 18:29:45 +00005671 if (done.IsReferenced()) {
5672 __ Bind(&done);
5673 }
Artem Serov02d37832016-10-25 15:25:33 +01005674}
5675
5676void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5677 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005678 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005679 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5680 locations->SetInAt(i, Location::Any());
5681 }
5682 locations->SetOut(Location::Any());
5683}
5684
5685void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5686 LOG(FATAL) << "Unreachable";
5687}
5688
5689void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5690 // TODO (ported from quick): revisit ARM barrier kinds.
5691 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5692 switch (kind) {
5693 case MemBarrierKind::kAnyStore:
5694 case MemBarrierKind::kLoadAny:
5695 case MemBarrierKind::kAnyAny: {
5696 flavor = DmbOptions::ISH;
5697 break;
5698 }
5699 case MemBarrierKind::kStoreStore: {
5700 flavor = DmbOptions::ISHST;
5701 break;
5702 }
5703 default:
5704 LOG(FATAL) << "Unexpected memory barrier " << kind;
5705 }
5706 __ Dmb(flavor);
5707}
5708
5709void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5710 uint32_t offset,
5711 vixl32::Register out_lo,
5712 vixl32::Register out_hi) {
5713 UseScratchRegisterScope temps(GetVIXLAssembler());
5714 if (offset != 0) {
5715 vixl32::Register temp = temps.Acquire();
5716 __ Add(temp, addr, offset);
5717 addr = temp;
5718 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005719 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005720}
5721
5722void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5723 uint32_t offset,
5724 vixl32::Register value_lo,
5725 vixl32::Register value_hi,
5726 vixl32::Register temp1,
5727 vixl32::Register temp2,
5728 HInstruction* instruction) {
5729 UseScratchRegisterScope temps(GetVIXLAssembler());
5730 vixl32::Label fail;
5731 if (offset != 0) {
5732 vixl32::Register temp = temps.Acquire();
5733 __ Add(temp, addr, offset);
5734 addr = temp;
5735 }
5736 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005737 {
5738 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005739 ExactAssemblyScope aas(GetVIXLAssembler(),
5740 vixl32::kMaxInstructionSizeInBytes,
5741 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005742 // We need a load followed by store. (The address used in a STREX instruction must
5743 // be the same as the address in the most recently executed LDREX instruction.)
5744 __ ldrexd(temp1, temp2, MemOperand(addr));
5745 codegen_->MaybeRecordImplicitNullCheck(instruction);
5746 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005747 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005748 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005749}
Artem Serov02109dd2016-09-23 17:17:54 +01005750
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005751void LocationsBuilderARMVIXL::HandleFieldSet(
5752 HInstruction* instruction, const FieldInfo& field_info) {
5753 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5754
5755 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005756 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005757 locations->SetInAt(0, Location::RequiresRegister());
5758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005759 DataType::Type field_type = field_info.GetFieldType();
5760 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005761 locations->SetInAt(1, Location::RequiresFpuRegister());
5762 } else {
5763 locations->SetInAt(1, Location::RequiresRegister());
5764 }
5765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005766 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005767 bool generate_volatile = field_info.IsVolatile()
5768 && is_wide
5769 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5770 bool needs_write_barrier =
5771 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5772 // Temporary registers for the write barrier.
5773 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5774 if (needs_write_barrier) {
5775 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5776 locations->AddTemp(Location::RequiresRegister());
5777 } else if (generate_volatile) {
5778 // ARM encoding have some additional constraints for ldrexd/strexd:
5779 // - registers need to be consecutive
5780 // - the first register should be even but not R14.
5781 // We don't test for ARM yet, and the assertion makes sure that we
5782 // revisit this if we ever enable ARM encoding.
5783 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5784
5785 locations->AddTemp(Location::RequiresRegister());
5786 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005787 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005788 // For doubles we need two more registers to copy the value.
5789 locations->AddTemp(LocationFrom(r2));
5790 locations->AddTemp(LocationFrom(r3));
5791 }
5792 }
5793}
5794
5795void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5796 const FieldInfo& field_info,
5797 bool value_can_be_null) {
5798 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5799
5800 LocationSummary* locations = instruction->GetLocations();
5801 vixl32::Register base = InputRegisterAt(instruction, 0);
5802 Location value = locations->InAt(1);
Alex Light3a73ffb2021-01-25 14:11:05 +00005803 std::optional<vixl::aarch32::Label> pred_is_null;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005804
Alex Light3a73ffb2021-01-25 14:11:05 +00005805 bool is_predicated =
5806 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005807 bool is_volatile = field_info.IsVolatile();
5808 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005810 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5811 bool needs_write_barrier =
5812 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5813
Alex Light3a73ffb2021-01-25 14:11:05 +00005814 if (is_predicated) {
5815 pred_is_null.emplace();
5816 __ CompareAndBranchIfZero(base, &*pred_is_null, /* is_far_target= */ false);
5817 }
5818
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005819 if (is_volatile) {
5820 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5821 }
5822
5823 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005824 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005825 case DataType::Type::kUint8:
5826 case DataType::Type::kInt8:
5827 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005828 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005829 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005830 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5831 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005832 StoreOperandType operand_type = GetStoreOperandType(field_type);
5833 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005835 break;
5836 }
5837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 case DataType::Type::kReference: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005839 vixl32::Register value_reg = RegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005840 if (kPoisonHeapReferences && needs_write_barrier) {
5841 // Note that in the case where `value` is a null reference,
5842 // we do not enter this block, as a null reference does not
5843 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005844 DCHECK_EQ(field_type, DataType::Type::kReference);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005845 value_reg = RegisterFrom(locations->GetTemp(0));
5846 __ Mov(value_reg, RegisterFrom(value));
5847 GetAssembler()->PoisonHeapReference(value_reg);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005848 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005849 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5850 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5851 GetAssembler()->StoreToOffset(kStoreWord, value_reg, base, offset);
5852 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005853 break;
5854 }
5855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005856 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005857 if (is_volatile && !atomic_ldrd_strd) {
5858 GenerateWideAtomicStore(base,
5859 offset,
5860 LowRegisterFrom(value),
5861 HighRegisterFrom(value),
5862 RegisterFrom(locations->GetTemp(0)),
5863 RegisterFrom(locations->GetTemp(1)),
5864 instruction);
5865 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005866 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5867 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005868 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5869 codegen_->MaybeRecordImplicitNullCheck(instruction);
5870 }
5871 break;
5872 }
5873
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005875 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5876 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005877 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005878 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005879 break;
5880 }
5881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005883 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005884 if (is_volatile && !atomic_ldrd_strd) {
5885 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5886 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5887
5888 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5889
5890 GenerateWideAtomicStore(base,
5891 offset,
5892 value_reg_lo,
5893 value_reg_hi,
5894 RegisterFrom(locations->GetTemp(2)),
5895 RegisterFrom(locations->GetTemp(3)),
5896 instruction);
5897 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005898 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5899 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005900 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5901 codegen_->MaybeRecordImplicitNullCheck(instruction);
5902 }
5903 break;
5904 }
5905
Aart Bik66c158e2018-01-31 12:55:04 -08005906 case DataType::Type::kUint32:
5907 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005909 LOG(FATAL) << "Unreachable type " << field_type;
5910 UNREACHABLE();
5911 }
5912
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005913 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5914 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5915 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5916 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5917 }
5918
5919 if (is_volatile) {
5920 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5921 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005922
5923 if (is_predicated) {
5924 __ Bind(&*pred_is_null);
5925 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005926}
5927
Artem Serov02d37832016-10-25 15:25:33 +01005928void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5929 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005930 DCHECK(instruction->IsInstanceFieldGet() ||
5931 instruction->IsStaticFieldGet() ||
5932 instruction->IsPredicatedInstanceFieldGet());
Artem Serov02d37832016-10-25 15:25:33 +01005933
5934 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00005936 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Artem Serov02d37832016-10-25 15:25:33 +01005937 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005938 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5939 object_field_get_with_read_barrier
5940 ? LocationSummary::kCallOnSlowPath
5941 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005942 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5943 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5944 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005945 // Input for object receiver.
5946 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
Artem Serov02d37832016-10-25 15:25:33 +01005947
5948 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005950 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5951 // The output overlaps in case of volatile long: we don't want the
5952 // code generated by GenerateWideAtomicLoad to overwrite the
5953 // object's location. Likewise, in the case of an object field get
5954 // with read barriers enabled, we do not want the load to overwrite
5955 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005956 bool overlap =
5957 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005958 object_field_get_with_read_barrier;
5959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005960 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005961 if (is_predicated) {
5962 locations->SetInAt(0, Location::RequiresFpuRegister());
5963 locations->SetOut(Location::SameAsFirstInput());
5964 } else {
5965 locations->SetOut(Location::RequiresFpuRegister());
5966 }
Artem Serov02d37832016-10-25 15:25:33 +01005967 } else {
Alex Light3a73ffb2021-01-25 14:11:05 +00005968 if (is_predicated) {
5969 locations->SetInAt(0, Location::RequiresRegister());
5970 locations->SetOut(Location::SameAsFirstInput());
5971 } else {
5972 locations->SetOut(Location::RequiresRegister(),
5973 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5974 }
Artem Serov02d37832016-10-25 15:25:33 +01005975 }
5976 if (volatile_for_double) {
5977 // ARM encoding have some additional constraints for ldrexd/strexd:
5978 // - registers need to be consecutive
5979 // - the first register should be even but not R14.
5980 // We don't test for ARM yet, and the assertion makes sure that we
5981 // revisit this if we ever enable ARM encoding.
5982 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5983 locations->AddTemp(Location::RequiresRegister());
5984 locations->AddTemp(Location::RequiresRegister());
5985 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005986 // We need a temporary register for the read barrier load in
5987 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5988 // only if the offset is too big.
5989 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005990 locations->AddTemp(Location::RequiresRegister());
5991 }
Artem Serov02d37832016-10-25 15:25:33 +01005992 }
5993}
5994
5995Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005996 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005997 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5998 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5999 return Location::ConstantLocation(input->AsConstant());
6000 } else {
6001 return Location::RequiresFpuRegister();
6002 }
6003}
6004
Artem Serov02109dd2016-09-23 17:17:54 +01006005Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
6006 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006007 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01006008 if (constant->IsConstant() &&
6009 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
6010 return Location::ConstantLocation(constant->AsConstant());
6011 }
6012 return Location::RequiresRegister();
6013}
6014
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00006015static bool CanEncode32BitConstantAsImmediate(
6016 CodeGeneratorARMVIXL* codegen,
6017 uint32_t value,
6018 Opcode opcode,
6019 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
6020 ArmVIXLAssembler* assembler = codegen->GetAssembler();
6021 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01006022 return true;
6023 }
6024 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006025 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01006026 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006027 case AND: neg_opcode = BIC; neg_value = ~value; break;
6028 case ORR: neg_opcode = ORN; neg_value = ~value; break;
6029 case ADD: neg_opcode = SUB; neg_value = -value; break;
6030 case ADC: neg_opcode = SBC; neg_value = ~value; break;
6031 case SUB: neg_opcode = ADD; neg_value = -value; break;
6032 case SBC: neg_opcode = ADC; neg_value = ~value; break;
6033 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01006034 default:
6035 return false;
6036 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006037
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00006038 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00006039 return true;
6040 }
6041
6042 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01006043}
6044
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00006045bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
6046 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
6047 if (DataType::Is64BitType(input_cst->GetType())) {
6048 Opcode high_opcode = opcode;
6049 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
6050 switch (opcode) {
6051 case SUB:
6052 // Flip the operation to an ADD.
6053 value = -value;
6054 opcode = ADD;
6055 FALLTHROUGH_INTENDED;
6056 case ADD:
6057 if (Low32Bits(value) == 0u) {
6058 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
6059 }
6060 high_opcode = ADC;
6061 low_flags_update = vixl32::FlagsUpdate::SetFlags;
6062 break;
6063 default:
6064 break;
6065 }
6066 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
6067 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
6068 } else {
6069 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
6070 }
6071}
6072
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006073void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
6074 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00006075 DCHECK(instruction->IsInstanceFieldGet() ||
6076 instruction->IsStaticFieldGet() ||
6077 instruction->IsPredicatedInstanceFieldGet());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006078
6079 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00006080 uint32_t receiver_input = instruction->IsPredicatedInstanceFieldGet() ? 1 : 0;
6081 vixl32::Register base = InputRegisterAt(instruction, receiver_input);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006082 Location out = locations->Out();
6083 bool is_volatile = field_info.IsVolatile();
6084 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01006085 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6086 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006087 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
6088
Vladimir Marko61b92282017-10-11 13:23:17 +01006089 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006090 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006091 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006092 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006093 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006094 case DataType::Type::kInt16:
6095 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006096 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6097 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01006098 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006099 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006100 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006101 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006102 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006104 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006105 // /* HeapReference<Object> */ out = *(base + offset)
6106 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006107 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006108 // Note that a potential implicit null check is handled in this
6109 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
6110 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006111 instruction, out, base, offset, maybe_temp, /* needs_null_check= */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006112 if (is_volatile) {
6113 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6114 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006115 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006116 {
6117 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6118 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6119 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
6120 codegen_->MaybeRecordImplicitNullCheck(instruction);
6121 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006122 if (is_volatile) {
6123 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6124 }
6125 // If read barriers are enabled, emit read barriers other than
6126 // Baker's using a slow path (and also unpoison the loaded
6127 // reference, if heap poisoning is enabled).
Alex Light3a73ffb2021-01-25 14:11:05 +00006128 codegen_->MaybeGenerateReadBarrierSlow(
6129 instruction, out, out, locations->InAt(receiver_input), offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006130 }
6131 break;
6132 }
6133
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006134 case DataType::Type::kInt64: {
6135 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6136 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006137 if (is_volatile && !atomic_ldrd_strd) {
6138 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6139 } else {
6140 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6141 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006143 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006144 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006145
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006146 case DataType::Type::kFloat32: {
6147 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6148 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006149 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006150 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006151 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006152 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006154 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006155 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6156 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006157 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006158 if (is_volatile && !atomic_ldrd_strd) {
6159 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6160 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6161 GenerateWideAtomicLoad(base, offset, lo, hi);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006162 codegen_->MaybeRecordImplicitNullCheck(instruction);
6163 __ Vmov(out_dreg, lo, hi);
6164 } else {
6165 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006166 codegen_->MaybeRecordImplicitNullCheck(instruction);
6167 }
6168 break;
6169 }
6170
Aart Bik66c158e2018-01-31 12:55:04 -08006171 case DataType::Type::kUint32:
6172 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006173 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006174 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006175 UNREACHABLE();
6176 }
6177
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006178 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006179 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006180 // Memory barriers, in the case of references, are also handled
6181 // in the previous switch statement.
6182 } else {
6183 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6184 }
6185 }
6186}
6187
6188void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6189 HandleFieldSet(instruction, instruction->GetFieldInfo());
6190}
6191
6192void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6193 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6194}
6195
6196void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6197 HandleFieldGet(instruction, instruction->GetFieldInfo());
6198}
6199
Alex Light3a73ffb2021-01-25 14:11:05 +00006200void LocationsBuilderARMVIXL::VisitPredicatedInstanceFieldGet(
6201 HPredicatedInstanceFieldGet* instruction) {
6202 HandleFieldGet(instruction, instruction->GetFieldInfo());
6203}
6204
6205void InstructionCodeGeneratorARMVIXL::VisitPredicatedInstanceFieldGet(
6206 HPredicatedInstanceFieldGet* instruction) {
6207 vixl::aarch32::Label finish;
6208 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 1), &finish, false);
6209 HandleFieldGet(instruction, instruction->GetFieldInfo());
6210 __ Bind(&finish);
6211}
6212
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006213void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6214 HandleFieldGet(instruction, instruction->GetFieldInfo());
6215}
6216
6217void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6218 HandleFieldGet(instruction, instruction->GetFieldInfo());
6219}
6220
6221void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6222 HandleFieldGet(instruction, instruction->GetFieldInfo());
6223}
6224
Scott Wakelingc34dba72016-10-03 10:14:44 +01006225void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6226 HandleFieldSet(instruction, instruction->GetFieldInfo());
6227}
6228
6229void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6230 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6231}
6232
Vladimir Marko552a1342017-10-31 10:56:47 +00006233void LocationsBuilderARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6234 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(r0));
6235}
6236
6237void InstructionCodeGeneratorARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6238 __ Mov(r0, instruction->GetFormat()->GetValue());
6239 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6240}
6241
Artem Serovcfbe9132016-10-14 15:58:56 +01006242void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6243 HUnresolvedInstanceFieldGet* instruction) {
6244 FieldAccessCallingConventionARMVIXL calling_convention;
6245 codegen_->CreateUnresolvedFieldLocationSummary(
6246 instruction, instruction->GetFieldType(), calling_convention);
6247}
6248
6249void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6250 HUnresolvedInstanceFieldGet* instruction) {
6251 FieldAccessCallingConventionARMVIXL calling_convention;
6252 codegen_->GenerateUnresolvedFieldAccess(instruction,
6253 instruction->GetFieldType(),
6254 instruction->GetFieldIndex(),
6255 instruction->GetDexPc(),
6256 calling_convention);
6257}
6258
6259void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6260 HUnresolvedInstanceFieldSet* instruction) {
6261 FieldAccessCallingConventionARMVIXL calling_convention;
6262 codegen_->CreateUnresolvedFieldLocationSummary(
6263 instruction, instruction->GetFieldType(), calling_convention);
6264}
6265
6266void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6267 HUnresolvedInstanceFieldSet* instruction) {
6268 FieldAccessCallingConventionARMVIXL calling_convention;
6269 codegen_->GenerateUnresolvedFieldAccess(instruction,
6270 instruction->GetFieldType(),
6271 instruction->GetFieldIndex(),
6272 instruction->GetDexPc(),
6273 calling_convention);
6274}
6275
6276void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6277 HUnresolvedStaticFieldGet* instruction) {
6278 FieldAccessCallingConventionARMVIXL calling_convention;
6279 codegen_->CreateUnresolvedFieldLocationSummary(
6280 instruction, instruction->GetFieldType(), calling_convention);
6281}
6282
6283void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6284 HUnresolvedStaticFieldGet* instruction) {
6285 FieldAccessCallingConventionARMVIXL calling_convention;
6286 codegen_->GenerateUnresolvedFieldAccess(instruction,
6287 instruction->GetFieldType(),
6288 instruction->GetFieldIndex(),
6289 instruction->GetDexPc(),
6290 calling_convention);
6291}
6292
6293void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6294 HUnresolvedStaticFieldSet* instruction) {
6295 FieldAccessCallingConventionARMVIXL calling_convention;
6296 codegen_->CreateUnresolvedFieldLocationSummary(
6297 instruction, instruction->GetFieldType(), calling_convention);
6298}
6299
6300void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6301 HUnresolvedStaticFieldSet* instruction) {
6302 FieldAccessCallingConventionARMVIXL calling_convention;
6303 codegen_->GenerateUnresolvedFieldAccess(instruction,
6304 instruction->GetFieldType(),
6305 instruction->GetFieldIndex(),
6306 instruction->GetDexPc(),
6307 calling_convention);
6308}
6309
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006310void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006311 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006312 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006313}
6314
6315void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6316 if (CanMoveNullCheckToUser(instruction)) {
6317 return;
6318 }
6319
6320 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006321 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006322 ExactAssemblyScope aas(GetVIXLAssembler(),
6323 vixl32::kMaxInstructionSizeInBytes,
6324 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006325 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6326 RecordPcInfo(instruction, instruction->GetDexPc());
6327}
6328
6329void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6330 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006331 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006332 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006333 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006334}
6335
6336void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6337 codegen_->GenerateNullCheck(instruction);
6338}
6339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006341 Location out_loc,
6342 vixl32::Register base,
6343 vixl32::Register reg_index,
6344 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006346 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6347
6348 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006349 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006350 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006351 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6352 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006353 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006354 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6355 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006356 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006357 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6358 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006359 case DataType::Type::kInt16:
6360 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6361 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 case DataType::Type::kReference:
6363 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006364 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6365 break;
6366 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006367 case DataType::Type::kInt64:
6368 case DataType::Type::kFloat32:
6369 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006370 default:
6371 LOG(FATAL) << "Unreachable type " << type;
6372 UNREACHABLE();
6373 }
6374}
6375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006376void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006377 Location loc,
6378 vixl32::Register base,
6379 vixl32::Register reg_index,
6380 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006381 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006382 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6383
6384 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006385 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006386 case DataType::Type::kUint8:
6387 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006388 __ Strb(cond, RegisterFrom(loc), mem_address);
6389 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006391 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006392 __ Strh(cond, RegisterFrom(loc), mem_address);
6393 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006394 case DataType::Type::kReference:
6395 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006396 __ Str(cond, RegisterFrom(loc), mem_address);
6397 break;
6398 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 case DataType::Type::kInt64:
6400 case DataType::Type::kFloat32:
6401 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006402 default:
6403 LOG(FATAL) << "Unreachable type " << type;
6404 UNREACHABLE();
6405 }
6406}
6407
6408void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6409 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006410 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006411 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006412 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6413 object_array_get_with_read_barrier
6414 ? LocationSummary::kCallOnSlowPath
6415 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006416 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006417 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006418 }
6419 locations->SetInAt(0, Location::RequiresRegister());
6420 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006421 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006422 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6423 } else {
6424 // The output overlaps in the case of an object array get with
6425 // read barriers enabled: we do not want the move to overwrite the
6426 // array's location, as we need it to emit the read barrier.
6427 locations->SetOut(
6428 Location::RequiresRegister(),
6429 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6430 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006431 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006432 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006433 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01006434 // We need a temporary register for the read barrier load in
6435 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
6436 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006437 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6438 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006440 if (offset >= kReferenceLoadMinFarOffset) {
6441 locations->AddTemp(Location::RequiresRegister());
6442 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006443 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006444 // We need a non-scratch temporary for the array data pointer in
6445 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006446 locations->AddTemp(Location::RequiresRegister());
6447 }
6448 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6449 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006450 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006451 }
6452}
6453
6454void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006455 LocationSummary* locations = instruction->GetLocations();
6456 Location obj_loc = locations->InAt(0);
6457 vixl32::Register obj = InputRegisterAt(instruction, 0);
6458 Location index = locations->InAt(1);
6459 Location out_loc = locations->Out();
6460 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006461 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006462 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6463 instruction->IsStringCharAt();
6464 HInstruction* array_instr = instruction->GetArray();
6465 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006466
6467 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006468 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006469 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006470 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006471 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006472 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006473 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006474 vixl32::Register length;
6475 if (maybe_compressed_char_at) {
6476 length = RegisterFrom(locations->GetTemp(0));
6477 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006478 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6479 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006480 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6481 codegen_->MaybeRecordImplicitNullCheck(instruction);
6482 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006483 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006484 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006485 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006486 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006487 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006488 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6489 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6490 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006491 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006492 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6493 RegisterFrom(out_loc),
6494 obj,
6495 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006496 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006497 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006498 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006499 RegisterFrom(out_loc),
6500 obj,
6501 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006502 if (done.IsReferenced()) {
6503 __ Bind(&done);
6504 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006505 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006506 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006507
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006508 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6509 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 LoadOperandType load_type = GetLoadOperandType(type);
6511 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006512 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006513 }
6514 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006515 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006516 vixl32::Register temp = temps.Acquire();
6517
6518 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006519 // We do not need to compute the intermediate address from the array: the
6520 // input instruction has done it already. See the comment in
6521 // `TryExtractArrayAccessAddress()`.
6522 if (kIsDebugBuild) {
6523 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006524 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006525 }
6526 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006527 } else {
6528 __ Add(temp, obj, data_offset);
6529 }
6530 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006531 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006532 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006533 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6534 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6535 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006536 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006537 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006538 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006539 __ Bind(&uncompressed_load);
6540 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006541 if (done.IsReferenced()) {
6542 __ Bind(&done);
6543 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006544 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006545 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6546 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006547 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006548 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006549 }
6550 }
6551 break;
6552 }
6553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006554 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006555 // The read barrier instrumentation of object ArrayGet
6556 // instructions does not support the HIntermediateAddress
6557 // instruction.
6558 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6559
Scott Wakelingc34dba72016-10-03 10:14:44 +01006560 static_assert(
6561 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6562 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6563 // /* HeapReference<Object> */ out =
6564 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6565 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006566 // Note that a potential implicit null check is handled in this
6567 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006568 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6569 if (index.IsConstant()) {
6570 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006571 Location maybe_temp =
6572 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006573 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006574 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6575 out_loc,
6576 obj,
6577 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006578 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006579 /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006580 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006581 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006582 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006583 out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006584 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006585 } else {
6586 vixl32::Register out = OutputRegister(instruction);
6587 if (index.IsConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006588 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6589 {
6590 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6591 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6592 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
6593 codegen_->MaybeRecordImplicitNullCheck(instruction);
6594 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006595 // If read barriers are enabled, emit read barriers other than
6596 // Baker's using a slow path (and also unpoison the loaded
6597 // reference, if heap poisoning is enabled).
6598 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6599 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006600 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006601 vixl32::Register temp = temps.Acquire();
6602
6603 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006604 // We do not need to compute the intermediate address from the array: the
6605 // input instruction has done it already. See the comment in
6606 // `TryExtractArrayAccessAddress()`.
6607 if (kIsDebugBuild) {
6608 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006609 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006610 }
6611 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006612 } else {
6613 __ Add(temp, obj, data_offset);
6614 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006615 {
6616 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6617 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6618 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6619 temps.Close();
6620 codegen_->MaybeRecordImplicitNullCheck(instruction);
6621 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006622 // If read barriers are enabled, emit read barriers other than
6623 // Baker's using a slow path (and also unpoison the loaded
6624 // reference, if heap poisoning is enabled).
6625 codegen_->MaybeGenerateReadBarrierSlow(
6626 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6627 }
6628 }
6629 break;
6630 }
6631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006632 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006633 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6634 // As two macro instructions can be emitted the max size is doubled.
6635 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006636 if (index.IsConstant()) {
6637 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006638 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006639 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6640 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006641 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006642 vixl32::Register temp = temps.Acquire();
6643 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6644 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6645 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006646 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006647 break;
6648 }
6649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006651 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6652 // As two macro instructions can be emitted the max size is doubled.
6653 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006654 vixl32::SRegister out = SRegisterFrom(out_loc);
6655 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006656 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006657 GetAssembler()->LoadSFromOffset(out, obj, offset);
6658 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006659 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006660 vixl32::Register temp = temps.Acquire();
6661 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6662 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6663 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006664 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006665 break;
6666 }
6667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006668 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006669 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6670 // As two macro instructions can be emitted the max size is doubled.
6671 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006672 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006673 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006674 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6675 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006676 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006677 vixl32::Register temp = temps.Acquire();
6678 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6679 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6680 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006681 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006682 break;
6683 }
6684
Aart Bik66c158e2018-01-31 12:55:04 -08006685 case DataType::Type::kUint32:
6686 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006687 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006688 LOG(FATAL) << "Unreachable type " << type;
6689 UNREACHABLE();
6690 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006691}
6692
6693void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006694 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006695
6696 bool needs_write_barrier =
6697 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006698 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006699
Vladimir Markoca6fff82017-10-03 14:49:14 +01006700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006701 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006702 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006703
6704 locations->SetInAt(0, Location::RequiresRegister());
6705 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006706 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006707 locations->SetInAt(2, Location::RequiresFpuRegister());
6708 } else {
6709 locations->SetInAt(2, Location::RequiresRegister());
6710 }
6711 if (needs_write_barrier) {
6712 // Temporary registers for the write barrier.
6713 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6714 locations->AddTemp(Location::RequiresRegister());
6715 }
6716}
6717
6718void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006719 LocationSummary* locations = instruction->GetLocations();
6720 vixl32::Register array = InputRegisterAt(instruction, 0);
6721 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006722 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006723 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006724 bool needs_write_barrier =
6725 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6726 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006728 Location value_loc = locations->InAt(2);
6729 HInstruction* array_instr = instruction->GetArray();
6730 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006731
6732 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006733 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006734 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006735 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006736 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006737 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006738 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006739 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006740 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006741 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006742 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006743 StoreOperandType store_type = GetStoreOperandType(value_type);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006744 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6745 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006746 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006748 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006749 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006750 vixl32::Register temp = temps.Acquire();
6751
6752 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006753 // We do not need to compute the intermediate address from the array: the
6754 // input instruction has done it already. See the comment in
6755 // `TryExtractArrayAccessAddress()`.
6756 if (kIsDebugBuild) {
6757 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006758 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006759 }
6760 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006761 } else {
6762 __ Add(temp, array, data_offset);
6763 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006764 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6765 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006766 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006768 }
6769 break;
6770 }
6771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006772 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006773 vixl32::Register value = RegisterFrom(value_loc);
6774 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6775 // See the comment in instruction_simplifier_shared.cc.
6776 DCHECK(!has_intermediate_address);
6777
6778 if (instruction->InputAt(2)->IsNullConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006779 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6780 // As two macro instructions can be emitted the max size is doubled.
6781 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006782 // Just setting null.
6783 if (index.IsConstant()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006784 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006785 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6786 } else {
6787 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006788 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006789 vixl32::Register temp = temps.Acquire();
6790 __ Add(temp, array, data_offset);
6791 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6792 }
6793 codegen_->MaybeRecordImplicitNullCheck(instruction);
6794 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006795 DCHECK(!needs_type_check);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006796 break;
6797 }
6798
6799 DCHECK(needs_write_barrier);
6800 Location temp1_loc = locations->GetTemp(0);
6801 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6802 Location temp2_loc = locations->GetTemp(1);
6803 vixl32::Register temp2 = RegisterFrom(temp2_loc);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006804
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006805 bool can_value_be_null = instruction->GetValueCanBeNull();
6806 vixl32::Label do_store;
6807 if (can_value_be_null) {
6808 __ CompareAndBranchIfZero(value, &do_store, /* is_far_target= */ false);
6809 }
6810
6811 SlowPathCodeARMVIXL* slow_path = nullptr;
6812 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006813 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006814 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006815
6816 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6817 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6818 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006819
6820 // Note that when read barriers are enabled, the type checks
6821 // are performed without read barriers. This is fine, even in
6822 // the case where a class object is in the from-space after
6823 // the flip, as a comparison involving such a type would not
6824 // produce a false positive; it may of course produce a false
6825 // negative, in which case we would take the ArraySet slow
6826 // path.
6827
Alexandre Rames374ddf32016-11-04 10:40:49 +00006828 {
6829 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006830 ExactAssemblyScope aas(GetVIXLAssembler(),
6831 vixl32::kMaxInstructionSizeInBytes,
6832 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006833 // /* HeapReference<Class> */ temp1 = array->klass_
6834 __ ldr(temp1, MemOperand(array, class_offset));
6835 codegen_->MaybeRecordImplicitNullCheck(instruction);
6836 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006837 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6838
6839 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6840 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6841 // /* HeapReference<Class> */ temp2 = value->klass_
6842 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6843 // If heap poisoning is enabled, no need to unpoison `temp1`
6844 // nor `temp2`, as we are comparing two poisoned references.
6845 __ Cmp(temp1, temp2);
6846
6847 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006848 vixl32::Label do_put;
6849 __ B(eq, &do_put, /* is_far_target= */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006850 // If heap poisoning is enabled, the `temp1` reference has
6851 // not been unpoisoned yet; unpoison it now.
6852 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6853
6854 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6855 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6856 // If heap poisoning is enabled, no need to unpoison
6857 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006858 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006859 __ Bind(&do_put);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006860 } else {
6861 __ B(ne, slow_path->GetEntryLabel());
6862 }
6863 }
6864
Vladimir Markoac3fcff2020-11-17 12:17:58 +00006865 codegen_->MarkGCCard(temp1, temp2, array, value, /* value_can_be_null= */ false);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006866
6867 if (can_value_be_null) {
6868 DCHECK(do_store.IsReferenced());
6869 __ Bind(&do_store);
6870 }
6871
Scott Wakelingc34dba72016-10-03 10:14:44 +01006872 vixl32::Register source = value;
6873 if (kPoisonHeapReferences) {
6874 // Note that in the case where `value` is a null reference,
6875 // we do not enter this block, as a null reference does not
6876 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006877 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006878 __ Mov(temp1, value);
6879 GetAssembler()->PoisonHeapReference(temp1);
6880 source = temp1;
6881 }
6882
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006883 {
6884 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6885 // As two macro instructions can be emitted the max size is doubled.
6886 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
6887 if (index.IsConstant()) {
6888 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6889 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6890 } else {
6891 DCHECK(index.IsRegister()) << index;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006892
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006893 UseScratchRegisterScope temps(GetVIXLAssembler());
6894 vixl32::Register temp = temps.Acquire();
6895 __ Add(temp, array, data_offset);
6896 codegen_->StoreToShiftedRegOffset(value_type,
6897 LocationFrom(source),
6898 temp,
6899 RegisterFrom(index));
6900 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006901
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006902 if (can_value_be_null || !needs_type_check) {
6903 codegen_->MaybeRecordImplicitNullCheck(instruction);
6904 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006905 }
6906
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006907 if (slow_path != nullptr) {
6908 __ Bind(slow_path->GetExitLabel());
6909 }
6910
Scott Wakelingc34dba72016-10-03 10:14:44 +01006911 break;
6912 }
6913
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006914 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006915 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6916 // As two macro instructions can be emitted the max size is doubled.
6917 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006918 Location value = locations->InAt(2);
6919 if (index.IsConstant()) {
6920 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006921 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006922 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6923 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006924 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006925 vixl32::Register temp = temps.Acquire();
6926 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6927 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6928 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006929 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006930 break;
6931 }
6932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006933 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006934 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6935 // As two macro instructions can be emitted the max size is doubled.
6936 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006937 Location value = locations->InAt(2);
6938 DCHECK(value.IsFpuRegister());
6939 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006940 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006941 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6942 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006943 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006944 vixl32::Register temp = temps.Acquire();
6945 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6946 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6947 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006948 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006949 break;
6950 }
6951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006952 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006953 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6954 // As two macro instructions can be emitted the max size is doubled.
6955 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006956 Location value = locations->InAt(2);
6957 DCHECK(value.IsFpuRegisterPair());
6958 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006959 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006960 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6961 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006962 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006963 vixl32::Register temp = temps.Acquire();
6964 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6965 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6966 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006967 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006968 break;
6969 }
6970
Aart Bik66c158e2018-01-31 12:55:04 -08006971 case DataType::Type::kUint32:
6972 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006973 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006974 LOG(FATAL) << "Unreachable type " << value_type;
6975 UNREACHABLE();
6976 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006977}
6978
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006979void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6980 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006981 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006982 locations->SetInAt(0, Location::RequiresRegister());
6983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6984}
6985
6986void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6987 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6988 vixl32::Register obj = InputRegisterAt(instruction, 0);
6989 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006990 {
Artem Serov0fb37192016-12-06 18:13:40 +00006991 ExactAssemblyScope aas(GetVIXLAssembler(),
6992 vixl32::kMaxInstructionSizeInBytes,
6993 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006994 __ ldr(out, MemOperand(obj, offset));
6995 codegen_->MaybeRecordImplicitNullCheck(instruction);
6996 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006997 // Mask out compression flag from String's array length.
6998 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006999 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007000 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007001}
7002
Artem Serov2bbc9532016-10-21 11:51:50 +01007003void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01007004 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007005 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01007006
7007 locations->SetInAt(0, Location::RequiresRegister());
7008 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
7009 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7010}
7011
7012void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
7013 vixl32::Register out = OutputRegister(instruction);
7014 vixl32::Register first = InputRegisterAt(instruction, 0);
7015 Location second = instruction->GetLocations()->InAt(1);
7016
Artem Serov2bbc9532016-10-21 11:51:50 +01007017 if (second.IsRegister()) {
7018 __ Add(out, first, RegisterFrom(second));
7019 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00007020 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01007021 }
7022}
7023
Artem Serove1811ed2017-04-27 16:50:47 +01007024void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
7025 HIntermediateAddressIndex* instruction) {
7026 LOG(FATAL) << "Unreachable " << instruction->GetId();
7027}
7028
7029void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
7030 HIntermediateAddressIndex* instruction) {
7031 LOG(FATAL) << "Unreachable " << instruction->GetId();
7032}
7033
Scott Wakelingc34dba72016-10-03 10:14:44 +01007034void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
7035 RegisterSet caller_saves = RegisterSet::Empty();
7036 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7037 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
7038 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
7039 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00007040
7041 HInstruction* index = instruction->InputAt(0);
7042 HInstruction* length = instruction->InputAt(1);
7043 // If both index and length are constants we can statically check the bounds. But if at least one
7044 // of them is not encodable ArmEncodableConstantOrRegister will create
7045 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
7046 // locations.
7047 bool both_const = index->IsConstant() && length->IsConstant();
7048 locations->SetInAt(0, both_const
7049 ? Location::ConstantLocation(index->AsConstant())
7050 : ArmEncodableConstantOrRegister(index, CMP));
7051 locations->SetInAt(1, both_const
7052 ? Location::ConstantLocation(length->AsConstant())
7053 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01007054}
7055
7056void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00007057 LocationSummary* locations = instruction->GetLocations();
7058 Location index_loc = locations->InAt(0);
7059 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01007060
Artem Serov2dd053d2017-03-08 14:54:06 +00007061 if (length_loc.IsConstant()) {
7062 int32_t length = Int32ConstantFrom(length_loc);
7063 if (index_loc.IsConstant()) {
7064 // BCE will remove the bounds check if we are guaranteed to pass.
7065 int32_t index = Int32ConstantFrom(index_loc);
7066 if (index < 0 || index >= length) {
7067 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007068 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007069 codegen_->AddSlowPath(slow_path);
7070 __ B(slow_path->GetEntryLabel());
7071 } else {
7072 // Some optimization after BCE may have generated this, and we should not
7073 // generate a bounds check if it is a valid range.
7074 }
7075 return;
7076 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01007077
Artem Serov2dd053d2017-03-08 14:54:06 +00007078 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007079 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007080 __ Cmp(RegisterFrom(index_loc), length);
7081 codegen_->AddSlowPath(slow_path);
7082 __ B(hs, slow_path->GetEntryLabel());
7083 } else {
7084 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007085 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00007086 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
7087 codegen_->AddSlowPath(slow_path);
7088 __ B(ls, slow_path->GetEntryLabel());
7089 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01007090}
7091
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007092void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
7093 vixl32::Register card,
7094 vixl32::Register object,
7095 vixl32::Register value,
Vladimir Markoac3fcff2020-11-17 12:17:58 +00007096 bool value_can_be_null) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007097 vixl32::Label is_null;
Vladimir Markoac3fcff2020-11-17 12:17:58 +00007098 if (value_can_be_null) {
7099 __ CompareAndBranchIfZero(value, &is_null, /* is_far_target=*/ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007100 }
Roland Levillainc73f0522018-08-14 15:16:50 +01007101 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007102 GetAssembler()->LoadFromOffset(
7103 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01007104 // Calculate the offset (in the card table) of the card corresponding to
7105 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00007106 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01007107 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
7108 // `object`'s card.
7109 //
7110 // Register `card` contains the address of the card table. Note that the card
7111 // table's base is biased during its creation so that it always starts at an
7112 // address whose least-significant byte is equal to `kCardDirty` (see
7113 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
7114 // below writes the `kCardDirty` (byte) value into the `object`'s card
7115 // (located at `card + object >> kCardShift`).
7116 //
7117 // This dual use of the value in register `card` (1. to calculate the location
7118 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
7119 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007120 __ Strb(card, MemOperand(card, temp));
Vladimir Markoac3fcff2020-11-17 12:17:58 +00007121 if (value_can_be_null) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007122 __ Bind(&is_null);
7123 }
7124}
7125
Scott Wakelingfe885462016-09-22 10:24:38 +01007126void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7127 LOG(FATAL) << "Unreachable";
7128}
7129
7130void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01007131 if (instruction->GetNext()->IsSuspendCheck() &&
7132 instruction->GetBlock()->GetLoopInformation() != nullptr) {
7133 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
7134 // The back edge will generate the suspend check.
7135 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
7136 }
7137
Scott Wakelingfe885462016-09-22 10:24:38 +01007138 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7139}
7140
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007141void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007142 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7143 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00007144 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007145}
7146
7147void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
7148 HBasicBlock* block = instruction->GetBlock();
7149 if (block->GetLoopInformation() != nullptr) {
7150 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7151 // The back edge will generate the suspend check.
7152 return;
7153 }
7154 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7155 // The goto will generate the suspend check.
7156 return;
7157 }
7158 GenerateSuspendCheck(instruction, nullptr);
Andra Danciua0130e82020-07-23 12:34:56 +00007159 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007160}
7161
7162void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7163 HBasicBlock* successor) {
7164 SuspendCheckSlowPathARMVIXL* slow_path =
7165 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7166 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007167 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007168 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007169 instruction->SetSlowPath(slow_path);
7170 codegen_->AddSlowPath(slow_path);
7171 if (successor != nullptr) {
7172 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007173 }
7174 } else {
7175 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7176 }
7177
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007178 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007179 vixl32::Register temp = temps.Acquire();
7180 GetAssembler()->LoadFromOffset(
7181 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7182 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007183 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007184 __ Bind(slow_path->GetReturnLabel());
7185 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007186 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007187 __ B(slow_path->GetEntryLabel());
7188 }
7189}
7190
Scott Wakelingfe885462016-09-22 10:24:38 +01007191ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7192 return codegen_->GetAssembler();
7193}
7194
7195void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007196 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007197 MoveOperands* move = moves_[index];
7198 Location source = move->GetSource();
7199 Location destination = move->GetDestination();
7200
7201 if (source.IsRegister()) {
7202 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007203 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007204 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007205 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007206 } else {
7207 DCHECK(destination.IsStackSlot());
7208 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007209 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007210 sp,
7211 destination.GetStackIndex());
7212 }
7213 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007214 if (destination.IsRegister()) {
7215 GetAssembler()->LoadFromOffset(kLoadWord,
7216 RegisterFrom(destination),
7217 sp,
7218 source.GetStackIndex());
7219 } else if (destination.IsFpuRegister()) {
7220 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7221 } else {
7222 DCHECK(destination.IsStackSlot());
7223 vixl32::Register temp = temps.Acquire();
7224 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7225 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7226 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007227 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007228 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007229 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007230 } else if (destination.IsFpuRegister()) {
7231 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7232 } else {
7233 DCHECK(destination.IsStackSlot());
7234 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7235 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007236 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007237 if (destination.IsDoubleStackSlot()) {
7238 vixl32::DRegister temp = temps.AcquireD();
7239 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7240 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7241 } else if (destination.IsRegisterPair()) {
7242 DCHECK(ExpectedPairLayout(destination));
7243 GetAssembler()->LoadFromOffset(
7244 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7245 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007246 DCHECK(destination.IsFpuRegisterPair()) << destination;
7247 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007248 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007249 } else if (source.IsRegisterPair()) {
7250 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007251 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7252 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007253 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007254 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007255 } else {
7256 DCHECK(destination.IsDoubleStackSlot()) << destination;
7257 DCHECK(ExpectedPairLayout(source));
7258 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007259 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007260 sp,
7261 destination.GetStackIndex());
7262 }
7263 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007264 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007265 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007266 } else if (destination.IsFpuRegisterPair()) {
7267 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7268 } else {
7269 DCHECK(destination.IsDoubleStackSlot()) << destination;
7270 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7271 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007272 } else {
7273 DCHECK(source.IsConstant()) << source;
7274 HConstant* constant = source.GetConstant();
7275 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7276 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7277 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007278 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007279 } else {
7280 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007281 vixl32::Register temp = temps.Acquire();
7282 __ Mov(temp, value);
7283 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7284 }
7285 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007286 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007287 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007288 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7289 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007290 } else {
7291 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007292 vixl32::Register temp = temps.Acquire();
7293 __ Mov(temp, Low32Bits(value));
7294 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7295 __ Mov(temp, High32Bits(value));
7296 GetAssembler()->StoreToOffset(kStoreWord,
7297 temp,
7298 sp,
7299 destination.GetHighStackIndex(kArmWordSize));
7300 }
7301 } else if (constant->IsDoubleConstant()) {
7302 double value = constant->AsDoubleConstant()->GetValue();
7303 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007304 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007305 } else {
7306 DCHECK(destination.IsDoubleStackSlot()) << destination;
7307 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007308 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007309 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007310 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007311 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007312 GetAssembler()->StoreToOffset(kStoreWord,
7313 temp,
7314 sp,
7315 destination.GetHighStackIndex(kArmWordSize));
7316 }
7317 } else {
7318 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7319 float value = constant->AsFloatConstant()->GetValue();
7320 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007321 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007322 } else {
7323 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007324 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007325 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007326 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7327 }
7328 }
7329 }
7330}
7331
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007332void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7333 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7334 vixl32::Register temp = temps.Acquire();
7335 __ Mov(temp, reg);
7336 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7337 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007338}
7339
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007340void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7341 // TODO(VIXL32): Double check the performance of this implementation.
7342 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007343 vixl32::Register temp1 = temps.Acquire();
7344 ScratchRegisterScope ensure_scratch(
7345 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7346 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007347
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007348 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7349 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7350 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7351 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7352 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007353}
7354
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007355void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7356 MoveOperands* move = moves_[index];
7357 Location source = move->GetSource();
7358 Location destination = move->GetDestination();
7359 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7360
7361 if (source.IsRegister() && destination.IsRegister()) {
7362 vixl32::Register temp = temps.Acquire();
7363 DCHECK(!RegisterFrom(source).Is(temp));
7364 DCHECK(!RegisterFrom(destination).Is(temp));
7365 __ Mov(temp, RegisterFrom(destination));
7366 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7367 __ Mov(RegisterFrom(source), temp);
7368 } else if (source.IsRegister() && destination.IsStackSlot()) {
7369 Exchange(RegisterFrom(source), destination.GetStackIndex());
7370 } else if (source.IsStackSlot() && destination.IsRegister()) {
7371 Exchange(RegisterFrom(destination), source.GetStackIndex());
7372 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007373 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007374 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007375 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007376 __ Vmov(temp, SRegisterFrom(source));
7377 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7378 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007379 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7380 vixl32::DRegister temp = temps.AcquireD();
7381 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7382 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7383 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7384 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7385 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7386 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7387 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7388 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7389 vixl32::DRegister temp = temps.AcquireD();
7390 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7391 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7392 GetAssembler()->StoreDToOffset(temp, sp, mem);
7393 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007394 vixl32::DRegister first = DRegisterFrom(source);
7395 vixl32::DRegister second = DRegisterFrom(destination);
7396 vixl32::DRegister temp = temps.AcquireD();
7397 __ Vmov(temp, first);
7398 __ Vmov(first, second);
7399 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007400 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007401 vixl32::DRegister reg = source.IsFpuRegisterPair()
7402 ? DRegisterFrom(source)
7403 : DRegisterFrom(destination);
7404 int mem = source.IsFpuRegisterPair()
7405 ? destination.GetStackIndex()
7406 : source.GetStackIndex();
7407 vixl32::DRegister temp = temps.AcquireD();
7408 __ Vmov(temp, reg);
7409 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7410 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007411 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007412 vixl32::SRegister reg = source.IsFpuRegister()
7413 ? SRegisterFrom(source)
7414 : SRegisterFrom(destination);
7415 int mem = source.IsFpuRegister()
7416 ? destination.GetStackIndex()
7417 : source.GetStackIndex();
7418 vixl32::Register temp = temps.Acquire();
7419 __ Vmov(temp, reg);
7420 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7421 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007422 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7423 vixl32::DRegister temp1 = temps.AcquireD();
7424 vixl32::DRegister temp2 = temps.AcquireD();
7425 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7426 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7427 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7428 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7429 } else {
7430 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7431 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007432}
7433
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007434void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7435 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007436}
7437
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007438void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7439 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007440}
7441
Artem Serov02d37832016-10-25 15:25:33 +01007442HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007443 HLoadClass::LoadKind desired_class_load_kind) {
7444 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007445 case HLoadClass::LoadKind::kInvalid:
7446 LOG(FATAL) << "UNREACHABLE";
7447 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007448 case HLoadClass::LoadKind::kReferrersClass:
7449 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007450 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007451 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007452 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007453 case HLoadClass::LoadKind::kBssEntryPublic:
7454 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007455 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007456 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007457 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007458 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007459 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007460 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007461 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007462 break;
7463 }
7464 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007465}
7466
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007467void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007468 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007469 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007470 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007471 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007472 cls,
7473 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007474 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007475 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007476 return;
7477 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007478 DCHECK_EQ(cls->NeedsAccessCheck(),
7479 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7480 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelingfe885462016-09-22 10:24:38 +01007481
Artem Serovd4cc5b22016-11-04 11:19:09 +00007482 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7483 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007484 ? LocationSummary::kCallOnSlowPath
7485 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007486 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007487 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007488 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007489 }
7490
Vladimir Marko41559982017-01-06 14:04:23 +00007491 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007492 locations->SetInAt(0, Location::RequiresRegister());
7493 }
7494 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007495 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7496 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7497 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007498 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007499 } else {
7500 // For non-Baker read barrier we have a temp-clobbering call.
7501 }
7502 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007503}
7504
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007505// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7506// move.
7507void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007508 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007509 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007510 codegen_->GenerateLoadClassRuntimeCall(cls);
Andra Danciua0130e82020-07-23 12:34:56 +00007511 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007512 return;
7513 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007514 DCHECK_EQ(cls->NeedsAccessCheck(),
7515 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7516 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007517
Vladimir Marko41559982017-01-06 14:04:23 +00007518 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007519 Location out_loc = locations->Out();
7520 vixl32::Register out = OutputRegister(cls);
7521
Artem Serovd4cc5b22016-11-04 11:19:09 +00007522 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7523 ? kWithoutReadBarrier
7524 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007525 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007526 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007527 case HLoadClass::LoadKind::kReferrersClass: {
7528 DCHECK(!cls->CanCallRuntime());
7529 DCHECK(!cls->MustGenerateClinitCheck());
7530 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7531 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007532 codegen_->GenerateGcRootFieldLoad(cls,
7533 out_loc,
7534 current_method,
7535 ArtMethod::DeclaringClassOffset().Int32Value(),
7536 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007537 break;
7538 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007539 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007540 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7541 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007542 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7543 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007544 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007545 codegen_->EmitMovwMovtPlaceholder(labels, out);
7546 break;
7547 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007548 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007549 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7550 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markode91ca92020-10-27 13:41:40 +00007551 codegen_->NewBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007552 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007553 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007554 break;
7555 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007556 case HLoadClass::LoadKind::kBssEntry:
7557 case HLoadClass::LoadKind::kBssEntryPublic:
7558 case HLoadClass::LoadKind::kBssEntryPackage: {
7559 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels = codegen_->NewTypeBssEntryPatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00007560 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007561 // All aligned loads are implicitly atomic consume operations on ARM.
Andreas Gampe3db70682018-12-26 15:12:03 -08007562 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007563 generate_null_check = true;
7564 break;
7565 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007566 case HLoadClass::LoadKind::kJitBootImageAddress: {
7567 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7568 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7569 DCHECK_NE(address, 0u);
7570 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7571 break;
7572 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007573 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007574 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7575 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007576 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007577 // /* GcRoot<mirror::Class> */ out = *out
Andreas Gampe3db70682018-12-26 15:12:03 -08007578 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007579 break;
7580 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007581 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007582 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007583 LOG(FATAL) << "UNREACHABLE";
7584 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007585 }
7586
7587 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7588 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007589 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007590 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007591 codegen_->AddSlowPath(slow_path);
7592 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007593 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007594 }
7595 if (cls->MustGenerateClinitCheck()) {
7596 GenerateClassInitializationCheck(slow_path, out);
7597 } else {
7598 __ Bind(slow_path->GetExitLabel());
7599 }
Andra Danciua0130e82020-07-23 12:34:56 +00007600 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 16);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007601 }
7602}
7603
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007604void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7605 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7606 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7607 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7608}
7609
7610void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7611 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7612}
7613
Orion Hodson18259d72018-04-12 11:18:23 +01007614void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7615 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7616 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7617 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7618}
7619
7620void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7621 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7622}
7623
Artem Serov02d37832016-10-25 15:25:33 +01007624void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7625 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007626 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007627 locations->SetInAt(0, Location::RequiresRegister());
7628 if (check->HasUses()) {
7629 locations->SetOut(Location::SameAsFirstInput());
7630 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007631 // Rely on the type initialization to save everything we need.
7632 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007633}
7634
7635void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7636 // We assume the class is not null.
7637 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007638 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007639 codegen_->AddSlowPath(slow_path);
7640 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7641}
7642
7643void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7644 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7645 UseScratchRegisterScope temps(GetVIXLAssembler());
7646 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007647 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Markobf121912019-06-04 13:49:05 +01007648 constexpr uint32_t shifted_visibly_initialized_value =
7649 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << status_lsb_position;
Vladimir Markodc682aa2018-01-04 18:42:57 +00007650
Vladimir Markobf121912019-06-04 13:49:05 +01007651 const size_t status_offset = mirror::Class::StatusOffset().SizeValue();
7652 GetAssembler()->LoadFromOffset(kLoadWord, temp, class_reg, status_offset);
7653 __ Cmp(temp, shifted_visibly_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007654 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007655 __ Bind(slow_path->GetExitLabel());
7656}
7657
Vladimir Marko175e7862018-03-27 09:03:13 +00007658void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7659 HTypeCheckInstruction* check,
7660 vixl32::Register temp,
7661 vixl32::FlagsUpdate flags_update) {
7662 uint32_t path_to_root = check->GetBitstringPathToRoot();
7663 uint32_t mask = check->GetBitstringMask();
7664 DCHECK(IsPowerOfTwo(mask + 1));
7665 size_t mask_bits = WhichPowerOf2(mask + 1);
7666
7667 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7668 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7669 if (mask_bits == 16u) {
7670 // Load only the bitstring part of the status word.
7671 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7672 // Check if the bitstring bits are equal to `path_to_root`.
7673 if (flags_update == SetFlags) {
7674 __ Cmp(temp, path_to_root);
7675 } else {
7676 __ Sub(temp, temp, path_to_root);
7677 }
7678 } else {
7679 // /* uint32_t */ temp = temp->status_
7680 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7681 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7682 // Compare the bitstring bits using SUB.
7683 __ Sub(temp, temp, path_to_root);
7684 // Shift out bits that do not contribute to the comparison.
7685 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7686 } else if (IsUint<16>(path_to_root)) {
7687 if (temp.IsLow()) {
7688 // Note: Optimized for size but contains one more dependent instruction than necessary.
7689 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7690 // macro assembler would use the high reg IP for the constant by default.
7691 // Compare the bitstring bits using SUB.
7692 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7693 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7694 // Shift out bits that do not contribute to the comparison.
7695 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7696 } else {
7697 // Extract the bitstring bits.
7698 __ Ubfx(temp, temp, 0, mask_bits);
7699 // Check if the bitstring bits are equal to `path_to_root`.
7700 if (flags_update == SetFlags) {
7701 __ Cmp(temp, path_to_root);
7702 } else {
7703 __ Sub(temp, temp, path_to_root);
7704 }
7705 }
7706 } else {
7707 // Shift out bits that do not contribute to the comparison.
7708 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7709 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7710 if (flags_update == SetFlags) {
7711 __ Cmp(temp, path_to_root << (32u - mask_bits));
7712 } else {
7713 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7714 }
7715 }
7716 }
7717}
7718
Artem Serov02d37832016-10-25 15:25:33 +01007719HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007720 HLoadString::LoadKind desired_string_load_kind) {
7721 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007722 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007723 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007724 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007725 DCHECK(!GetCompilerOptions().IsJitCompiler());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007726 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007727 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007728 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007729 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007730 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007731 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007732 break;
7733 }
7734 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007735}
7736
7737void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007738 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007739 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007740 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007741 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007742 locations->SetOut(LocationFrom(r0));
7743 } else {
7744 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007745 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7746 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007747 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007748 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007749 } else {
7750 // For non-Baker read barrier we have a temp-clobbering call.
7751 }
7752 }
Artem Serov02d37832016-10-25 15:25:33 +01007753 }
7754}
7755
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007756// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7757// move.
7758void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007759 LocationSummary* locations = load->GetLocations();
7760 Location out_loc = locations->Out();
7761 vixl32::Register out = OutputRegister(load);
7762 HLoadString::LoadKind load_kind = load->GetLoadKind();
7763
7764 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007765 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007766 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7767 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007768 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007769 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007770 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007771 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007772 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007773 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007774 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7775 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markode91ca92020-10-27 13:41:40 +00007776 codegen_->NewBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007777 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007778 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007779 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007780 }
7781 case HLoadString::LoadKind::kBssEntry: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007782 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007783 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007784 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007785 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007786 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007787 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007788 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007789 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007790 codegen_->AddSlowPath(slow_path);
7791 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7792 __ Bind(slow_path->GetExitLabel());
Andra Danciua0130e82020-07-23 12:34:56 +00007793 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 17);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007794 return;
7795 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007796 case HLoadString::LoadKind::kJitBootImageAddress: {
7797 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7798 DCHECK_NE(address, 0u);
7799 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7800 return;
7801 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007802 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007803 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007804 load->GetStringIndex(),
7805 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007806 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007807 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007808 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007809 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007810 }
7811 default:
7812 break;
7813 }
Artem Serov02d37832016-10-25 15:25:33 +01007814
7815 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007816 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007817 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007818 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007819 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7820 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andra Danciua0130e82020-07-23 12:34:56 +00007821 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 18);
Artem Serov02d37832016-10-25 15:25:33 +01007822}
7823
7824static int32_t GetExceptionTlsOffset() {
7825 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7826}
7827
7828void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7829 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007830 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007831 locations->SetOut(Location::RequiresRegister());
7832}
7833
7834void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7835 vixl32::Register out = OutputRegister(load);
7836 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7837}
7838
7839
7840void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007841 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007842}
7843
7844void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7845 UseScratchRegisterScope temps(GetVIXLAssembler());
7846 vixl32::Register temp = temps.Acquire();
7847 __ Mov(temp, 0);
7848 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7849}
7850
7851void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007852 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7853 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007854 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7855 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7856}
7857
7858void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7859 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7860 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7861}
7862
Artem Serov657022c2016-11-23 14:19:38 +00007863// Temp is used for read barrier.
7864static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7865 if (kEmitCompilerReadBarrier &&
7866 (kUseBakerReadBarrier ||
7867 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7868 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7869 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7870 return 1;
7871 }
7872 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007873}
7874
Artem Serov657022c2016-11-23 14:19:38 +00007875// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7876// interface pointer, one for loading the current interface.
7877// The other checks have one temp for loading the object's class.
7878static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7879 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7880 return 3;
7881 }
7882 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7883}
Artem Serovcfbe9132016-10-14 15:58:56 +01007884
7885void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7886 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7887 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7888 bool baker_read_barrier_slow_path = false;
7889 switch (type_check_kind) {
7890 case TypeCheckKind::kExactCheck:
7891 case TypeCheckKind::kAbstractClassCheck:
7892 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007893 case TypeCheckKind::kArrayObjectCheck: {
7894 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7895 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7896 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007897 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007898 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007899 case TypeCheckKind::kArrayCheck:
7900 case TypeCheckKind::kUnresolvedCheck:
7901 case TypeCheckKind::kInterfaceCheck:
7902 call_kind = LocationSummary::kCallOnSlowPath;
7903 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007904 case TypeCheckKind::kBitstringCheck:
7905 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007906 }
7907
Vladimir Markoca6fff82017-10-03 14:49:14 +01007908 LocationSummary* locations =
7909 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007910 if (baker_read_barrier_slow_path) {
7911 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7912 }
7913 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007914 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7915 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7916 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7917 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7918 } else {
7919 locations->SetInAt(1, Location::RequiresRegister());
7920 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007921 // The "out" register is used as a temporary, so it overlaps with the inputs.
7922 // Note that TypeCheckSlowPathARM uses this register too.
7923 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007924 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007925}
7926
7927void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7928 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7929 LocationSummary* locations = instruction->GetLocations();
7930 Location obj_loc = locations->InAt(0);
7931 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007932 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7933 ? vixl32::Register()
7934 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007935 Location out_loc = locations->Out();
7936 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007937 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7938 DCHECK_LE(num_temps, 1u);
7939 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007940 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7941 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7942 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7943 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007944 vixl32::Label done;
7945 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007946 SlowPathCodeARMVIXL* slow_path = nullptr;
7947
7948 // Return 0 if `obj` is null.
7949 // avoid null check if we know obj is not null.
7950 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007951 DCHECK(!out.Is(obj));
7952 __ Mov(out, 0);
Andreas Gampe3db70682018-12-26 15:12:03 -08007953 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007954 }
7955
Artem Serovcfbe9132016-10-14 15:58:56 +01007956 switch (type_check_kind) {
7957 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007958 ReadBarrierOption read_barrier_option =
7959 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007960 // /* HeapReference<Class> */ out = obj->klass_
7961 GenerateReferenceLoadTwoRegisters(instruction,
7962 out_loc,
7963 obj_loc,
7964 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007965 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007966 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007967 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007968 __ Cmp(out, cls);
7969 // We speculatively set the result to false without changing the condition
7970 // flags, which allows us to avoid some branching later.
7971 __ Mov(LeaveFlags, out, 0);
7972
7973 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7974 // we check that the output is in a low register, so that a 16-bit MOV
7975 // encoding can be used.
7976 if (out.IsLow()) {
7977 // We use the scope because of the IT block that follows.
7978 ExactAssemblyScope guard(GetVIXLAssembler(),
7979 2 * vixl32::k16BitT32InstructionSizeInBytes,
7980 CodeBufferCheckScope::kExactSize);
7981
7982 __ it(eq);
7983 __ mov(eq, out, 1);
7984 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007985 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007986 __ Mov(out, 1);
7987 }
7988
Artem Serovcfbe9132016-10-14 15:58:56 +01007989 break;
7990 }
7991
7992 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007993 ReadBarrierOption read_barrier_option =
7994 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007995 // /* HeapReference<Class> */ out = obj->klass_
7996 GenerateReferenceLoadTwoRegisters(instruction,
7997 out_loc,
7998 obj_loc,
7999 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008000 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008001 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01008002 // If the class is abstract, we eagerly fetch the super class of the
8003 // object to avoid doing a comparison we know will fail.
8004 vixl32::Label loop;
8005 __ Bind(&loop);
8006 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008007 GenerateReferenceLoadOneRegister(instruction,
8008 out_loc,
8009 super_offset,
8010 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008011 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008012 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08008013 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008014 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008015 __ B(ne, &loop, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008016 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008017 break;
8018 }
8019
8020 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00008021 ReadBarrierOption read_barrier_option =
8022 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08008023 // /* HeapReference<Class> */ out = obj->klass_
8024 GenerateReferenceLoadTwoRegisters(instruction,
8025 out_loc,
8026 obj_loc,
8027 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008028 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008029 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01008030 // Walk over the class hierarchy to find a match.
8031 vixl32::Label loop, success;
8032 __ Bind(&loop);
8033 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008034 __ B(eq, &success, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008035 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008036 GenerateReferenceLoadOneRegister(instruction,
8037 out_loc,
8038 super_offset,
8039 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008040 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008041 // This is essentially a null check, but it sets the condition flags to the
8042 // proper value for the code that follows the loop, i.e. not `eq`.
8043 __ Cmp(out, 1);
Andreas Gampe3db70682018-12-26 15:12:03 -08008044 __ B(hs, &loop, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008045
8046 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
8047 // we check that the output is in a low register, so that a 16-bit MOV
8048 // encoding can be used.
8049 if (out.IsLow()) {
8050 // If `out` is null, we use it for the result, and the condition flags
8051 // have already been set to `ne`, so the IT block that comes afterwards
8052 // (and which handles the successful case) turns into a NOP (instead of
8053 // overwriting `out`).
8054 __ Bind(&success);
8055
8056 // We use the scope because of the IT block that follows.
8057 ExactAssemblyScope guard(GetVIXLAssembler(),
8058 2 * vixl32::k16BitT32InstructionSizeInBytes,
8059 CodeBufferCheckScope::kExactSize);
8060
8061 // There is only one branch to the `success` label (which is bound to this
8062 // IT block), and it has the same condition, `eq`, so in that case the MOV
8063 // is executed.
8064 __ it(eq);
8065 __ mov(eq, out, 1);
8066 } else {
8067 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00008068 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008069 __ Bind(&success);
8070 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008071 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008072
Artem Serovcfbe9132016-10-14 15:58:56 +01008073 break;
8074 }
8075
8076 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00008077 ReadBarrierOption read_barrier_option =
8078 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08008079 // /* HeapReference<Class> */ out = obj->klass_
8080 GenerateReferenceLoadTwoRegisters(instruction,
8081 out_loc,
8082 obj_loc,
8083 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008084 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008085 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01008086 // Do an exact check.
8087 vixl32::Label exact_check;
8088 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008089 __ B(eq, &exact_check, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008090 // Otherwise, we need to check that the object's class is a non-primitive array.
8091 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008092 GenerateReferenceLoadOneRegister(instruction,
8093 out_loc,
8094 component_offset,
8095 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00008096 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008097 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08008098 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008099 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
8100 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008101 __ Cmp(out, 0);
8102 // We speculatively set the result to false without changing the condition
8103 // flags, which allows us to avoid some branching later.
8104 __ Mov(LeaveFlags, out, 0);
8105
8106 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
8107 // we check that the output is in a low register, so that a 16-bit MOV
8108 // encoding can be used.
8109 if (out.IsLow()) {
8110 __ Bind(&exact_check);
8111
8112 // We use the scope because of the IT block that follows.
8113 ExactAssemblyScope guard(GetVIXLAssembler(),
8114 2 * vixl32::k16BitT32InstructionSizeInBytes,
8115 CodeBufferCheckScope::kExactSize);
8116
8117 __ it(eq);
8118 __ mov(eq, out, 1);
8119 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08008120 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00008121 __ Bind(&exact_check);
8122 __ Mov(out, 1);
8123 }
8124
Artem Serovcfbe9132016-10-14 15:58:56 +01008125 break;
8126 }
8127
8128 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008129 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08008130 // /* HeapReference<Class> */ out = obj->klass_
8131 GenerateReferenceLoadTwoRegisters(instruction,
8132 out_loc,
8133 obj_loc,
8134 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008135 maybe_temp_loc,
8136 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008137 __ Cmp(out, cls);
8138 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008139 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008140 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008141 codegen_->AddSlowPath(slow_path);
8142 __ B(ne, slow_path->GetEntryLabel());
8143 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008144 break;
8145 }
8146
8147 case TypeCheckKind::kUnresolvedCheck:
8148 case TypeCheckKind::kInterfaceCheck: {
8149 // Note that we indeed only call on slow path, but we always go
8150 // into the slow path for the unresolved and interface check
8151 // cases.
8152 //
8153 // We cannot directly call the InstanceofNonTrivial runtime
8154 // entry point without resorting to a type checking slow path
8155 // here (i.e. by calling InvokeRuntime directly), as it would
8156 // require to assign fixed registers for the inputs of this
8157 // HInstanceOf instruction (following the runtime calling
8158 // convention), which might be cluttered by the potential first
8159 // read barrier emission at the beginning of this method.
8160 //
8161 // TODO: Introduce a new runtime entry point taking the object
8162 // to test (instead of its class) as argument, and let it deal
8163 // with the read barrier issues. This will let us refactor this
8164 // case of the `switch` code as it was previously (with a direct
8165 // call to the runtime not using a type checking slow path).
8166 // This should also be beneficial for the other cases above.
8167 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008168 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008169 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008170 codegen_->AddSlowPath(slow_path);
8171 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008172 break;
8173 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008174
8175 case TypeCheckKind::kBitstringCheck: {
8176 // /* HeapReference<Class> */ temp = obj->klass_
8177 GenerateReferenceLoadTwoRegisters(instruction,
8178 out_loc,
8179 obj_loc,
8180 class_offset,
8181 maybe_temp_loc,
8182 kWithoutReadBarrier);
8183
8184 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
8185 // If `out` is a low reg and we would have another low reg temp, we could
8186 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
8187 //
8188 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
8189 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
8190 // would be the same and we would have fewer direct data dependencies.
8191 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
8192 break;
8193 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008194 }
8195
Artem Serovcfbe9132016-10-14 15:58:56 +01008196 if (done.IsReferenced()) {
8197 __ Bind(&done);
8198 }
8199
8200 if (slow_path != nullptr) {
8201 __ Bind(slow_path->GetExitLabel());
8202 }
8203}
8204
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008205void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008206 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008207 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008208 LocationSummary* locations =
8209 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008210 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00008211 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
8212 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
8213 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
8214 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
8215 } else {
8216 locations->SetInAt(1, Location::RequiresRegister());
8217 }
Artem Serov657022c2016-11-23 14:19:38 +00008218 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008219}
8220
8221void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8222 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8223 LocationSummary* locations = instruction->GetLocations();
8224 Location obj_loc = locations->InAt(0);
8225 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00008226 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
8227 ? vixl32::Register()
8228 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008229 Location temp_loc = locations->GetTemp(0);
8230 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008231 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8232 DCHECK_LE(num_temps, 3u);
8233 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8234 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8235 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8236 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8237 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8238 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8239 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8240 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8241 const uint32_t object_array_data_offset =
8242 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008243
Vladimir Marko87584542017-12-12 17:47:52 +00008244 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008245 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008246 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8247 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008248 codegen_->AddSlowPath(type_check_slow_path);
8249
8250 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008251 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008252 // Avoid null check if we know obj is not null.
8253 if (instruction->MustDoNullCheck()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08008254 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008255 }
8256
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008257 switch (type_check_kind) {
8258 case TypeCheckKind::kExactCheck:
8259 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008260 // /* HeapReference<Class> */ temp = obj->klass_
8261 GenerateReferenceLoadTwoRegisters(instruction,
8262 temp_loc,
8263 obj_loc,
8264 class_offset,
8265 maybe_temp2_loc,
8266 kWithoutReadBarrier);
8267
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008268 __ Cmp(temp, cls);
8269 // Jump to slow path for throwing the exception or doing a
8270 // more involved array check.
8271 __ B(ne, type_check_slow_path->GetEntryLabel());
8272 break;
8273 }
8274
8275 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008276 // /* HeapReference<Class> */ temp = obj->klass_
8277 GenerateReferenceLoadTwoRegisters(instruction,
8278 temp_loc,
8279 obj_loc,
8280 class_offset,
8281 maybe_temp2_loc,
8282 kWithoutReadBarrier);
8283
Artem Serovcfbe9132016-10-14 15:58:56 +01008284 // If the class is abstract, we eagerly fetch the super class of the
8285 // object to avoid doing a comparison we know will fail.
8286 vixl32::Label loop;
8287 __ Bind(&loop);
8288 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008289 GenerateReferenceLoadOneRegister(instruction,
8290 temp_loc,
8291 super_offset,
8292 maybe_temp2_loc,
8293 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008294
8295 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8296 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008297 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008298
8299 // Otherwise, compare the classes.
8300 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008301 __ B(ne, &loop, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008302 break;
8303 }
8304
8305 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008306 // /* HeapReference<Class> */ temp = obj->klass_
8307 GenerateReferenceLoadTwoRegisters(instruction,
8308 temp_loc,
8309 obj_loc,
8310 class_offset,
8311 maybe_temp2_loc,
8312 kWithoutReadBarrier);
8313
Artem Serovcfbe9132016-10-14 15:58:56 +01008314 // Walk over the class hierarchy to find a match.
8315 vixl32::Label loop;
8316 __ Bind(&loop);
8317 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008318 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008319
8320 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008321 GenerateReferenceLoadOneRegister(instruction,
8322 temp_loc,
8323 super_offset,
8324 maybe_temp2_loc,
8325 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008326
8327 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8328 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008329 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008330 // Otherwise, jump to the beginning of the loop.
8331 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008332 break;
8333 }
8334
Artem Serovcfbe9132016-10-14 15:58:56 +01008335 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008336 // /* HeapReference<Class> */ temp = obj->klass_
8337 GenerateReferenceLoadTwoRegisters(instruction,
8338 temp_loc,
8339 obj_loc,
8340 class_offset,
8341 maybe_temp2_loc,
8342 kWithoutReadBarrier);
8343
Artem Serovcfbe9132016-10-14 15:58:56 +01008344 // Do an exact check.
8345 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008346 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008347
8348 // Otherwise, we need to check that the object's class is a non-primitive array.
8349 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008350 GenerateReferenceLoadOneRegister(instruction,
8351 temp_loc,
8352 component_offset,
8353 maybe_temp2_loc,
8354 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008355 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008356 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008357 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8358 // to further check that this component type is not a primitive type.
8359 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008360 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008361 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008362 break;
8363 }
8364
8365 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008366 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008367 // We cannot directly call the CheckCast runtime entry point
8368 // without resorting to a type checking slow path here (i.e. by
8369 // calling InvokeRuntime directly), as it would require to
8370 // assign fixed registers for the inputs of this HInstanceOf
8371 // instruction (following the runtime calling convention), which
8372 // might be cluttered by the potential first read barrier
8373 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008374
Artem Serovcfbe9132016-10-14 15:58:56 +01008375 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008376 break;
Artem Serov657022c2016-11-23 14:19:38 +00008377
8378 case TypeCheckKind::kInterfaceCheck: {
8379 // Avoid read barriers to improve performance of the fast path. We can not get false
8380 // positives by doing this.
8381 // /* HeapReference<Class> */ temp = obj->klass_
8382 GenerateReferenceLoadTwoRegisters(instruction,
8383 temp_loc,
8384 obj_loc,
8385 class_offset,
8386 maybe_temp2_loc,
8387 kWithoutReadBarrier);
8388
8389 // /* HeapReference<Class> */ temp = temp->iftable_
8390 GenerateReferenceLoadTwoRegisters(instruction,
8391 temp_loc,
8392 temp_loc,
8393 iftable_offset,
8394 maybe_temp2_loc,
8395 kWithoutReadBarrier);
8396 // Iftable is never null.
8397 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8398 // Loop through the iftable and check if any class matches.
8399 vixl32::Label start_loop;
8400 __ Bind(&start_loop);
8401 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8402 type_check_slow_path->GetEntryLabel());
8403 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8404 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8405 // Go to next interface.
8406 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8407 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8408 // Compare the classes and continue the loop if they do not match.
8409 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Andreas Gampe3db70682018-12-26 15:12:03 -08008410 __ B(ne, &start_loop, /* is_far_target= */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008411 break;
8412 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008413
8414 case TypeCheckKind::kBitstringCheck: {
8415 // /* HeapReference<Class> */ temp = obj->klass_
8416 GenerateReferenceLoadTwoRegisters(instruction,
8417 temp_loc,
8418 obj_loc,
8419 class_offset,
8420 maybe_temp2_loc,
8421 kWithoutReadBarrier);
8422
8423 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8424 __ B(ne, type_check_slow_path->GetEntryLabel());
8425 break;
8426 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008427 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008428 if (done.IsReferenced()) {
8429 __ Bind(&done);
8430 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008431
8432 __ Bind(type_check_slow_path->GetExitLabel());
8433}
8434
Artem Serov551b28f2016-10-18 19:11:30 +01008435void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008436 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8437 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008438 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8439 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8440}
8441
8442void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8443 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8444 instruction,
8445 instruction->GetDexPc());
8446 if (instruction->IsEnter()) {
8447 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8448 } else {
8449 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8450 }
Andra Danciua0130e82020-07-23 12:34:56 +00008451 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 19);
Artem Serov551b28f2016-10-18 19:11:30 +01008452}
8453
Artem Serov02109dd2016-09-23 17:17:54 +01008454void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8455 HandleBitwiseOperation(instruction, AND);
8456}
8457
8458void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8459 HandleBitwiseOperation(instruction, ORR);
8460}
8461
8462void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8463 HandleBitwiseOperation(instruction, EOR);
8464}
8465
8466void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8467 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008468 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008469 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8470 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008471 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8472 locations->SetInAt(0, Location::RequiresRegister());
8473 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8475}
8476
8477void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8478 HandleBitwiseOperation(instruction);
8479}
8480
8481void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8482 HandleBitwiseOperation(instruction);
8483}
8484
8485void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8486 HandleBitwiseOperation(instruction);
8487}
8488
Artem Serov2bbc9532016-10-21 11:51:50 +01008489void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8490 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008491 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008492 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8493 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008494
8495 locations->SetInAt(0, Location::RequiresRegister());
8496 locations->SetInAt(1, Location::RequiresRegister());
8497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8498}
8499
8500void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8501 LocationSummary* locations = instruction->GetLocations();
8502 Location first = locations->InAt(0);
8503 Location second = locations->InAt(1);
8504 Location out = locations->Out();
8505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008506 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008507 vixl32::Register first_reg = RegisterFrom(first);
8508 vixl32::Register second_reg = RegisterFrom(second);
8509 vixl32::Register out_reg = RegisterFrom(out);
8510
8511 switch (instruction->GetOpKind()) {
8512 case HInstruction::kAnd:
8513 __ Bic(out_reg, first_reg, second_reg);
8514 break;
8515 case HInstruction::kOr:
8516 __ Orn(out_reg, first_reg, second_reg);
8517 break;
8518 // There is no EON on arm.
8519 case HInstruction::kXor:
8520 default:
8521 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8522 UNREACHABLE();
8523 }
8524 return;
8525
8526 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008527 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008528 vixl32::Register first_low = LowRegisterFrom(first);
8529 vixl32::Register first_high = HighRegisterFrom(first);
8530 vixl32::Register second_low = LowRegisterFrom(second);
8531 vixl32::Register second_high = HighRegisterFrom(second);
8532 vixl32::Register out_low = LowRegisterFrom(out);
8533 vixl32::Register out_high = HighRegisterFrom(out);
8534
8535 switch (instruction->GetOpKind()) {
8536 case HInstruction::kAnd:
8537 __ Bic(out_low, first_low, second_low);
8538 __ Bic(out_high, first_high, second_high);
8539 break;
8540 case HInstruction::kOr:
8541 __ Orn(out_low, first_low, second_low);
8542 __ Orn(out_high, first_high, second_high);
8543 break;
8544 // There is no EON on arm.
8545 case HInstruction::kXor:
8546 default:
8547 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8548 UNREACHABLE();
8549 }
8550 }
8551}
8552
Anton Kirilov74234da2017-01-13 14:42:47 +00008553void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8554 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008555 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8556 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008557 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008558 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008559 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008560 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8561
8562 locations->SetInAt(0, Location::RequiresRegister());
8563 locations->SetInAt(1, Location::RequiresRegister());
8564 locations->SetOut(Location::RequiresRegister(),
8565 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8566}
8567
8568void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8569 HDataProcWithShifterOp* instruction) {
8570 const LocationSummary* const locations = instruction->GetLocations();
8571 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8572 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008574 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008575 const vixl32::Register first = InputRegisterAt(instruction, 0);
8576 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008577 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008578 ? LowRegisterFrom(locations->InAt(1))
8579 : InputRegisterAt(instruction, 1);
8580
Anton Kirilov420ee302017-02-21 18:10:26 +00008581 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8582 DCHECK_EQ(kind, HInstruction::kAdd);
8583
8584 switch (op_kind) {
8585 case HDataProcWithShifterOp::kUXTB:
8586 __ Uxtab(output, first, second);
8587 break;
8588 case HDataProcWithShifterOp::kUXTH:
8589 __ Uxtah(output, first, second);
8590 break;
8591 case HDataProcWithShifterOp::kSXTB:
8592 __ Sxtab(output, first, second);
8593 break;
8594 case HDataProcWithShifterOp::kSXTH:
8595 __ Sxtah(output, first, second);
8596 break;
8597 default:
8598 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8599 UNREACHABLE();
8600 }
8601 } else {
8602 GenerateDataProcInstruction(kind,
8603 output,
8604 first,
8605 Operand(second,
8606 ShiftFromOpKind(op_kind),
8607 instruction->GetShiftAmount()),
8608 codegen_);
8609 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008610 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008611 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008612
8613 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8614 const vixl32::Register second = InputRegisterAt(instruction, 1);
8615
8616 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8617 GenerateDataProc(kind,
8618 locations->Out(),
8619 locations->InAt(0),
8620 second,
8621 Operand(second, ShiftType::ASR, 31),
8622 codegen_);
8623 } else {
8624 GenerateLongDataProc(instruction, codegen_);
8625 }
8626 }
8627}
8628
Artem Serov02109dd2016-09-23 17:17:54 +01008629// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8630void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8631 vixl32::Register first,
8632 uint32_t value) {
8633 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8634 if (value == 0xffffffffu) {
8635 if (!out.Is(first)) {
8636 __ Mov(out, first);
8637 }
8638 return;
8639 }
8640 if (value == 0u) {
8641 __ Mov(out, 0);
8642 return;
8643 }
8644 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008645 __ And(out, first, value);
8646 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8647 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008648 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008649 DCHECK(IsPowerOfTwo(value + 1));
8650 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008651 }
8652}
8653
8654// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8655void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8656 vixl32::Register first,
8657 uint32_t value) {
8658 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8659 if (value == 0u) {
8660 if (!out.Is(first)) {
8661 __ Mov(out, first);
8662 }
8663 return;
8664 }
8665 if (value == 0xffffffffu) {
8666 __ Mvn(out, 0);
8667 return;
8668 }
8669 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8670 __ Orr(out, first, value);
8671 } else {
8672 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8673 __ Orn(out, first, ~value);
8674 }
8675}
8676
8677// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8678void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8679 vixl32::Register first,
8680 uint32_t value) {
8681 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8682 if (value == 0u) {
8683 if (!out.Is(first)) {
8684 __ Mov(out, first);
8685 }
8686 return;
8687 }
8688 __ Eor(out, first, value);
8689}
8690
Anton Kirilovdda43962016-11-21 19:55:20 +00008691void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8692 Location first,
8693 uint64_t value) {
8694 vixl32::Register out_low = LowRegisterFrom(out);
8695 vixl32::Register out_high = HighRegisterFrom(out);
8696 vixl32::Register first_low = LowRegisterFrom(first);
8697 vixl32::Register first_high = HighRegisterFrom(first);
8698 uint32_t value_low = Low32Bits(value);
8699 uint32_t value_high = High32Bits(value);
8700 if (value_low == 0u) {
8701 if (!out_low.Is(first_low)) {
8702 __ Mov(out_low, first_low);
8703 }
8704 __ Add(out_high, first_high, value_high);
8705 return;
8706 }
8707 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008708 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008709 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008710 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008711 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8712 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008713 }
8714}
8715
Artem Serov02109dd2016-09-23 17:17:54 +01008716void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8717 LocationSummary* locations = instruction->GetLocations();
8718 Location first = locations->InAt(0);
8719 Location second = locations->InAt(1);
8720 Location out = locations->Out();
8721
8722 if (second.IsConstant()) {
8723 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8724 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008725 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008726 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8727 vixl32::Register out_reg = OutputRegister(instruction);
8728 if (instruction->IsAnd()) {
8729 GenerateAndConst(out_reg, first_reg, value_low);
8730 } else if (instruction->IsOr()) {
8731 GenerateOrrConst(out_reg, first_reg, value_low);
8732 } else {
8733 DCHECK(instruction->IsXor());
8734 GenerateEorConst(out_reg, first_reg, value_low);
8735 }
8736 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008737 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008738 uint32_t value_high = High32Bits(value);
8739 vixl32::Register first_low = LowRegisterFrom(first);
8740 vixl32::Register first_high = HighRegisterFrom(first);
8741 vixl32::Register out_low = LowRegisterFrom(out);
8742 vixl32::Register out_high = HighRegisterFrom(out);
8743 if (instruction->IsAnd()) {
8744 GenerateAndConst(out_low, first_low, value_low);
8745 GenerateAndConst(out_high, first_high, value_high);
8746 } else if (instruction->IsOr()) {
8747 GenerateOrrConst(out_low, first_low, value_low);
8748 GenerateOrrConst(out_high, first_high, value_high);
8749 } else {
8750 DCHECK(instruction->IsXor());
8751 GenerateEorConst(out_low, first_low, value_low);
8752 GenerateEorConst(out_high, first_high, value_high);
8753 }
8754 }
8755 return;
8756 }
8757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008758 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008759 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8760 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8761 vixl32::Register out_reg = OutputRegister(instruction);
8762 if (instruction->IsAnd()) {
8763 __ And(out_reg, first_reg, second_reg);
8764 } else if (instruction->IsOr()) {
8765 __ Orr(out_reg, first_reg, second_reg);
8766 } else {
8767 DCHECK(instruction->IsXor());
8768 __ Eor(out_reg, first_reg, second_reg);
8769 }
8770 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008771 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008772 vixl32::Register first_low = LowRegisterFrom(first);
8773 vixl32::Register first_high = HighRegisterFrom(first);
8774 vixl32::Register second_low = LowRegisterFrom(second);
8775 vixl32::Register second_high = HighRegisterFrom(second);
8776 vixl32::Register out_low = LowRegisterFrom(out);
8777 vixl32::Register out_high = HighRegisterFrom(out);
8778 if (instruction->IsAnd()) {
8779 __ And(out_low, first_low, second_low);
8780 __ And(out_high, first_high, second_high);
8781 } else if (instruction->IsOr()) {
8782 __ Orr(out_low, first_low, second_low);
8783 __ Orr(out_high, first_high, second_high);
8784 } else {
8785 DCHECK(instruction->IsXor());
8786 __ Eor(out_low, first_low, second_low);
8787 __ Eor(out_high, first_high, second_high);
8788 }
8789 }
8790}
8791
Artem Serovcfbe9132016-10-14 15:58:56 +01008792void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008793 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008794 Location out,
8795 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008796 Location maybe_temp,
8797 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008798 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008799 if (read_barrier_option == kWithReadBarrier) {
8800 CHECK(kEmitCompilerReadBarrier);
8801 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8802 if (kUseBakerReadBarrier) {
8803 // Load with fast path based Baker's read barrier.
8804 // /* HeapReference<Object> */ out = *(out + offset)
8805 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008806 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008807 } else {
8808 // Load with slow path based read barrier.
8809 // Save the value of `out` into `maybe_temp` before overwriting it
8810 // in the following move operation, as we will need it for the
8811 // read barrier below.
8812 __ Mov(RegisterFrom(maybe_temp), out_reg);
8813 // /* HeapReference<Object> */ out = *(out + offset)
8814 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8815 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8816 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008817 } else {
8818 // Plain load with no read barrier.
8819 // /* HeapReference<Object> */ out = *(out + offset)
8820 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8821 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8822 }
8823}
8824
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008825void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008826 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008827 Location out,
8828 Location obj,
8829 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008830 Location maybe_temp,
8831 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008832 vixl32::Register out_reg = RegisterFrom(out);
8833 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008834 if (read_barrier_option == kWithReadBarrier) {
8835 CHECK(kEmitCompilerReadBarrier);
8836 if (kUseBakerReadBarrier) {
8837 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8838 // Load with fast path based Baker's read barrier.
8839 // /* HeapReference<Object> */ out = *(obj + offset)
8840 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008841 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008842 } else {
8843 // Load with slow path based read barrier.
8844 // /* HeapReference<Object> */ out = *(obj + offset)
8845 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8846 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8847 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008848 } else {
8849 // Plain load with no read barrier.
8850 // /* HeapReference<Object> */ out = *(obj + offset)
8851 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8852 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8853 }
8854}
8855
Vladimir Markoca1e0382018-04-11 09:58:41 +00008856void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008857 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008858 Location root,
8859 vixl32::Register obj,
8860 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008861 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008862 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008863 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008864 DCHECK(kEmitCompilerReadBarrier);
8865 if (kUseBakerReadBarrier) {
8866 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008867 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008868
Vladimir Marko008e09f32018-08-06 15:42:43 +01008869 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8870 // the Marking Register) to decide whether we need to enter
8871 // the slow path to mark the GC root.
8872 //
8873 // We use shared thunks for the slow path; shared within the method
8874 // for JIT, across methods for AOT. That thunk checks the reference
8875 // and jumps to the entrypoint if needed.
8876 //
8877 // lr = &return_address;
8878 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8879 // if (mr) { // Thread::Current()->GetIsGcMarking()
8880 // goto gc_root_thunk<root_reg>(lr)
8881 // }
8882 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008883
Vladimir Marko008e09f32018-08-06 15:42:43 +01008884 UseScratchRegisterScope temps(GetVIXLAssembler());
8885 temps.Exclude(ip);
8886 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8887 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008888
Vladimir Markod887ed82018-08-14 13:52:12 +00008889 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8890 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8891 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8892 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8893 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008894 vixl32::Label return_address;
8895 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8896 __ cmp(mr, Operand(0));
8897 // Currently the offset is always within range. If that changes,
8898 // we shall have to split the load the same way as for fields.
8899 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8900 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8901 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8902 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008903 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008904 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8905 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8906 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008907 } else {
8908 // GC root loaded through a slow path for read barriers other
8909 // than Baker's.
8910 // /* GcRoot<mirror::Object>* */ root = obj + offset
8911 __ Add(root_reg, obj, offset);
8912 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008913 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008914 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008915 } else {
8916 // Plain GC root load with no read barrier.
8917 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8918 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8919 // Note that GC roots are not affected by heap poisoning, thus we
8920 // do not have to unpoison `root_reg` here.
8921 }
Andra Danciua0130e82020-07-23 12:34:56 +00008922 MaybeGenerateMarkingRegisterCheck(/* code= */ 20);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008923}
8924
Vladimir Marko3d350a82020-11-18 14:14:27 +00008925void CodeGeneratorARMVIXL::GenerateIntrinsicCasMoveWithBakerReadBarrier(
8926 vixl::aarch32::Register marked_old_value,
8927 vixl::aarch32::Register old_value) {
Vladimir Markod887ed82018-08-14 13:52:12 +00008928 DCHECK(kEmitCompilerReadBarrier);
8929 DCHECK(kUseBakerReadBarrier);
8930
Vladimir Marko3d350a82020-11-18 14:14:27 +00008931 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
8932 // For low registers, we can reuse the GC root narrow entrypoint, for high registers
8933 // we use a specialized entrypoint because the register bits are 8-11 instead of 12-15.
8934 bool narrow_mov = marked_old_value.IsLow();
8935 uint32_t custom_data = narrow_mov
8936 ? EncodeBakerReadBarrierGcRootData(marked_old_value.GetCode(), /*narrow=*/ true)
8937 : EncodeBakerReadBarrierIntrinsicCasData(marked_old_value.GetCode());
Vladimir Markod887ed82018-08-14 13:52:12 +00008938
Vladimir Marko3d350a82020-11-18 14:14:27 +00008939 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* MOV */ (narrow_mov ? 1u : 0u);
8940 size_t wide_instructions = /* ADR+CMP+MOV+BNE */ 4u - narrow_instructions;
Vladimir Markod887ed82018-08-14 13:52:12 +00008941 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8942 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8943 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8944 vixl32::Label return_address;
8945 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8946 __ cmp(mr, Operand(0));
8947 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko3d350a82020-11-18 14:14:27 +00008948 __ mov(EncodingSize(narrow_mov ? Narrow : Wide), marked_old_value, old_value);
Vladimir Markod887ed82018-08-14 13:52:12 +00008949 EmitBakerReadBarrierBne(custom_data);
8950 __ bind(&return_address);
8951 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
Vladimir Marko3d350a82020-11-18 14:14:27 +00008952 narrow_mov
8953 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8954 : BAKER_MARK_INTROSPECTION_INTRINSIC_CAS_MOV_OFFSET);
Vladimir Markod887ed82018-08-14 13:52:12 +00008955}
8956
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008957void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8958 Location ref,
8959 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008960 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008961 bool needs_null_check) {
8962 DCHECK(kEmitCompilerReadBarrier);
8963 DCHECK(kUseBakerReadBarrier);
8964
Vladimir Marko008e09f32018-08-06 15:42:43 +01008965 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8966 // Marking Register) to decide whether we need to enter the slow
8967 // path to mark the reference. Then, in the slow path, check the
8968 // gray bit in the lock word of the reference's holder (`obj`) to
8969 // decide whether to mark `ref` or not.
8970 //
8971 // We use shared thunks for the slow path; shared within the method
8972 // for JIT, across methods for AOT. That thunk checks the holder
8973 // and jumps to the entrypoint if needed. If the holder is not gray,
8974 // it creates a fake dependency and returns to the LDR instruction.
8975 //
8976 // lr = &gray_return_address;
8977 // if (mr) { // Thread::Current()->GetIsGcMarking()
8978 // goto field_thunk<holder_reg, base_reg>(lr)
8979 // }
8980 // not_gray_return_address:
8981 // // Original reference load. If the offset is too large to fit
8982 // // into LDR, we use an adjusted base register here.
8983 // HeapReference<mirror::Object> reference = *(obj+offset);
8984 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008985
Vladimir Marko248141f2018-08-10 10:40:07 +01008986 DCHECK(src.GetAddrMode() == vixl32::Offset);
8987 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008988 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008989 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8990
Vladimir Marko008e09f32018-08-06 15:42:43 +01008991 UseScratchRegisterScope temps(GetVIXLAssembler());
8992 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008993 uint32_t custom_data =
8994 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008995
Vladimir Marko008e09f32018-08-06 15:42:43 +01008996 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008997 size_t narrow_instructions =
8998 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8999 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
9000 size_t wide_instructions =
9001 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
9002 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
9003 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
9004 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01009005 vixl32::Label return_address;
9006 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
9007 __ cmp(mr, Operand(0));
9008 EmitBakerReadBarrierBne(custom_data);
9009 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01009010 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01009011 if (needs_null_check) {
9012 MaybeRecordImplicitNullCheck(instruction);
9013 }
9014 // Note: We need a specific width for the unpoisoning NEG.
9015 if (kPoisonHeapReferences) {
9016 if (narrow) {
9017 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
9018 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
9019 } else {
9020 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
9021 }
9022 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009023 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01009024 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
9025 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
9026 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
9027 }
Andra Danciua0130e82020-07-23 12:34:56 +00009028 MaybeGenerateMarkingRegisterCheck(/* code= */ 21, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00009029}
9030
Vladimir Marko248141f2018-08-10 10:40:07 +01009031void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
9032 Location ref,
9033 vixl32::Register obj,
9034 uint32_t offset,
Vladimir Marko01b65522020-10-28 15:43:54 +00009035 Location maybe_temp,
Vladimir Marko248141f2018-08-10 10:40:07 +01009036 bool needs_null_check) {
9037 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
9038 vixl32::Register base = obj;
9039 if (offset >= kReferenceLoadMinFarOffset) {
Vladimir Marko01b65522020-10-28 15:43:54 +00009040 base = RegisterFrom(maybe_temp);
Vladimir Marko248141f2018-08-10 10:40:07 +01009041 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
9042 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
9043 offset &= (kReferenceLoadMinFarOffset - 1u);
9044 }
9045 GenerateFieldLoadWithBakerReadBarrier(
9046 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
9047}
9048
Vladimir Marko008e09f32018-08-06 15:42:43 +01009049void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009050 vixl32::Register obj,
9051 uint32_t data_offset,
9052 Location index,
9053 Location temp,
9054 bool needs_null_check) {
9055 DCHECK(kEmitCompilerReadBarrier);
9056 DCHECK(kUseBakerReadBarrier);
9057
9058 static_assert(
9059 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
9060 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009061 ScaleFactor scale_factor = TIMES_4;
9062
Vladimir Marko008e09f32018-08-06 15:42:43 +01009063 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
9064 // Marking Register) to decide whether we need to enter the slow
9065 // path to mark the reference. Then, in the slow path, check the
9066 // gray bit in the lock word of the reference's holder (`obj`) to
9067 // decide whether to mark `ref` or not.
9068 //
9069 // We use shared thunks for the slow path; shared within the method
9070 // for JIT, across methods for AOT. That thunk checks the holder
9071 // and jumps to the entrypoint if needed. If the holder is not gray,
9072 // it creates a fake dependency and returns to the LDR instruction.
9073 //
9074 // lr = &gray_return_address;
9075 // if (mr) { // Thread::Current()->GetIsGcMarking()
9076 // goto array_thunk<base_reg>(lr)
9077 // }
9078 // not_gray_return_address:
9079 // // Original reference load. If the offset is too large to fit
9080 // // into LDR, we use an adjusted base register here.
9081 // HeapReference<mirror::Object> reference = data[index];
9082 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009083
Vladimir Marko008e09f32018-08-06 15:42:43 +01009084 DCHECK(index.IsValid());
9085 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
9086 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
9087 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009088
Vladimir Marko008e09f32018-08-06 15:42:43 +01009089 UseScratchRegisterScope temps(GetVIXLAssembler());
9090 temps.Exclude(ip);
9091 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009092
Vladimir Marko008e09f32018-08-06 15:42:43 +01009093 __ Add(data_reg, obj, Operand(data_offset));
9094 {
Vladimir Markod887ed82018-08-14 13:52:12 +00009095 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
9096 size_t wide_instructions =
9097 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
9098 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
9099 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
9100 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01009101 vixl32::Label return_address;
9102 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
9103 __ cmp(mr, Operand(0));
9104 EmitBakerReadBarrierBne(custom_data);
9105 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
9106 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
9107 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
9108 // Note: We need a Wide NEG for the unpoisoning.
9109 if (kPoisonHeapReferences) {
9110 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009111 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009112 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01009113 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
9114 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009115 }
Andra Danciua0130e82020-07-23 12:34:56 +00009116 MaybeGenerateMarkingRegisterCheck(/* code= */ 22, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00009117}
9118
Roland Levillain5daa4952017-07-03 17:23:56 +01009119void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
9120 // The following condition is a compile-time one, so it does not have a run-time cost.
9121 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
9122 // The following condition is a run-time one; it is executed after the
9123 // previous compile-time test, to avoid penalizing non-debug builds.
9124 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
9125 UseScratchRegisterScope temps(GetVIXLAssembler());
9126 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
9127 GetAssembler()->GenerateMarkingRegisterCheck(temp,
9128 kMarkingRegisterCheckBreakCodeBaseCode + code);
9129 }
9130 }
9131}
9132
Vladimir Marko3d350a82020-11-18 14:14:27 +00009133SlowPathCodeARMVIXL* CodeGeneratorARMVIXL::AddReadBarrierSlowPath(HInstruction* instruction,
9134 Location out,
9135 Location ref,
9136 Location obj,
9137 uint32_t offset,
9138 Location index) {
9139 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
9140 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9141 AddSlowPath(slow_path);
9142 return slow_path;
9143}
9144
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009145void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
9146 Location out,
9147 Location ref,
9148 Location obj,
9149 uint32_t offset,
9150 Location index) {
9151 DCHECK(kEmitCompilerReadBarrier);
9152
9153 // Insert a slow path based read barrier *after* the reference load.
9154 //
9155 // If heap poisoning is enabled, the unpoisoning of the loaded
9156 // reference will be carried out by the runtime within the slow
9157 // path.
9158 //
9159 // Note that `ref` currently does not get unpoisoned (when heap
9160 // poisoning is enabled), which is alright as the `ref` argument is
9161 // not used by the artReadBarrierSlow entry point.
9162 //
9163 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko3d350a82020-11-18 14:14:27 +00009164 SlowPathCodeARMVIXL* slow_path =
9165 AddReadBarrierSlowPath(instruction, out, ref, obj, offset, index);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009166
9167 __ B(slow_path->GetEntryLabel());
9168 __ Bind(slow_path->GetExitLabel());
9169}
9170
9171void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009172 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009173 Location ref,
9174 Location obj,
9175 uint32_t offset,
9176 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009177 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009178 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009179 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009180 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009181 // If heap poisoning is enabled, unpoisoning will be taken care of
9182 // by the runtime within the slow path.
9183 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009184 } else if (kPoisonHeapReferences) {
9185 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9186 }
9187}
9188
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009189void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9190 Location out,
9191 Location root) {
9192 DCHECK(kEmitCompilerReadBarrier);
9193
9194 // Insert a slow path based read barrier *after* the GC root load.
9195 //
9196 // Note that GC roots are not affected by heap poisoning, so we do
9197 // not need to do anything special for this here.
9198 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009199 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009200 AddSlowPath(slow_path);
9201
9202 __ B(slow_path->GetEntryLabel());
9203 __ Bind(slow_path->GetExitLabel());
9204}
9205
Artem Serov02d37832016-10-25 15:25:33 +01009206// Check if the desired_dispatch_info is supported. If it is, return it,
9207// otherwise return a fall-back info that should be used instead.
9208HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009209 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Vladimir Marko86c87522020-05-11 16:55:55 +01009210 ArtMethod* method) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009211 if (method->IsIntrinsic() &&
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009212 desired_dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009213 // As a work-around for soft-float native ABI interfering with type checks, we are
9214 // inserting fake calls to Float.floatToRawIntBits() or Double.doubleToRawLongBits()
9215 // when a float or double argument is passed in core registers but we cannot do that
9216 // for actual intrinsic implementations that expect them in FP registers. Therefore
9217 // we do not use `kCallCriticalNative` for intrinsics with FP arguments; if they are
9218 // properly intrinsified, the dispatch type does not matter anyway.
Vladimir Marko86c87522020-05-11 16:55:55 +01009219 ScopedObjectAccess soa(Thread::Current());
9220 uint32_t shorty_len;
9221 const char* shorty = method->GetShorty(&shorty_len);
Vladimir Marko86c87522020-05-11 16:55:55 +01009222 for (uint32_t i = 1; i != shorty_len; ++i) {
Vladimir Marko86c87522020-05-11 16:55:55 +01009223 if (shorty[i] == 'D' || shorty[i] == 'F') {
9224 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009225 dispatch_info.code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Marko86c87522020-05-11 16:55:55 +01009226 return dispatch_info;
9227 }
Vladimir Marko86c87522020-05-11 16:55:55 +01009228 }
9229 }
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009230 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009231}
9232
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009233
9234void CodeGeneratorARMVIXL::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
9235 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009236 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01009237 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009238 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01009239 vixl32::Register temp_reg = RegisterFrom(temp);
9240 EmitMovwMovtPlaceholder(labels, temp_reg);
9241 break;
9242 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009243 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009244 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00009245 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
9246 vixl32::Register temp_reg = RegisterFrom(temp);
9247 EmitMovwMovtPlaceholder(labels, temp_reg);
9248 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
9249 break;
9250 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009251 case MethodLoadKind::kBssEntry: {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009252 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009253 vixl32::Register temp_reg = RegisterFrom(temp);
9254 EmitMovwMovtPlaceholder(labels, temp_reg);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01009255 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009256 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009257 break;
9258 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009259 case MethodLoadKind::kJitDirectAddress: {
9260 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetResolvedMethod()));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009261 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009262 }
9263 case MethodLoadKind::kRuntimeCall: {
9264 // Test situation, don't do anything.
9265 break;
9266 }
9267 default: {
9268 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
9269 UNREACHABLE();
9270 }
9271 }
9272}
9273
9274void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
9275 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
9276 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
9277 switch (invoke->GetMethodLoadKind()) {
9278 case MethodLoadKind::kStringInit: {
9279 uint32_t offset =
9280 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9281 // temp = thread->string_init_entrypoint
9282 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9283 break;
9284 }
9285 case MethodLoadKind::kRecursive: {
9286 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
9287 break;
9288 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009289 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009290 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9291 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009292 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01009293 case MethodLoadKind::kBootImageLinkTimePcRelative:
9294 // Note: Unlike arm64, x86 and x86-64, we do not avoid the materialization of method
9295 // pointer for kCallCriticalNative because it would not save us an instruction from
9296 // the current sequence MOVW+MOVT+ADD(pc)+LDR+BL. The ADD(pc) separates the patched
9297 // offset instructions MOVW+MOVT from the entrypoint load, so they cannot be fused.
9298 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009299 default: {
9300 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
9301 break;
9302 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009303 }
9304
Vladimir Marko86c87522020-05-11 16:55:55 +01009305 auto call_code_pointer_member = [&](MemberOffset offset) {
9306 // LR = callee_method->member;
9307 GetAssembler()->LoadFromOffset(kLoadWord, lr, RegisterFrom(callee_method), offset.Int32Value());
9308 {
9309 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9310 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9311 ExactAssemblyScope aas(GetVIXLAssembler(),
9312 vixl32::k16BitT32InstructionSizeInBytes,
9313 CodeBufferCheckScope::kExactSize);
9314 // LR()
9315 __ blx(lr);
9316 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9317 }
9318 };
Artem Serovd4cc5b22016-11-04 11:19:09 +00009319 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009320 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009321 {
Nicolas Geoffray282795c2021-09-24 18:16:41 +01009322 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009323 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9324 ExactAssemblyScope aas(GetVIXLAssembler(),
9325 vixl32::k32BitT32InstructionSizeInBytes,
9326 CodeBufferCheckScope::kMaximumSize);
9327 __ bl(GetFrameEntryLabel());
9328 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9329 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009330 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009331 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01009332 size_t out_frame_size =
9333 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARMVIXL,
9334 kAapcsStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01009335 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01009336 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArmPointerSize));
9337 // Move the result when needed due to native and managed ABI mismatch.
9338 switch (invoke->GetType()) {
9339 case DataType::Type::kFloat32:
9340 __ Vmov(s0, r0);
9341 break;
9342 case DataType::Type::kFloat64:
9343 __ Vmov(d0, r0, r1);
9344 break;
9345 case DataType::Type::kBool:
9346 case DataType::Type::kInt8:
9347 case DataType::Type::kUint16:
9348 case DataType::Type::kInt16:
9349 case DataType::Type::kInt32:
9350 case DataType::Type::kInt64:
9351 case DataType::Type::kVoid:
9352 break;
9353 default:
9354 DCHECK(false) << invoke->GetType();
9355 break;
9356 }
9357 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01009358 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01009359 }
9360 break;
9361 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009362 case CodePtrLocation::kCallArtMethod:
Vladimir Marko86c87522020-05-11 16:55:55 +01009363 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009364 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009365 }
9366
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009367 DCHECK(!IsLeafMethod());
9368}
9369
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009370void CodeGeneratorARMVIXL::GenerateVirtualCall(
9371 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009372 vixl32::Register temp = RegisterFrom(temp_location);
9373 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9374 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9375
9376 // Use the calling convention instead of the location of the receiver, as
9377 // intrinsics may have put the receiver in a different register. In the intrinsics
9378 // slow path, the arguments have been moved to the right place, so here we are
9379 // guaranteed that the receiver is the first register of the calling convention.
9380 InvokeDexCallingConventionARMVIXL calling_convention;
9381 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9382 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009383 {
9384 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009385 ExactAssemblyScope aas(GetVIXLAssembler(),
9386 vixl32::kMaxInstructionSizeInBytes,
9387 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009388 // /* HeapReference<Class> */ temp = receiver->klass_
9389 __ ldr(temp, MemOperand(receiver, class_offset));
9390 MaybeRecordImplicitNullCheck(invoke);
9391 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009392 // Instead of simply (possibly) unpoisoning `temp` here, we should
9393 // emit a read barrier for the previous class reference load.
9394 // However this is not required in practice, as this is an
9395 // intermediate/temporary reference and because the current
9396 // concurrent copying collector keeps the from-space memory
9397 // intact/accessible until the end of the marking phase (the
9398 // concurrent copying collector may not in the future).
9399 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9400
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00009401 // If we're compiling baseline, update the inline cache.
9402 MaybeGenerateInlineCacheCheck(invoke, temp);
9403
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009404 // temp = temp->GetMethodAt(method_offset);
9405 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9406 kArmPointerSize).Int32Value();
9407 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9408 // LR = temp->GetEntryPoint();
9409 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009410 {
9411 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9412 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9413 ExactAssemblyScope aas(GetVIXLAssembler(),
9414 vixl32::k16BitT32InstructionSizeInBytes,
9415 CodeBufferCheckScope::kExactSize);
9416 // LR();
9417 __ blx(lr);
9418 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9419 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009420}
9421
Vladimir Marko6fd16062018-06-26 11:02:04 +01009422CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
9423 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009424 return NewPcRelativePatch(/* dex_file= */ nullptr, intrinsic_data, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009425}
9426
Vladimir Markob066d432018-01-03 13:14:37 +00009427CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9428 uint32_t boot_image_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08009429 return NewPcRelativePatch(/* dex_file= */ nullptr,
Vladimir Markob066d432018-01-03 13:14:37 +00009430 boot_image_offset,
Vladimir Marko2d06e022019-07-08 15:45:19 +01009431 &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00009432}
9433
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009434CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009435 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009436 return NewPcRelativePatch(
9437 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009438}
9439
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009440CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9441 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009442 return NewPcRelativePatch(
9443 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009444}
9445
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009446CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009447 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009448 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009449}
9450
Vladimir Marko1998cd02017-01-13 13:02:58 +00009451CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01009452 HLoadClass* load_class) {
9453 const DexFile& dex_file = load_class->GetDexFile();
9454 dex::TypeIndex type_index = load_class->GetTypeIndex();
9455 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
9456 switch (load_class->GetLoadKind()) {
9457 case HLoadClass::LoadKind::kBssEntry:
9458 patches = &type_bss_entry_patches_;
9459 break;
9460 case HLoadClass::LoadKind::kBssEntryPublic:
9461 patches = &public_type_bss_entry_patches_;
9462 break;
9463 case HLoadClass::LoadKind::kBssEntryPackage:
9464 patches = &package_type_bss_entry_patches_;
9465 break;
9466 default:
9467 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
9468 UNREACHABLE();
9469 }
Vladimir Markobaade402020-09-30 14:45:39 +00009470 return NewPcRelativePatch(&dex_file, type_index.index_, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009471}
9472
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009473CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009474 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009475 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009476}
9477
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009478CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9479 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009480 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009481}
9482
Artem Serovd4cc5b22016-11-04 11:19:09 +00009483CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009484 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009485 patches->emplace_back(dex_file, offset_or_index);
9486 return &patches->back();
9487}
9488
Vladimir Markof6675082019-05-17 12:05:28 +01009489void CodeGeneratorARMVIXL::EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset) {
9490 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009491 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01009492 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
9493 vixl::aarch32::Label* bl_label = &call_entrypoint_patches_.back().label;
9494 __ bind(bl_label);
9495 vixl32::Label placeholder_label;
9496 __ bl(&placeholder_label); // Placeholder, patched at link-time.
9497 __ bind(&placeholder_label);
9498}
9499
Vladimir Marko966b46f2018-08-03 10:20:19 +00009500void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00009501 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009502 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00009503 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
9504 vixl::aarch32::Label* slow_path_entry = &it->second.label;
9505 __ b(ne, EncodingSize(Wide), slow_path_entry);
9506 } else {
9507 baker_read_barrier_patches_.emplace_back(custom_data);
9508 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
9509 __ bind(patch_label);
9510 vixl32::Label placeholder_label;
9511 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
9512 __ bind(&placeholder_label);
9513 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009514}
9515
Artem Serovc5fcb442016-12-02 19:19:58 +00009516VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009517 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009518}
9519
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009520VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9521 const DexFile& dex_file,
9522 dex::StringIndex string_index,
9523 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009524 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009525 return jit_string_patches_.GetOrCreate(
9526 StringReference(&dex_file, string_index),
9527 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009528 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009529 });
9530}
9531
9532VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9533 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009534 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009535 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009536 return jit_class_patches_.GetOrCreate(
9537 TypeReference(&dex_file, type_index),
9538 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009539 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009540 });
9541}
9542
Vladimir Marko6fd16062018-06-26 11:02:04 +01009543void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
9544 uint32_t boot_image_reference) {
9545 if (GetCompilerOptions().IsBootImage()) {
9546 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9547 NewBootImageIntrinsicPatch(boot_image_reference);
9548 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01009549 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009550 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9551 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009552 EmitMovwMovtPlaceholder(labels, reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08009553 __ Ldr(reg, MemOperand(reg, /* offset= */ 0));
Vladimir Markoeebb8212018-06-05 14:57:24 +01009554 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01009555 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01009556 gc::Heap* heap = Runtime::Current()->GetHeap();
9557 DCHECK(!heap->GetBootImageSpaces().empty());
9558 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01009559 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009560 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
9561 }
9562}
9563
Vladimir Marko7968cae2021-01-19 12:02:35 +00009564void CodeGeneratorARMVIXL::LoadTypeForBootImageIntrinsic(vixl::aarch32::Register reg,
9565 TypeReference target_type) {
9566 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
9567 DCHECK(GetCompilerOptions().IsBootImage());
9568 PcRelativePatchInfo* labels =
9569 NewBootImageTypePatch(*target_type.dex_file, target_type.TypeIndex());
9570 EmitMovwMovtPlaceholder(labels, reg);
9571}
9572
Vladimir Markode91ca92020-10-27 13:41:40 +00009573void CodeGeneratorARMVIXL::LoadIntrinsicDeclaringClass(vixl32::Register reg, HInvoke* invoke) {
9574 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009575 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009576 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01009577 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
Vladimir Marko7968cae2021-01-19 12:02:35 +00009578 LoadTypeForBootImageIntrinsic(reg, TypeReference(target_method.dex_file, type_idx));
Vladimir Marko6fd16062018-06-26 11:02:04 +01009579 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00009580 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
9581 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009582 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01009583}
9584
Vladimir Marko7968cae2021-01-19 12:02:35 +00009585void CodeGeneratorARMVIXL::LoadClassRootForIntrinsic(vixl::aarch32::Register reg,
9586 ClassRoot class_root) {
9587 if (GetCompilerOptions().IsBootImage()) {
9588 ScopedObjectAccess soa(Thread::Current());
9589 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
9590 TypeReference target_type(&klass->GetDexFile(), klass->GetDexTypeIndex());
9591 LoadTypeForBootImageIntrinsic(reg, target_type);
9592 } else {
9593 uint32_t boot_image_offset = GetBootImageOffset(class_root);
9594 LoadBootImageAddress(reg, boot_image_offset);
9595 }
9596}
9597
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009598template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009599inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9600 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009601 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009602 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009603 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009604 size_t offset_or_index = info.offset_or_index;
9605 DCHECK(info.add_pc_label.IsBound());
9606 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9607 // Add MOVW patch.
9608 DCHECK(info.movw_label.IsBound());
9609 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009610 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009611 // Add MOVT patch.
9612 DCHECK(info.movt_label.IsBound());
9613 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009614 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009615 }
9616}
9617
Vladimir Marko6fd16062018-06-26 11:02:04 +01009618template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
9619linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
9620 const DexFile* target_dex_file,
9621 uint32_t pc_insn_offset,
9622 uint32_t boot_image_offset) {
9623 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
9624 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00009625}
9626
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009627void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009628 DCHECK(linker_patches->empty());
9629 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009630 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009631 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009632 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009633 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01009634 /* MOVW+MOVT for each entry */ 2u * public_type_bss_entry_patches_.size() +
9635 /* MOVW+MOVT for each entry */ 2u * package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009636 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009637 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01009638 /* MOVW+MOVT for each entry */ 2u * boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01009639 call_entrypoint_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009640 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009641 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01009642 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009643 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009644 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009645 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009646 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009647 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009648 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009649 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009650 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009651 DCHECK(boot_image_type_patches_.empty());
9652 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01009653 }
9654 if (GetCompilerOptions().IsBootImage()) {
9655 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
9656 boot_image_other_patches_, linker_patches);
9657 } else {
9658 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
9659 boot_image_other_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009660 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009661 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9662 method_bss_entry_patches_, linker_patches);
9663 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9664 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01009665 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
9666 public_type_bss_entry_patches_, linker_patches);
9667 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
9668 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009669 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9670 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01009671 for (const PatchInfo<vixl32::Label>& info : call_entrypoint_patches_) {
9672 DCHECK(info.target_dex_file == nullptr);
9673 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
9674 info.label.GetLocation(), info.offset_or_index));
9675 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009676 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009677 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9678 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009679 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009680 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009681}
9682
Vladimir Markoca1e0382018-04-11 09:58:41 +00009683bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01009684 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
9685 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00009686 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9687}
9688
9689void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9690 /*out*/ ArenaVector<uint8_t>* code,
9691 /*out*/ std::string* debug_name) {
9692 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9693 switch (patch.GetType()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009694 case linker::LinkerPatch::Type::kCallRelative: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009695 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9696 // to the generic JNI and interpreter trampolines.
Vladimir Markof6675082019-05-17 12:05:28 +01009697 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
9698 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, vixl32::r0, offset.Int32Value());
Vladimir Markoca1e0382018-04-11 09:58:41 +00009699 assembler.GetVIXLAssembler()->Bkpt(0);
David Srbecky889da942021-04-30 13:03:14 +01009700 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009701 *debug_name = "MethodCallThunk";
9702 }
9703 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009704 }
9705 case linker::LinkerPatch::Type::kCallEntrypoint: {
9706 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, tr, patch.EntrypointOffset());
9707 assembler.GetVIXLAssembler()->Bkpt(0);
David Srbecky889da942021-04-30 13:03:14 +01009708 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009709 *debug_name = "EntrypointCallThunk_" + std::to_string(patch.EntrypointOffset());
9710 }
9711 break;
9712 }
9713 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009714 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9715 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9716 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009717 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009718 default:
9719 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9720 UNREACHABLE();
9721 }
9722
9723 // Ensure we emit the literal pool if any.
9724 assembler.FinalizeCode();
9725 code->resize(assembler.CodeSize());
9726 MemoryRegion code_region(code->data(), code->size());
9727 assembler.FinalizeInstructions(code_region);
9728}
9729
Artem Serovc5fcb442016-12-02 19:19:58 +00009730VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9731 uint32_t value,
9732 Uint32ToLiteralMap* map) {
9733 return map->GetOrCreate(
9734 value,
9735 [this, value]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009736 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ value);
Artem Serovc5fcb442016-12-02 19:19:58 +00009737 });
9738}
9739
Artem Serov2bbc9532016-10-21 11:51:50 +01009740void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9741 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009742 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009743 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9744 Location::RequiresRegister());
9745 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9746 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9748}
9749
9750void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9751 vixl32::Register res = OutputRegister(instr);
9752 vixl32::Register accumulator =
9753 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9754 vixl32::Register mul_left =
9755 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9756 vixl32::Register mul_right =
9757 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9758
9759 if (instr->GetOpKind() == HInstruction::kAdd) {
9760 __ Mla(res, mul_left, mul_right, accumulator);
9761 } else {
9762 __ Mls(res, mul_left, mul_right, accumulator);
9763 }
9764}
9765
Artem Serov551b28f2016-10-18 19:11:30 +01009766void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9767 // Nothing to do, this should be removed during prepare for register allocator.
9768 LOG(FATAL) << "Unreachable";
9769}
9770
9771void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9772 // Nothing to do, this should be removed during prepare for register allocator.
9773 LOG(FATAL) << "Unreachable";
9774}
9775
9776// Simple implementation of packed switch - generate cascaded compare/jumps.
9777void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9778 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009779 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009780 locations->SetInAt(0, Location::RequiresRegister());
9781 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9782 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9783 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9784 if (switch_instr->GetStartValue() != 0) {
9785 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9786 }
9787 }
9788}
9789
9790// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9791void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9792 int32_t lower_bound = switch_instr->GetStartValue();
9793 uint32_t num_entries = switch_instr->GetNumEntries();
9794 LocationSummary* locations = switch_instr->GetLocations();
9795 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9796 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9797
9798 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9799 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9800 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009801 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009802 vixl32::Register temp_reg = temps.Acquire();
9803 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9804 // the immediate, because IP is used as the destination register. For the other
9805 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9806 // and they can be encoded in the instruction without making use of IP register.
9807 __ Adds(temp_reg, value_reg, -lower_bound);
9808
9809 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9810 // Jump to successors[0] if value == lower_bound.
9811 __ B(eq, codegen_->GetLabelOf(successors[0]));
9812 int32_t last_index = 0;
9813 for (; num_entries - last_index > 2; last_index += 2) {
9814 __ Adds(temp_reg, temp_reg, -2);
9815 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9816 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9817 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9818 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9819 }
9820 if (num_entries - last_index == 2) {
9821 // The last missing case_value.
9822 __ Cmp(temp_reg, 1);
9823 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9824 }
9825
9826 // And the default for any other value.
9827 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9828 __ B(codegen_->GetLabelOf(default_block));
9829 }
9830 } else {
9831 // Create a table lookup.
9832 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9833
9834 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9835
9836 // Remove the bias.
9837 vixl32::Register key_reg;
9838 if (lower_bound != 0) {
9839 key_reg = RegisterFrom(locations->GetTemp(1));
9840 __ Sub(key_reg, value_reg, lower_bound);
9841 } else {
9842 key_reg = value_reg;
9843 }
9844
9845 // Check whether the value is in the table, jump to default block if not.
9846 __ Cmp(key_reg, num_entries - 1);
9847 __ B(hi, codegen_->GetLabelOf(default_block));
9848
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009849 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009850 vixl32::Register jump_offset = temps.Acquire();
9851
9852 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009853 {
9854 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9855 ExactAssemblyScope aas(GetVIXLAssembler(),
9856 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9857 CodeBufferCheckScope::kMaximumSize);
9858 __ adr(table_base, jump_table->GetTableStartLabel());
9859 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009860
Scott Wakeling86e9d262017-01-18 15:59:24 +00009861 // Jump to target block by branching to table_base(pc related) + offset.
9862 vixl32::Register target_address = table_base;
9863 __ add(target_address, table_base, jump_offset);
9864 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009865
Scott Wakeling86e9d262017-01-18 15:59:24 +00009866 jump_table->EmitTable(codegen_);
9867 }
Artem Serov551b28f2016-10-18 19:11:30 +01009868 }
9869}
9870
Artem Serov02d37832016-10-25 15:25:33 +01009871// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009872void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009873 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009874 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009875 return;
9876 }
9877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009878 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009879
Artem Serovd4cc5b22016-11-04 11:19:09 +00009880 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009881 if (return_loc.Equals(trg)) {
9882 return;
9883 }
9884
Vladimir Markoac3fcff2020-11-17 12:17:58 +00009885 // Let the parallel move resolver take care of all of this.
9886 HParallelMove parallel_move(GetGraph()->GetAllocator());
9887 parallel_move.AddMove(return_loc, trg, type, nullptr);
9888 GetMoveResolver()->EmitNativeCode(&parallel_move);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009889}
Scott Wakelingfe885462016-09-22 10:24:38 +01009890
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009891void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9892 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009893 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009894 locations->SetInAt(0, Location::RequiresRegister());
9895 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009896}
9897
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009898void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9899 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9900 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9901 instruction->GetIndex(), kArmPointerSize).SizeValue();
9902 GetAssembler()->LoadFromOffset(kLoadWord,
9903 OutputRegister(instruction),
9904 InputRegisterAt(instruction, 0),
9905 method_offset);
9906 } else {
9907 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9908 instruction->GetIndex(), kArmPointerSize));
9909 GetAssembler()->LoadFromOffset(kLoadWord,
9910 OutputRegister(instruction),
9911 InputRegisterAt(instruction, 0),
9912 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9913 GetAssembler()->LoadFromOffset(kLoadWord,
9914 OutputRegister(instruction),
9915 OutputRegister(instruction),
9916 method_offset);
9917 }
Artem Serov551b28f2016-10-18 19:11:30 +01009918}
9919
Artem Serovc5fcb442016-12-02 19:19:58 +00009920static void PatchJitRootUse(uint8_t* code,
9921 const uint8_t* roots_data,
9922 VIXLUInt32Literal* literal,
9923 uint64_t index_in_table) {
9924 DCHECK(literal->IsBound());
9925 uint32_t literal_offset = literal->GetLocation();
9926 uintptr_t address =
9927 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9928 uint8_t* data = code + literal_offset;
9929 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9930}
9931
9932void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9933 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009934 const StringReference& string_reference = entry.first;
9935 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009936 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009937 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009938 }
9939 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009940 const TypeReference& type_reference = entry.first;
9941 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009942 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009943 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009944 }
9945}
9946
Artem Serovd4cc5b22016-11-04 11:19:09 +00009947void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9948 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9949 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009950 ExactAssemblyScope aas(GetVIXLAssembler(),
9951 3 * vixl32::kMaxInstructionSizeInBytes,
9952 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009953 // TODO(VIXL): Think about using mov instead of movw.
9954 __ bind(&labels->movw_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009955 __ movw(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009956 __ bind(&labels->movt_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009957 __ movt(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009958 __ bind(&labels->add_pc_label);
9959 __ add(out, out, pc);
9960}
9961
Scott Wakelingfe885462016-09-22 10:24:38 +01009962#undef __
9963#undef QUICK_ENTRY_POINT
9964#undef TODO_VIXL32
9965
Vladimir Markoca1e0382018-04-11 09:58:41 +00009966#define __ assembler.GetVIXLAssembler()->
9967
9968static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9969 vixl32::Register base_reg,
9970 vixl32::MemOperand& lock_word,
9971 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009972 int32_t raw_ldr_offset,
9973 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009974 // Load the lock word containing the rb_state.
9975 __ Ldr(ip, lock_word);
9976 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009977 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009978 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9979 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
Andreas Gampe3db70682018-12-26 15:12:03 -08009980 __ B(ne, slow_path, /* is_far_target= */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009981 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9982 if (throw_npe != nullptr) {
9983 __ Bind(throw_npe);
9984 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009985 __ Add(lr, lr, raw_ldr_offset);
9986 // Introduce a dependency on the lock_word including rb_state,
9987 // to prevent load-load reordering, and without using
9988 // a memory barrier (which would be more expensive).
9989 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9990 __ Bx(lr); // And return back to the function.
9991 // Note: The fake dependency is unnecessary for the slow path.
9992}
9993
9994// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009995static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009996 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009997 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9998 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009999 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
10000 DCHECK_EQ(ip.GetCode(), 12u);
10001 const int32_t entry_point_offset =
10002 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
10003 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +010010004 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +000010005}
10006
10007void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
10008 uint32_t encoded_data,
10009 /*out*/ std::string* debug_name) {
10010 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
10011 switch (kind) {
10012 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +000010013 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
10014 CheckValidReg(base_reg.GetCode());
10015 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
10016 CheckValidReg(holder_reg.GetCode());
10017 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
10018 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
10019 temps.Exclude(ip);
Roland Levillain988c3912019-09-25 19:33:35 +010010020 // In the case of a field load, if `base_reg` differs from
10021 // `holder_reg`, the offset was too large and we must have emitted (during the construction
10022 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
10023 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
10024 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
10025 // not necessarily do that check before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +010010026 vixl32::Label throw_npe_label;
10027 vixl32::Label* throw_npe = nullptr;
10028 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
10029 throw_npe = &throw_npe_label;
Andreas Gampe3db70682018-12-26 15:12:03 -080010030 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +000010031 }
Vladimir Marko7a695052018-04-12 10:26:50 +010010032 // Check if the holder is gray and, if not, add fake dependency to the base register
10033 // and return to the LDR instruction to load the reference. Otherwise, use introspection
10034 // to load the reference and call the entrypoint that performs further checks on the
10035 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +000010036 vixl32::Label slow_path;
10037 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
10038 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
10039 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
10040 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +010010041 EmitGrayCheckAndFastPath(
10042 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +000010043 __ Bind(&slow_path);
10044 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
10045 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +010010046 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +000010047 if (width == BakerReadBarrierWidth::kWide) {
10048 MemOperand ldr_half_address(lr, ldr_offset + 2);
10049 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
10050 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
10051 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
10052 } else {
10053 MemOperand ldr_address(lr, ldr_offset);
10054 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
10055 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
10056 ep_reg, // for narrow LDR.
10057 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
10058 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
10059 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
10060 }
10061 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
10062 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +000010063 break;
10064 }
10065 case BakerReadBarrierKind::kArray: {
10066 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
10067 CheckValidReg(base_reg.GetCode());
10068 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10069 BakerReadBarrierSecondRegField::Decode(encoded_data));
10070 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10071 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
10072 temps.Exclude(ip);
10073 vixl32::Label slow_path;
10074 int32_t data_offset =
10075 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
10076 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
10077 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
10078 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
10079 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
10080 __ Bind(&slow_path);
10081 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
10082 raw_ldr_offset;
10083 MemOperand ldr_address(lr, ldr_offset + 2);
10084 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
10085 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +010010086 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +000010087 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
10088 // a switch case target based on the index register.
10089 __ Mov(ip, base_reg); // Move the base register to ip0.
10090 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
10091 break;
10092 }
Vladimir Markod887ed82018-08-14 13:52:12 +000010093 case BakerReadBarrierKind::kGcRoot:
Vladimir Marko3d350a82020-11-18 14:14:27 +000010094 case BakerReadBarrierKind::kIntrinsicCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +000010095 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
10096 // and it does not have a forwarding address), call the correct introspection entrypoint;
10097 // otherwise return the reference (or the extracted forwarding address).
10098 // There is no gray bit check for GC roots.
10099 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
10100 CheckValidReg(root_reg.GetCode());
10101 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10102 BakerReadBarrierSecondRegField::Decode(encoded_data));
10103 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
10104 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
10105 temps.Exclude(ip);
10106 vixl32::Label return_label, not_marked, forwarding_address;
Andreas Gampe3db70682018-12-26 15:12:03 -080010107 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +000010108 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
10109 __ Ldr(ip, lock_word);
10110 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
10111 __ B(eq, &not_marked);
10112 __ Bind(&return_label);
10113 __ Bx(lr);
10114 __ Bind(&not_marked);
10115 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
10116 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
10117 " the highest bits and the 'forwarding address' state to have all bits set");
10118 __ Cmp(ip, Operand(0xc0000000));
10119 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +010010120 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Marko3d350a82020-11-18 14:14:27 +000010121 // Adjust the art_quick_read_barrier_mark_introspection address
10122 // in kBakerCcEntrypointRegister to one of
10123 // art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},intrinsic_cas}.
10124 if (kind == BakerReadBarrierKind::kIntrinsicCas) {
10125 DCHECK(width == BakerReadBarrierWidth::kWide);
10126 DCHECK(!root_reg.IsLow());
10127 }
Vladimir Markod887ed82018-08-14 13:52:12 +000010128 int32_t entrypoint_offset =
10129 (kind == BakerReadBarrierKind::kGcRoot)
10130 ? (width == BakerReadBarrierWidth::kWide)
10131 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
10132 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
Vladimir Marko3d350a82020-11-18 14:14:27 +000010133 : BAKER_MARK_INTROSPECTION_INTRINSIC_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +000010134 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
10135 __ Mov(ip, root_reg);
10136 __ Bx(ep_reg);
10137 __ Bind(&forwarding_address);
10138 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
10139 __ Bx(lr);
10140 break;
10141 }
10142 default:
10143 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
10144 UNREACHABLE();
10145 }
10146
Vladimir Marko966b46f2018-08-03 10:20:19 +000010147 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +010010148 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +010010149 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +000010150 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +000010151 std::ostringstream oss;
10152 oss << "BakerReadBarrierThunk";
10153 switch (kind) {
10154 case BakerReadBarrierKind::kField:
10155 oss << "Field";
10156 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
10157 oss << "Wide";
10158 }
10159 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
10160 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
10161 break;
10162 case BakerReadBarrierKind::kArray:
10163 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10164 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10165 BakerReadBarrierSecondRegField::Decode(encoded_data));
10166 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10167 break;
10168 case BakerReadBarrierKind::kGcRoot:
10169 oss << "GcRoot";
10170 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
10171 oss << "Wide";
10172 }
10173 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10174 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10175 BakerReadBarrierSecondRegField::Decode(encoded_data));
10176 break;
Vladimir Marko3d350a82020-11-18 14:14:27 +000010177 case BakerReadBarrierKind::kIntrinsicCas:
10178 oss << "IntrinsicCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markod887ed82018-08-14 13:52:12 +000010179 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10180 BakerReadBarrierSecondRegField::Decode(encoded_data));
10181 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10182 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +000010183 }
10184 *debug_name = oss.str();
10185 }
10186}
10187
10188#undef __
10189
Scott Wakelingfe885462016-09-22 10:24:38 +010010190} // namespace arm
10191} // namespace art