blob: 940f521108d8f0c0913caa2968ebf47eea6e00ae [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 Marko94ec2db2017-09-06 17:21:03 +010025#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010026#include "code_generator_utils.h"
27#include "common_arm.h"
28#include "compiled_method.h"
29#include "entrypoints/quick/quick_entrypoints.h"
30#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010031#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070032#include "heap_poisoning.h"
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +010033#include "interpreter/mterp/nterp.h"
Vladimir Marko6fd16062018-06-26 11:02:04 +010034#include "intrinsics.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010035#include "intrinsics_arm_vixl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010036#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010037#include "mirror/array-inl.h"
38#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000039#include "scoped_thread_state_change-inl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010040#include "thread.h"
41#include "utils/arm/assembler_arm_vixl.h"
42#include "utils/arm/managed_register_arm.h"
43#include "utils/assembler.h"
44#include "utils/stack_checks.h"
45
Vladimir Marko0a516052019-10-14 13:00:44 +000046namespace art {
Scott Wakelingfe885462016-09-22 10:24:38 +010047namespace arm {
48
49namespace vixl32 = vixl::aarch32;
50using namespace vixl32; // NOLINT(build/namespaces)
51
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010052using helpers::DRegisterFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080054using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010055using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010056using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010057using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010058using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000059using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010060using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000061using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000062using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010063using helpers::LocationFrom;
64using helpers::LowRegisterFrom;
65using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080066using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010067using helpers::OutputRegister;
68using helpers::OutputSRegister;
69using helpers::OutputVRegister;
70using helpers::RegisterFrom;
71using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000072using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010073
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +010074using vixl::EmissionCheckScope;
Artem Serov0fb37192016-12-06 18:13:40 +000075using vixl::ExactAssemblyScope;
76using vixl::CodeBufferCheckScope;
77
Scott Wakelingfe885462016-09-22 10:24:38 +010078using RegisterList = vixl32::RegisterList;
79
80static bool ExpectedPairLayout(Location location) {
81 // We expected this for both core and fpu register pairs.
82 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
83}
Artem Serovd4cc5b22016-11-04 11:19:09 +000084// Use a local definition to prevent copying mistakes.
85static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
86static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010087static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010088
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010089// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
90// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010091// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010092// the offset explicitly.
93constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
94
Roland Levillain5daa4952017-07-03 17:23:56 +010095// Using a base helps identify when we hit Marking Register check breakpoints.
96constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
97
Scott Wakelingfe885462016-09-22 10:24:38 +010098#ifdef __
99#error "ARM Codegen VIXL macro-assembler macro already defined."
100#endif
101
Scott Wakelingfe885462016-09-22 10:24:38 +0100102// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
103#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
104#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
105
106// Marker that code is yet to be, and must, be implemented.
107#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
108
Vladimir Marko88abba22017-05-03 17:09:25 +0100109static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
110 return rt.IsLow() && rn.IsLow() && offset < 32u;
111}
112
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100113class EmitAdrCode {
114 public:
115 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
116 : assembler_(assembler), rd_(rd), label_(label) {
Vladimir Markod887ed82018-08-14 13:52:12 +0000117 DCHECK(!assembler->AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100118 adr_location_ = assembler->GetCursorOffset();
119 assembler->adr(EncodingSize(Wide), rd, label);
120 }
121
122 ~EmitAdrCode() {
123 DCHECK(label_->IsBound());
124 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
125 // TODO: Maybe extend VIXL to allow ADR for return address?
126 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
127 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
128 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
129 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
130 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
131 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
132 // Add the Thumb mode bit.
133 raw_adr[2] |= 0x01u;
134 }
135
136 private:
137 ArmVIXLMacroAssembler* const assembler_;
138 vixl32::Register rd_;
139 vixl32::Label* const label_;
140 int32_t adr_location_;
141};
142
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100143static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
144 InvokeRuntimeCallingConventionARMVIXL calling_convention;
145 RegisterSet caller_saves = RegisterSet::Empty();
146 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
147 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
148 // that the the kPrimNot result register is the same as the first argument register.
149 return caller_saves;
150}
151
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100152// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
153// for each live D registers they treat two corresponding S registers as live ones.
154//
155// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
156// from a list of contiguous S registers a list of contiguous D registers (processing first/last
157// S registers corner cases) and save/restore this new list treating them as D registers.
158// - decreasing code size
159// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
160// restored and then used in regular non SlowPath code as D register.
161//
162// For the following example (v means the S register is live):
163// D names: | D0 | D1 | D2 | D4 | ...
164// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
165// Live? | | v | v | v | v | v | v | | ...
166//
167// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
168// as D registers.
169//
170// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
171// for lists of floating-point registers.
172static size_t SaveContiguousSRegisterList(size_t first,
173 size_t last,
174 CodeGenerator* codegen,
175 size_t stack_offset) {
176 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
177 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
178 DCHECK_LE(first, last);
179 if ((first == last) && (first == 0)) {
180 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
181 return stack_offset + kSRegSizeInBytes;
182 }
183 if (first % 2 == 1) {
184 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
185 stack_offset += kSRegSizeInBytes;
186 }
187
188 bool save_last = false;
189 if (last % 2 == 0) {
190 save_last = true;
191 --last;
192 }
193
194 if (first < last) {
195 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
196 DCHECK_EQ((last - first + 1) % 2, 0u);
197 size_t number_of_d_regs = (last - first + 1) / 2;
198
199 if (number_of_d_regs == 1) {
200 __ Vstr(d_reg, MemOperand(sp, stack_offset));
201 } else if (number_of_d_regs > 1) {
202 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
203 vixl32::Register base = sp;
204 if (stack_offset != 0) {
205 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000206 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100207 }
208 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
209 }
210 stack_offset += number_of_d_regs * kDRegSizeInBytes;
211 }
212
213 if (save_last) {
214 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
215 stack_offset += kSRegSizeInBytes;
216 }
217
218 return stack_offset;
219}
220
221static size_t RestoreContiguousSRegisterList(size_t first,
222 size_t last,
223 CodeGenerator* codegen,
224 size_t stack_offset) {
225 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
226 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
227 DCHECK_LE(first, last);
228 if ((first == last) && (first == 0)) {
229 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
230 return stack_offset + kSRegSizeInBytes;
231 }
232 if (first % 2 == 1) {
233 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
234 stack_offset += kSRegSizeInBytes;
235 }
236
237 bool restore_last = false;
238 if (last % 2 == 0) {
239 restore_last = true;
240 --last;
241 }
242
243 if (first < last) {
244 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
245 DCHECK_EQ((last - first + 1) % 2, 0u);
246 size_t number_of_d_regs = (last - first + 1) / 2;
247 if (number_of_d_regs == 1) {
248 __ Vldr(d_reg, MemOperand(sp, stack_offset));
249 } else if (number_of_d_regs > 1) {
250 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
251 vixl32::Register base = sp;
252 if (stack_offset != 0) {
253 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000254 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100255 }
256 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
257 }
258 stack_offset += number_of_d_regs * kDRegSizeInBytes;
259 }
260
261 if (restore_last) {
262 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
263 stack_offset += kSRegSizeInBytes;
264 }
265
266 return stack_offset;
267}
268
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100269static LoadOperandType GetLoadOperandType(DataType::Type type) {
270 switch (type) {
271 case DataType::Type::kReference:
272 return kLoadWord;
273 case DataType::Type::kBool:
274 case DataType::Type::kUint8:
275 return kLoadUnsignedByte;
276 case DataType::Type::kInt8:
277 return kLoadSignedByte;
278 case DataType::Type::kUint16:
279 return kLoadUnsignedHalfword;
280 case DataType::Type::kInt16:
281 return kLoadSignedHalfword;
282 case DataType::Type::kInt32:
283 return kLoadWord;
284 case DataType::Type::kInt64:
285 return kLoadWordPair;
286 case DataType::Type::kFloat32:
287 return kLoadSWord;
288 case DataType::Type::kFloat64:
289 return kLoadDWord;
290 default:
291 LOG(FATAL) << "Unreachable type " << type;
292 UNREACHABLE();
293 }
294}
295
296static StoreOperandType GetStoreOperandType(DataType::Type type) {
297 switch (type) {
298 case DataType::Type::kReference:
299 return kStoreWord;
300 case DataType::Type::kBool:
301 case DataType::Type::kUint8:
302 case DataType::Type::kInt8:
303 return kStoreByte;
304 case DataType::Type::kUint16:
305 case DataType::Type::kInt16:
306 return kStoreHalfword;
307 case DataType::Type::kInt32:
308 return kStoreWord;
309 case DataType::Type::kInt64:
310 return kStoreWordPair;
311 case DataType::Type::kFloat32:
312 return kStoreSWord;
313 case DataType::Type::kFloat64:
314 return kStoreDWord;
315 default:
316 LOG(FATAL) << "Unreachable type " << type;
317 UNREACHABLE();
318 }
319}
320
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100321void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
322 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
323 size_t orig_offset = stack_offset;
324
Andreas Gampe3db70682018-12-26 15:12:03 -0800325 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100326 for (uint32_t i : LowToHighBits(core_spills)) {
327 // If the register holds an object, update the stack mask.
328 if (locations->RegisterContainsObject(i)) {
329 locations->SetStackBit(stack_offset / kVRegSize);
330 }
331 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
332 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
333 saved_core_stack_offsets_[i] = stack_offset;
334 stack_offset += kArmWordSize;
335 }
336
337 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
338 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
339
Andreas Gampe3db70682018-12-26 15:12:03 -0800340 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100341 orig_offset = stack_offset;
342 for (uint32_t i : LowToHighBits(fp_spills)) {
343 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
344 saved_fpu_stack_offsets_[i] = stack_offset;
345 stack_offset += kArmWordSize;
346 }
347
348 stack_offset = orig_offset;
349 while (fp_spills != 0u) {
350 uint32_t begin = CTZ(fp_spills);
351 uint32_t tmp = fp_spills + (1u << begin);
352 fp_spills &= tmp; // Clear the contiguous range of 1s.
353 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
354 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
355 }
356 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
357}
358
359void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
360 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
361 size_t orig_offset = stack_offset;
362
Andreas Gampe3db70682018-12-26 15:12:03 -0800363 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100364 for (uint32_t i : LowToHighBits(core_spills)) {
365 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
366 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
367 stack_offset += kArmWordSize;
368 }
369
370 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
371 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
372 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
373
Andreas Gampe3db70682018-12-26 15:12:03 -0800374 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100375 while (fp_spills != 0u) {
376 uint32_t begin = CTZ(fp_spills);
377 uint32_t tmp = fp_spills + (1u << begin);
378 fp_spills &= tmp; // Clear the contiguous range of 1s.
379 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
380 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
381 }
382 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
383}
384
385class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
386 public:
387 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100389 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100390 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
391 __ Bind(GetEntryLabel());
392 if (instruction_->CanThrowIntoCatchBlock()) {
393 // Live registers will be restored in the catch block if caught.
394 SaveLiveRegisters(codegen, instruction_->GetLocations());
395 }
396 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
397 instruction_,
398 instruction_->GetDexPc(),
399 this);
400 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
401 }
402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100403 bool IsFatal() const override { return true; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100405 const char* GetDescription() const override { return "NullCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100406
407 private:
408 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
409};
410
Scott Wakelingfe885462016-09-22 10:24:38 +0100411class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
412 public:
413 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
414 : SlowPathCodeARMVIXL(instruction) {}
415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100416 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100417 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100418 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100419 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100420 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
421 }
422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100423 bool IsFatal() const override { return true; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100425 const char* GetDescription() const override { return "DivZeroCheckSlowPathARMVIXL"; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100426
427 private:
428 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
429};
430
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100431class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
432 public:
433 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
434 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100436 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100437 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
438 __ Bind(GetEntryLabel());
439 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
440 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
441 if (successor_ == nullptr) {
442 __ B(GetReturnLabel());
443 } else {
444 __ B(arm_codegen->GetLabelOf(successor_));
445 }
446 }
447
448 vixl32::Label* GetReturnLabel() {
449 DCHECK(successor_ == nullptr);
450 return &return_label_;
451 }
452
453 HBasicBlock* GetSuccessor() const {
454 return successor_;
455 }
456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100457 const char* GetDescription() const override { return "SuspendCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100458
459 private:
460 // If not null, the block to branch to after the suspend check.
461 HBasicBlock* const successor_;
462
463 // If `successor_` is null, the label to branch to after the suspend check.
464 vixl32::Label return_label_;
465
466 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
467};
468
Scott Wakelingc34dba72016-10-03 10:14:44 +0100469class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
470 public:
471 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
472 : SlowPathCodeARMVIXL(instruction) {}
473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100475 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
476 LocationSummary* locations = instruction_->GetLocations();
477
478 __ Bind(GetEntryLabel());
479 if (instruction_->CanThrowIntoCatchBlock()) {
480 // Live registers will be restored in the catch block if caught.
481 SaveLiveRegisters(codegen, instruction_->GetLocations());
482 }
483 // We're moving two locations to locations that could overlap, so we need a parallel
484 // move resolver.
485 InvokeRuntimeCallingConventionARMVIXL calling_convention;
486 codegen->EmitParallelMoves(
487 locations->InAt(0),
488 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100489 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100490 locations->InAt(1),
491 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100493 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
494 ? kQuickThrowStringBounds
495 : kQuickThrowArrayBounds;
496 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
497 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
498 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
499 }
500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100501 bool IsFatal() const override { return true; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100503 const char* GetDescription() const override { return "BoundsCheckSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100504
505 private:
506 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
507};
508
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100509class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
510 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100511 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at)
512 : SlowPathCodeARMVIXL(at), cls_(cls) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100513 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100514 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100515 }
516
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100517 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000518 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000519 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100520 const uint32_t dex_pc = instruction_->GetDexPc();
521 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
522 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100523
524 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
525 __ Bind(GetEntryLabel());
526 SaveLiveRegisters(codegen, locations);
527
528 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100529 if (must_resolve_type) {
530 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm_codegen->GetGraph()->GetDexFile()));
531 dex::TypeIndex type_index = cls_->GetTypeIndex();
532 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100533 if (cls_->NeedsAccessCheck()) {
534 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
535 arm_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
536 } else {
537 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
538 arm_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
539 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100540 // If we also must_do_clinit, the resolved type is now in the correct register.
541 } else {
542 DCHECK(must_do_clinit);
543 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
544 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), source);
545 }
546 if (must_do_clinit) {
547 arm_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
548 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100549 }
550
551 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100552 if (out.IsValid()) {
553 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
554 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
555 }
556 RestoreLiveRegisters(codegen, locations);
557 __ B(GetExitLabel());
558 }
559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100560 const char* GetDescription() const override { return "LoadClassSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100561
562 private:
563 // The class this slow path will load.
564 HLoadClass* const cls_;
565
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100566 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
567};
568
Artem Serovd4cc5b22016-11-04 11:19:09 +0000569class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
570 public:
571 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
572 : SlowPathCodeARMVIXL(instruction) {}
573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100574 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000575 DCHECK(instruction_->IsLoadString());
576 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000577 LocationSummary* locations = instruction_->GetLocations();
578 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000579 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000580
581 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
582 __ Bind(GetEntryLabel());
583 SaveLiveRegisters(codegen, locations);
584
585 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000586 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000587 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
588 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
589
Artem Serovd4cc5b22016-11-04 11:19:09 +0000590 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
591 RestoreLiveRegisters(codegen, locations);
592
593 __ B(GetExitLabel());
594 }
595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100596 const char* GetDescription() const override { return "LoadStringSlowPathARMVIXL"; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000597
598 private:
599 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
600};
601
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100602class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
603 public:
604 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
605 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
606
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100607 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100608 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100609 DCHECK(instruction_->IsCheckCast()
610 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
611
612 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
613 __ Bind(GetEntryLabel());
614
Vladimir Marko87584542017-12-12 17:47:52 +0000615 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100616 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100617 }
618
619 // We're moving two locations to locations that could overlap, so we need a parallel
620 // move resolver.
621 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100622
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800623 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800624 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100625 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800626 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800627 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100628 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100630 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
631 instruction_,
632 instruction_->GetDexPc(),
633 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800634 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100635 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100636 } else {
637 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800638 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
639 instruction_,
640 instruction_->GetDexPc(),
641 this);
642 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100643 }
644
645 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100646 RestoreLiveRegisters(codegen, locations);
647 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100648 }
649 }
650
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100651 const char* GetDescription() const override { return "TypeCheckSlowPathARMVIXL"; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100652
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100653 bool IsFatal() const override { return is_fatal_; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100654
655 private:
656 const bool is_fatal_;
657
658 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
659};
660
Scott Wakelingc34dba72016-10-03 10:14:44 +0100661class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
662 public:
663 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
664 : SlowPathCodeARMVIXL(instruction) {}
665
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100666 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100667 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
668 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100669 LocationSummary* locations = instruction_->GetLocations();
670 SaveLiveRegisters(codegen, locations);
671 InvokeRuntimeCallingConventionARMVIXL calling_convention;
672 __ Mov(calling_convention.GetRegisterAt(0),
673 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
674
Scott Wakelingc34dba72016-10-03 10:14:44 +0100675 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100676 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100677 }
678
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100679 const char* GetDescription() const override { return "DeoptimizationSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100680
681 private:
682 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
683};
684
685class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
686 public:
687 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
688
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100689 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100690 LocationSummary* locations = instruction_->GetLocations();
691 __ Bind(GetEntryLabel());
692 SaveLiveRegisters(codegen, locations);
693
694 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100696 parallel_move.AddMove(
697 locations->InAt(0),
698 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100699 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100700 nullptr);
701 parallel_move.AddMove(
702 locations->InAt(1),
703 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100704 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100705 nullptr);
706 parallel_move.AddMove(
707 locations->InAt(2),
708 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100709 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100710 nullptr);
711 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
712
713 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
714 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
715 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
716 RestoreLiveRegisters(codegen, locations);
717 __ B(GetExitLabel());
718 }
719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100720 const char* GetDescription() const override { return "ArraySetSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100721
722 private:
723 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
724};
725
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000726// Slow path generating a read barrier for a heap reference.
727class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
728 public:
729 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
730 Location out,
731 Location ref,
732 Location obj,
733 uint32_t offset,
734 Location index)
735 : SlowPathCodeARMVIXL(instruction),
736 out_(out),
737 ref_(ref),
738 obj_(obj),
739 offset_(offset),
740 index_(index) {
741 DCHECK(kEmitCompilerReadBarrier);
742 // If `obj` is equal to `out` or `ref`, it means the initial object
743 // has been overwritten by (or after) the heap object reference load
744 // to be instrumented, e.g.:
745 //
746 // __ LoadFromOffset(kLoadWord, out, out, offset);
747 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
748 //
749 // In that case, we have lost the information about the original
750 // object, and the emitted read barrier cannot work properly.
751 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
752 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
753 }
754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100755 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000756 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
757 LocationSummary* locations = instruction_->GetLocations();
758 vixl32::Register reg_out = RegisterFrom(out_);
759 DCHECK(locations->CanCall());
760 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
761 DCHECK(instruction_->IsInstanceFieldGet() ||
762 instruction_->IsStaticFieldGet() ||
763 instruction_->IsArrayGet() ||
764 instruction_->IsInstanceOf() ||
765 instruction_->IsCheckCast() ||
Vladimir Marko94d2c812020-11-05 10:04:45 +0000766 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000767 << "Unexpected instruction in read barrier for heap reference slow path: "
768 << instruction_->DebugName();
769 // The read barrier instrumentation of object ArrayGet
770 // instructions does not support the HIntermediateAddress
771 // instruction.
772 DCHECK(!(instruction_->IsArrayGet() &&
773 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
774
775 __ Bind(GetEntryLabel());
776 SaveLiveRegisters(codegen, locations);
777
778 // We may have to change the index's value, but as `index_` is a
779 // constant member (like other "inputs" of this slow path),
780 // introduce a copy of it, `index`.
781 Location index = index_;
782 if (index_.IsValid()) {
783 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
784 if (instruction_->IsArrayGet()) {
785 // Compute the actual memory offset and store it in `index`.
786 vixl32::Register index_reg = RegisterFrom(index_);
787 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
788 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
789 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +0100790 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
791 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000792 // not been saved by the previous call to
793 // art::SlowPathCode::SaveLiveRegisters, as it is a
794 // callee-save register --
795 // art::SlowPathCode::SaveLiveRegisters does not consider
796 // callee-save registers, as it has been designed with the
797 // assumption that callee-save registers are supposed to be
798 // handled by the called function. So, as a callee-save
799 // register, `index_reg` _would_ eventually be saved onto
800 // the stack, but it would be too late: we would have
801 // changed its value earlier. Therefore, we manually save
802 // it here into another freely available register,
803 // `free_reg`, chosen of course among the caller-save
804 // registers (as a callee-save `free_reg` register would
805 // exhibit the same problem).
806 //
807 // Note we could have requested a temporary register from
808 // the register allocator instead; but we prefer not to, as
809 // this is a slow path, and we know we can find a
810 // caller-save register that is available.
811 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
812 __ Mov(free_reg, index_reg);
813 index_reg = free_reg;
814 index = LocationFrom(index_reg);
815 } else {
816 // The initial register stored in `index_` has already been
817 // saved in the call to art::SlowPathCode::SaveLiveRegisters
818 // (as it is not a callee-save register), so we can freely
819 // use it.
820 }
821 // Shifting the index value contained in `index_reg` by the scale
822 // factor (2) cannot overflow in practice, as the runtime is
823 // unable to allocate object arrays with a size larger than
824 // 2^26 - 1 (that is, 2^28 - 4 bytes).
825 __ Lsl(index_reg, index_reg, TIMES_4);
826 static_assert(
827 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
828 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
829 __ Add(index_reg, index_reg, offset_);
830 } else {
831 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
832 // intrinsics, `index_` is not shifted by a scale factor of 2
833 // (as in the case of ArrayGet), as it is actually an offset
834 // to an object field within an object.
835 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
836 DCHECK(instruction_->GetLocations()->Intrinsified());
837 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
838 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
839 << instruction_->AsInvoke()->GetIntrinsic();
840 DCHECK_EQ(offset_, 0U);
841 DCHECK(index_.IsRegisterPair());
842 // UnsafeGet's offset location is a register pair, the low
843 // part contains the correct offset.
844 index = index_.ToLow();
845 }
846 }
847
848 // We're moving two or three locations to locations that could
849 // overlap, so we need a parallel move resolver.
850 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100851 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000852 parallel_move.AddMove(ref_,
853 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000855 nullptr);
856 parallel_move.AddMove(obj_,
857 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100858 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000859 nullptr);
860 if (index.IsValid()) {
861 parallel_move.AddMove(index,
862 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100863 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000864 nullptr);
865 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
866 } else {
867 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
868 __ Mov(calling_convention.GetRegisterAt(2), offset_);
869 }
870 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
871 CheckEntrypointTypes<
872 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
873 arm_codegen->Move32(out_, LocationFrom(r0));
874
875 RestoreLiveRegisters(codegen, locations);
876 __ B(GetExitLabel());
877 }
878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100879 const char* GetDescription() const override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000880 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
881 }
882
883 private:
884 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
885 uint32_t ref = RegisterFrom(ref_).GetCode();
886 uint32_t obj = RegisterFrom(obj_).GetCode();
887 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
888 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
889 return vixl32::Register(i);
890 }
891 }
892 // We shall never fail to find a free caller-save register, as
893 // there are more than two core caller-save registers on ARM
894 // (meaning it is possible to find one which is different from
895 // `ref` and `obj`).
896 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
897 LOG(FATAL) << "Could not find a free caller-save register";
898 UNREACHABLE();
899 }
900
901 const Location out_;
902 const Location ref_;
903 const Location obj_;
904 const uint32_t offset_;
905 // An additional location containing an index to an array.
906 // Only used for HArrayGet and the UnsafeGetObject &
907 // UnsafeGetObjectVolatile intrinsics.
908 const Location index_;
909
910 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
911};
912
913// Slow path generating a read barrier for a GC root.
914class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
915 public:
916 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
917 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
918 DCHECK(kEmitCompilerReadBarrier);
919 }
920
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100921 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000922 LocationSummary* locations = instruction_->GetLocations();
923 vixl32::Register reg_out = RegisterFrom(out_);
924 DCHECK(locations->CanCall());
925 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
926 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
927 << "Unexpected instruction in read barrier for GC root slow path: "
928 << instruction_->DebugName();
929
930 __ Bind(GetEntryLabel());
931 SaveLiveRegisters(codegen, locations);
932
933 InvokeRuntimeCallingConventionARMVIXL calling_convention;
934 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
935 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
936 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
937 instruction_,
938 instruction_->GetDexPc(),
939 this);
940 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
941 arm_codegen->Move32(out_, LocationFrom(r0));
942
943 RestoreLiveRegisters(codegen, locations);
944 __ B(GetExitLabel());
945 }
946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100947 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARMVIXL"; }
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000948
949 private:
950 const Location out_;
951 const Location root_;
952
953 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
954};
Scott Wakelingc34dba72016-10-03 10:14:44 +0100955
Scott Wakelingfe885462016-09-22 10:24:38 +0100956inline vixl32::Condition ARMCondition(IfCondition cond) {
957 switch (cond) {
958 case kCondEQ: return eq;
959 case kCondNE: return ne;
960 case kCondLT: return lt;
961 case kCondLE: return le;
962 case kCondGT: return gt;
963 case kCondGE: return ge;
964 case kCondB: return lo;
965 case kCondBE: return ls;
966 case kCondA: return hi;
967 case kCondAE: return hs;
968 }
969 LOG(FATAL) << "Unreachable";
970 UNREACHABLE();
971}
972
973// Maps signed condition to unsigned condition.
974inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
975 switch (cond) {
976 case kCondEQ: return eq;
977 case kCondNE: return ne;
978 // Signed to unsigned.
979 case kCondLT: return lo;
980 case kCondLE: return ls;
981 case kCondGT: return hi;
982 case kCondGE: return hs;
983 // Unsigned remain unchanged.
984 case kCondB: return lo;
985 case kCondBE: return ls;
986 case kCondA: return hi;
987 case kCondAE: return hs;
988 }
989 LOG(FATAL) << "Unreachable";
990 UNREACHABLE();
991}
992
993inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
994 // The ARM condition codes can express all the necessary branches, see the
995 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
996 // There is no dex instruction or HIR that would need the missing conditions
997 // "equal or unordered" or "not equal".
998 switch (cond) {
999 case kCondEQ: return eq;
1000 case kCondNE: return ne /* unordered */;
1001 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1002 case kCondLE: return gt_bias ? ls : le /* unordered */;
1003 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1004 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1005 default:
1006 LOG(FATAL) << "UNREACHABLE";
1007 UNREACHABLE();
1008 }
1009}
1010
Anton Kirilov74234da2017-01-13 14:42:47 +00001011inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1012 switch (op_kind) {
1013 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1014 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1015 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1016 default:
1017 LOG(FATAL) << "Unexpected op kind " << op_kind;
1018 UNREACHABLE();
1019 }
1020}
1021
Scott Wakelingfe885462016-09-22 10:24:38 +01001022void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1023 stream << vixl32::Register(reg);
1024}
1025
1026void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1027 stream << vixl32::SRegister(reg);
1028}
1029
Vladimir Markoa0431112018-06-25 09:32:54 +01001030const ArmInstructionSetFeatures& CodeGeneratorARMVIXL::GetInstructionSetFeatures() const {
1031 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArmInstructionSetFeatures();
1032}
1033
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001034static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001035 uint32_t mask = 0;
1036 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1037 i <= regs.GetLastSRegister().GetCode();
1038 ++i) {
1039 mask |= (1 << i);
1040 }
1041 return mask;
1042}
1043
Artem Serovd4cc5b22016-11-04 11:19:09 +00001044// Saves the register in the stack. Returns the size taken on stack.
1045size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1046 uint32_t reg_id ATTRIBUTE_UNUSED) {
1047 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001048 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001049}
1050
1051// Restores the register from the stack. Returns the size taken on stack.
1052size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1053 uint32_t reg_id ATTRIBUTE_UNUSED) {
1054 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001055 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001056}
1057
1058size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1059 uint32_t reg_id ATTRIBUTE_UNUSED) {
1060 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001061 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001062}
1063
1064size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1065 uint32_t reg_id ATTRIBUTE_UNUSED) {
1066 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001067 UNREACHABLE();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001068}
1069
Anton Kirilov74234da2017-01-13 14:42:47 +00001070static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1071 vixl32::Register out,
1072 vixl32::Register first,
1073 const Operand& second,
1074 CodeGeneratorARMVIXL* codegen) {
1075 if (second.IsImmediate() && second.GetImmediate() == 0) {
1076 const Operand in = kind == HInstruction::kAnd
1077 ? Operand(0)
1078 : Operand(first);
1079
1080 __ Mov(out, in);
1081 } else {
1082 switch (kind) {
1083 case HInstruction::kAdd:
1084 __ Add(out, first, second);
1085 break;
1086 case HInstruction::kAnd:
1087 __ And(out, first, second);
1088 break;
1089 case HInstruction::kOr:
1090 __ Orr(out, first, second);
1091 break;
1092 case HInstruction::kSub:
1093 __ Sub(out, first, second);
1094 break;
1095 case HInstruction::kXor:
1096 __ Eor(out, first, second);
1097 break;
1098 default:
1099 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1100 UNREACHABLE();
1101 }
1102 }
1103}
1104
1105static void GenerateDataProc(HInstruction::InstructionKind kind,
1106 const Location& out,
1107 const Location& first,
1108 const Operand& second_lo,
1109 const Operand& second_hi,
1110 CodeGeneratorARMVIXL* codegen) {
1111 const vixl32::Register first_hi = HighRegisterFrom(first);
1112 const vixl32::Register first_lo = LowRegisterFrom(first);
1113 const vixl32::Register out_hi = HighRegisterFrom(out);
1114 const vixl32::Register out_lo = LowRegisterFrom(out);
1115
1116 if (kind == HInstruction::kAdd) {
1117 __ Adds(out_lo, first_lo, second_lo);
1118 __ Adc(out_hi, first_hi, second_hi);
1119 } else if (kind == HInstruction::kSub) {
1120 __ Subs(out_lo, first_lo, second_lo);
1121 __ Sbc(out_hi, first_hi, second_hi);
1122 } else {
1123 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1124 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1125 }
1126}
1127
1128static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1129 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1130}
1131
1132static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1133 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001134 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001135 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1136
1137 const LocationSummary* const locations = instruction->GetLocations();
1138 const uint32_t shift_value = instruction->GetShiftAmount();
1139 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1140 const Location first = locations->InAt(0);
1141 const Location second = locations->InAt(1);
1142 const Location out = locations->Out();
1143 const vixl32::Register first_hi = HighRegisterFrom(first);
1144 const vixl32::Register first_lo = LowRegisterFrom(first);
1145 const vixl32::Register out_hi = HighRegisterFrom(out);
1146 const vixl32::Register out_lo = LowRegisterFrom(out);
1147 const vixl32::Register second_hi = HighRegisterFrom(second);
1148 const vixl32::Register second_lo = LowRegisterFrom(second);
1149 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1150
1151 if (shift_value >= 32) {
1152 if (shift == ShiftType::LSL) {
1153 GenerateDataProcInstruction(kind,
1154 out_hi,
1155 first_hi,
1156 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1157 codegen);
1158 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1159 } else if (shift == ShiftType::ASR) {
1160 GenerateDataProc(kind,
1161 out,
1162 first,
1163 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1164 Operand(second_hi, ShiftType::ASR, 31),
1165 codegen);
1166 } else {
1167 DCHECK_EQ(shift, ShiftType::LSR);
1168 GenerateDataProc(kind,
1169 out,
1170 first,
1171 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1172 0,
1173 codegen);
1174 }
1175 } else {
1176 DCHECK_GT(shift_value, 1U);
1177 DCHECK_LT(shift_value, 32U);
1178
1179 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1180
1181 if (shift == ShiftType::LSL) {
1182 // We are not doing this for HInstruction::kAdd because the output will require
1183 // Location::kOutputOverlap; not applicable to other cases.
1184 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1185 GenerateDataProcInstruction(kind,
1186 out_hi,
1187 first_hi,
1188 Operand(second_hi, ShiftType::LSL, shift_value),
1189 codegen);
1190 GenerateDataProcInstruction(kind,
1191 out_hi,
1192 out_hi,
1193 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1194 codegen);
1195 GenerateDataProcInstruction(kind,
1196 out_lo,
1197 first_lo,
1198 Operand(second_lo, ShiftType::LSL, shift_value),
1199 codegen);
1200 } else {
1201 const vixl32::Register temp = temps.Acquire();
1202
1203 __ Lsl(temp, second_hi, shift_value);
1204 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1205 GenerateDataProc(kind,
1206 out,
1207 first,
1208 Operand(second_lo, ShiftType::LSL, shift_value),
1209 temp,
1210 codegen);
1211 }
1212 } else {
1213 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1214
1215 // We are not doing this for HInstruction::kAdd because the output will require
1216 // Location::kOutputOverlap; not applicable to other cases.
1217 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1218 GenerateDataProcInstruction(kind,
1219 out_lo,
1220 first_lo,
1221 Operand(second_lo, ShiftType::LSR, shift_value),
1222 codegen);
1223 GenerateDataProcInstruction(kind,
1224 out_lo,
1225 out_lo,
1226 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1227 codegen);
1228 GenerateDataProcInstruction(kind,
1229 out_hi,
1230 first_hi,
1231 Operand(second_hi, shift, shift_value),
1232 codegen);
1233 } else {
1234 const vixl32::Register temp = temps.Acquire();
1235
1236 __ Lsr(temp, second_lo, shift_value);
1237 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1238 GenerateDataProc(kind,
1239 out,
1240 first,
1241 temp,
1242 Operand(second_hi, shift, shift_value),
1243 codegen);
1244 }
1245 }
1246 }
1247}
1248
Donghui Bai426b49c2016-11-08 14:55:38 +08001249static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1250 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1251 if (rhs_loc.IsConstant()) {
1252 // 0.0 is the only immediate that can be encoded directly in
1253 // a VCMP instruction.
1254 //
1255 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1256 // specify that in a floating-point comparison, positive zero
1257 // and negative zero are considered equal, so we can use the
1258 // literal 0.0 for both cases here.
1259 //
1260 // Note however that some methods (Float.equal, Float.compare,
1261 // Float.compareTo, Double.equal, Double.compare,
1262 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1263 // StrictMath.min) consider 0.0 to be (strictly) greater than
1264 // -0.0. So if we ever translate calls to these methods into a
1265 // HCompare instruction, we must handle the -0.0 case with
1266 // care here.
1267 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001271 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001272 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1273 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001275 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1276 }
1277 } else {
1278 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1279 }
1280}
1281
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001282static int64_t AdjustConstantForCondition(int64_t value,
1283 IfCondition* condition,
1284 IfCondition* opposite) {
1285 if (value == 1) {
1286 if (*condition == kCondB) {
1287 value = 0;
1288 *condition = kCondEQ;
1289 *opposite = kCondNE;
1290 } else if (*condition == kCondAE) {
1291 value = 0;
1292 *condition = kCondNE;
1293 *opposite = kCondEQ;
1294 }
1295 } else if (value == -1) {
1296 if (*condition == kCondGT) {
1297 value = 0;
1298 *condition = kCondGE;
1299 *opposite = kCondLT;
1300 } else if (*condition == kCondLE) {
1301 value = 0;
1302 *condition = kCondLT;
1303 *opposite = kCondGE;
1304 }
1305 }
1306
1307 return value;
1308}
1309
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001310static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1311 HCondition* condition,
1312 bool invert,
1313 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001315
1316 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001317 IfCondition cond = condition->GetCondition();
1318 IfCondition opposite = condition->GetOppositeCondition();
1319
1320 if (invert) {
1321 std::swap(cond, opposite);
1322 }
1323
1324 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001325 const Location left = locations->InAt(0);
1326 const Location right = locations->InAt(1);
1327
1328 DCHECK(right.IsConstant());
1329
1330 const vixl32::Register left_high = HighRegisterFrom(left);
1331 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001332 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1333 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1334
1335 // Comparisons against 0 are common enough to deserve special attention.
1336 if (value == 0) {
1337 switch (cond) {
1338 case kCondNE:
1339 // x > 0 iff x != 0 when the comparison is unsigned.
1340 case kCondA:
1341 ret = std::make_pair(ne, eq);
1342 FALLTHROUGH_INTENDED;
1343 case kCondEQ:
1344 // x <= 0 iff x == 0 when the comparison is unsigned.
1345 case kCondBE:
1346 __ Orrs(temps.Acquire(), left_low, left_high);
1347 return ret;
1348 case kCondLT:
1349 case kCondGE:
1350 __ Cmp(left_high, 0);
1351 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1352 // Trivially true or false.
1353 case kCondB:
1354 ret = std::make_pair(ne, eq);
1355 FALLTHROUGH_INTENDED;
1356 case kCondAE:
1357 __ Cmp(left_low, left_low);
1358 return ret;
1359 default:
1360 break;
1361 }
1362 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001363
1364 switch (cond) {
1365 case kCondEQ:
1366 case kCondNE:
1367 case kCondB:
1368 case kCondBE:
1369 case kCondA:
1370 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001371 const uint32_t value_low = Low32Bits(value);
1372 Operand operand_low(value_low);
1373
Donghui Bai426b49c2016-11-08 14:55:38 +08001374 __ Cmp(left_high, High32Bits(value));
1375
Anton Kirilov23b752b2017-07-20 14:40:44 +01001376 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1377 // we must ensure that the operands corresponding to the least significant
1378 // halves of the inputs fit into a 16-bit CMP encoding.
1379 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1380 operand_low = Operand(temps.Acquire());
1381 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1382 }
1383
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001384 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001385 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1386 2 * vixl32::k16BitT32InstructionSizeInBytes,
1387 CodeBufferCheckScope::kExactSize);
1388
1389 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001390 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001391 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001392 break;
1393 }
1394 case kCondLE:
1395 case kCondGT:
1396 // Trivially true or false.
1397 if (value == std::numeric_limits<int64_t>::max()) {
1398 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001399 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001400 break;
1401 }
1402
1403 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001404 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001405 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001406 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001407 } else {
1408 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001409 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001410 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001411 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001412 }
1413
1414 value++;
1415 FALLTHROUGH_INTENDED;
1416 case kCondGE:
1417 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001418 __ Cmp(left_low, Low32Bits(value));
1419 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001420 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001421 break;
1422 }
1423 default:
1424 LOG(FATAL) << "Unreachable";
1425 UNREACHABLE();
1426 }
1427
1428 return ret;
1429}
1430
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001431static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1432 HCondition* condition,
1433 bool invert,
1434 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001435 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001436
1437 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001438 IfCondition cond = condition->GetCondition();
1439 IfCondition opposite = condition->GetOppositeCondition();
1440
1441 if (invert) {
1442 std::swap(cond, opposite);
1443 }
1444
1445 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001446 Location left = locations->InAt(0);
1447 Location right = locations->InAt(1);
1448
1449 DCHECK(right.IsRegisterPair());
1450
1451 switch (cond) {
1452 case kCondEQ:
1453 case kCondNE:
1454 case kCondB:
1455 case kCondBE:
1456 case kCondA:
1457 case kCondAE: {
1458 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1459
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001460 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001461 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1462 2 * vixl32::k16BitT32InstructionSizeInBytes,
1463 CodeBufferCheckScope::kExactSize);
1464
1465 __ it(eq);
1466 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001467 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001468 break;
1469 }
1470 case kCondLE:
1471 case kCondGT:
1472 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001473 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001474 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001475 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001476 } else {
1477 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001478 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001479 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001480 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001481 }
1482
1483 std::swap(left, right);
1484 FALLTHROUGH_INTENDED;
1485 case kCondGE:
1486 case kCondLT: {
1487 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1488
1489 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1490 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001491 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001492 break;
1493 }
1494 default:
1495 LOG(FATAL) << "Unreachable";
1496 UNREACHABLE();
1497 }
1498
1499 return ret;
1500}
1501
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001502static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1503 bool invert,
1504 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001506 IfCondition cond = condition->GetCondition();
1507 IfCondition opposite = condition->GetOppositeCondition();
1508 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001509
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001510 if (invert) {
1511 std::swap(cond, opposite);
1512 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001515 ret = condition->GetLocations()->InAt(1).IsConstant()
1516 ? GenerateLongTestConstant(condition, invert, codegen)
1517 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001518 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001519 GenerateVcmp(condition, codegen);
1520 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1521 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1522 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001523 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001524 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001525 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1526 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001527 }
1528
1529 return ret;
1530}
1531
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001532static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001533 const vixl32::Register out = OutputRegister(cond);
1534 const auto condition = GenerateTest(cond, false, codegen);
1535
1536 __ Mov(LeaveFlags, out, 0);
1537
1538 if (out.IsLow()) {
1539 // We use the scope because of the IT block that follows.
1540 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1541 2 * vixl32::k16BitT32InstructionSizeInBytes,
1542 CodeBufferCheckScope::kExactSize);
1543
1544 __ it(condition.first);
1545 __ mov(condition.first, out, 1);
1546 } else {
1547 vixl32::Label done_label;
1548 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1549
Andreas Gampe3db70682018-12-26 15:12:03 -08001550 __ B(condition.second, final_label, /* is_far_target= */ false);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001551 __ Mov(out, 1);
1552
1553 if (done_label.IsReferenced()) {
1554 __ Bind(&done_label);
1555 }
1556 }
1557}
1558
1559static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001561
1562 const LocationSummary* const locations = cond->GetLocations();
1563 IfCondition condition = cond->GetCondition();
1564 const vixl32::Register out = OutputRegister(cond);
1565 const Location left = locations->InAt(0);
1566 const Location right = locations->InAt(1);
1567 vixl32::Register left_high = HighRegisterFrom(left);
1568 vixl32::Register left_low = LowRegisterFrom(left);
1569 vixl32::Register temp;
1570 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1571
1572 if (right.IsConstant()) {
1573 IfCondition opposite = cond->GetOppositeCondition();
1574 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
1575 &condition,
1576 &opposite);
1577 Operand right_high = High32Bits(value);
1578 Operand right_low = Low32Bits(value);
1579
1580 // The output uses Location::kNoOutputOverlap.
1581 if (out.Is(left_high)) {
1582 std::swap(left_low, left_high);
1583 std::swap(right_low, right_high);
1584 }
1585
1586 __ Sub(out, left_low, right_low);
1587 temp = temps.Acquire();
1588 __ Sub(temp, left_high, right_high);
1589 } else {
1590 DCHECK(right.IsRegisterPair());
1591 temp = temps.Acquire();
1592 __ Sub(temp, left_high, HighRegisterFrom(right));
1593 __ Sub(out, left_low, LowRegisterFrom(right));
1594 }
1595
1596 // Need to check after calling AdjustConstantForCondition().
1597 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
1598
1599 if (condition == kCondNE && out.IsLow()) {
1600 __ Orrs(out, out, temp);
1601
1602 // We use the scope because of the IT block that follows.
1603 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1604 2 * vixl32::k16BitT32InstructionSizeInBytes,
1605 CodeBufferCheckScope::kExactSize);
1606
1607 __ it(ne);
1608 __ mov(ne, out, 1);
1609 } else {
1610 __ Orr(out, out, temp);
1611 codegen->GenerateConditionWithZero(condition, out, out, temp);
1612 }
1613}
1614
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001615static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001616 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001617
1618 const LocationSummary* const locations = cond->GetLocations();
1619 IfCondition condition = cond->GetCondition();
1620 const vixl32::Register out = OutputRegister(cond);
1621 const Location left = locations->InAt(0);
1622 const Location right = locations->InAt(1);
1623
1624 if (right.IsConstant()) {
1625 IfCondition opposite = cond->GetOppositeCondition();
1626
1627 // Comparisons against 0 are common enough to deserve special attention.
1628 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
1629 switch (condition) {
1630 case kCondNE:
1631 case kCondA:
1632 if (out.IsLow()) {
1633 // We only care if both input registers are 0 or not.
1634 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
1635
1636 // We use the scope because of the IT block that follows.
1637 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1638 2 * vixl32::k16BitT32InstructionSizeInBytes,
1639 CodeBufferCheckScope::kExactSize);
1640
1641 __ it(ne);
1642 __ mov(ne, out, 1);
1643 return;
1644 }
1645
1646 FALLTHROUGH_INTENDED;
1647 case kCondEQ:
1648 case kCondBE:
1649 // We only care if both input registers are 0 or not.
1650 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
1651 codegen->GenerateConditionWithZero(condition, out, out);
1652 return;
1653 case kCondLT:
1654 case kCondGE:
1655 // We only care about the sign bit.
1656 FALLTHROUGH_INTENDED;
1657 case kCondAE:
1658 case kCondB:
1659 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
1660 return;
1661 case kCondLE:
1662 case kCondGT:
1663 default:
1664 break;
1665 }
1666 }
1667 }
1668
Anton Kirilov23b752b2017-07-20 14:40:44 +01001669 // If `out` is a low register, then the GenerateConditionGeneric()
1670 // function generates a shorter code sequence that is still branchless.
1671 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001672 GenerateEqualLong(cond, codegen);
1673 return;
1674 }
1675
Anton Kirilov23b752b2017-07-20 14:40:44 +01001676 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001677}
1678
Roland Levillain6d729a72017-06-30 18:34:01 +01001679static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
1680 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001685 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001686 GenerateConditionLong(cond, codegen);
1687 return;
1688 }
1689
1690 IfCondition condition = cond->GetCondition();
1691 vixl32::Register in = InputRegisterAt(cond, 0);
1692 const vixl32::Register out = OutputRegister(cond);
1693 const Location right = cond->GetLocations()->InAt(1);
1694 int64_t value;
1695
1696 if (right.IsConstant()) {
1697 IfCondition opposite = cond->GetOppositeCondition();
1698
1699 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
1700
1701 // Comparisons against 0 are common enough to deserve special attention.
1702 if (value == 0) {
1703 switch (condition) {
1704 case kCondNE:
1705 case kCondA:
1706 if (out.IsLow() && out.Is(in)) {
1707 __ Cmp(out, 0);
1708
1709 // We use the scope because of the IT block that follows.
1710 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1711 2 * vixl32::k16BitT32InstructionSizeInBytes,
1712 CodeBufferCheckScope::kExactSize);
1713
1714 __ it(ne);
1715 __ mov(ne, out, 1);
1716 return;
1717 }
1718
1719 FALLTHROUGH_INTENDED;
1720 case kCondEQ:
1721 case kCondBE:
1722 case kCondLT:
1723 case kCondGE:
1724 case kCondAE:
1725 case kCondB:
1726 codegen->GenerateConditionWithZero(condition, out, in);
1727 return;
1728 case kCondLE:
1729 case kCondGT:
1730 default:
1731 break;
1732 }
1733 }
1734 }
1735
1736 if (condition == kCondEQ || condition == kCondNE) {
1737 Operand operand(0);
1738
1739 if (right.IsConstant()) {
1740 operand = Operand::From(value);
1741 } else if (out.Is(RegisterFrom(right))) {
1742 // Avoid 32-bit instructions if possible.
1743 operand = InputOperandAt(cond, 0);
1744 in = RegisterFrom(right);
1745 } else {
1746 operand = InputOperandAt(cond, 1);
1747 }
1748
1749 if (condition == kCondNE && out.IsLow()) {
1750 __ Subs(out, in, operand);
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 } else {
1760 __ Sub(out, in, operand);
1761 codegen->GenerateConditionWithZero(condition, out, out);
1762 }
1763
1764 return;
1765 }
1766
1767 GenerateConditionGeneric(cond, codegen);
1768}
1769
Donghui Bai426b49c2016-11-08 14:55:38 +08001770static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001772 bool ret = false;
1773
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001774 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001776 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001777 const uint64_t value = Uint64ConstantFrom(constant);
1778
1779 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1780 } else {
1781 ret = IsUint<8>(Int32ConstantFrom(constant));
1782 }
1783
1784 return ret;
1785}
1786
1787static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001788 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001789
1790 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1791 return Location::ConstantLocation(constant->AsConstant());
1792 }
1793
1794 return Location::RequiresRegister();
1795}
1796
1797static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1798 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1799 // we check that we are not dealing with floating-point output (there is no
1800 // 16-bit VMOV encoding).
1801 if (!out.IsRegister() && !out.IsRegisterPair()) {
1802 return false;
1803 }
1804
1805 // For constants, we also check that the output is in one or two low registers,
1806 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1807 // MOV encoding can be used.
1808 if (src.IsConstant()) {
1809 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1810 return false;
1811 }
1812
1813 if (out.IsRegister()) {
1814 if (!RegisterFrom(out).IsLow()) {
1815 return false;
1816 }
1817 } else {
1818 DCHECK(out.IsRegisterPair());
1819
1820 if (!HighRegisterFrom(out).IsLow()) {
1821 return false;
1822 }
1823 }
1824 }
1825
1826 return true;
1827}
1828
Scott Wakelingfe885462016-09-22 10:24:38 +01001829#undef __
1830
Donghui Bai426b49c2016-11-08 14:55:38 +08001831vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1832 vixl32::Label* final_label) {
1833 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001834 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001835
1836 const HBasicBlock* const block = instruction->GetBlock();
1837 const HLoopInformation* const info = block->GetLoopInformation();
1838 HInstruction* const next = instruction->GetNext();
1839
1840 // Avoid a branch to a branch.
1841 if (next->IsGoto() && (info == nullptr ||
1842 !info->IsBackEdge(*block) ||
1843 !info->HasSuspendCheck())) {
1844 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1845 }
1846
1847 return final_label;
1848}
1849
Scott Wakelingfe885462016-09-22 10:24:38 +01001850CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +01001851 const CompilerOptions& compiler_options,
1852 OptimizingCompilerStats* stats)
1853 : CodeGenerator(graph,
1854 kNumberOfCoreRegisters,
1855 kNumberOfSRegisters,
1856 kNumberOfRegisterPairs,
1857 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001858 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001859 compiler_options,
1860 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1862 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001863 location_builder_(graph, this),
1864 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001865 move_resolver_(graph->GetAllocator(), this),
1866 assembler_(graph->GetAllocator()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001867 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001869 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001870 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001871 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1872 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001873 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001874 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001875 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001876 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001877 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001878 uint32_literals_(std::less<uint32_t>(),
1879 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001880 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001881 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001882 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001883 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1884 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1885 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001886 // Always save the LR register to mimic Quick.
1887 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00001888 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
1889 // S0-S31, which alias to D0-D15.
1890 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
1891 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01001892}
1893
Artem Serov551b28f2016-10-18 19:11:30 +01001894void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1895 uint32_t num_entries = switch_instr_->GetNumEntries();
1896 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1897
1898 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001899 // underlying code buffer and we have generated a jump table of the right size, using
1900 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001901 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1902 num_entries * sizeof(int32_t),
1903 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001904 // TODO(VIXL): Check that using lower case bind is fine here.
1905 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001906 for (uint32_t i = 0; i < num_entries; i++) {
1907 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1908 }
1909}
1910
1911void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1912 uint32_t num_entries = switch_instr_->GetNumEntries();
1913 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1914
Artem Serov551b28f2016-10-18 19:11:30 +01001915 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1916 for (uint32_t i = 0; i < num_entries; i++) {
1917 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1918 DCHECK(target_label->IsBound());
1919 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1920 // When doing BX to address we need to have lower bit set to 1 in T32.
1921 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1922 jump_offset++;
1923 }
1924 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1925 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001926
Scott Wakelingb77051e2016-11-21 19:46:00 +00001927 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001928 }
1929}
1930
Artem Serov09a940d2016-11-11 16:15:11 +00001931void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001932 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001933 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001934 }
1935}
1936
Andreas Gampeca620d72016-11-08 08:09:33 -08001937#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001938
1939void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001940 FixJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001941
1942 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001943 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001944 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1945 uint32_t encoded_data = entry.first;
1946 vixl::aarch32::Label* slow_path_entry = &entry.second.label;
1947 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001948 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001949 }
1950
Scott Wakelingfe885462016-09-22 10:24:38 +01001951 GetAssembler()->FinalizeCode();
1952 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001953
1954 // Verify Baker read barrier linker patches.
1955 if (kIsDebugBuild) {
1956 ArrayRef<const uint8_t> code = allocator->GetMemory();
1957 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1958 DCHECK(info.label.IsBound());
1959 uint32_t literal_offset = info.label.GetLocation();
1960 DCHECK_ALIGNED(literal_offset, 2u);
1961
1962 auto GetInsn16 = [&code](uint32_t offset) {
1963 DCHECK_ALIGNED(offset, 2u);
1964 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
1965 (static_cast<uint32_t>(code[offset + 1]) << 8);
1966 };
1967 auto GetInsn32 = [=](uint32_t offset) {
1968 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
1969 };
1970
1971 uint32_t encoded_data = info.custom_data;
1972 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1973 // Check that the next instruction matches the expected LDR.
1974 switch (kind) {
1975 case BakerReadBarrierKind::kField: {
1976 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1977 if (width == BakerReadBarrierWidth::kWide) {
1978 DCHECK_GE(code.size() - literal_offset, 8u);
1979 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1980 // LDR (immediate), encoding T3, with correct base_reg.
1981 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1982 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1983 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
1984 } else {
1985 DCHECK_GE(code.size() - literal_offset, 6u);
1986 uint32_t next_insn = GetInsn16(literal_offset + 4u);
1987 // LDR (immediate), encoding T1, with correct base_reg.
1988 CheckValidReg(next_insn & 0x7u); // Check destination register.
1989 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1990 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
1991 }
1992 break;
1993 }
1994 case BakerReadBarrierKind::kArray: {
1995 DCHECK_GE(code.size() - literal_offset, 8u);
1996 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1997 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
1998 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1999 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2000 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
2001 CheckValidReg(next_insn & 0xf); // Check index register
2002 break;
2003 }
2004 case BakerReadBarrierKind::kGcRoot: {
2005 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
2006 if (width == BakerReadBarrierWidth::kWide) {
2007 DCHECK_GE(literal_offset, 4u);
2008 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Markof28be432018-08-14 12:20:51 +00002009 // LDR (immediate), encoding T3, with correct root_reg.
Vladimir Markoca1e0382018-04-11 09:58:41 +00002010 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markof28be432018-08-14 12:20:51 +00002011 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
Vladimir Markoca1e0382018-04-11 09:58:41 +00002012 } else {
2013 DCHECK_GE(literal_offset, 2u);
2014 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
2015 // LDR (immediate), encoding T1, with correct root_reg.
2016 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2017 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2018 }
2019 break;
2020 }
Vladimir Markod887ed82018-08-14 13:52:12 +00002021 case BakerReadBarrierKind::kUnsafeCas: {
2022 DCHECK_GE(literal_offset, 4u);
2023 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
2024 // ADD (register), encoding T3, with correct root_reg.
2025 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2026 CHECK_EQ(prev_insn & 0xfff0fff0u, 0xeb000000u | (root_reg << 8));
2027 break;
2028 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002029 default:
2030 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2031 UNREACHABLE();
2032 }
2033 }
2034 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002035}
2036
2037void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002038 // Stack register, LR and PC are always reserved.
2039 blocked_core_registers_[SP] = true;
2040 blocked_core_registers_[LR] = true;
2041 blocked_core_registers_[PC] = true;
2042
Roland Levillain6d729a72017-06-30 18:34:01 +01002043 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2044 // Reserve marking register.
2045 blocked_core_registers_[MR] = true;
2046 }
2047
Scott Wakelingfe885462016-09-22 10:24:38 +01002048 // Reserve thread register.
2049 blocked_core_registers_[TR] = true;
2050
2051 // Reserve temp register.
2052 blocked_core_registers_[IP] = true;
2053
2054 if (GetGraph()->IsDebuggable()) {
2055 // Stubs do not save callee-save floating point registers. If the graph
2056 // is debuggable, we need to deal with these registers differently. For
2057 // now, just block them.
2058 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2059 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2060 ++i) {
2061 blocked_fpu_registers_[i] = true;
2062 }
2063 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002064}
2065
Scott Wakelingfe885462016-09-22 10:24:38 +01002066InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2067 CodeGeneratorARMVIXL* codegen)
2068 : InstructionCodeGenerator(graph, codegen),
2069 assembler_(codegen->GetAssembler()),
2070 codegen_(codegen) {}
2071
2072void CodeGeneratorARMVIXL::ComputeSpillMask() {
2073 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Vladimir Marko460f0542019-07-04 14:02:08 +01002074 DCHECK_NE(core_spill_mask_ & (1u << kLrCode), 0u)
2075 << "At least the return address register must be saved";
2076 // 16-bit PUSH/POP (T1) can save/restore just the LR/PC.
2077 DCHECK(GetVIXLAssembler()->IsUsingT32());
Scott Wakelingfe885462016-09-22 10:24:38 +01002078 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2079 // We use vpush and vpop for saving and restoring floating point registers, which take
2080 // a SRegister and the number of registers to save/restore after that SRegister. We
2081 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2082 // but in the range.
2083 if (fpu_spill_mask_ != 0) {
2084 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2085 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2086 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2087 fpu_spill_mask_ |= (1 << i);
2088 }
2089 }
2090}
2091
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002092void CodeGeneratorARMVIXL::MaybeIncrementHotness(bool is_frame_entry) {
2093 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2094 UseScratchRegisterScope temps(GetVIXLAssembler());
2095 vixl32::Register temp = temps.Acquire();
2096 static_assert(ArtMethod::MaxCounter() == 0xFFFF, "asm is probably wrong");
2097 if (!is_frame_entry) {
2098 __ Push(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002099 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002100 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2101 }
2102 // Load with zero extend to clear the high bits for integer overflow check.
2103 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2104 __ Add(temp, temp, 1);
2105 // Subtract one if the counter would overflow.
2106 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
2107 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2108 if (!is_frame_entry) {
2109 __ Pop(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002110 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002111 }
2112 }
2113
2114 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002115 ScopedProfilingInfoUse spiu(
2116 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2117 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002118 if (info != nullptr) {
2119 uint32_t address = reinterpret_cast32<uint32_t>(info);
2120 vixl::aarch32::Label done;
2121 UseScratchRegisterScope temps(GetVIXLAssembler());
2122 temps.Exclude(ip);
2123 if (!is_frame_entry) {
2124 __ Push(r4); // Will be used as temporary. For frame entry, r4 is always available.
Vladimir Markodec78172020-06-19 15:31:23 +01002125 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002126 }
2127 __ Mov(r4, address);
2128 __ Ldrh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2129 __ Add(ip, ip, 1);
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01002130 instruction_visitor_.GenerateAndConst(ip, ip, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002131 __ Strh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2132 if (!is_frame_entry) {
2133 __ Pop(r4);
Vladimir Markodec78172020-06-19 15:31:23 +01002134 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002135 }
2136 __ Lsls(ip, ip, 16);
2137 __ B(ne, &done);
2138 uint32_t entry_point_offset =
2139 GetThreadOffset<kArmPointerSize>(kQuickCompileOptimized).Int32Value();
2140 if (HasEmptyFrame()) {
2141 CHECK(is_frame_entry);
2142 // For leaf methods, we need to spill lr and r0. Also spill r1 and r2 for
2143 // alignment.
2144 uint32_t core_spill_mask =
2145 (1 << lr.GetCode()) | (1 << r0.GetCode()) | (1 << r1.GetCode()) | (1 << r2.GetCode());
2146 __ Push(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002147 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002148 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2149 __ Blx(lr);
2150 __ Pop(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002151 GetAssembler()->cfi().AdjustCFAOffset(
2152 -static_cast<int>(kArmWordSize) * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002153 } else {
2154 if (!RequiresCurrentMethod()) {
2155 CHECK(is_frame_entry);
2156 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2157 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002158 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2159 __ Blx(lr);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002160 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002161 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002162 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002163 }
2164}
2165
Scott Wakelingfe885462016-09-22 10:24:38 +01002166void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2167 bool skip_overflow_check =
2168 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2169 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2170 __ Bind(&frame_entry_label_);
2171
2172 if (HasEmptyFrame()) {
David Srbecky30021842019-02-13 14:19:36 +00002173 // Ensure that the CFI opcode list is not empty.
2174 GetAssembler()->cfi().Nop();
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002175 MaybeIncrementHotness(/* is_frame_entry= */ true);
Scott Wakelingfe885462016-09-22 10:24:38 +01002176 return;
2177 }
2178
Scott Wakelingfe885462016-09-22 10:24:38 +01002179 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002180 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002181 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002182 vixl32::Register temp;
2183 // TODO: Remove this check when R4 is made a callee-save register
2184 // in ART compiled code (b/72801708). Currently we need to make
2185 // sure r4 is not blocked, e.g. in special purpose
2186 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2187 // here.
2188 if (!blocked_core_registers_[R4]) {
2189 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2190 DCHECK(!reg.Is(r4));
2191 }
2192 DCHECK(!kCoreCalleeSaves.Includes(r4));
2193 temp = r4;
2194 } else {
2195 temp = temps.Acquire();
2196 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002197 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002198 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002199 ExactAssemblyScope aas(GetVIXLAssembler(),
2200 vixl32::kMaxInstructionSizeInBytes,
2201 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002202 __ ldr(temp, MemOperand(temp));
2203 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002204 }
2205
Vladimir Marko1a225a72019-07-05 13:37:42 +01002206 uint32_t frame_size = GetFrameSize();
2207 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2208 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2209 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2210 core_spills_offset <= 3u * kArmWordSize) {
2211 // Do a single PUSH for core registers including the method and up to two
2212 // filler registers. Then store the single FP spill if any.
2213 // (The worst case is when the method is not required and we actually
2214 // store 3 extra registers but they are stored in the same properly
2215 // aligned 16-byte chunk where we're already writing anyway.)
2216 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2217 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize);
2218 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(core_spill_mask_));
2219 __ Push(RegisterList(core_spill_mask_ | extra_regs));
2220 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
2221 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2222 core_spills_offset,
2223 core_spill_mask_,
2224 kArmWordSize);
2225 if (fpu_spill_mask_ != 0u) {
2226 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2227 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2228 GetAssembler()->StoreSToOffset(sreg, sp, fp_spills_offset);
2229 GetAssembler()->cfi().RelOffset(DWARFReg(sreg), /*offset=*/ fp_spills_offset);
2230 }
2231 } else {
2232 __ Push(RegisterList(core_spill_mask_));
2233 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2234 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2235 /*offset=*/ 0,
2236 core_spill_mask_,
2237 kArmWordSize);
2238 if (fpu_spill_mask_ != 0) {
2239 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002240
Vladimir Marko1a225a72019-07-05 13:37:42 +01002241 // Check that list is contiguous.
2242 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002243
Vladimir Marko1a225a72019-07-05 13:37:42 +01002244 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2245 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
2246 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0),
2247 /*offset=*/ 0,
2248 fpu_spill_mask_,
2249 kArmWordSize);
2250 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002251
Vladimir Marko1a225a72019-07-05 13:37:42 +01002252 // Adjust SP and save the current method if we need it. Note that we do
2253 // not save the method in HCurrentMethod, as the instruction might have
2254 // been removed in the SSA graph.
2255 if (RequiresCurrentMethod() && fp_spills_offset <= 3 * kArmWordSize) {
2256 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2257 __ Push(RegisterList(MaxInt<uint32_t>(fp_spills_offset / kArmWordSize)));
2258 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2259 } else {
Vladimir Markodec78172020-06-19 15:31:23 +01002260 IncreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002261 if (RequiresCurrentMethod()) {
2262 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2263 }
2264 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002265 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002266
2267 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2268 UseScratchRegisterScope temps(GetVIXLAssembler());
2269 vixl32::Register temp = temps.Acquire();
2270 // Initialize should_deoptimize flag to 0.
2271 __ Mov(temp, 0);
2272 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2273 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002274
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002275 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08002276 MaybeGenerateMarkingRegisterCheck(/* code= */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002277}
2278
2279void CodeGeneratorARMVIXL::GenerateFrameExit() {
2280 if (HasEmptyFrame()) {
2281 __ Bx(lr);
2282 return;
2283 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002284
Scott Wakelingfe885462016-09-22 10:24:38 +01002285 // Pop LR into PC to return.
2286 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2287 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
Vladimir Marko1a225a72019-07-05 13:37:42 +01002288
2289 uint32_t frame_size = GetFrameSize();
2290 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2291 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2292 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2293 // r4 is blocked by TestCodeGeneratorARMVIXL used by some tests.
2294 core_spills_offset <= (blocked_core_registers_[r4.GetCode()] ? 2u : 3u) * kArmWordSize) {
2295 // Load the FP spill if any and then do a single POP including the method
2296 // and up to two filler registers. If we have no FP spills, this also has
2297 // the advantage that we do not need to emit CFI directives.
2298 if (fpu_spill_mask_ != 0u) {
2299 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2300 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2301 GetAssembler()->cfi().RememberState();
2302 GetAssembler()->LoadSFromOffset(sreg, sp, fp_spills_offset);
2303 GetAssembler()->cfi().Restore(DWARFReg(sreg));
2304 }
2305 // Clobber registers r2-r4 as they are caller-save in ART managed ABI and
2306 // never hold the return value.
2307 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize) << r2.GetCode();
2308 DCHECK_EQ(extra_regs & kCoreCalleeSaves.GetList(), 0u);
2309 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(pop_mask));
2310 __ Pop(RegisterList(pop_mask | extra_regs));
2311 if (fpu_spill_mask_ != 0u) {
2312 GetAssembler()->cfi().RestoreState();
2313 }
2314 } else {
2315 GetAssembler()->cfi().RememberState();
Vladimir Markodec78172020-06-19 15:31:23 +01002316 DecreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002317 if (fpu_spill_mask_ != 0) {
2318 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2319
2320 // Check that list is contiguous.
2321 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2322
2323 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2324 GetAssembler()->cfi().AdjustCFAOffset(
2325 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
2326 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
2327 }
2328 __ Pop(RegisterList(pop_mask));
2329 GetAssembler()->cfi().RestoreState();
2330 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2331 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002332}
2333
2334void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2335 __ Bind(GetLabelOf(block));
2336}
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002339 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002340 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002342 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 case DataType::Type::kInt8:
2344 case DataType::Type::kUint16:
2345 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002346 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002347 uint32_t index = gp_index_++;
2348 uint32_t stack_index = stack_index_++;
2349 if (index < calling_convention.GetNumberOfRegisters()) {
2350 return LocationFrom(calling_convention.GetRegisterAt(index));
2351 } else {
2352 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2353 }
2354 }
2355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002357 uint32_t index = gp_index_;
2358 uint32_t stack_index = stack_index_;
2359 gp_index_ += 2;
2360 stack_index_ += 2;
2361 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2362 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2363 // Skip R1, and use R2_R3 instead.
2364 gp_index_++;
2365 index++;
2366 }
2367 }
2368 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2369 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2370 calling_convention.GetRegisterAt(index + 1).GetCode());
2371
2372 return LocationFrom(calling_convention.GetRegisterAt(index),
2373 calling_convention.GetRegisterAt(index + 1));
2374 } else {
2375 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2376 }
2377 }
2378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002380 uint32_t stack_index = stack_index_++;
2381 if (float_index_ % 2 == 0) {
2382 float_index_ = std::max(double_index_, float_index_);
2383 }
2384 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2385 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2386 } else {
2387 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2388 }
2389 }
2390
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002391 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002392 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2393 uint32_t stack_index = stack_index_;
2394 stack_index_ += 2;
2395 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2396 uint32_t index = double_index_;
2397 double_index_ += 2;
2398 Location result = LocationFrom(
2399 calling_convention.GetFpuRegisterAt(index),
2400 calling_convention.GetFpuRegisterAt(index + 1));
2401 DCHECK(ExpectedPairLayout(result));
2402 return result;
2403 } else {
2404 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2405 }
2406 }
2407
Aart Bik66c158e2018-01-31 12:55:04 -08002408 case DataType::Type::kUint32:
2409 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002411 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002412 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002413 }
2414 return Location::NoLocation();
2415}
2416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002418 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002419 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002421 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 case DataType::Type::kInt8:
2423 case DataType::Type::kUint16:
2424 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002425 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002426 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002427 return LocationFrom(r0);
2428 }
2429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002430 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002431 return LocationFrom(s0);
2432 }
2433
Aart Bik66c158e2018-01-31 12:55:04 -08002434 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002435 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002436 return LocationFrom(r0, r1);
2437 }
2438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002440 return LocationFrom(s0, s1);
2441 }
2442
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002444 return Location::NoLocation();
2445 }
2446
2447 UNREACHABLE();
2448}
2449
2450Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2451 return LocationFrom(kMethodRegister);
2452}
2453
Vladimir Marko86c87522020-05-11 16:55:55 +01002454Location CriticalNativeCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
2455 DCHECK_NE(type, DataType::Type::kReference);
2456
2457 // Native ABI uses the same registers as managed, except that the method register r0
2458 // is a normal argument.
2459 Location location = Location::NoLocation();
2460 if (DataType::Is64BitType(type)) {
2461 gpr_index_ = RoundUp(gpr_index_, 2u);
2462 stack_offset_ = RoundUp(stack_offset_, 2 * kFramePointerSize);
2463 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2464 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u],
2465 kParameterCoreRegistersVIXL[gpr_index_]);
2466 gpr_index_ += 2u;
2467 }
2468 } else {
2469 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2470 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u]);
2471 ++gpr_index_;
2472 }
2473 }
2474 if (location.IsInvalid()) {
2475 if (DataType::Is64BitType(type)) {
2476 location = Location::DoubleStackSlot(stack_offset_);
2477 stack_offset_ += 2 * kFramePointerSize;
2478 } else {
2479 location = Location::StackSlot(stack_offset_);
2480 stack_offset_ += kFramePointerSize;
2481 }
2482
2483 if (for_register_allocation_) {
2484 location = Location::Any();
2485 }
2486 }
2487 return location;
2488}
2489
2490Location CriticalNativeCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type)
2491 const {
2492 // We perform conversion to the managed ABI return register after the call if needed.
2493 InvokeDexCallingConventionVisitorARMVIXL dex_calling_convention;
2494 return dex_calling_convention.GetReturnLocation(type);
2495}
2496
2497Location CriticalNativeCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2498 // Pass the method in the hidden argument R4.
2499 return Location::RegisterLocation(R4);
2500}
2501
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002502void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2503 if (source.Equals(destination)) {
2504 return;
2505 }
2506 if (destination.IsRegister()) {
2507 if (source.IsRegister()) {
2508 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2509 } else if (source.IsFpuRegister()) {
2510 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2511 } else {
2512 GetAssembler()->LoadFromOffset(kLoadWord,
2513 RegisterFrom(destination),
2514 sp,
2515 source.GetStackIndex());
2516 }
2517 } else if (destination.IsFpuRegister()) {
2518 if (source.IsRegister()) {
2519 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2520 } else if (source.IsFpuRegister()) {
2521 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2522 } else {
2523 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2524 }
2525 } else {
2526 DCHECK(destination.IsStackSlot()) << destination;
2527 if (source.IsRegister()) {
2528 GetAssembler()->StoreToOffset(kStoreWord,
2529 RegisterFrom(source),
2530 sp,
2531 destination.GetStackIndex());
2532 } else if (source.IsFpuRegister()) {
2533 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2534 } else {
2535 DCHECK(source.IsStackSlot()) << source;
2536 UseScratchRegisterScope temps(GetVIXLAssembler());
2537 vixl32::Register temp = temps.Acquire();
2538 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2539 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2540 }
2541 }
2542}
2543
Artem Serovcfbe9132016-10-14 15:58:56 +01002544void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2545 DCHECK(location.IsRegister());
2546 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002547}
2548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002550 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2551 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002552 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002553 move.AddMove(src, dst, dst_type, nullptr);
2554 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002555}
2556
Artem Serovcfbe9132016-10-14 15:58:56 +01002557void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2558 if (location.IsRegister()) {
2559 locations->AddTemp(location);
2560 } else if (location.IsRegisterPair()) {
2561 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2562 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2563 } else {
2564 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2565 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002566}
2567
2568void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2569 HInstruction* instruction,
2570 uint32_t dex_pc,
2571 SlowPathCode* slow_path) {
2572 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Vladimir Markof6675082019-05-17 12:05:28 +01002573
2574 ThreadOffset32 entrypoint_offset = GetThreadOffset<kArmPointerSize>(entrypoint);
2575 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
2576 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
2577 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01002578 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01002579 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
2580 // Ensure the pc position is recorded immediately after the `blx` instruction.
2581 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
2582 ExactAssemblyScope aas(GetVIXLAssembler(),
2583 vixl32::k16BitT32InstructionSizeInBytes,
2584 CodeBufferCheckScope::kExactSize);
2585 __ blx(lr);
2586 if (EntrypointRequiresStackMap(entrypoint)) {
2587 RecordPcInfo(instruction, dex_pc, slow_path);
2588 }
2589 } else {
2590 // Ensure the pc position is recorded immediately after the `bl` instruction.
2591 ExactAssemblyScope aas(GetVIXLAssembler(),
2592 vixl32::k32BitT32InstructionSizeInBytes,
2593 CodeBufferCheckScope::kExactSize);
2594 EmitEntrypointThunkCall(entrypoint_offset);
2595 if (EntrypointRequiresStackMap(entrypoint)) {
2596 RecordPcInfo(instruction, dex_pc, slow_path);
2597 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002598 }
2599}
2600
2601void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2602 HInstruction* instruction,
2603 SlowPathCode* slow_path) {
2604 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002605 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002606 __ Blx(lr);
2607}
2608
Scott Wakelingfe885462016-09-22 10:24:38 +01002609void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002610 if (successor->IsExitBlock()) {
2611 DCHECK(got->GetPrevious()->AlwaysThrows());
2612 return; // no code needed
2613 }
2614
Scott Wakelingfe885462016-09-22 10:24:38 +01002615 HBasicBlock* block = got->GetBlock();
2616 HInstruction* previous = got->GetPrevious();
2617 HLoopInformation* info = block->GetLoopInformation();
2618
2619 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002620 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Scott Wakelingfe885462016-09-22 10:24:38 +01002621 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2622 return;
2623 }
2624 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2625 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08002626 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002627 }
2628 if (!codegen_->GoesToNextBlock(block, successor)) {
2629 __ B(codegen_->GetLabelOf(successor));
2630 }
2631}
2632
2633void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2634 got->SetLocations(nullptr);
2635}
2636
2637void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2638 HandleGoto(got, got->GetSuccessor());
2639}
2640
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002641void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2642 try_boundary->SetLocations(nullptr);
2643}
2644
2645void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2646 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2647 if (!successor->IsExitBlock()) {
2648 HandleGoto(try_boundary, successor);
2649 }
2650}
2651
Scott Wakelingfe885462016-09-22 10:24:38 +01002652void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2653 exit->SetLocations(nullptr);
2654}
2655
2656void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2657}
2658
Scott Wakelingfe885462016-09-22 10:24:38 +01002659void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002660 vixl32::Label* true_target,
2661 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002662 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002663 if (true_target == false_target) {
2664 DCHECK(true_target != nullptr);
2665 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002666 return;
2667 }
2668
Anton Kirilov23b752b2017-07-20 14:40:44 +01002669 vixl32::Label* non_fallthrough_target;
2670 bool invert;
2671 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002672
Anton Kirilov23b752b2017-07-20 14:40:44 +01002673 if (true_target == nullptr) {
2674 // The true target is fallthrough.
2675 DCHECK(false_target != nullptr);
2676 non_fallthrough_target = false_target;
2677 invert = true;
2678 emit_both_branches = false;
2679 } else {
2680 non_fallthrough_target = true_target;
2681 invert = false;
2682 // Either the false target is fallthrough, or there is no fallthrough
2683 // and both branches must be emitted.
2684 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002685 }
2686
Anton Kirilov23b752b2017-07-20 14:40:44 +01002687 const auto cond = GenerateTest(condition, invert, codegen_);
2688
2689 __ B(cond.first, non_fallthrough_target, is_far_target);
2690
2691 if (emit_both_branches) {
2692 // No target falls through, we need to branch.
2693 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002694 }
2695}
2696
2697void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2698 size_t condition_input_index,
2699 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002700 vixl32::Label* false_target,
2701 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002702 HInstruction* cond = instruction->InputAt(condition_input_index);
2703
2704 if (true_target == nullptr && false_target == nullptr) {
2705 // Nothing to do. The code always falls through.
2706 return;
2707 } else if (cond->IsIntConstant()) {
2708 // Constant condition, statically compared against "true" (integer value 1).
2709 if (cond->AsIntConstant()->IsTrue()) {
2710 if (true_target != nullptr) {
2711 __ B(true_target);
2712 }
2713 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002714 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002715 if (false_target != nullptr) {
2716 __ B(false_target);
2717 }
2718 }
2719 return;
2720 }
2721
2722 // The following code generates these patterns:
2723 // (1) true_target == nullptr && false_target != nullptr
2724 // - opposite condition true => branch to false_target
2725 // (2) true_target != nullptr && false_target == nullptr
2726 // - condition true => branch to true_target
2727 // (3) true_target != nullptr && false_target != nullptr
2728 // - condition true => branch to true_target
2729 // - branch to false_target
2730 if (IsBooleanValueOrMaterializedCondition(cond)) {
2731 // Condition has been materialized, compare the output to 0.
2732 if (kIsDebugBuild) {
2733 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2734 DCHECK(cond_val.IsRegister());
2735 }
2736 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002737 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2738 false_target,
2739 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002740 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002741 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2742 true_target,
2743 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002744 }
2745 } else {
2746 // Condition has not been materialized. Use its inputs as the comparison and
2747 // its condition as the branch condition.
2748 HCondition* condition = cond->AsCondition();
2749
2750 // If this is a long or FP comparison that has been folded into
2751 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 DataType::Type type = condition->InputAt(0)->GetType();
2753 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002754 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002755 return;
2756 }
2757
Donghui Bai426b49c2016-11-08 14:55:38 +08002758 vixl32::Label* non_fallthrough_target;
2759 vixl32::Condition arm_cond = vixl32::Condition::None();
2760 const vixl32::Register left = InputRegisterAt(cond, 0);
2761 const Operand right = InputOperandAt(cond, 1);
2762
Scott Wakelingfe885462016-09-22 10:24:38 +01002763 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002764 arm_cond = ARMCondition(condition->GetOppositeCondition());
2765 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002766 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002767 arm_cond = ARMCondition(condition->GetCondition());
2768 non_fallthrough_target = true_target;
2769 }
2770
2771 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2772 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002773 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002774 } else {
2775 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002776 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002777 }
2778 } else {
2779 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002780 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002781 }
2782 }
2783
2784 // If neither branch falls through (case 3), the conditional branch to `true_target`
2785 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2786 if (true_target != nullptr && false_target != nullptr) {
2787 __ B(false_target);
2788 }
2789}
2790
2791void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002792 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002793 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2794 locations->SetInAt(0, Location::RequiresRegister());
2795 }
2796}
2797
2798void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2799 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2800 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002801 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2802 nullptr : codegen_->GetLabelOf(true_successor);
2803 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2804 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002805 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002806}
2807
Scott Wakelingc34dba72016-10-03 10:14:44 +01002808void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002809 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002810 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002811 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2812 RegisterSet caller_saves = RegisterSet::Empty();
2813 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2814 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002815 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2816 locations->SetInAt(0, Location::RequiresRegister());
2817 }
2818}
2819
2820void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2821 SlowPathCodeARMVIXL* slow_path =
2822 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2823 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002824 /* condition_input_index= */ 0,
Scott Wakelingc34dba72016-10-03 10:14:44 +01002825 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002826 /* false_target= */ nullptr);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002827}
2828
Artem Serovd4cc5b22016-11-04 11:19:09 +00002829void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002830 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002831 LocationSummary(flag, LocationSummary::kNoCall);
2832 locations->SetOut(Location::RequiresRegister());
2833}
2834
2835void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2836 GetAssembler()->LoadFromOffset(kLoadWord,
2837 OutputRegister(flag),
2838 sp,
2839 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2840}
2841
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002842void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002843 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002845
2846 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002847 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002848 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002849 } else {
2850 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002851 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002852 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002853
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002854 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002855 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2856 // The code generator handles overlap with the values, but not with the condition.
2857 locations->SetOut(Location::SameAsFirstInput());
2858 } else if (is_floating_point) {
2859 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2860 } else {
2861 if (!locations->InAt(1).IsConstant()) {
2862 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2863 }
2864
2865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002866 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002867}
2868
2869void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002870 HInstruction* const condition = select->GetCondition();
2871 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002873 const Location first = locations->InAt(0);
2874 const Location out = locations->Out();
2875 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002876
2877 // In the unlucky case the output of this instruction overlaps
2878 // with an input of an "emitted-at-use-site" condition, and
2879 // the output of this instruction is not one of its inputs, we'll
2880 // need to fallback to branches instead of conditional ARM instructions.
2881 bool output_overlaps_with_condition_inputs =
2882 !IsBooleanValueOrMaterializedCondition(condition) &&
2883 !out.Equals(first) &&
2884 !out.Equals(second) &&
2885 (condition->GetLocations()->InAt(0).Equals(out) ||
2886 condition->GetLocations()->InAt(1).Equals(out));
2887 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002888 Location src;
2889
2890 if (condition->IsIntConstant()) {
2891 if (condition->AsIntConstant()->IsFalse()) {
2892 src = first;
2893 } else {
2894 src = second;
2895 }
2896
2897 codegen_->MoveLocation(out, src, type);
2898 return;
2899 }
2900
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002901 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002902 bool invert = false;
2903
2904 if (out.Equals(second)) {
2905 src = first;
2906 invert = true;
2907 } else if (out.Equals(first)) {
2908 src = second;
2909 } else if (second.IsConstant()) {
2910 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2911 src = second;
2912 } else if (first.IsConstant()) {
2913 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2914 src = first;
2915 invert = true;
2916 } else {
2917 src = second;
2918 }
2919
2920 if (CanGenerateConditionalMove(out, src)) {
2921 if (!out.Equals(first) && !out.Equals(second)) {
2922 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2923 }
2924
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002925 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2926
2927 if (IsBooleanValueOrMaterializedCondition(condition)) {
2928 __ Cmp(InputRegisterAt(select, 2), 0);
2929 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2930 } else {
2931 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2932 }
2933
Donghui Bai426b49c2016-11-08 14:55:38 +08002934 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002935 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002936 ExactAssemblyScope guard(GetVIXLAssembler(),
2937 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2938 CodeBufferCheckScope::kExactSize);
2939
2940 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002941 __ it(cond.first);
2942 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002943 } else {
2944 DCHECK(out.IsRegisterPair());
2945
2946 Operand operand_high(0);
2947 Operand operand_low(0);
2948
2949 if (src.IsConstant()) {
2950 const int64_t value = Int64ConstantFrom(src);
2951
2952 operand_high = High32Bits(value);
2953 operand_low = Low32Bits(value);
2954 } else {
2955 DCHECK(src.IsRegisterPair());
2956 operand_high = HighRegisterFrom(src);
2957 operand_low = LowRegisterFrom(src);
2958 }
2959
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002960 __ it(cond.first);
2961 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2962 __ it(cond.first);
2963 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002964 }
2965
2966 return;
2967 }
2968 }
2969
2970 vixl32::Label* false_target = nullptr;
2971 vixl32::Label* true_target = nullptr;
2972 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002973 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08002974 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2975
2976 if (out.Equals(second)) {
2977 true_target = target;
2978 src = first;
2979 } else {
2980 false_target = target;
2981 src = second;
2982
2983 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002984 if (output_overlaps_with_condition_inputs) {
2985 false_target = &other_case;
2986 } else {
2987 codegen_->MoveLocation(out, first, type);
2988 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002989 }
2990 }
2991
Andreas Gampe3db70682018-12-26 15:12:03 -08002992 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target= */ false);
Donghui Bai426b49c2016-11-08 14:55:38 +08002993 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002994 if (output_overlaps_with_condition_inputs) {
2995 __ B(target);
2996 __ Bind(&other_case);
2997 codegen_->MoveLocation(out, first, type);
2998 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002999
3000 if (select_end.IsReferenced()) {
3001 __ Bind(&select_end);
3002 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003003}
3004
Artem Serov551b28f2016-10-18 19:11:30 +01003005void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003006 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003007}
3008
3009void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3010 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3011}
3012
Vladimir Markodec78172020-06-19 15:31:23 +01003013void CodeGeneratorARMVIXL::IncreaseFrame(size_t adjustment) {
3014 __ Claim(adjustment);
3015 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3016}
3017
3018void CodeGeneratorARMVIXL::DecreaseFrame(size_t adjustment) {
3019 __ Drop(adjustment);
3020 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3021}
3022
Scott Wakelingfe885462016-09-22 10:24:38 +01003023void CodeGeneratorARMVIXL::GenerateNop() {
3024 __ Nop();
3025}
3026
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003027// `temp` is an extra temporary register that is used for some conditions;
3028// callers may not specify it, in which case the method will use a scratch
3029// register instead.
3030void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3031 vixl32::Register out,
3032 vixl32::Register in,
3033 vixl32::Register temp) {
3034 switch (condition) {
3035 case kCondEQ:
3036 // x <= 0 iff x == 0 when the comparison is unsigned.
3037 case kCondBE:
3038 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3039 temp = out;
3040 }
3041
3042 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3043 // different as well.
3044 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3045 // temp = - in; only 0 sets the carry flag.
3046 __ Rsbs(temp, in, 0);
3047
3048 if (out.Is(in)) {
3049 std::swap(in, temp);
3050 }
3051
3052 // out = - in + in + carry = carry
3053 __ Adc(out, temp, in);
3054 } else {
3055 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3056 __ Clz(out, in);
3057 // Any number less than 32 logically shifted right by 5 bits results in 0;
3058 // the same operation on 32 yields 1.
3059 __ Lsr(out, out, 5);
3060 }
3061
3062 break;
3063 case kCondNE:
3064 // x > 0 iff x != 0 when the comparison is unsigned.
3065 case kCondA: {
3066 UseScratchRegisterScope temps(GetVIXLAssembler());
3067
3068 if (out.Is(in)) {
3069 if (!temp.IsValid() || in.Is(temp)) {
3070 temp = temps.Acquire();
3071 }
3072 } else if (!temp.IsValid() || !temp.IsLow()) {
3073 temp = out;
3074 }
3075
3076 // temp = in - 1; only 0 does not set the carry flag.
3077 __ Subs(temp, in, 1);
3078 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3079 __ Sbc(out, in, temp);
3080 break;
3081 }
3082 case kCondGE:
3083 __ Mvn(out, in);
3084 in = out;
3085 FALLTHROUGH_INTENDED;
3086 case kCondLT:
3087 // We only care about the sign bit.
3088 __ Lsr(out, in, 31);
3089 break;
3090 case kCondAE:
3091 // Trivially true.
3092 __ Mov(out, 1);
3093 break;
3094 case kCondB:
3095 // Trivially false.
3096 __ Mov(out, 0);
3097 break;
3098 default:
3099 LOG(FATAL) << "Unexpected condition " << condition;
3100 UNREACHABLE();
3101 }
3102}
3103
Scott Wakelingfe885462016-09-22 10:24:38 +01003104void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3105 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003106 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003107 const DataType::Type type = cond->InputAt(0)->GetType();
3108 if (DataType::IsFloatingPointType(type)) {
3109 locations->SetInAt(0, Location::RequiresFpuRegister());
3110 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
3111 } else {
3112 locations->SetInAt(0, Location::RequiresRegister());
3113 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3114 }
3115 if (!cond->IsEmittedAtUseSite()) {
3116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003117 }
3118}
3119
3120void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3121 if (cond->IsEmittedAtUseSite()) {
3122 return;
3123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003128 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003129 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003130 }
3131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003133
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003134 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003135
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003136 // A condition with only one boolean input, or two boolean inputs without being equality or
3137 // inequality results from transformations done by the instruction simplifier, and is handled
3138 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 if (type == DataType::Type::kBool &&
3140 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003141 (condition == kCondEQ || condition == kCondNE)) {
3142 vixl32::Register left = InputRegisterAt(cond, 0);
3143 const vixl32::Register out = OutputRegister(cond);
3144 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003145
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003146 // The constant case is handled by the instruction simplifier.
3147 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003148
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003149 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003150
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003151 // Avoid 32-bit instructions if possible.
3152 if (out.Is(right)) {
3153 std::swap(left, right);
3154 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003155
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003156 __ Eor(out, left, right);
3157
3158 if (condition == kCondEQ) {
3159 __ Eor(out, out, 1);
3160 }
3161
3162 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003163 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003164
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003165 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003166}
3167
3168void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3169 HandleCondition(comp);
3170}
3171
3172void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3173 HandleCondition(comp);
3174}
3175
3176void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3177 HandleCondition(comp);
3178}
3179
3180void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3181 HandleCondition(comp);
3182}
3183
3184void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3185 HandleCondition(comp);
3186}
3187
3188void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3189 HandleCondition(comp);
3190}
3191
3192void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3193 HandleCondition(comp);
3194}
3195
3196void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3197 HandleCondition(comp);
3198}
3199
3200void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3201 HandleCondition(comp);
3202}
3203
3204void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3205 HandleCondition(comp);
3206}
3207
3208void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3209 HandleCondition(comp);
3210}
3211
3212void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3213 HandleCondition(comp);
3214}
3215
3216void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3217 HandleCondition(comp);
3218}
3219
3220void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3221 HandleCondition(comp);
3222}
3223
3224void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3225 HandleCondition(comp);
3226}
3227
3228void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3229 HandleCondition(comp);
3230}
3231
3232void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3233 HandleCondition(comp);
3234}
3235
3236void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3237 HandleCondition(comp);
3238}
3239
3240void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3241 HandleCondition(comp);
3242}
3243
3244void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3245 HandleCondition(comp);
3246}
3247
3248void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3249 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003250 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003251 locations->SetOut(Location::ConstantLocation(constant));
3252}
3253
3254void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3255 // Will be generated at use site.
3256}
3257
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003258void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3259 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003260 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003261 locations->SetOut(Location::ConstantLocation(constant));
3262}
3263
3264void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3265 // Will be generated at use site.
3266}
3267
Scott Wakelingfe885462016-09-22 10:24:38 +01003268void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3269 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003270 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003271 locations->SetOut(Location::ConstantLocation(constant));
3272}
3273
3274void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3275 // Will be generated at use site.
3276}
3277
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003278void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3279 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003280 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003281 locations->SetOut(Location::ConstantLocation(constant));
3282}
3283
Scott Wakelingc34dba72016-10-03 10:14:44 +01003284void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3285 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003286 // Will be generated at use site.
3287}
3288
3289void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3290 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003291 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003292 locations->SetOut(Location::ConstantLocation(constant));
3293}
3294
Scott Wakelingc34dba72016-10-03 10:14:44 +01003295void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3296 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003297 // Will be generated at use site.
3298}
3299
Igor Murashkind01745e2017-04-05 16:40:31 -07003300void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3301 constructor_fence->SetLocations(nullptr);
3302}
3303
3304void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3305 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3306 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3307}
3308
Scott Wakelingfe885462016-09-22 10:24:38 +01003309void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3310 memory_barrier->SetLocations(nullptr);
3311}
3312
3313void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3314 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3315}
3316
3317void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3318 ret->SetLocations(nullptr);
3319}
3320
3321void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3322 codegen_->GenerateFrameExit();
3323}
3324
3325void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3326 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003327 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003328 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3329}
3330
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00003331void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret) {
3332 if (GetGraph()->IsCompilingOsr()) {
3333 // To simplify callers of an OSR method, we put the return value in both
3334 // floating point and core registers.
3335 switch (ret->InputAt(0)->GetType()) {
3336 case DataType::Type::kFloat32:
3337 __ Vmov(r0, s0);
3338 break;
3339 case DataType::Type::kFloat64:
3340 __ Vmov(r0, r1, d0);
3341 break;
3342 default:
3343 break;
3344 }
3345 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003346 codegen_->GenerateFrameExit();
3347}
3348
Artem Serovcfbe9132016-10-14 15:58:56 +01003349void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3350 // The trampoline uses the same calling convention as dex calling conventions,
3351 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3352 // the method_idx.
3353 HandleInvoke(invoke);
3354}
3355
3356void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3357 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003358 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003359}
3360
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003361void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3362 // Explicit clinit checks triggered by static invokes must have been pruned by
3363 // art::PrepareForRegisterAllocation.
3364 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3365
Anton Kirilov5ec62182016-10-13 20:16:02 +01003366 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3367 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003368 return;
3369 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003370
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01003371 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01003372 CriticalNativeCallingConventionVisitorARMVIXL calling_convention_visitor(
3373 /*for_register_allocation=*/ true);
3374 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3375 } else {
3376 HandleInvoke(invoke);
3377 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003378}
3379
Anton Kirilov5ec62182016-10-13 20:16:02 +01003380static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3381 if (invoke->GetLocations()->Intrinsified()) {
3382 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3383 intrinsic.Dispatch(invoke);
3384 return true;
3385 }
3386 return false;
3387}
3388
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003389void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3390 // Explicit clinit checks triggered by static invokes must have been pruned by
3391 // art::PrepareForRegisterAllocation.
3392 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3393
Anton Kirilov5ec62182016-10-13 20:16:02 +01003394 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003395 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003396 return;
3397 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003398
3399 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003400 codegen_->GenerateStaticOrDirectCall(
3401 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003402
Andreas Gampe3db70682018-12-26 15:12:03 -08003403 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003404}
3405
3406void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003407 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003408 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3409}
3410
3411void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003412 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3413 if (intrinsic.TryDispatch(invoke)) {
3414 return;
3415 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003416
3417 HandleInvoke(invoke);
3418}
3419
3420void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003421 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003422 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003423 return;
3424 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003425
3426 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003427 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003428
Andreas Gampe3db70682018-12-26 15:12:03 -08003429 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003430}
3431
Artem Serovcfbe9132016-10-14 15:58:56 +01003432void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3433 HandleInvoke(invoke);
3434 // Add the hidden argument.
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003435 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
3436 // We cannot request r12 as it's blocked by the register allocator.
3437 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1, Location::Any());
3438 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003439}
3440
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003441void CodeGeneratorARMVIXL::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
3442 vixl32::Register klass) {
3443 DCHECK_EQ(r0.GetCode(), klass.GetCode());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003444 // We know the destination of an intrinsic, so no need to record inline
3445 // caches.
3446 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00003447 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003448 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003449 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01003450 ScopedProfilingInfoUse spiu(
3451 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
3452 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00003453 if (info != nullptr) {
3454 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
3455 uint32_t address = reinterpret_cast32<uint32_t>(cache);
3456 vixl32::Label done;
3457 UseScratchRegisterScope temps(GetVIXLAssembler());
3458 temps.Exclude(ip);
3459 __ Mov(r4, address);
3460 __ Ldr(ip, MemOperand(r4, InlineCache::ClassesOffset().Int32Value()));
3461 // Fast path for a monomorphic cache.
3462 __ Cmp(klass, ip);
3463 __ B(eq, &done, /* is_far_target= */ false);
3464 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
3465 __ Bind(&done);
3466 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003467 }
3468}
3469
Artem Serovcfbe9132016-10-14 15:58:56 +01003470void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3471 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3472 LocationSummary* locations = invoke->GetLocations();
3473 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Artem Serovcfbe9132016-10-14 15:58:56 +01003474 Location receiver = locations->InAt(0);
3475 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3476
3477 DCHECK(!receiver.IsStackSlot());
3478
Alexandre Rames374ddf32016-11-04 10:40:49 +00003479 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3480 {
Artem Serov0fb37192016-12-06 18:13:40 +00003481 ExactAssemblyScope aas(GetVIXLAssembler(),
3482 vixl32::kMaxInstructionSizeInBytes,
3483 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003484 // /* HeapReference<Class> */ temp = receiver->klass_
3485 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3486 codegen_->MaybeRecordImplicitNullCheck(invoke);
3487 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003488 // Instead of simply (possibly) unpoisoning `temp` here, we should
3489 // emit a read barrier for the previous class reference load.
3490 // However this is not required in practice, as this is an
3491 // intermediate/temporary reference and because the current
3492 // concurrent copying collector keeps the from-space memory
3493 // intact/accessible until the end of the marking phase (the
3494 // concurrent copying collector may not in the future).
3495 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003496
3497 // If we're compiling baseline, update the inline cache.
3498 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
3499
Artem Serovcfbe9132016-10-14 15:58:56 +01003500 GetAssembler()->LoadFromOffset(kLoadWord,
3501 temp,
3502 temp,
3503 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003504
Artem Serovcfbe9132016-10-14 15:58:56 +01003505 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3506 invoke->GetImtIndex(), kArmPointerSize));
3507 // temp = temp->GetImtEntryAt(method_offset);
3508 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3509 uint32_t entry_point =
3510 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3511 // LR = temp->GetEntryPoint();
3512 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3513
Scott Wakelingb77051e2016-11-21 19:46:00 +00003514 {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003515 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3516 // instruction from clobbering it as they might use r12 as a scratch register.
3517 Location hidden_reg = Location::RegisterLocation(r12.GetCode());
Scott Wakelingb77051e2016-11-21 19:46:00 +00003518 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3519 // so it checks if the application is using them (by passing them to the macro assembler
3520 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3521 // what is available, and is the opposite of the standard usage: Instead of requesting a
3522 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3523 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3524 // (to materialize the constant), since the destination register becomes available for such use
3525 // internally for the duration of the macro instruction.
3526 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01003527 temps.Exclude(RegisterFrom(hidden_reg));
3528 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
3529 Location current_method = locations->InAt(invoke->GetNumberOfArguments() - 1);
3530 if (current_method.IsStackSlot()) {
3531 GetAssembler()->LoadFromOffset(
3532 kLoadWord, RegisterFrom(hidden_reg), sp, current_method.GetStackIndex());
3533 } else {
3534 __ Mov(RegisterFrom(hidden_reg), RegisterFrom(current_method));
3535 }
3536 } else {
3537 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), hidden_reg, invoke);
3538 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00003539 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003540 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003541 // Ensure the pc position is recorded immediately after the `blx` instruction.
3542 // 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 +00003543 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003544 vixl32::k16BitT32InstructionSizeInBytes,
3545 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003546 // LR();
3547 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003548 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003549 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003550 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003551
Andreas Gampe3db70682018-12-26 15:12:03 -08003552 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003553}
3554
Orion Hodsonac141392017-01-13 11:53:47 +00003555void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003556 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3557 if (intrinsic.TryDispatch(invoke)) {
3558 return;
3559 }
Orion Hodsonac141392017-01-13 11:53:47 +00003560 HandleInvoke(invoke);
3561}
3562
3563void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003564 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3565 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 9);
3566 return;
3567 }
Orion Hodsonac141392017-01-13 11:53:47 +00003568 codegen_->GenerateInvokePolymorphicCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003569 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 10);
Orion Hodsonac141392017-01-13 11:53:47 +00003570}
3571
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003572void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3573 HandleInvoke(invoke);
3574}
3575
3576void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3577 codegen_->GenerateInvokeCustomCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003578 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 11);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003579}
3580
Artem Serov02109dd2016-09-23 17:17:54 +01003581void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3582 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003583 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003584 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003586 locations->SetInAt(0, Location::RequiresRegister());
3587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3588 break;
3589 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003590 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003591 locations->SetInAt(0, Location::RequiresRegister());
3592 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3593 break;
3594 }
3595
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003596 case DataType::Type::kFloat32:
3597 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003598 locations->SetInAt(0, Location::RequiresFpuRegister());
3599 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3600 break;
3601
3602 default:
3603 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3604 }
3605}
3606
3607void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3608 LocationSummary* locations = neg->GetLocations();
3609 Location out = locations->Out();
3610 Location in = locations->InAt(0);
3611 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003612 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003613 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3614 break;
3615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003616 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003617 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3618 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3619 // We cannot emit an RSC (Reverse Subtract with Carry)
3620 // instruction here, as it does not exist in the Thumb-2
3621 // instruction set. We use the following approach
3622 // using SBC and SUB instead.
3623 //
3624 // out.hi = -C
3625 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3626 // out.hi = out.hi - in.hi
3627 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3628 break;
3629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003630 case DataType::Type::kFloat32:
3631 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003632 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003633 break;
3634
3635 default:
3636 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3637 }
3638}
3639
Scott Wakelingfe885462016-09-22 10:24:38 +01003640void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003641 DataType::Type result_type = conversion->GetResultType();
3642 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003643 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3644 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003645
3646 // The float-to-long, double-to-long and long-to-float type conversions
3647 // rely on a call to the runtime.
3648 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3650 && result_type == DataType::Type::kInt64)
3651 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003652 ? LocationSummary::kCallOnMainOnly
3653 : LocationSummary::kNoCall;
3654 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003655 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003656
Scott Wakelingfe885462016-09-22 10:24:38 +01003657 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003658 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003659 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003660 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003662 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3663 locations->SetInAt(0, Location::RequiresRegister());
3664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003665 break;
3666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003668 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003669 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003670 locations->SetInAt(0, Location::Any());
3671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3672 break;
3673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003674 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003675 locations->SetInAt(0, Location::RequiresFpuRegister());
3676 locations->SetOut(Location::RequiresRegister());
3677 locations->AddTemp(Location::RequiresFpuRegister());
3678 break;
3679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003681 locations->SetInAt(0, Location::RequiresFpuRegister());
3682 locations->SetOut(Location::RequiresRegister());
3683 locations->AddTemp(Location::RequiresFpuRegister());
3684 break;
3685
3686 default:
3687 LOG(FATAL) << "Unexpected type conversion from " << input_type
3688 << " to " << result_type;
3689 }
3690 break;
3691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003692 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003693 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003694 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003695 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003697 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003698 case DataType::Type::kInt16:
3699 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003700 locations->SetInAt(0, Location::RequiresRegister());
3701 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3702 break;
3703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003704 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003705 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3706 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3707 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003708 break;
3709 }
3710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003712 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3713 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3714 calling_convention.GetFpuRegisterAt(1)));
3715 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003716 break;
3717 }
3718
3719 default:
3720 LOG(FATAL) << "Unexpected type conversion from " << input_type
3721 << " to " << result_type;
3722 }
3723 break;
3724
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003725 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003726 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003728 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003729 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003730 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003731 case DataType::Type::kInt16:
3732 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003733 locations->SetInAt(0, Location::RequiresRegister());
3734 locations->SetOut(Location::RequiresFpuRegister());
3735 break;
3736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003738 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3739 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3740 calling_convention.GetRegisterAt(1)));
3741 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003742 break;
3743 }
3744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003745 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003746 locations->SetInAt(0, Location::RequiresFpuRegister());
3747 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3748 break;
3749
3750 default:
3751 LOG(FATAL) << "Unexpected type conversion from " << input_type
3752 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003753 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003754 break;
3755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003756 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003757 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003758 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003759 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003760 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003761 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003762 case DataType::Type::kInt16:
3763 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003764 locations->SetInAt(0, Location::RequiresRegister());
3765 locations->SetOut(Location::RequiresFpuRegister());
3766 break;
3767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003768 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003769 locations->SetInAt(0, Location::RequiresRegister());
3770 locations->SetOut(Location::RequiresFpuRegister());
3771 locations->AddTemp(Location::RequiresFpuRegister());
3772 locations->AddTemp(Location::RequiresFpuRegister());
3773 break;
3774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003775 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003776 locations->SetInAt(0, Location::RequiresFpuRegister());
3777 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3778 break;
3779
3780 default:
3781 LOG(FATAL) << "Unexpected type conversion from " << input_type
3782 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003783 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003784 break;
3785
3786 default:
3787 LOG(FATAL) << "Unexpected type conversion from " << input_type
3788 << " to " << result_type;
3789 }
3790}
3791
3792void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3793 LocationSummary* locations = conversion->GetLocations();
3794 Location out = locations->Out();
3795 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 DataType::Type result_type = conversion->GetResultType();
3797 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003798 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3799 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003800 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003801 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003802 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003803 case DataType::Type::kInt8:
3804 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003805 case DataType::Type::kInt16:
3806 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003807 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3808 break;
3809 case DataType::Type::kInt64:
3810 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3811 break;
3812
3813 default:
3814 LOG(FATAL) << "Unexpected type conversion from " << input_type
3815 << " to " << result_type;
3816 }
3817 break;
3818
3819 case DataType::Type::kInt8:
3820 switch (input_type) {
3821 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003822 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003823 case DataType::Type::kInt16:
3824 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003825 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3826 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003827 case DataType::Type::kInt64:
3828 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3829 break;
3830
3831 default:
3832 LOG(FATAL) << "Unexpected type conversion from " << input_type
3833 << " to " << result_type;
3834 }
3835 break;
3836
3837 case DataType::Type::kUint16:
3838 switch (input_type) {
3839 case DataType::Type::kInt8:
3840 case DataType::Type::kInt16:
3841 case DataType::Type::kInt32:
3842 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3843 break;
3844 case DataType::Type::kInt64:
3845 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3846 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003847
3848 default:
3849 LOG(FATAL) << "Unexpected type conversion from " << input_type
3850 << " to " << result_type;
3851 }
3852 break;
3853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003855 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003856 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003857 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003858 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3859 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003860 case DataType::Type::kInt64:
3861 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3862 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003863
3864 default:
3865 LOG(FATAL) << "Unexpected type conversion from " << input_type
3866 << " to " << result_type;
3867 }
3868 break;
3869
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003870 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003871 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003872 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003873 DCHECK(out.IsRegister());
3874 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003875 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003876 } else if (in.IsDoubleStackSlot()) {
3877 GetAssembler()->LoadFromOffset(kLoadWord,
3878 OutputRegister(conversion),
3879 sp,
3880 in.GetStackIndex());
3881 } else {
3882 DCHECK(in.IsConstant());
3883 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003884 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3885 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003886 }
3887 break;
3888
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003889 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003890 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003891 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003892 __ Vmov(OutputRegister(conversion), temp);
3893 break;
3894 }
3895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003897 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003898 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003899 __ Vmov(OutputRegister(conversion), temp_s);
3900 break;
3901 }
3902
3903 default:
3904 LOG(FATAL) << "Unexpected type conversion from " << input_type
3905 << " to " << result_type;
3906 }
3907 break;
3908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003909 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003910 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003912 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003913 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003914 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003915 case DataType::Type::kInt16:
3916 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003917 DCHECK(out.IsRegisterPair());
3918 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003919 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003920 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003921 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003922 break;
3923
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003924 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003925 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3926 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3927 break;
3928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003929 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003930 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3931 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3932 break;
3933
3934 default:
3935 LOG(FATAL) << "Unexpected type conversion from " << input_type
3936 << " to " << result_type;
3937 }
3938 break;
3939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003940 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003941 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003942 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003943 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003944 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003945 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003946 case DataType::Type::kInt16:
3947 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003948 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003949 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003950 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003952 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003953 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3954 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3955 break;
3956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003957 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003958 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003959 break;
3960
3961 default:
3962 LOG(FATAL) << "Unexpected type conversion from " << input_type
3963 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003964 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003965 break;
3966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003967 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003968 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003969 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003970 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003971 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003972 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003973 case DataType::Type::kInt16:
3974 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003975 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003976 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003977 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003978
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003979 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003980 vixl32::Register low = LowRegisterFrom(in);
3981 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003982 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003983 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003984 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003985 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003986 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003987
3988 // temp_d = int-to-double(high)
3989 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003990 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003991 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003992 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003993 // out_d = unsigned-to-double(low)
3994 __ Vmov(out_s, low);
3995 __ Vcvt(F64, U32, out_d, out_s);
3996 // out_d += temp_d * constant_d
3997 __ Vmla(F64, out_d, temp_d, constant_d);
3998 break;
3999 }
4000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004001 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01004002 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004003 break;
4004
4005 default:
4006 LOG(FATAL) << "Unexpected type conversion from " << input_type
4007 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08004008 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004009 break;
4010
4011 default:
4012 LOG(FATAL) << "Unexpected type conversion from " << input_type
4013 << " to " << result_type;
4014 }
4015}
4016
4017void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4018 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004019 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004020 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004021 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004022 locations->SetInAt(0, Location::RequiresRegister());
4023 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4024 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4025 break;
4026 }
4027
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004028 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004029 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004030 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4032 break;
4033 }
4034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004035 case DataType::Type::kFloat32:
4036 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004037 locations->SetInAt(0, Location::RequiresFpuRegister());
4038 locations->SetInAt(1, Location::RequiresFpuRegister());
4039 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4040 break;
4041 }
4042
4043 default:
4044 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4045 }
4046}
4047
4048void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4049 LocationSummary* locations = add->GetLocations();
4050 Location out = locations->Out();
4051 Location first = locations->InAt(0);
4052 Location second = locations->InAt(1);
4053
4054 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004055 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004056 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4057 }
4058 break;
4059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004060 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004061 if (second.IsConstant()) {
4062 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4063 GenerateAddLongConst(out, first, value);
4064 } else {
4065 DCHECK(second.IsRegisterPair());
4066 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4067 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4068 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004069 break;
4070 }
4071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004072 case DataType::Type::kFloat32:
4073 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004074 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004075 break;
4076
4077 default:
4078 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4079 }
4080}
4081
4082void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4083 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004084 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004085 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004086 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4090 break;
4091 }
4092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004093 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004094 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004095 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4097 break;
4098 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004099 case DataType::Type::kFloat32:
4100 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004101 locations->SetInAt(0, Location::RequiresFpuRegister());
4102 locations->SetInAt(1, Location::RequiresFpuRegister());
4103 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4104 break;
4105 }
4106 default:
4107 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4108 }
4109}
4110
4111void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4112 LocationSummary* locations = sub->GetLocations();
4113 Location out = locations->Out();
4114 Location first = locations->InAt(0);
4115 Location second = locations->InAt(1);
4116 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004117 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004118 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004119 break;
4120 }
4121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004123 if (second.IsConstant()) {
4124 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4125 GenerateAddLongConst(out, first, -value);
4126 } else {
4127 DCHECK(second.IsRegisterPair());
4128 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4129 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4130 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004131 break;
4132 }
4133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004134 case DataType::Type::kFloat32:
4135 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004136 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004137 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004138
4139 default:
4140 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4141 }
4142}
4143
4144void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4145 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004146 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004147 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004148 case DataType::Type::kInt32:
4149 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004150 locations->SetInAt(0, Location::RequiresRegister());
4151 locations->SetInAt(1, Location::RequiresRegister());
4152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4153 break;
4154 }
4155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004156 case DataType::Type::kFloat32:
4157 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004158 locations->SetInAt(0, Location::RequiresFpuRegister());
4159 locations->SetInAt(1, Location::RequiresFpuRegister());
4160 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4161 break;
4162 }
4163
4164 default:
4165 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4166 }
4167}
4168
4169void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4170 LocationSummary* locations = mul->GetLocations();
4171 Location out = locations->Out();
4172 Location first = locations->InAt(0);
4173 Location second = locations->InAt(1);
4174 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004175 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004176 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4177 break;
4178 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004179 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004180 vixl32::Register out_hi = HighRegisterFrom(out);
4181 vixl32::Register out_lo = LowRegisterFrom(out);
4182 vixl32::Register in1_hi = HighRegisterFrom(first);
4183 vixl32::Register in1_lo = LowRegisterFrom(first);
4184 vixl32::Register in2_hi = HighRegisterFrom(second);
4185 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004186
4187 // Extra checks to protect caused by the existence of R1_R2.
4188 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4189 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004190 DCHECK(!out_hi.Is(in1_lo));
4191 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004192
4193 // input: in1 - 64 bits, in2 - 64 bits
4194 // output: out
4195 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4196 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4197 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4198
4199 UseScratchRegisterScope temps(GetVIXLAssembler());
4200 vixl32::Register temp = temps.Acquire();
4201 // temp <- in1.lo * in2.hi
4202 __ Mul(temp, in1_lo, in2_hi);
4203 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4204 __ Mla(out_hi, in1_hi, in2_lo, temp);
4205 // out.lo <- (in1.lo * in2.lo)[31:0];
4206 __ Umull(out_lo, temp, in1_lo, in2_lo);
4207 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004208 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004209 break;
4210 }
4211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 case DataType::Type::kFloat32:
4213 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004214 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004215 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004216
4217 default:
4218 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4219 }
4220}
4221
Scott Wakelingfe885462016-09-22 10:24:38 +01004222void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4223 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004224 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004225
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004226 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004227 DCHECK(second.IsConstant());
4228
4229 vixl32::Register out = OutputRegister(instruction);
4230 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004231 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004232 DCHECK(imm == 1 || imm == -1);
4233
4234 if (instruction->IsRem()) {
4235 __ Mov(out, 0);
4236 } else {
4237 if (imm == 1) {
4238 __ Mov(out, dividend);
4239 } else {
4240 __ Rsb(out, dividend, 0);
4241 }
4242 }
4243}
4244
4245void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4246 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004247 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004248
4249 LocationSummary* locations = instruction->GetLocations();
4250 Location second = locations->InAt(1);
4251 DCHECK(second.IsConstant());
4252
4253 vixl32::Register out = OutputRegister(instruction);
4254 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004255 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004256 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4257 int ctz_imm = CTZ(abs_imm);
4258
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004259 auto generate_div_code = [this, imm, ctz_imm](vixl32::Register out, vixl32::Register in) {
4260 __ Asr(out, in, ctz_imm);
Scott Wakelingfe885462016-09-22 10:24:38 +01004261 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004262 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004263 }
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004264 };
4265
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004266 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004267 // No need to adjust the result for non-negative dividends or the INT32_MIN dividend.
4268 // NOTE: The generated code for HDiv/HRem correctly works for the INT32_MIN dividend:
4269 // imm == 2
4270 // HDiv
4271 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
4272 // asr out, out(0x80000001), #1 => out = 0xc0000000
4273 // This is the same as 'asr out, dividend(0x80000000), #1'
4274 //
4275 // imm > 2
4276 // HDiv
4277 // asr out, dividend(0x80000000), #31 => out = -1
4278 // add out, dividend(0x80000000), out(-1), lsr #(32 - ctz_imm) => out = 0b10..01..1,
4279 // where the number of the rightmost 1s is ctz_imm.
4280 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
4281 // leftmost 1s is ctz_imm + 1.
4282 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
4283 //
4284 // imm == INT32_MIN
4285 // HDiv
4286 // asr out, dividend(0x80000000), #31 => out = -1
4287 // add out, dividend(0x80000000), out(-1), lsr #1 => out = 0xc0000000
4288 // asr out, out(0xc0000000), #31 => out = -1
4289 // rsb out, out(-1), #0 => out = 1
4290 // This is the same as
4291 // asr out, dividend(0x80000000), #31
4292 // rsb out, out, #0
4293 //
4294 //
4295 // INT_MIN % imm must be 0 for any imm of power 2. 'and' and 'ubfx' work only with bits
4296 // 0..30 of a dividend. For INT32_MIN those bits are zeros. So 'and' and 'ubfx' always
4297 // produce zero.
4298 if (instruction->IsDiv()) {
4299 generate_div_code(out, dividend);
4300 } else {
4301 if (GetVIXLAssembler()->IsModifiedImmediate(abs_imm - 1)) {
4302 __ And(out, dividend, abs_imm - 1);
4303 } else {
4304 __ Ubfx(out, dividend, 0, ctz_imm);
4305 }
4306 return;
4307 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004308 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004309 vixl32::Register add_right_input = dividend;
4310 if (ctz_imm > 1) {
4311 __ Asr(out, dividend, 31);
4312 add_right_input = out;
4313 }
4314 __ Add(out, dividend, Operand(add_right_input, vixl32::LSR, 32 - ctz_imm));
4315
4316 if (instruction->IsDiv()) {
4317 generate_div_code(out, out);
4318 } else {
4319 __ Bfc(out, 0, ctz_imm);
4320 __ Sub(out, dividend, out);
4321 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004322 }
4323}
4324
4325void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4326 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004327 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004328
4329 LocationSummary* locations = instruction->GetLocations();
4330 Location second = locations->InAt(1);
4331 DCHECK(second.IsConstant());
4332
4333 vixl32::Register out = OutputRegister(instruction);
4334 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004335 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4336 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004337 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004338
4339 int64_t magic;
4340 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004341 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Scott Wakelingfe885462016-09-22 10:24:38 +01004342
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004343 auto generate_unsigned_div_code =[this, magic, shift](vixl32::Register out,
4344 vixl32::Register dividend,
4345 vixl32::Register temp1,
4346 vixl32::Register temp2) {
4347 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4348 __ Mov(temp1, static_cast<int32_t>(magic));
4349 if (magic > 0 && shift == 0) {
4350 __ Smull(temp2, out, dividend, temp1);
4351 } else {
4352 __ Smull(temp2, temp1, dividend, temp1);
4353 if (magic < 0) {
4354 // The negative magic M = static_cast<int>(m) means that the multiplier m is greater
4355 // than INT32_MAX. In such a case shift is never 0.
4356 // Proof:
4357 // m = (2^p + d - 2^p % d) / d, where p = 32 + shift, d > 2
4358 //
4359 // If shift == 0, m = (2^32 + d - 2^32 % d) / d =
4360 // = (2^32 + d - (2^32 - (2^32 / d) * d)) / d =
4361 // = (d + (2^32 / d) * d) / d = 1 + (2^32 / d), here '/' is the integer division.
4362 //
4363 // 1 + (2^32 / d) is decreasing when d is increasing.
4364 // The maximum is 1 431 655 766, when d == 3. This value is less than INT32_MAX.
4365 // the minimum is 3, when d = 2^31 -1.
4366 // So for all values of d in [3, INT32_MAX] m with p == 32 is in [3, INT32_MAX) and
4367 // is never less than 0.
4368 __ Add(temp1, temp1, dividend);
4369 }
4370 DCHECK_NE(shift, 0);
4371 __ Lsr(out, temp1, shift);
4372 }
4373 };
Scott Wakelingfe885462016-09-22 10:24:38 +01004374
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004375 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004376 // No need to adjust the result for a non-negative dividend and a positive divisor.
4377 if (instruction->IsDiv()) {
4378 generate_unsigned_div_code(out, dividend, temp1, temp2);
4379 } else {
4380 generate_unsigned_div_code(temp1, dividend, temp1, temp2);
4381 __ Mov(temp2, imm);
4382 __ Mls(out, temp1, temp2, dividend);
4383 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004384 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004385 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4386 __ Mov(temp1, static_cast<int32_t>(magic));
4387 __ Smull(temp2, temp1, dividend, temp1);
4388
4389 if (imm > 0 && magic < 0) {
4390 __ Add(temp1, temp1, dividend);
4391 } else if (imm < 0 && magic > 0) {
4392 __ Sub(temp1, temp1, dividend);
4393 }
4394
4395 if (shift != 0) {
4396 __ Asr(temp1, temp1, shift);
4397 }
4398
4399 if (instruction->IsDiv()) {
4400 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4401 } else {
4402 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4403 // TODO: Strength reduction for mls.
4404 __ Mov(temp2, imm);
4405 __ Mls(out, temp1, temp2, dividend);
4406 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004407 }
4408}
4409
4410void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4411 HBinaryOperation* instruction) {
4412 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004413 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004414
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004415 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004416 DCHECK(second.IsConstant());
4417
Anton Kirilov644032c2016-12-06 17:51:43 +00004418 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004419 if (imm == 0) {
4420 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4421 } else if (imm == 1 || imm == -1) {
4422 DivRemOneOrMinusOne(instruction);
4423 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4424 DivRemByPowerOfTwo(instruction);
4425 } else {
4426 DCHECK(imm <= -2 || imm >= 2);
4427 GenerateDivRemWithAnyConstant(instruction);
4428 }
4429}
4430
4431void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4432 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004433 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004434 // pLdiv runtime call.
4435 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004436 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004437 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004438 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004439 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4440 // pIdivmod runtime call.
4441 call_kind = LocationSummary::kCallOnMainOnly;
4442 }
4443
Vladimir Markoca6fff82017-10-03 14:49:14 +01004444 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004445
4446 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004447 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004448 if (div->InputAt(1)->IsConstant()) {
4449 locations->SetInAt(0, Location::RequiresRegister());
4450 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004451 int32_t value = Int32ConstantFrom(div->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004452 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004453 if (value == 1 || value == 0 || value == -1) {
4454 // No temp register required.
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004455 } else if (IsPowerOfTwo(AbsOrMin(value)) &&
4456 value != 2 &&
4457 value != -2 &&
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004458 !HasNonNegativeOrMinIntInputAt(div, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004459 // The "out" register is used as a temporary, so it overlaps with the inputs.
4460 out_overlaps = Location::kOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004461 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004462 locations->AddRegisterTemps(2);
Scott Wakelingfe885462016-09-22 10:24:38 +01004463 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004464 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Scott Wakelingfe885462016-09-22 10:24:38 +01004465 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4466 locations->SetInAt(0, Location::RequiresRegister());
4467 locations->SetInAt(1, Location::RequiresRegister());
4468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4469 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004470 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4471 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4472 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004473 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004474 // we only need the former.
4475 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004476 }
4477 break;
4478 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004479 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004480 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4481 locations->SetInAt(0, LocationFrom(
4482 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4483 locations->SetInAt(1, LocationFrom(
4484 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4485 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004486 break;
4487 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004488 case DataType::Type::kFloat32:
4489 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004490 locations->SetInAt(0, Location::RequiresFpuRegister());
4491 locations->SetInAt(1, Location::RequiresFpuRegister());
4492 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4493 break;
4494 }
4495
4496 default:
4497 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4498 }
4499}
4500
4501void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004502 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004503 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004504
4505 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004506 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004507 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004508 GenerateDivRemConstantIntegral(div);
4509 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4510 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4511 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004512 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4513 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4514 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4515 DCHECK(r0.Is(OutputRegister(div)));
4516
4517 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4518 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004519 }
4520 break;
4521 }
4522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004523 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004524 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4525 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4526 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4527 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4528 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4529 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4530 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4531
4532 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4533 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004534 break;
4535 }
4536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004537 case DataType::Type::kFloat32:
4538 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004539 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004540 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004541
4542 default:
4543 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4544 }
4545}
4546
Artem Serov551b28f2016-10-18 19:11:30 +01004547void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004548 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004549
4550 // Most remainders are implemented in the runtime.
4551 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004552 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004553 // sdiv will be replaced by other instruction sequence.
4554 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004555 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004556 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4557 // Have hardware divide instruction for int, do it with three instructions.
4558 call_kind = LocationSummary::kNoCall;
4559 }
4560
Vladimir Markoca6fff82017-10-03 14:49:14 +01004561 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004562
4563 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004564 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004565 if (rem->InputAt(1)->IsConstant()) {
4566 locations->SetInAt(0, Location::RequiresRegister());
4567 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004568 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004569 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004570 if (value == 1 || value == 0 || value == -1) {
4571 // No temp register required.
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004572 } else if (IsPowerOfTwo(AbsOrMin(value)) && !HasNonNegativeOrMinIntInputAt(rem, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004573 // The "out" register is used as a temporary, so it overlaps with the inputs.
4574 out_overlaps = Location::kOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004575 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004576 locations->AddRegisterTemps(2);
Artem Serov551b28f2016-10-18 19:11:30 +01004577 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004578 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Artem Serov551b28f2016-10-18 19:11:30 +01004579 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4580 locations->SetInAt(0, Location::RequiresRegister());
4581 locations->SetInAt(1, Location::RequiresRegister());
4582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4583 locations->AddTemp(Location::RequiresRegister());
4584 } else {
4585 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4586 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4587 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004588 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004589 // we only need the latter.
4590 locations->SetOut(LocationFrom(r1));
4591 }
4592 break;
4593 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004594 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004595 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4596 locations->SetInAt(0, LocationFrom(
4597 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4598 locations->SetInAt(1, LocationFrom(
4599 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4600 // The runtime helper puts the output in R2,R3.
4601 locations->SetOut(LocationFrom(r2, r3));
4602 break;
4603 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004605 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4606 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4607 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4608 locations->SetOut(LocationFrom(s0));
4609 break;
4610 }
4611
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004612 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004613 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4614 locations->SetInAt(0, LocationFrom(
4615 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4616 locations->SetInAt(1, LocationFrom(
4617 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4618 locations->SetOut(LocationFrom(s0, s1));
4619 break;
4620 }
4621
4622 default:
4623 LOG(FATAL) << "Unexpected rem type " << type;
4624 }
4625}
4626
4627void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4628 LocationSummary* locations = rem->GetLocations();
4629 Location second = locations->InAt(1);
4630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004631 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004632 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004633 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004634 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4635 vixl32::Register out_reg = OutputRegister(rem);
4636 if (second.IsConstant()) {
4637 GenerateDivRemConstantIntegral(rem);
4638 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4639 vixl32::Register reg2 = RegisterFrom(second);
4640 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4641
4642 // temp = reg1 / reg2 (integer division)
4643 // dest = reg1 - temp * reg2
4644 __ Sdiv(temp, reg1, reg2);
4645 __ Mls(out_reg, temp, reg2, reg1);
4646 } else {
4647 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4648 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4649 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4650 DCHECK(out_reg.Is(r1));
4651
4652 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4653 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4654 }
4655 break;
4656 }
4657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004658 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004659 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4660 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4661 break;
4662 }
4663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004664 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004665 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4666 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4667 break;
4668 }
4669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004670 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004671 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4672 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4673 break;
4674 }
4675
4676 default:
4677 LOG(FATAL) << "Unexpected rem type " << type;
4678 }
4679}
4680
Aart Bik1f8d51b2018-02-15 10:42:37 -08004681static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4682 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4683 switch (minmax->GetResultType()) {
4684 case DataType::Type::kInt32:
4685 locations->SetInAt(0, Location::RequiresRegister());
4686 locations->SetInAt(1, Location::RequiresRegister());
4687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4688 break;
4689 case DataType::Type::kInt64:
4690 locations->SetInAt(0, Location::RequiresRegister());
4691 locations->SetInAt(1, Location::RequiresRegister());
4692 locations->SetOut(Location::SameAsFirstInput());
4693 break;
4694 case DataType::Type::kFloat32:
4695 locations->SetInAt(0, Location::RequiresFpuRegister());
4696 locations->SetInAt(1, Location::RequiresFpuRegister());
4697 locations->SetOut(Location::SameAsFirstInput());
4698 locations->AddTemp(Location::RequiresRegister());
4699 break;
4700 case DataType::Type::kFloat64:
4701 locations->SetInAt(0, Location::RequiresFpuRegister());
4702 locations->SetInAt(1, Location::RequiresFpuRegister());
4703 locations->SetOut(Location::SameAsFirstInput());
4704 break;
4705 default:
4706 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4707 }
4708}
4709
Aart Bik351df3e2018-03-07 11:54:57 -08004710void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004711 Location op1_loc = locations->InAt(0);
4712 Location op2_loc = locations->InAt(1);
4713 Location out_loc = locations->Out();
4714
4715 vixl32::Register op1 = RegisterFrom(op1_loc);
4716 vixl32::Register op2 = RegisterFrom(op2_loc);
4717 vixl32::Register out = RegisterFrom(out_loc);
4718
4719 __ Cmp(op1, op2);
4720
4721 {
4722 ExactAssemblyScope aas(GetVIXLAssembler(),
4723 3 * kMaxInstructionSizeInBytes,
4724 CodeBufferCheckScope::kMaximumSize);
4725
4726 __ ite(is_min ? lt : gt);
4727 __ mov(is_min ? lt : gt, out, op1);
4728 __ mov(is_min ? ge : le, out, op2);
4729 }
4730}
4731
4732void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4733 Location op1_loc = locations->InAt(0);
4734 Location op2_loc = locations->InAt(1);
4735 Location out_loc = locations->Out();
4736
4737 // Optimization: don't generate any code if inputs are the same.
4738 if (op1_loc.Equals(op2_loc)) {
4739 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4740 return;
4741 }
4742
4743 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4744 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4745 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4746 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4747 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4748 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4749 UseScratchRegisterScope temps(GetVIXLAssembler());
4750 const vixl32::Register temp = temps.Acquire();
4751
4752 DCHECK(op1_lo.Is(out_lo));
4753 DCHECK(op1_hi.Is(out_hi));
4754
4755 // Compare op1 >= op2, or op1 < op2.
4756 __ Cmp(out_lo, op2_lo);
4757 __ Sbcs(temp, out_hi, op2_hi);
4758
4759 // Now GE/LT condition code is correct for the long comparison.
4760 {
4761 vixl32::ConditionType cond = is_min ? ge : lt;
4762 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4763 3 * kMaxInstructionSizeInBytes,
4764 CodeBufferCheckScope::kMaximumSize);
4765 __ itt(cond);
4766 __ mov(cond, out_lo, op2_lo);
4767 __ mov(cond, out_hi, op2_hi);
4768 }
4769}
4770
Aart Bik351df3e2018-03-07 11:54:57 -08004771void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4772 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004773 Location op1_loc = locations->InAt(0);
4774 Location op2_loc = locations->InAt(1);
4775 Location out_loc = locations->Out();
4776
4777 // Optimization: don't generate any code if inputs are the same.
4778 if (op1_loc.Equals(op2_loc)) {
4779 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4780 return;
4781 }
4782
4783 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4784 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4785 vixl32::SRegister out = SRegisterFrom(out_loc);
4786
4787 UseScratchRegisterScope temps(GetVIXLAssembler());
4788 const vixl32::Register temp1 = temps.Acquire();
4789 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4790 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004791 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004792
4793 DCHECK(op1.Is(out));
4794
4795 __ Vcmp(op1, op2);
4796 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004797 __ B(vs, &nan, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004798
4799 // op1 <> op2
4800 vixl32::ConditionType cond = is_min ? gt : lt;
4801 {
4802 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4803 2 * kMaxInstructionSizeInBytes,
4804 CodeBufferCheckScope::kMaximumSize);
4805 __ it(cond);
4806 __ vmov(cond, F32, out, op2);
4807 }
4808 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004809 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004810
4811 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4812 __ Vmov(temp1, op1);
4813 __ Vmov(temp2, op2);
4814 if (is_min) {
4815 __ Orr(temp1, temp1, temp2);
4816 } else {
4817 __ And(temp1, temp1, temp2);
4818 }
4819 __ Vmov(out, temp1);
4820 __ B(final_label);
4821
4822 // handle NaN input.
4823 __ Bind(&nan);
4824 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4825 __ Vmov(out, temp1);
4826
4827 if (done.IsReferenced()) {
4828 __ Bind(&done);
4829 }
4830}
4831
Aart Bik351df3e2018-03-07 11:54:57 -08004832void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4833 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004834 Location op1_loc = locations->InAt(0);
4835 Location op2_loc = locations->InAt(1);
4836 Location out_loc = locations->Out();
4837
4838 // Optimization: don't generate any code if inputs are the same.
4839 if (op1_loc.Equals(op2_loc)) {
4840 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4841 return;
4842 }
4843
4844 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4845 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4846 vixl32::DRegister out = DRegisterFrom(out_loc);
4847 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004848 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004849
4850 DCHECK(op1.Is(out));
4851
4852 __ Vcmp(op1, op2);
4853 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004854 __ B(vs, &handle_nan_eq, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004855
4856 // op1 <> op2
4857 vixl32::ConditionType cond = is_min ? gt : lt;
4858 {
4859 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4860 2 * kMaxInstructionSizeInBytes,
4861 CodeBufferCheckScope::kMaximumSize);
4862 __ it(cond);
4863 __ vmov(cond, F64, out, op2);
4864 }
4865 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004866 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004867
4868 // handle op1 == op2, max(+0.0,-0.0).
4869 if (!is_min) {
4870 __ Vand(F64, out, op1, op2);
4871 __ B(final_label);
4872 }
4873
4874 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4875 __ Bind(&handle_nan_eq);
4876 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4877
4878 if (done.IsReferenced()) {
4879 __ Bind(&done);
4880 }
4881}
4882
Aart Bik351df3e2018-03-07 11:54:57 -08004883void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4884 DataType::Type type = minmax->GetResultType();
4885 switch (type) {
4886 case DataType::Type::kInt32:
4887 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4888 break;
4889 case DataType::Type::kInt64:
4890 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4891 break;
4892 case DataType::Type::kFloat32:
4893 GenerateMinMaxFloat(minmax, is_min);
4894 break;
4895 case DataType::Type::kFloat64:
4896 GenerateMinMaxDouble(minmax, is_min);
4897 break;
4898 default:
4899 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4900 }
4901}
4902
Aart Bik1f8d51b2018-02-15 10:42:37 -08004903void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4904 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4905}
4906
4907void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004908 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004909}
4910
4911void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4912 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4913}
4914
4915void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004916 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004917}
4918
Aart Bik3dad3412018-02-28 12:01:46 -08004919void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4920 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4921 switch (abs->GetResultType()) {
4922 case DataType::Type::kInt32:
4923 case DataType::Type::kInt64:
4924 locations->SetInAt(0, Location::RequiresRegister());
4925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4926 locations->AddTemp(Location::RequiresRegister());
4927 break;
4928 case DataType::Type::kFloat32:
4929 case DataType::Type::kFloat64:
4930 locations->SetInAt(0, Location::RequiresFpuRegister());
4931 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4932 break;
4933 default:
4934 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4935 }
4936}
4937
4938void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4939 LocationSummary* locations = abs->GetLocations();
4940 switch (abs->GetResultType()) {
4941 case DataType::Type::kInt32: {
4942 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4943 vixl32::Register out_reg = RegisterFrom(locations->Out());
4944 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4945 __ Asr(mask, in_reg, 31);
4946 __ Add(out_reg, in_reg, mask);
4947 __ Eor(out_reg, out_reg, mask);
4948 break;
4949 }
4950 case DataType::Type::kInt64: {
4951 Location in = locations->InAt(0);
4952 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4953 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4954 Location output = locations->Out();
4955 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4956 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4957 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4958 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4959 __ Asr(mask, in_reg_hi, 31);
4960 __ Adds(out_reg_lo, in_reg_lo, mask);
4961 __ Adc(out_reg_hi, in_reg_hi, mask);
4962 __ Eor(out_reg_lo, out_reg_lo, mask);
4963 __ Eor(out_reg_hi, out_reg_hi, mask);
4964 break;
4965 }
4966 case DataType::Type::kFloat32:
4967 case DataType::Type::kFloat64:
4968 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4969 break;
4970 default:
4971 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4972 }
4973}
Artem Serov551b28f2016-10-18 19:11:30 +01004974
Scott Wakelingfe885462016-09-22 10:24:38 +01004975void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004976 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004977 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004978}
4979
4980void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4981 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004982 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004983 codegen_->AddSlowPath(slow_path);
4984
4985 LocationSummary* locations = instruction->GetLocations();
4986 Location value = locations->InAt(0);
4987
4988 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004989 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004990 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004991 case DataType::Type::kInt8:
4992 case DataType::Type::kUint16:
4993 case DataType::Type::kInt16:
4994 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004995 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004996 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004997 } else {
4998 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004999 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005000 __ B(slow_path->GetEntryLabel());
5001 }
5002 }
5003 break;
5004 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005005 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01005006 if (value.IsRegisterPair()) {
5007 UseScratchRegisterScope temps(GetVIXLAssembler());
5008 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005009 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01005010 __ B(eq, slow_path->GetEntryLabel());
5011 } else {
5012 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005013 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005014 __ B(slow_path->GetEntryLabel());
5015 }
5016 }
5017 break;
5018 }
5019 default:
5020 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5021 }
5022}
5023
Artem Serov02109dd2016-09-23 17:17:54 +01005024void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5025 LocationSummary* locations = ror->GetLocations();
5026 vixl32::Register in = InputRegisterAt(ror, 0);
5027 Location rhs = locations->InAt(1);
5028 vixl32::Register out = OutputRegister(ror);
5029
5030 if (rhs.IsConstant()) {
5031 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5032 // so map all rotations to a +ve. equivalent in that range.
5033 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5034 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5035 if (rot) {
5036 // Rotate, mapping left rotations to right equivalents if necessary.
5037 // (e.g. left by 2 bits == right by 30.)
5038 __ Ror(out, in, rot);
5039 } else if (!out.Is(in)) {
5040 __ Mov(out, in);
5041 }
5042 } else {
5043 __ Ror(out, in, RegisterFrom(rhs));
5044 }
5045}
5046
5047// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5048// rotates by swapping input regs (effectively rotating by the first 32-bits of
5049// a larger rotation) or flipping direction (thus treating larger right/left
5050// rotations as sub-word sized rotations in the other direction) as appropriate.
5051void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5052 LocationSummary* locations = ror->GetLocations();
5053 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5054 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5055 Location rhs = locations->InAt(1);
5056 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5057 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5058
5059 if (rhs.IsConstant()) {
5060 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5061 // Map all rotations to +ve. equivalents on the interval [0,63].
5062 rot &= kMaxLongShiftDistance;
5063 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5064 // logic below to a simple pair of binary orr.
5065 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5066 if (rot >= kArmBitsPerWord) {
5067 rot -= kArmBitsPerWord;
5068 std::swap(in_reg_hi, in_reg_lo);
5069 }
5070 // Rotate, or mov to out for zero or word size rotations.
5071 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005072 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005073 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005074 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005075 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5076 } else {
5077 __ Mov(out_reg_lo, in_reg_lo);
5078 __ Mov(out_reg_hi, in_reg_hi);
5079 }
5080 } else {
5081 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5082 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5083 vixl32::Label end;
5084 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005085 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005086
5087 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5088 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005089 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Andreas Gampe3db70682018-12-26 15:12:03 -08005090 __ B(cc, &shift_by_32_plus_shift_right, /* is_far_target= */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005091
5092 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5093 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5094 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5095 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5096 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5097 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5098 __ Lsr(shift_left, in_reg_hi, shift_right);
5099 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005100 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005101
5102 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5103 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5104 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5105 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5106 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5107 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5108 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5109 __ Lsl(shift_right, in_reg_hi, shift_left);
5110 __ Add(out_reg_lo, out_reg_lo, shift_right);
5111
Anton Kirilov6f644202017-02-27 18:29:45 +00005112 if (end.IsReferenced()) {
5113 __ Bind(&end);
5114 }
Artem Serov02109dd2016-09-23 17:17:54 +01005115 }
5116}
5117
5118void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5119 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005120 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005121 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005122 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005123 locations->SetInAt(0, Location::RequiresRegister());
5124 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5126 break;
5127 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005128 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005129 locations->SetInAt(0, Location::RequiresRegister());
5130 if (ror->InputAt(1)->IsConstant()) {
5131 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5132 } else {
5133 locations->SetInAt(1, Location::RequiresRegister());
5134 locations->AddTemp(Location::RequiresRegister());
5135 locations->AddTemp(Location::RequiresRegister());
5136 }
5137 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5138 break;
5139 }
5140 default:
5141 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5142 }
5143}
5144
5145void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005146 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005147 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005148 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005149 HandleIntegerRotate(ror);
5150 break;
5151 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005152 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005153 HandleLongRotate(ror);
5154 break;
5155 }
5156 default:
5157 LOG(FATAL) << "Unexpected operation type " << type;
5158 UNREACHABLE();
5159 }
5160}
5161
Artem Serov02d37832016-10-25 15:25:33 +01005162void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5163 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5164
5165 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005166 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005167
5168 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005169 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005170 locations->SetInAt(0, Location::RequiresRegister());
5171 if (op->InputAt(1)->IsConstant()) {
5172 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5173 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5174 } else {
5175 locations->SetInAt(1, Location::RequiresRegister());
5176 // Make the output overlap, as it will be used to hold the masked
5177 // second input.
5178 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5179 }
5180 break;
5181 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005182 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005183 locations->SetInAt(0, Location::RequiresRegister());
5184 if (op->InputAt(1)->IsConstant()) {
5185 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5186 // For simplicity, use kOutputOverlap even though we only require that low registers
5187 // don't clash with high registers which the register allocator currently guarantees.
5188 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5189 } else {
5190 locations->SetInAt(1, Location::RequiresRegister());
5191 locations->AddTemp(Location::RequiresRegister());
5192 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5193 }
5194 break;
5195 }
5196 default:
5197 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5198 }
5199}
5200
5201void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5202 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5203
5204 LocationSummary* locations = op->GetLocations();
5205 Location out = locations->Out();
5206 Location first = locations->InAt(0);
5207 Location second = locations->InAt(1);
5208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005210 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005211 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005212 vixl32::Register out_reg = OutputRegister(op);
5213 vixl32::Register first_reg = InputRegisterAt(op, 0);
5214 if (second.IsRegister()) {
5215 vixl32::Register second_reg = RegisterFrom(second);
5216 // ARM doesn't mask the shift count so we need to do it ourselves.
5217 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5218 if (op->IsShl()) {
5219 __ Lsl(out_reg, first_reg, out_reg);
5220 } else if (op->IsShr()) {
5221 __ Asr(out_reg, first_reg, out_reg);
5222 } else {
5223 __ Lsr(out_reg, first_reg, out_reg);
5224 }
5225 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005226 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005227 uint32_t shift_value = cst & kMaxIntShiftDistance;
5228 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5229 __ Mov(out_reg, first_reg);
5230 } else if (op->IsShl()) {
5231 __ Lsl(out_reg, first_reg, shift_value);
5232 } else if (op->IsShr()) {
5233 __ Asr(out_reg, first_reg, shift_value);
5234 } else {
5235 __ Lsr(out_reg, first_reg, shift_value);
5236 }
5237 }
5238 break;
5239 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005240 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005241 vixl32::Register o_h = HighRegisterFrom(out);
5242 vixl32::Register o_l = LowRegisterFrom(out);
5243
5244 vixl32::Register high = HighRegisterFrom(first);
5245 vixl32::Register low = LowRegisterFrom(first);
5246
5247 if (second.IsRegister()) {
5248 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5249
5250 vixl32::Register second_reg = RegisterFrom(second);
5251
5252 if (op->IsShl()) {
5253 __ And(o_l, second_reg, kMaxLongShiftDistance);
5254 // Shift the high part
5255 __ Lsl(o_h, high, o_l);
5256 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005257 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005258 __ Lsr(temp, low, temp);
5259 __ Orr(o_h, o_h, temp);
5260 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005261 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005262 {
Artem Serov0fb37192016-12-06 18:13:40 +00005263 ExactAssemblyScope guard(GetVIXLAssembler(),
5264 2 * vixl32::kMaxInstructionSizeInBytes,
5265 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005266 __ it(pl);
5267 __ lsl(pl, o_h, low, temp);
5268 }
5269 // Shift the low part
5270 __ Lsl(o_l, low, o_l);
5271 } else if (op->IsShr()) {
5272 __ And(o_h, second_reg, kMaxLongShiftDistance);
5273 // Shift the low part
5274 __ Lsr(o_l, low, o_h);
5275 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005276 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005277 __ Lsl(temp, high, temp);
5278 __ Orr(o_l, o_l, temp);
5279 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005280 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005281 {
Artem Serov0fb37192016-12-06 18:13:40 +00005282 ExactAssemblyScope guard(GetVIXLAssembler(),
5283 2 * vixl32::kMaxInstructionSizeInBytes,
5284 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005285 __ it(pl);
5286 __ asr(pl, o_l, high, temp);
5287 }
5288 // Shift the high part
5289 __ Asr(o_h, high, o_h);
5290 } else {
5291 __ And(o_h, second_reg, kMaxLongShiftDistance);
5292 // same as Shr except we use `Lsr`s and not `Asr`s
5293 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005294 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005295 __ Lsl(temp, high, temp);
5296 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005297 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005298 {
Artem Serov0fb37192016-12-06 18:13:40 +00005299 ExactAssemblyScope guard(GetVIXLAssembler(),
5300 2 * vixl32::kMaxInstructionSizeInBytes,
5301 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005302 __ it(pl);
5303 __ lsr(pl, o_l, high, temp);
5304 }
5305 __ Lsr(o_h, high, o_h);
5306 }
5307 } else {
5308 // Register allocator doesn't create partial overlap.
5309 DCHECK(!o_l.Is(high));
5310 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005311 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005312 uint32_t shift_value = cst & kMaxLongShiftDistance;
5313 if (shift_value > 32) {
5314 if (op->IsShl()) {
5315 __ Lsl(o_h, low, shift_value - 32);
5316 __ Mov(o_l, 0);
5317 } else if (op->IsShr()) {
5318 __ Asr(o_l, high, shift_value - 32);
5319 __ Asr(o_h, high, 31);
5320 } else {
5321 __ Lsr(o_l, high, shift_value - 32);
5322 __ Mov(o_h, 0);
5323 }
5324 } else if (shift_value == 32) {
5325 if (op->IsShl()) {
5326 __ Mov(o_h, low);
5327 __ Mov(o_l, 0);
5328 } else if (op->IsShr()) {
5329 __ Mov(o_l, high);
5330 __ Asr(o_h, high, 31);
5331 } else {
5332 __ Mov(o_l, high);
5333 __ Mov(o_h, 0);
5334 }
5335 } else if (shift_value == 1) {
5336 if (op->IsShl()) {
5337 __ Lsls(o_l, low, 1);
5338 __ Adc(o_h, high, high);
5339 } else if (op->IsShr()) {
5340 __ Asrs(o_h, high, 1);
5341 __ Rrx(o_l, low);
5342 } else {
5343 __ Lsrs(o_h, high, 1);
5344 __ Rrx(o_l, low);
5345 }
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005346 } else if (shift_value == 0) {
5347 __ Mov(o_l, low);
5348 __ Mov(o_h, high);
Artem Serov02d37832016-10-25 15:25:33 +01005349 } else {
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005350 DCHECK(0 < shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01005351 if (op->IsShl()) {
5352 __ Lsl(o_h, high, shift_value);
5353 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5354 __ Lsl(o_l, low, shift_value);
5355 } else if (op->IsShr()) {
5356 __ Lsr(o_l, low, shift_value);
5357 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5358 __ Asr(o_h, high, shift_value);
5359 } else {
5360 __ Lsr(o_l, low, shift_value);
5361 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5362 __ Lsr(o_h, high, shift_value);
5363 }
5364 }
5365 }
5366 break;
5367 }
5368 default:
5369 LOG(FATAL) << "Unexpected operation type " << type;
5370 UNREACHABLE();
5371 }
5372}
5373
5374void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5375 HandleShift(shl);
5376}
5377
5378void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5379 HandleShift(shl);
5380}
5381
5382void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5383 HandleShift(shr);
5384}
5385
5386void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5387 HandleShift(shr);
5388}
5389
5390void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5391 HandleShift(ushr);
5392}
5393
5394void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5395 HandleShift(ushr);
5396}
5397
5398void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005399 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5400 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005401 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5402 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005403 locations->SetOut(LocationFrom(r0));
5404}
5405
5406void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005407 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5408 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andra Danciua0130e82020-07-23 12:34:56 +00005409 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005410}
5411
5412void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005413 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5414 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005415 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005416 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005417 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5418 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005419}
5420
5421void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005422 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5423 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005424 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005425 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005426 DCHECK(!codegen_->IsLeafMethod());
Andra Danciua0130e82020-07-23 12:34:56 +00005427 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 13);
Artem Serov02d37832016-10-25 15:25:33 +01005428}
5429
5430void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5431 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005432 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005433 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5434 if (location.IsStackSlot()) {
5435 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5436 } else if (location.IsDoubleStackSlot()) {
5437 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5438 }
5439 locations->SetOut(location);
5440}
5441
5442void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5443 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5444 // Nothing to do, the parameter is already at its location.
5445}
5446
5447void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5448 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005449 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005450 locations->SetOut(LocationFrom(kMethodRegister));
5451}
5452
5453void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5454 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5455 // Nothing to do, the method is already at its location.
5456}
5457
5458void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5459 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005460 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005461 locations->SetInAt(0, Location::RequiresRegister());
5462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5463}
5464
5465void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5466 LocationSummary* locations = not_->GetLocations();
5467 Location out = locations->Out();
5468 Location in = locations->InAt(0);
5469 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005470 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005471 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5472 break;
5473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005474 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005475 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5476 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5477 break;
5478
5479 default:
5480 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5481 }
5482}
5483
Scott Wakelingc34dba72016-10-03 10:14:44 +01005484void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5485 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005486 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005487 locations->SetInAt(0, Location::RequiresRegister());
5488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5489}
5490
5491void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5492 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5493}
5494
Artem Serov02d37832016-10-25 15:25:33 +01005495void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5496 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005497 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005498 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005499 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005500 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005503 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 case DataType::Type::kInt32:
5505 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005506 locations->SetInAt(0, Location::RequiresRegister());
5507 locations->SetInAt(1, Location::RequiresRegister());
5508 // Output overlaps because it is written before doing the low comparison.
5509 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5510 break;
5511 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005512 case DataType::Type::kFloat32:
5513 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005514 locations->SetInAt(0, Location::RequiresFpuRegister());
5515 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5516 locations->SetOut(Location::RequiresRegister());
5517 break;
5518 }
5519 default:
5520 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5521 }
5522}
5523
5524void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5525 LocationSummary* locations = compare->GetLocations();
5526 vixl32::Register out = OutputRegister(compare);
5527 Location left = locations->InAt(0);
5528 Location right = locations->InAt(1);
5529
5530 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005531 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005533 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005534 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005535 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005536 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005537 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005539 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005541 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5542 __ Mov(out, 0);
5543 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5544 less_cond = lt;
5545 break;
5546 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005548 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Andreas Gampe3db70682018-12-26 15:12:03 -08005549 __ B(lt, &less, /* is_far_target= */ false);
5550 __ B(gt, &greater, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005551 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5552 __ Mov(out, 0);
5553 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5554 less_cond = lo;
5555 break;
5556 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005557 case DataType::Type::kFloat32:
5558 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005559 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005560 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005561 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5562 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5563 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5564 break;
5565 }
5566 default:
5567 LOG(FATAL) << "Unexpected compare type " << type;
5568 UNREACHABLE();
5569 }
5570
Andreas Gampe3db70682018-12-26 15:12:03 -08005571 __ B(eq, final_label, /* is_far_target= */ false);
5572 __ B(less_cond, &less, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005573
5574 __ Bind(&greater);
5575 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005576 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005577
5578 __ Bind(&less);
5579 __ Mov(out, -1);
5580
Anton Kirilov6f644202017-02-27 18:29:45 +00005581 if (done.IsReferenced()) {
5582 __ Bind(&done);
5583 }
Artem Serov02d37832016-10-25 15:25:33 +01005584}
5585
5586void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5587 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005588 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005589 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5590 locations->SetInAt(i, Location::Any());
5591 }
5592 locations->SetOut(Location::Any());
5593}
5594
5595void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5596 LOG(FATAL) << "Unreachable";
5597}
5598
5599void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5600 // TODO (ported from quick): revisit ARM barrier kinds.
5601 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5602 switch (kind) {
5603 case MemBarrierKind::kAnyStore:
5604 case MemBarrierKind::kLoadAny:
5605 case MemBarrierKind::kAnyAny: {
5606 flavor = DmbOptions::ISH;
5607 break;
5608 }
5609 case MemBarrierKind::kStoreStore: {
5610 flavor = DmbOptions::ISHST;
5611 break;
5612 }
5613 default:
5614 LOG(FATAL) << "Unexpected memory barrier " << kind;
5615 }
5616 __ Dmb(flavor);
5617}
5618
5619void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5620 uint32_t offset,
5621 vixl32::Register out_lo,
5622 vixl32::Register out_hi) {
5623 UseScratchRegisterScope temps(GetVIXLAssembler());
5624 if (offset != 0) {
5625 vixl32::Register temp = temps.Acquire();
5626 __ Add(temp, addr, offset);
5627 addr = temp;
5628 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005629 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005630}
5631
5632void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5633 uint32_t offset,
5634 vixl32::Register value_lo,
5635 vixl32::Register value_hi,
5636 vixl32::Register temp1,
5637 vixl32::Register temp2,
5638 HInstruction* instruction) {
5639 UseScratchRegisterScope temps(GetVIXLAssembler());
5640 vixl32::Label fail;
5641 if (offset != 0) {
5642 vixl32::Register temp = temps.Acquire();
5643 __ Add(temp, addr, offset);
5644 addr = temp;
5645 }
5646 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005647 {
5648 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005649 ExactAssemblyScope aas(GetVIXLAssembler(),
5650 vixl32::kMaxInstructionSizeInBytes,
5651 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005652 // We need a load followed by store. (The address used in a STREX instruction must
5653 // be the same as the address in the most recently executed LDREX instruction.)
5654 __ ldrexd(temp1, temp2, MemOperand(addr));
5655 codegen_->MaybeRecordImplicitNullCheck(instruction);
5656 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005657 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005658 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005659}
Artem Serov02109dd2016-09-23 17:17:54 +01005660
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005661void LocationsBuilderARMVIXL::HandleFieldSet(
5662 HInstruction* instruction, const FieldInfo& field_info) {
5663 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5664
5665 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005666 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005667 locations->SetInAt(0, Location::RequiresRegister());
5668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005669 DataType::Type field_type = field_info.GetFieldType();
5670 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005671 locations->SetInAt(1, Location::RequiresFpuRegister());
5672 } else {
5673 locations->SetInAt(1, Location::RequiresRegister());
5674 }
5675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005676 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005677 bool generate_volatile = field_info.IsVolatile()
5678 && is_wide
5679 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5680 bool needs_write_barrier =
5681 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5682 // Temporary registers for the write barrier.
5683 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5684 if (needs_write_barrier) {
5685 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5686 locations->AddTemp(Location::RequiresRegister());
5687 } else if (generate_volatile) {
5688 // ARM encoding have some additional constraints for ldrexd/strexd:
5689 // - registers need to be consecutive
5690 // - the first register should be even but not R14.
5691 // We don't test for ARM yet, and the assertion makes sure that we
5692 // revisit this if we ever enable ARM encoding.
5693 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5694
5695 locations->AddTemp(Location::RequiresRegister());
5696 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005697 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005698 // For doubles we need two more registers to copy the value.
5699 locations->AddTemp(LocationFrom(r2));
5700 locations->AddTemp(LocationFrom(r3));
5701 }
5702 }
5703}
5704
5705void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5706 const FieldInfo& field_info,
5707 bool value_can_be_null) {
5708 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5709
5710 LocationSummary* locations = instruction->GetLocations();
5711 vixl32::Register base = InputRegisterAt(instruction, 0);
5712 Location value = locations->InAt(1);
5713
5714 bool is_volatile = field_info.IsVolatile();
5715 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005716 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005717 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5718 bool needs_write_barrier =
5719 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5720
5721 if (is_volatile) {
5722 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5723 }
5724
5725 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005726 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005727 case DataType::Type::kUint8:
5728 case DataType::Type::kInt8:
5729 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005730 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005731 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005732 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5733 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005734 StoreOperandType operand_type = GetStoreOperandType(field_type);
5735 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005736 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005737 break;
5738 }
5739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005740 case DataType::Type::kReference: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005741 vixl32::Register value_reg = RegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005742 if (kPoisonHeapReferences && needs_write_barrier) {
5743 // Note that in the case where `value` is a null reference,
5744 // we do not enter this block, as a null reference does not
5745 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005746 DCHECK_EQ(field_type, DataType::Type::kReference);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005747 value_reg = RegisterFrom(locations->GetTemp(0));
5748 __ Mov(value_reg, RegisterFrom(value));
5749 GetAssembler()->PoisonHeapReference(value_reg);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005750 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005751 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5752 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5753 GetAssembler()->StoreToOffset(kStoreWord, value_reg, base, offset);
5754 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005755 break;
5756 }
5757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005758 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005759 if (is_volatile && !atomic_ldrd_strd) {
5760 GenerateWideAtomicStore(base,
5761 offset,
5762 LowRegisterFrom(value),
5763 HighRegisterFrom(value),
5764 RegisterFrom(locations->GetTemp(0)),
5765 RegisterFrom(locations->GetTemp(1)),
5766 instruction);
5767 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005768 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5769 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005770 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5771 codegen_->MaybeRecordImplicitNullCheck(instruction);
5772 }
5773 break;
5774 }
5775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005777 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5778 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005779 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005780 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005781 break;
5782 }
5783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005785 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005786 if (is_volatile && !atomic_ldrd_strd) {
5787 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5788 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5789
5790 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5791
5792 GenerateWideAtomicStore(base,
5793 offset,
5794 value_reg_lo,
5795 value_reg_hi,
5796 RegisterFrom(locations->GetTemp(2)),
5797 RegisterFrom(locations->GetTemp(3)),
5798 instruction);
5799 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005800 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5801 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005802 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5803 codegen_->MaybeRecordImplicitNullCheck(instruction);
5804 }
5805 break;
5806 }
5807
Aart Bik66c158e2018-01-31 12:55:04 -08005808 case DataType::Type::kUint32:
5809 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005811 LOG(FATAL) << "Unreachable type " << field_type;
5812 UNREACHABLE();
5813 }
5814
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005815 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5816 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5817 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5818 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5819 }
5820
5821 if (is_volatile) {
5822 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5823 }
5824}
5825
Artem Serov02d37832016-10-25 15:25:33 +01005826void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5827 const FieldInfo& field_info) {
5828 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5829
5830 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005831 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005832 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005833 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5834 object_field_get_with_read_barrier
5835 ? LocationSummary::kCallOnSlowPath
5836 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005837 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5838 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5839 }
5840 locations->SetInAt(0, Location::RequiresRegister());
5841
5842 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005843 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005844 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5845 // The output overlaps in case of volatile long: we don't want the
5846 // code generated by GenerateWideAtomicLoad to overwrite the
5847 // object's location. Likewise, in the case of an object field get
5848 // with read barriers enabled, we do not want the load to overwrite
5849 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 bool overlap =
5851 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005852 object_field_get_with_read_barrier;
5853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005854 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005855 locations->SetOut(Location::RequiresFpuRegister());
5856 } else {
5857 locations->SetOut(Location::RequiresRegister(),
5858 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5859 }
5860 if (volatile_for_double) {
5861 // ARM encoding have some additional constraints for ldrexd/strexd:
5862 // - registers need to be consecutive
5863 // - the first register should be even but not R14.
5864 // We don't test for ARM yet, and the assertion makes sure that we
5865 // revisit this if we ever enable ARM encoding.
5866 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5867 locations->AddTemp(Location::RequiresRegister());
5868 locations->AddTemp(Location::RequiresRegister());
5869 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005870 // We need a temporary register for the read barrier load in
5871 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5872 // only if the offset is too big.
5873 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005874 locations->AddTemp(Location::RequiresRegister());
5875 }
Artem Serov02d37832016-10-25 15:25:33 +01005876 }
5877}
5878
5879Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005881 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5882 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5883 return Location::ConstantLocation(input->AsConstant());
5884 } else {
5885 return Location::RequiresFpuRegister();
5886 }
5887}
5888
Artem Serov02109dd2016-09-23 17:17:54 +01005889Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5890 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005892 if (constant->IsConstant() &&
5893 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5894 return Location::ConstantLocation(constant->AsConstant());
5895 }
5896 return Location::RequiresRegister();
5897}
5898
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005899static bool CanEncode32BitConstantAsImmediate(
5900 CodeGeneratorARMVIXL* codegen,
5901 uint32_t value,
5902 Opcode opcode,
5903 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5904 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5905 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005906 return true;
5907 }
5908 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005909 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005910 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005911 case AND: neg_opcode = BIC; neg_value = ~value; break;
5912 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5913 case ADD: neg_opcode = SUB; neg_value = -value; break;
5914 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5915 case SUB: neg_opcode = ADD; neg_value = -value; break;
5916 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5917 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005918 default:
5919 return false;
5920 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005921
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005922 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005923 return true;
5924 }
5925
5926 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005927}
5928
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005929bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5930 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5931 if (DataType::Is64BitType(input_cst->GetType())) {
5932 Opcode high_opcode = opcode;
5933 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5934 switch (opcode) {
5935 case SUB:
5936 // Flip the operation to an ADD.
5937 value = -value;
5938 opcode = ADD;
5939 FALLTHROUGH_INTENDED;
5940 case ADD:
5941 if (Low32Bits(value) == 0u) {
5942 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5943 }
5944 high_opcode = ADC;
5945 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5946 break;
5947 default:
5948 break;
5949 }
5950 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5951 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5952 } else {
5953 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5954 }
5955}
5956
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005957void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5958 const FieldInfo& field_info) {
5959 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5960
5961 LocationSummary* locations = instruction->GetLocations();
5962 vixl32::Register base = InputRegisterAt(instruction, 0);
5963 Location out = locations->Out();
5964 bool is_volatile = field_info.IsVolatile();
5965 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005966 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5967 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005968 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5969
Vladimir Marko61b92282017-10-11 13:23:17 +01005970 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005972 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005973 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005975 case DataType::Type::kInt16:
5976 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005977 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5978 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01005979 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005980 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005981 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005982 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005983 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005984
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005986 // /* HeapReference<Object> */ out = *(base + offset)
5987 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01005988 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005989 // Note that a potential implicit null check is handled in this
5990 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5991 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005992 instruction, out, base, offset, maybe_temp, /* needs_null_check= */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005993 if (is_volatile) {
5994 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5995 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005996 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005997 {
5998 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5999 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6000 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
6001 codegen_->MaybeRecordImplicitNullCheck(instruction);
6002 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006003 if (is_volatile) {
6004 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6005 }
6006 // If read barriers are enabled, emit read barriers other than
6007 // Baker's using a slow path (and also unpoison the loaded
6008 // reference, if heap poisoning is enabled).
6009 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
6010 }
6011 break;
6012 }
6013
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006014 case DataType::Type::kInt64: {
6015 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6016 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006017 if (is_volatile && !atomic_ldrd_strd) {
6018 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6019 } else {
6020 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6021 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006022 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006023 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006024 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006025
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006026 case DataType::Type::kFloat32: {
6027 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6028 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006029 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006030 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006031 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006032 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006035 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6036 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006037 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006038 if (is_volatile && !atomic_ldrd_strd) {
6039 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6040 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6041 GenerateWideAtomicLoad(base, offset, lo, hi);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006042 codegen_->MaybeRecordImplicitNullCheck(instruction);
6043 __ Vmov(out_dreg, lo, hi);
6044 } else {
6045 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006046 codegen_->MaybeRecordImplicitNullCheck(instruction);
6047 }
6048 break;
6049 }
6050
Aart Bik66c158e2018-01-31 12:55:04 -08006051 case DataType::Type::kUint32:
6052 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006053 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006054 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006055 UNREACHABLE();
6056 }
6057
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006058 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006059 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006060 // Memory barriers, in the case of references, are also handled
6061 // in the previous switch statement.
6062 } else {
6063 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6064 }
6065 }
6066}
6067
6068void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6069 HandleFieldSet(instruction, instruction->GetFieldInfo());
6070}
6071
6072void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6073 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6074}
6075
6076void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6077 HandleFieldGet(instruction, instruction->GetFieldInfo());
6078}
6079
6080void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6081 HandleFieldGet(instruction, instruction->GetFieldInfo());
6082}
6083
6084void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6085 HandleFieldGet(instruction, instruction->GetFieldInfo());
6086}
6087
6088void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6089 HandleFieldGet(instruction, instruction->GetFieldInfo());
6090}
6091
Scott Wakelingc34dba72016-10-03 10:14:44 +01006092void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6093 HandleFieldSet(instruction, instruction->GetFieldInfo());
6094}
6095
6096void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6097 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6098}
6099
Vladimir Marko552a1342017-10-31 10:56:47 +00006100void LocationsBuilderARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6101 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(r0));
6102}
6103
6104void InstructionCodeGeneratorARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6105 __ Mov(r0, instruction->GetFormat()->GetValue());
6106 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6107}
6108
Artem Serovcfbe9132016-10-14 15:58:56 +01006109void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6110 HUnresolvedInstanceFieldGet* instruction) {
6111 FieldAccessCallingConventionARMVIXL calling_convention;
6112 codegen_->CreateUnresolvedFieldLocationSummary(
6113 instruction, instruction->GetFieldType(), calling_convention);
6114}
6115
6116void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6117 HUnresolvedInstanceFieldGet* instruction) {
6118 FieldAccessCallingConventionARMVIXL calling_convention;
6119 codegen_->GenerateUnresolvedFieldAccess(instruction,
6120 instruction->GetFieldType(),
6121 instruction->GetFieldIndex(),
6122 instruction->GetDexPc(),
6123 calling_convention);
6124}
6125
6126void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6127 HUnresolvedInstanceFieldSet* instruction) {
6128 FieldAccessCallingConventionARMVIXL calling_convention;
6129 codegen_->CreateUnresolvedFieldLocationSummary(
6130 instruction, instruction->GetFieldType(), calling_convention);
6131}
6132
6133void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6134 HUnresolvedInstanceFieldSet* instruction) {
6135 FieldAccessCallingConventionARMVIXL calling_convention;
6136 codegen_->GenerateUnresolvedFieldAccess(instruction,
6137 instruction->GetFieldType(),
6138 instruction->GetFieldIndex(),
6139 instruction->GetDexPc(),
6140 calling_convention);
6141}
6142
6143void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6144 HUnresolvedStaticFieldGet* instruction) {
6145 FieldAccessCallingConventionARMVIXL calling_convention;
6146 codegen_->CreateUnresolvedFieldLocationSummary(
6147 instruction, instruction->GetFieldType(), calling_convention);
6148}
6149
6150void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6151 HUnresolvedStaticFieldGet* instruction) {
6152 FieldAccessCallingConventionARMVIXL calling_convention;
6153 codegen_->GenerateUnresolvedFieldAccess(instruction,
6154 instruction->GetFieldType(),
6155 instruction->GetFieldIndex(),
6156 instruction->GetDexPc(),
6157 calling_convention);
6158}
6159
6160void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6161 HUnresolvedStaticFieldSet* instruction) {
6162 FieldAccessCallingConventionARMVIXL calling_convention;
6163 codegen_->CreateUnresolvedFieldLocationSummary(
6164 instruction, instruction->GetFieldType(), calling_convention);
6165}
6166
6167void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6168 HUnresolvedStaticFieldSet* instruction) {
6169 FieldAccessCallingConventionARMVIXL calling_convention;
6170 codegen_->GenerateUnresolvedFieldAccess(instruction,
6171 instruction->GetFieldType(),
6172 instruction->GetFieldIndex(),
6173 instruction->GetDexPc(),
6174 calling_convention);
6175}
6176
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006177void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006178 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006179 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006180}
6181
6182void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6183 if (CanMoveNullCheckToUser(instruction)) {
6184 return;
6185 }
6186
6187 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006188 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006189 ExactAssemblyScope aas(GetVIXLAssembler(),
6190 vixl32::kMaxInstructionSizeInBytes,
6191 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006192 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6193 RecordPcInfo(instruction, instruction->GetDexPc());
6194}
6195
6196void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6197 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006198 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006199 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006200 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006201}
6202
6203void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6204 codegen_->GenerateNullCheck(instruction);
6205}
6206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006207void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006208 Location out_loc,
6209 vixl32::Register base,
6210 vixl32::Register reg_index,
6211 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006212 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006213 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6214
6215 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006216 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006217 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006218 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6219 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006221 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6222 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006224 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6225 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006226 case DataType::Type::kInt16:
6227 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6228 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006229 case DataType::Type::kReference:
6230 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006231 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6232 break;
6233 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006234 case DataType::Type::kInt64:
6235 case DataType::Type::kFloat32:
6236 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006237 default:
6238 LOG(FATAL) << "Unreachable type " << type;
6239 UNREACHABLE();
6240 }
6241}
6242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006243void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006244 Location loc,
6245 vixl32::Register base,
6246 vixl32::Register reg_index,
6247 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006248 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006249 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6250
6251 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006252 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006253 case DataType::Type::kUint8:
6254 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006255 __ Strb(cond, RegisterFrom(loc), mem_address);
6256 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006257 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006258 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006259 __ Strh(cond, RegisterFrom(loc), mem_address);
6260 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006261 case DataType::Type::kReference:
6262 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006263 __ Str(cond, RegisterFrom(loc), mem_address);
6264 break;
6265 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006266 case DataType::Type::kInt64:
6267 case DataType::Type::kFloat32:
6268 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006269 default:
6270 LOG(FATAL) << "Unreachable type " << type;
6271 UNREACHABLE();
6272 }
6273}
6274
6275void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6276 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006277 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006278 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006279 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6280 object_array_get_with_read_barrier
6281 ? LocationSummary::kCallOnSlowPath
6282 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006283 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006284 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006285 }
6286 locations->SetInAt(0, Location::RequiresRegister());
6287 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006289 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6290 } else {
6291 // The output overlaps in the case of an object array get with
6292 // read barriers enabled: we do not want the move to overwrite the
6293 // array's location, as we need it to emit the read barrier.
6294 locations->SetOut(
6295 Location::RequiresRegister(),
6296 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6297 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006298 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006299 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006300 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01006301 // We need a temporary register for the read barrier load in
6302 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
6303 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006304 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6305 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006307 if (offset >= kReferenceLoadMinFarOffset) {
6308 locations->AddTemp(Location::RequiresRegister());
6309 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006310 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006311 // We need a non-scratch temporary for the array data pointer in
6312 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006313 locations->AddTemp(Location::RequiresRegister());
6314 }
6315 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6316 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006317 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006318 }
6319}
6320
6321void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006322 LocationSummary* locations = instruction->GetLocations();
6323 Location obj_loc = locations->InAt(0);
6324 vixl32::Register obj = InputRegisterAt(instruction, 0);
6325 Location index = locations->InAt(1);
6326 Location out_loc = locations->Out();
6327 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006329 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6330 instruction->IsStringCharAt();
6331 HInstruction* array_instr = instruction->GetArray();
6332 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006333
6334 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006336 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006339 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006341 vixl32::Register length;
6342 if (maybe_compressed_char_at) {
6343 length = RegisterFrom(locations->GetTemp(0));
6344 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006345 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6346 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006347 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6348 codegen_->MaybeRecordImplicitNullCheck(instruction);
6349 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006350 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006351 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006352 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006353 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006354 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006355 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6356 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6357 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006358 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006359 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6360 RegisterFrom(out_loc),
6361 obj,
6362 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006363 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006364 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006365 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006366 RegisterFrom(out_loc),
6367 obj,
6368 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006369 if (done.IsReferenced()) {
6370 __ Bind(&done);
6371 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006372 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006374
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006375 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6376 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006377 LoadOperandType load_type = GetLoadOperandType(type);
6378 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006379 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006380 }
6381 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006382 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006383 vixl32::Register temp = temps.Acquire();
6384
6385 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006386 // We do not need to compute the intermediate address from the array: the
6387 // input instruction has done it already. See the comment in
6388 // `TryExtractArrayAccessAddress()`.
6389 if (kIsDebugBuild) {
6390 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006391 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006392 }
6393 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006394 } else {
6395 __ Add(temp, obj, data_offset);
6396 }
6397 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006398 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006399 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006400 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6401 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6402 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006403 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006404 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006405 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006406 __ Bind(&uncompressed_load);
6407 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006408 if (done.IsReferenced()) {
6409 __ Bind(&done);
6410 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006411 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006412 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6413 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006414 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006415 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006416 }
6417 }
6418 break;
6419 }
6420
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006421 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006422 // The read barrier instrumentation of object ArrayGet
6423 // instructions does not support the HIntermediateAddress
6424 // instruction.
6425 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6426
Scott Wakelingc34dba72016-10-03 10:14:44 +01006427 static_assert(
6428 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6429 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6430 // /* HeapReference<Object> */ out =
6431 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6432 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006433 // Note that a potential implicit null check is handled in this
6434 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006435 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6436 if (index.IsConstant()) {
6437 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006438 Location maybe_temp =
6439 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006440 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006441 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6442 out_loc,
6443 obj,
6444 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006445 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006446 /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006447 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006448 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006449 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006450 out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006451 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006452 } else {
6453 vixl32::Register out = OutputRegister(instruction);
6454 if (index.IsConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006455 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6456 {
6457 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6458 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6459 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
6460 codegen_->MaybeRecordImplicitNullCheck(instruction);
6461 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006462 // If read barriers are enabled, emit read barriers other than
6463 // Baker's using a slow path (and also unpoison the loaded
6464 // reference, if heap poisoning is enabled).
6465 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6466 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006467 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006468 vixl32::Register temp = temps.Acquire();
6469
6470 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006471 // We do not need to compute the intermediate address from the array: the
6472 // input instruction has done it already. See the comment in
6473 // `TryExtractArrayAccessAddress()`.
6474 if (kIsDebugBuild) {
6475 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006476 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006477 }
6478 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006479 } else {
6480 __ Add(temp, obj, data_offset);
6481 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006482 {
6483 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6484 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6485 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6486 temps.Close();
6487 codegen_->MaybeRecordImplicitNullCheck(instruction);
6488 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006489 // If read barriers are enabled, emit read barriers other than
6490 // Baker's using a slow path (and also unpoison the loaded
6491 // reference, if heap poisoning is enabled).
6492 codegen_->MaybeGenerateReadBarrierSlow(
6493 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6494 }
6495 }
6496 break;
6497 }
6498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006500 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6501 // As two macro instructions can be emitted the max size is doubled.
6502 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006503 if (index.IsConstant()) {
6504 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006505 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006506 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6507 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006508 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006509 vixl32::Register temp = temps.Acquire();
6510 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6511 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6512 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006513 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006514 break;
6515 }
6516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006517 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006518 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6519 // As two macro instructions can be emitted the max size is doubled.
6520 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006521 vixl32::SRegister out = SRegisterFrom(out_loc);
6522 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006523 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006524 GetAssembler()->LoadSFromOffset(out, obj, offset);
6525 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006526 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006527 vixl32::Register temp = temps.Acquire();
6528 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6529 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6530 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006531 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006532 break;
6533 }
6534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006535 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006536 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6537 // As two macro instructions can be emitted the max size is doubled.
6538 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006539 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006540 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006541 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6542 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006543 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006544 vixl32::Register temp = temps.Acquire();
6545 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6546 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6547 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006548 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006549 break;
6550 }
6551
Aart Bik66c158e2018-01-31 12:55:04 -08006552 case DataType::Type::kUint32:
6553 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006554 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006555 LOG(FATAL) << "Unreachable type " << type;
6556 UNREACHABLE();
6557 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006558}
6559
6560void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006561 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006562
6563 bool needs_write_barrier =
6564 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006565 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006566
Vladimir Markoca6fff82017-10-03 14:49:14 +01006567 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006568 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006569 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006570
6571 locations->SetInAt(0, Location::RequiresRegister());
6572 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006573 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006574 locations->SetInAt(2, Location::RequiresFpuRegister());
6575 } else {
6576 locations->SetInAt(2, Location::RequiresRegister());
6577 }
6578 if (needs_write_barrier) {
6579 // Temporary registers for the write barrier.
6580 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6581 locations->AddTemp(Location::RequiresRegister());
6582 }
6583}
6584
6585void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006586 LocationSummary* locations = instruction->GetLocations();
6587 vixl32::Register array = InputRegisterAt(instruction, 0);
6588 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006589 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006590 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006591 bool needs_write_barrier =
6592 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6593 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006594 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006595 Location value_loc = locations->InAt(2);
6596 HInstruction* array_instr = instruction->GetArray();
6597 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006598
6599 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006600 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006601 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006602 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006603 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006604 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006605 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006606 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006607 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006608 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006609 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006610 StoreOperandType store_type = GetStoreOperandType(value_type);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006611 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6612 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006613 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006614 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006615 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006616 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006617 vixl32::Register temp = temps.Acquire();
6618
6619 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006620 // We do not need to compute the intermediate address from the array: the
6621 // input instruction has done it already. See the comment in
6622 // `TryExtractArrayAccessAddress()`.
6623 if (kIsDebugBuild) {
6624 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006625 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006626 }
6627 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006628 } else {
6629 __ Add(temp, array, data_offset);
6630 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006631 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6632 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006633 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006634 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006635 }
6636 break;
6637 }
6638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006639 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006640 vixl32::Register value = RegisterFrom(value_loc);
6641 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6642 // See the comment in instruction_simplifier_shared.cc.
6643 DCHECK(!has_intermediate_address);
6644
6645 if (instruction->InputAt(2)->IsNullConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006646 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6647 // As two macro instructions can be emitted the max size is doubled.
6648 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006649 // Just setting null.
6650 if (index.IsConstant()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006651 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006652 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6653 } else {
6654 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006655 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006656 vixl32::Register temp = temps.Acquire();
6657 __ Add(temp, array, data_offset);
6658 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6659 }
6660 codegen_->MaybeRecordImplicitNullCheck(instruction);
6661 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006662 DCHECK(!needs_type_check);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006663 break;
6664 }
6665
6666 DCHECK(needs_write_barrier);
6667 Location temp1_loc = locations->GetTemp(0);
6668 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6669 Location temp2_loc = locations->GetTemp(1);
6670 vixl32::Register temp2 = RegisterFrom(temp2_loc);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006671
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006672 bool can_value_be_null = instruction->GetValueCanBeNull();
6673 vixl32::Label do_store;
6674 if (can_value_be_null) {
6675 __ CompareAndBranchIfZero(value, &do_store, /* is_far_target= */ false);
6676 }
6677
6678 SlowPathCodeARMVIXL* slow_path = nullptr;
6679 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006680 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006681 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006682
6683 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6684 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6685 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006686
6687 // Note that when read barriers are enabled, the type checks
6688 // are performed without read barriers. This is fine, even in
6689 // the case where a class object is in the from-space after
6690 // the flip, as a comparison involving such a type would not
6691 // produce a false positive; it may of course produce a false
6692 // negative, in which case we would take the ArraySet slow
6693 // path.
6694
Alexandre Rames374ddf32016-11-04 10:40:49 +00006695 {
6696 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006697 ExactAssemblyScope aas(GetVIXLAssembler(),
6698 vixl32::kMaxInstructionSizeInBytes,
6699 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006700 // /* HeapReference<Class> */ temp1 = array->klass_
6701 __ ldr(temp1, MemOperand(array, class_offset));
6702 codegen_->MaybeRecordImplicitNullCheck(instruction);
6703 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006704 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6705
6706 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6707 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6708 // /* HeapReference<Class> */ temp2 = value->klass_
6709 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6710 // If heap poisoning is enabled, no need to unpoison `temp1`
6711 // nor `temp2`, as we are comparing two poisoned references.
6712 __ Cmp(temp1, temp2);
6713
6714 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006715 vixl32::Label do_put;
6716 __ B(eq, &do_put, /* is_far_target= */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006717 // If heap poisoning is enabled, the `temp1` reference has
6718 // not been unpoisoned yet; unpoison it now.
6719 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6720
6721 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6722 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6723 // If heap poisoning is enabled, no need to unpoison
6724 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006725 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006726 __ Bind(&do_put);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006727 } else {
6728 __ B(ne, slow_path->GetEntryLabel());
6729 }
6730 }
6731
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006732 codegen_->MarkGCCard(temp1, temp2, array, value, /* can_be_null= */ false);
6733
6734 if (can_value_be_null) {
6735 DCHECK(do_store.IsReferenced());
6736 __ Bind(&do_store);
6737 }
6738
Scott Wakelingc34dba72016-10-03 10:14:44 +01006739 vixl32::Register source = value;
6740 if (kPoisonHeapReferences) {
6741 // Note that in the case where `value` is a null reference,
6742 // we do not enter this block, as a null reference does not
6743 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006744 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006745 __ Mov(temp1, value);
6746 GetAssembler()->PoisonHeapReference(temp1);
6747 source = temp1;
6748 }
6749
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006750 {
6751 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6752 // As two macro instructions can be emitted the max size is doubled.
6753 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
6754 if (index.IsConstant()) {
6755 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6756 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6757 } else {
6758 DCHECK(index.IsRegister()) << index;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006759
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006760 UseScratchRegisterScope temps(GetVIXLAssembler());
6761 vixl32::Register temp = temps.Acquire();
6762 __ Add(temp, array, data_offset);
6763 codegen_->StoreToShiftedRegOffset(value_type,
6764 LocationFrom(source),
6765 temp,
6766 RegisterFrom(index));
6767 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006768
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006769 if (can_value_be_null || !needs_type_check) {
6770 codegen_->MaybeRecordImplicitNullCheck(instruction);
6771 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006772 }
6773
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006774 if (slow_path != nullptr) {
6775 __ Bind(slow_path->GetExitLabel());
6776 }
6777
Scott Wakelingc34dba72016-10-03 10:14:44 +01006778 break;
6779 }
6780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006782 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6783 // As two macro instructions can be emitted the max size is doubled.
6784 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006785 Location value = locations->InAt(2);
6786 if (index.IsConstant()) {
6787 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006788 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006789 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6790 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006791 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006792 vixl32::Register temp = temps.Acquire();
6793 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6794 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6795 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006796 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006797 break;
6798 }
6799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006800 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006801 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6802 // As two macro instructions can be emitted the max size is doubled.
6803 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006804 Location value = locations->InAt(2);
6805 DCHECK(value.IsFpuRegister());
6806 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006807 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006808 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6809 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006810 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006811 vixl32::Register temp = temps.Acquire();
6812 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6813 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6814 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006815 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006816 break;
6817 }
6818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006819 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006820 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6821 // As two macro instructions can be emitted the max size is doubled.
6822 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006823 Location value = locations->InAt(2);
6824 DCHECK(value.IsFpuRegisterPair());
6825 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006826 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006827 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6828 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006829 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006830 vixl32::Register temp = temps.Acquire();
6831 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6832 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6833 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006835 break;
6836 }
6837
Aart Bik66c158e2018-01-31 12:55:04 -08006838 case DataType::Type::kUint32:
6839 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006840 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006841 LOG(FATAL) << "Unreachable type " << value_type;
6842 UNREACHABLE();
6843 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006844}
6845
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006846void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6847 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006848 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006849 locations->SetInAt(0, Location::RequiresRegister());
6850 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6851}
6852
6853void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6854 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6855 vixl32::Register obj = InputRegisterAt(instruction, 0);
6856 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006857 {
Artem Serov0fb37192016-12-06 18:13:40 +00006858 ExactAssemblyScope aas(GetVIXLAssembler(),
6859 vixl32::kMaxInstructionSizeInBytes,
6860 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006861 __ ldr(out, MemOperand(obj, offset));
6862 codegen_->MaybeRecordImplicitNullCheck(instruction);
6863 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006864 // Mask out compression flag from String's array length.
6865 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006866 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006867 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006868}
6869
Artem Serov2bbc9532016-10-21 11:51:50 +01006870void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006871 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006872 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006873
6874 locations->SetInAt(0, Location::RequiresRegister());
6875 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6876 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6877}
6878
6879void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6880 vixl32::Register out = OutputRegister(instruction);
6881 vixl32::Register first = InputRegisterAt(instruction, 0);
6882 Location second = instruction->GetLocations()->InAt(1);
6883
Artem Serov2bbc9532016-10-21 11:51:50 +01006884 if (second.IsRegister()) {
6885 __ Add(out, first, RegisterFrom(second));
6886 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006887 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006888 }
6889}
6890
Artem Serove1811ed2017-04-27 16:50:47 +01006891void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6892 HIntermediateAddressIndex* instruction) {
6893 LOG(FATAL) << "Unreachable " << instruction->GetId();
6894}
6895
6896void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6897 HIntermediateAddressIndex* instruction) {
6898 LOG(FATAL) << "Unreachable " << instruction->GetId();
6899}
6900
Scott Wakelingc34dba72016-10-03 10:14:44 +01006901void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6902 RegisterSet caller_saves = RegisterSet::Empty();
6903 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6904 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6905 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6906 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006907
6908 HInstruction* index = instruction->InputAt(0);
6909 HInstruction* length = instruction->InputAt(1);
6910 // If both index and length are constants we can statically check the bounds. But if at least one
6911 // of them is not encodable ArmEncodableConstantOrRegister will create
6912 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6913 // locations.
6914 bool both_const = index->IsConstant() && length->IsConstant();
6915 locations->SetInAt(0, both_const
6916 ? Location::ConstantLocation(index->AsConstant())
6917 : ArmEncodableConstantOrRegister(index, CMP));
6918 locations->SetInAt(1, both_const
6919 ? Location::ConstantLocation(length->AsConstant())
6920 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006921}
6922
6923void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006924 LocationSummary* locations = instruction->GetLocations();
6925 Location index_loc = locations->InAt(0);
6926 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006927
Artem Serov2dd053d2017-03-08 14:54:06 +00006928 if (length_loc.IsConstant()) {
6929 int32_t length = Int32ConstantFrom(length_loc);
6930 if (index_loc.IsConstant()) {
6931 // BCE will remove the bounds check if we are guaranteed to pass.
6932 int32_t index = Int32ConstantFrom(index_loc);
6933 if (index < 0 || index >= length) {
6934 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006935 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006936 codegen_->AddSlowPath(slow_path);
6937 __ B(slow_path->GetEntryLabel());
6938 } else {
6939 // Some optimization after BCE may have generated this, and we should not
6940 // generate a bounds check if it is a valid range.
6941 }
6942 return;
6943 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006944
Artem Serov2dd053d2017-03-08 14:54:06 +00006945 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006946 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006947 __ Cmp(RegisterFrom(index_loc), length);
6948 codegen_->AddSlowPath(slow_path);
6949 __ B(hs, slow_path->GetEntryLabel());
6950 } else {
6951 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006952 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006953 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6954 codegen_->AddSlowPath(slow_path);
6955 __ B(ls, slow_path->GetEntryLabel());
6956 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006957}
6958
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006959void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6960 vixl32::Register card,
6961 vixl32::Register object,
6962 vixl32::Register value,
6963 bool can_be_null) {
6964 vixl32::Label is_null;
6965 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006966 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006967 }
Roland Levillainc73f0522018-08-14 15:16:50 +01006968 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006969 GetAssembler()->LoadFromOffset(
6970 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01006971 // Calculate the offset (in the card table) of the card corresponding to
6972 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00006973 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01006974 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
6975 // `object`'s card.
6976 //
6977 // Register `card` contains the address of the card table. Note that the card
6978 // table's base is biased during its creation so that it always starts at an
6979 // address whose least-significant byte is equal to `kCardDirty` (see
6980 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
6981 // below writes the `kCardDirty` (byte) value into the `object`'s card
6982 // (located at `card + object >> kCardShift`).
6983 //
6984 // This dual use of the value in register `card` (1. to calculate the location
6985 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
6986 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006987 __ Strb(card, MemOperand(card, temp));
6988 if (can_be_null) {
6989 __ Bind(&is_null);
6990 }
6991}
6992
Scott Wakelingfe885462016-09-22 10:24:38 +01006993void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6994 LOG(FATAL) << "Unreachable";
6995}
6996
6997void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006998 if (instruction->GetNext()->IsSuspendCheck() &&
6999 instruction->GetBlock()->GetLoopInformation() != nullptr) {
7000 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
7001 // The back edge will generate the suspend check.
7002 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
7003 }
7004
Scott Wakelingfe885462016-09-22 10:24:38 +01007005 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7006}
7007
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007008void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007009 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7010 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00007011 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007012}
7013
7014void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
7015 HBasicBlock* block = instruction->GetBlock();
7016 if (block->GetLoopInformation() != nullptr) {
7017 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7018 // The back edge will generate the suspend check.
7019 return;
7020 }
7021 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7022 // The goto will generate the suspend check.
7023 return;
7024 }
7025 GenerateSuspendCheck(instruction, nullptr);
Andra Danciua0130e82020-07-23 12:34:56 +00007026 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007027}
7028
7029void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7030 HBasicBlock* successor) {
7031 SuspendCheckSlowPathARMVIXL* slow_path =
7032 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7033 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007034 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007035 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007036 instruction->SetSlowPath(slow_path);
7037 codegen_->AddSlowPath(slow_path);
7038 if (successor != nullptr) {
7039 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007040 }
7041 } else {
7042 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7043 }
7044
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007045 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007046 vixl32::Register temp = temps.Acquire();
7047 GetAssembler()->LoadFromOffset(
7048 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7049 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007050 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007051 __ Bind(slow_path->GetReturnLabel());
7052 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007053 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007054 __ B(slow_path->GetEntryLabel());
7055 }
7056}
7057
Scott Wakelingfe885462016-09-22 10:24:38 +01007058ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7059 return codegen_->GetAssembler();
7060}
7061
7062void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007063 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007064 MoveOperands* move = moves_[index];
7065 Location source = move->GetSource();
7066 Location destination = move->GetDestination();
7067
7068 if (source.IsRegister()) {
7069 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007070 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007071 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007072 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007073 } else {
7074 DCHECK(destination.IsStackSlot());
7075 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007076 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007077 sp,
7078 destination.GetStackIndex());
7079 }
7080 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007081 if (destination.IsRegister()) {
7082 GetAssembler()->LoadFromOffset(kLoadWord,
7083 RegisterFrom(destination),
7084 sp,
7085 source.GetStackIndex());
7086 } else if (destination.IsFpuRegister()) {
7087 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7088 } else {
7089 DCHECK(destination.IsStackSlot());
7090 vixl32::Register temp = temps.Acquire();
7091 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7092 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7093 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007094 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007095 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007096 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007097 } else if (destination.IsFpuRegister()) {
7098 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7099 } else {
7100 DCHECK(destination.IsStackSlot());
7101 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7102 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007103 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007104 if (destination.IsDoubleStackSlot()) {
7105 vixl32::DRegister temp = temps.AcquireD();
7106 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7107 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7108 } else if (destination.IsRegisterPair()) {
7109 DCHECK(ExpectedPairLayout(destination));
7110 GetAssembler()->LoadFromOffset(
7111 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7112 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007113 DCHECK(destination.IsFpuRegisterPair()) << destination;
7114 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007115 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007116 } else if (source.IsRegisterPair()) {
7117 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007118 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7119 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007120 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007121 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007122 } else {
7123 DCHECK(destination.IsDoubleStackSlot()) << destination;
7124 DCHECK(ExpectedPairLayout(source));
7125 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007126 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007127 sp,
7128 destination.GetStackIndex());
7129 }
7130 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007131 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007132 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007133 } else if (destination.IsFpuRegisterPair()) {
7134 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7135 } else {
7136 DCHECK(destination.IsDoubleStackSlot()) << destination;
7137 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7138 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007139 } else {
7140 DCHECK(source.IsConstant()) << source;
7141 HConstant* constant = source.GetConstant();
7142 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7143 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7144 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007145 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007146 } else {
7147 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007148 vixl32::Register temp = temps.Acquire();
7149 __ Mov(temp, value);
7150 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7151 }
7152 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007153 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007154 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007155 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7156 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007157 } else {
7158 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007159 vixl32::Register temp = temps.Acquire();
7160 __ Mov(temp, Low32Bits(value));
7161 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7162 __ Mov(temp, High32Bits(value));
7163 GetAssembler()->StoreToOffset(kStoreWord,
7164 temp,
7165 sp,
7166 destination.GetHighStackIndex(kArmWordSize));
7167 }
7168 } else if (constant->IsDoubleConstant()) {
7169 double value = constant->AsDoubleConstant()->GetValue();
7170 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007171 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007172 } else {
7173 DCHECK(destination.IsDoubleStackSlot()) << destination;
7174 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007175 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007176 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007177 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007178 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007179 GetAssembler()->StoreToOffset(kStoreWord,
7180 temp,
7181 sp,
7182 destination.GetHighStackIndex(kArmWordSize));
7183 }
7184 } else {
7185 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7186 float value = constant->AsFloatConstant()->GetValue();
7187 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007188 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007189 } else {
7190 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007191 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007192 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007193 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7194 }
7195 }
7196 }
7197}
7198
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007199void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7200 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7201 vixl32::Register temp = temps.Acquire();
7202 __ Mov(temp, reg);
7203 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7204 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007205}
7206
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007207void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7208 // TODO(VIXL32): Double check the performance of this implementation.
7209 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007210 vixl32::Register temp1 = temps.Acquire();
7211 ScratchRegisterScope ensure_scratch(
7212 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7213 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007214
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007215 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7216 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7217 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7218 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7219 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007220}
7221
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007222void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7223 MoveOperands* move = moves_[index];
7224 Location source = move->GetSource();
7225 Location destination = move->GetDestination();
7226 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7227
7228 if (source.IsRegister() && destination.IsRegister()) {
7229 vixl32::Register temp = temps.Acquire();
7230 DCHECK(!RegisterFrom(source).Is(temp));
7231 DCHECK(!RegisterFrom(destination).Is(temp));
7232 __ Mov(temp, RegisterFrom(destination));
7233 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7234 __ Mov(RegisterFrom(source), temp);
7235 } else if (source.IsRegister() && destination.IsStackSlot()) {
7236 Exchange(RegisterFrom(source), destination.GetStackIndex());
7237 } else if (source.IsStackSlot() && destination.IsRegister()) {
7238 Exchange(RegisterFrom(destination), source.GetStackIndex());
7239 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007240 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007241 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007242 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007243 __ Vmov(temp, SRegisterFrom(source));
7244 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7245 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007246 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7247 vixl32::DRegister temp = temps.AcquireD();
7248 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7249 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7250 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7251 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7252 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7253 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7254 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7255 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7256 vixl32::DRegister temp = temps.AcquireD();
7257 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7258 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7259 GetAssembler()->StoreDToOffset(temp, sp, mem);
7260 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007261 vixl32::DRegister first = DRegisterFrom(source);
7262 vixl32::DRegister second = DRegisterFrom(destination);
7263 vixl32::DRegister temp = temps.AcquireD();
7264 __ Vmov(temp, first);
7265 __ Vmov(first, second);
7266 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007267 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007268 vixl32::DRegister reg = source.IsFpuRegisterPair()
7269 ? DRegisterFrom(source)
7270 : DRegisterFrom(destination);
7271 int mem = source.IsFpuRegisterPair()
7272 ? destination.GetStackIndex()
7273 : source.GetStackIndex();
7274 vixl32::DRegister temp = temps.AcquireD();
7275 __ Vmov(temp, reg);
7276 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7277 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007278 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007279 vixl32::SRegister reg = source.IsFpuRegister()
7280 ? SRegisterFrom(source)
7281 : SRegisterFrom(destination);
7282 int mem = source.IsFpuRegister()
7283 ? destination.GetStackIndex()
7284 : source.GetStackIndex();
7285 vixl32::Register temp = temps.Acquire();
7286 __ Vmov(temp, reg);
7287 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7288 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007289 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7290 vixl32::DRegister temp1 = temps.AcquireD();
7291 vixl32::DRegister temp2 = temps.AcquireD();
7292 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7293 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7294 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7295 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7296 } else {
7297 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7298 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007299}
7300
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007301void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7302 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007303}
7304
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007305void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7306 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007307}
7308
Artem Serov02d37832016-10-25 15:25:33 +01007309HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007310 HLoadClass::LoadKind desired_class_load_kind) {
7311 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007312 case HLoadClass::LoadKind::kInvalid:
7313 LOG(FATAL) << "UNREACHABLE";
7314 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007315 case HLoadClass::LoadKind::kReferrersClass:
7316 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007317 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007318 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007319 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007320 case HLoadClass::LoadKind::kBssEntryPublic:
7321 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007322 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007323 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007324 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007325 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007326 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007327 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007328 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007329 break;
7330 }
7331 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007332}
7333
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007334void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007335 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007336 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007337 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007338 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007339 cls,
7340 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007341 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007342 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007343 return;
7344 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007345 DCHECK_EQ(cls->NeedsAccessCheck(),
7346 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7347 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelingfe885462016-09-22 10:24:38 +01007348
Artem Serovd4cc5b22016-11-04 11:19:09 +00007349 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7350 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007351 ? LocationSummary::kCallOnSlowPath
7352 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007353 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007354 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007355 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007356 }
7357
Vladimir Marko41559982017-01-06 14:04:23 +00007358 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007359 locations->SetInAt(0, Location::RequiresRegister());
7360 }
7361 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007362 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7363 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7364 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007365 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007366 } else {
7367 // For non-Baker read barrier we have a temp-clobbering call.
7368 }
7369 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007370}
7371
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007372// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7373// move.
7374void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007375 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007376 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007377 codegen_->GenerateLoadClassRuntimeCall(cls);
Andra Danciua0130e82020-07-23 12:34:56 +00007378 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007379 return;
7380 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007381 DCHECK_EQ(cls->NeedsAccessCheck(),
7382 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7383 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007384
Vladimir Marko41559982017-01-06 14:04:23 +00007385 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007386 Location out_loc = locations->Out();
7387 vixl32::Register out = OutputRegister(cls);
7388
Artem Serovd4cc5b22016-11-04 11:19:09 +00007389 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7390 ? kWithoutReadBarrier
7391 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007392 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007393 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007394 case HLoadClass::LoadKind::kReferrersClass: {
7395 DCHECK(!cls->CanCallRuntime());
7396 DCHECK(!cls->MustGenerateClinitCheck());
7397 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7398 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007399 codegen_->GenerateGcRootFieldLoad(cls,
7400 out_loc,
7401 current_method,
7402 ArtMethod::DeclaringClassOffset().Int32Value(),
7403 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007404 break;
7405 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007406 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007407 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7408 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007409 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7410 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007411 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007412 codegen_->EmitMovwMovtPlaceholder(labels, out);
7413 break;
7414 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007415 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007416 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7417 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markode91ca92020-10-27 13:41:40 +00007418 codegen_->NewBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007419 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007420 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007421 break;
7422 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007423 case HLoadClass::LoadKind::kBssEntry:
7424 case HLoadClass::LoadKind::kBssEntryPublic:
7425 case HLoadClass::LoadKind::kBssEntryPackage: {
7426 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels = codegen_->NewTypeBssEntryPatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00007427 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007428 // All aligned loads are implicitly atomic consume operations on ARM.
Andreas Gampe3db70682018-12-26 15:12:03 -08007429 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007430 generate_null_check = true;
7431 break;
7432 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007433 case HLoadClass::LoadKind::kJitBootImageAddress: {
7434 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7435 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7436 DCHECK_NE(address, 0u);
7437 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7438 break;
7439 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007440 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007441 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7442 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007443 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007444 // /* GcRoot<mirror::Class> */ out = *out
Andreas Gampe3db70682018-12-26 15:12:03 -08007445 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007446 break;
7447 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007448 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007449 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007450 LOG(FATAL) << "UNREACHABLE";
7451 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007452 }
7453
7454 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7455 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007456 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007457 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007458 codegen_->AddSlowPath(slow_path);
7459 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007460 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007461 }
7462 if (cls->MustGenerateClinitCheck()) {
7463 GenerateClassInitializationCheck(slow_path, out);
7464 } else {
7465 __ Bind(slow_path->GetExitLabel());
7466 }
Andra Danciua0130e82020-07-23 12:34:56 +00007467 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 16);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007468 }
7469}
7470
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007471void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7472 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7473 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7474 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7475}
7476
7477void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7478 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7479}
7480
Orion Hodson18259d72018-04-12 11:18:23 +01007481void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7482 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7483 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7484 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7485}
7486
7487void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7488 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7489}
7490
Artem Serov02d37832016-10-25 15:25:33 +01007491void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7492 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007493 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007494 locations->SetInAt(0, Location::RequiresRegister());
7495 if (check->HasUses()) {
7496 locations->SetOut(Location::SameAsFirstInput());
7497 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007498 // Rely on the type initialization to save everything we need.
7499 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007500}
7501
7502void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7503 // We assume the class is not null.
7504 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007505 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007506 codegen_->AddSlowPath(slow_path);
7507 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7508}
7509
7510void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7511 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7512 UseScratchRegisterScope temps(GetVIXLAssembler());
7513 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007514 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Markobf121912019-06-04 13:49:05 +01007515 constexpr uint32_t shifted_visibly_initialized_value =
7516 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << status_lsb_position;
Vladimir Markodc682aa2018-01-04 18:42:57 +00007517
Vladimir Markobf121912019-06-04 13:49:05 +01007518 const size_t status_offset = mirror::Class::StatusOffset().SizeValue();
7519 GetAssembler()->LoadFromOffset(kLoadWord, temp, class_reg, status_offset);
7520 __ Cmp(temp, shifted_visibly_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007521 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007522 __ Bind(slow_path->GetExitLabel());
7523}
7524
Vladimir Marko175e7862018-03-27 09:03:13 +00007525void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7526 HTypeCheckInstruction* check,
7527 vixl32::Register temp,
7528 vixl32::FlagsUpdate flags_update) {
7529 uint32_t path_to_root = check->GetBitstringPathToRoot();
7530 uint32_t mask = check->GetBitstringMask();
7531 DCHECK(IsPowerOfTwo(mask + 1));
7532 size_t mask_bits = WhichPowerOf2(mask + 1);
7533
7534 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7535 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7536 if (mask_bits == 16u) {
7537 // Load only the bitstring part of the status word.
7538 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7539 // Check if the bitstring bits are equal to `path_to_root`.
7540 if (flags_update == SetFlags) {
7541 __ Cmp(temp, path_to_root);
7542 } else {
7543 __ Sub(temp, temp, path_to_root);
7544 }
7545 } else {
7546 // /* uint32_t */ temp = temp->status_
7547 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7548 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7549 // Compare the bitstring bits using SUB.
7550 __ Sub(temp, temp, path_to_root);
7551 // Shift out bits that do not contribute to the comparison.
7552 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7553 } else if (IsUint<16>(path_to_root)) {
7554 if (temp.IsLow()) {
7555 // Note: Optimized for size but contains one more dependent instruction than necessary.
7556 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7557 // macro assembler would use the high reg IP for the constant by default.
7558 // Compare the bitstring bits using SUB.
7559 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7560 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7561 // Shift out bits that do not contribute to the comparison.
7562 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7563 } else {
7564 // Extract the bitstring bits.
7565 __ Ubfx(temp, temp, 0, mask_bits);
7566 // Check if the bitstring bits are equal to `path_to_root`.
7567 if (flags_update == SetFlags) {
7568 __ Cmp(temp, path_to_root);
7569 } else {
7570 __ Sub(temp, temp, path_to_root);
7571 }
7572 }
7573 } else {
7574 // Shift out bits that do not contribute to the comparison.
7575 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7576 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7577 if (flags_update == SetFlags) {
7578 __ Cmp(temp, path_to_root << (32u - mask_bits));
7579 } else {
7580 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7581 }
7582 }
7583 }
7584}
7585
Artem Serov02d37832016-10-25 15:25:33 +01007586HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007587 HLoadString::LoadKind desired_string_load_kind) {
7588 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007589 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007590 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007591 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007592 DCHECK(!GetCompilerOptions().IsJitCompiler());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007593 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007594 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007595 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007596 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007597 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007598 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007599 break;
7600 }
7601 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007602}
7603
7604void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007605 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007606 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007607 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007608 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007609 locations->SetOut(LocationFrom(r0));
7610 } else {
7611 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007612 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7613 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007614 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007615 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007616 } else {
7617 // For non-Baker read barrier we have a temp-clobbering call.
7618 }
7619 }
Artem Serov02d37832016-10-25 15:25:33 +01007620 }
7621}
7622
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007623// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7624// move.
7625void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007626 LocationSummary* locations = load->GetLocations();
7627 Location out_loc = locations->Out();
7628 vixl32::Register out = OutputRegister(load);
7629 HLoadString::LoadKind load_kind = load->GetLoadKind();
7630
7631 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007632 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007633 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7634 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007635 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007636 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007637 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007638 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007639 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007640 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007641 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7642 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markode91ca92020-10-27 13:41:40 +00007643 codegen_->NewBootImageRelRoPatch(CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007644 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007645 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007646 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007647 }
7648 case HLoadString::LoadKind::kBssEntry: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007649 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007650 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007651 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007652 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007653 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007654 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007655 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007656 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007657 codegen_->AddSlowPath(slow_path);
7658 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7659 __ Bind(slow_path->GetExitLabel());
Andra Danciua0130e82020-07-23 12:34:56 +00007660 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 17);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007661 return;
7662 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007663 case HLoadString::LoadKind::kJitBootImageAddress: {
7664 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7665 DCHECK_NE(address, 0u);
7666 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7667 return;
7668 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007669 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007670 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007671 load->GetStringIndex(),
7672 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007673 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007674 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007675 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007676 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007677 }
7678 default:
7679 break;
7680 }
Artem Serov02d37832016-10-25 15:25:33 +01007681
7682 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007683 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007684 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007685 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007686 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7687 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andra Danciua0130e82020-07-23 12:34:56 +00007688 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 18);
Artem Serov02d37832016-10-25 15:25:33 +01007689}
7690
7691static int32_t GetExceptionTlsOffset() {
7692 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7693}
7694
7695void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7696 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007697 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007698 locations->SetOut(Location::RequiresRegister());
7699}
7700
7701void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7702 vixl32::Register out = OutputRegister(load);
7703 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7704}
7705
7706
7707void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007708 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007709}
7710
7711void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7712 UseScratchRegisterScope temps(GetVIXLAssembler());
7713 vixl32::Register temp = temps.Acquire();
7714 __ Mov(temp, 0);
7715 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7716}
7717
7718void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007719 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7720 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007721 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7722 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7723}
7724
7725void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7726 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7727 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7728}
7729
Artem Serov657022c2016-11-23 14:19:38 +00007730// Temp is used for read barrier.
7731static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7732 if (kEmitCompilerReadBarrier &&
7733 (kUseBakerReadBarrier ||
7734 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7735 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7736 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7737 return 1;
7738 }
7739 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007740}
7741
Artem Serov657022c2016-11-23 14:19:38 +00007742// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7743// interface pointer, one for loading the current interface.
7744// The other checks have one temp for loading the object's class.
7745static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7746 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7747 return 3;
7748 }
7749 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7750}
Artem Serovcfbe9132016-10-14 15:58:56 +01007751
7752void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7753 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7754 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7755 bool baker_read_barrier_slow_path = false;
7756 switch (type_check_kind) {
7757 case TypeCheckKind::kExactCheck:
7758 case TypeCheckKind::kAbstractClassCheck:
7759 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007760 case TypeCheckKind::kArrayObjectCheck: {
7761 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7762 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7763 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007764 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007765 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007766 case TypeCheckKind::kArrayCheck:
7767 case TypeCheckKind::kUnresolvedCheck:
7768 case TypeCheckKind::kInterfaceCheck:
7769 call_kind = LocationSummary::kCallOnSlowPath;
7770 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007771 case TypeCheckKind::kBitstringCheck:
7772 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007773 }
7774
Vladimir Markoca6fff82017-10-03 14:49:14 +01007775 LocationSummary* locations =
7776 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007777 if (baker_read_barrier_slow_path) {
7778 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7779 }
7780 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007781 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7782 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7783 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7784 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7785 } else {
7786 locations->SetInAt(1, Location::RequiresRegister());
7787 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007788 // The "out" register is used as a temporary, so it overlaps with the inputs.
7789 // Note that TypeCheckSlowPathARM uses this register too.
7790 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007791 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007792}
7793
7794void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7795 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7796 LocationSummary* locations = instruction->GetLocations();
7797 Location obj_loc = locations->InAt(0);
7798 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007799 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7800 ? vixl32::Register()
7801 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007802 Location out_loc = locations->Out();
7803 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007804 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7805 DCHECK_LE(num_temps, 1u);
7806 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007807 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7808 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7809 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7810 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007811 vixl32::Label done;
7812 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007813 SlowPathCodeARMVIXL* slow_path = nullptr;
7814
7815 // Return 0 if `obj` is null.
7816 // avoid null check if we know obj is not null.
7817 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007818 DCHECK(!out.Is(obj));
7819 __ Mov(out, 0);
Andreas Gampe3db70682018-12-26 15:12:03 -08007820 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007821 }
7822
Artem Serovcfbe9132016-10-14 15:58:56 +01007823 switch (type_check_kind) {
7824 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007825 ReadBarrierOption read_barrier_option =
7826 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007827 // /* HeapReference<Class> */ out = obj->klass_
7828 GenerateReferenceLoadTwoRegisters(instruction,
7829 out_loc,
7830 obj_loc,
7831 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007832 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007833 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007834 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007835 __ Cmp(out, cls);
7836 // We speculatively set the result to false without changing the condition
7837 // flags, which allows us to avoid some branching later.
7838 __ Mov(LeaveFlags, out, 0);
7839
7840 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7841 // we check that the output is in a low register, so that a 16-bit MOV
7842 // encoding can be used.
7843 if (out.IsLow()) {
7844 // We use the scope because of the IT block that follows.
7845 ExactAssemblyScope guard(GetVIXLAssembler(),
7846 2 * vixl32::k16BitT32InstructionSizeInBytes,
7847 CodeBufferCheckScope::kExactSize);
7848
7849 __ it(eq);
7850 __ mov(eq, out, 1);
7851 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007852 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007853 __ Mov(out, 1);
7854 }
7855
Artem Serovcfbe9132016-10-14 15:58:56 +01007856 break;
7857 }
7858
7859 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007860 ReadBarrierOption read_barrier_option =
7861 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007862 // /* HeapReference<Class> */ out = obj->klass_
7863 GenerateReferenceLoadTwoRegisters(instruction,
7864 out_loc,
7865 obj_loc,
7866 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007867 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007868 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007869 // If the class is abstract, we eagerly fetch the super class of the
7870 // object to avoid doing a comparison we know will fail.
7871 vixl32::Label loop;
7872 __ Bind(&loop);
7873 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007874 GenerateReferenceLoadOneRegister(instruction,
7875 out_loc,
7876 super_offset,
7877 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007878 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007879 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007880 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007881 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007882 __ B(ne, &loop, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007883 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007884 break;
7885 }
7886
7887 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007888 ReadBarrierOption read_barrier_option =
7889 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007890 // /* HeapReference<Class> */ out = obj->klass_
7891 GenerateReferenceLoadTwoRegisters(instruction,
7892 out_loc,
7893 obj_loc,
7894 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007895 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007896 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007897 // Walk over the class hierarchy to find a match.
7898 vixl32::Label loop, success;
7899 __ Bind(&loop);
7900 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007901 __ B(eq, &success, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007902 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007903 GenerateReferenceLoadOneRegister(instruction,
7904 out_loc,
7905 super_offset,
7906 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007907 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007908 // This is essentially a null check, but it sets the condition flags to the
7909 // proper value for the code that follows the loop, i.e. not `eq`.
7910 __ Cmp(out, 1);
Andreas Gampe3db70682018-12-26 15:12:03 -08007911 __ B(hs, &loop, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007912
7913 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7914 // we check that the output is in a low register, so that a 16-bit MOV
7915 // encoding can be used.
7916 if (out.IsLow()) {
7917 // If `out` is null, we use it for the result, and the condition flags
7918 // have already been set to `ne`, so the IT block that comes afterwards
7919 // (and which handles the successful case) turns into a NOP (instead of
7920 // overwriting `out`).
7921 __ Bind(&success);
7922
7923 // We use the scope because of the IT block that follows.
7924 ExactAssemblyScope guard(GetVIXLAssembler(),
7925 2 * vixl32::k16BitT32InstructionSizeInBytes,
7926 CodeBufferCheckScope::kExactSize);
7927
7928 // There is only one branch to the `success` label (which is bound to this
7929 // IT block), and it has the same condition, `eq`, so in that case the MOV
7930 // is executed.
7931 __ it(eq);
7932 __ mov(eq, out, 1);
7933 } else {
7934 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007935 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007936 __ Bind(&success);
7937 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007938 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007939
Artem Serovcfbe9132016-10-14 15:58:56 +01007940 break;
7941 }
7942
7943 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007944 ReadBarrierOption read_barrier_option =
7945 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007946 // /* HeapReference<Class> */ out = obj->klass_
7947 GenerateReferenceLoadTwoRegisters(instruction,
7948 out_loc,
7949 obj_loc,
7950 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007951 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007952 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007953 // Do an exact check.
7954 vixl32::Label exact_check;
7955 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007956 __ B(eq, &exact_check, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007957 // Otherwise, we need to check that the object's class is a non-primitive array.
7958 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007959 GenerateReferenceLoadOneRegister(instruction,
7960 out_loc,
7961 component_offset,
7962 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007963 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007964 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007965 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007966 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7967 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007968 __ Cmp(out, 0);
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 __ Bind(&exact_check);
7978
7979 // We use the scope because of the IT block that follows.
7980 ExactAssemblyScope guard(GetVIXLAssembler(),
7981 2 * vixl32::k16BitT32InstructionSizeInBytes,
7982 CodeBufferCheckScope::kExactSize);
7983
7984 __ it(eq);
7985 __ mov(eq, out, 1);
7986 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007987 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007988 __ Bind(&exact_check);
7989 __ Mov(out, 1);
7990 }
7991
Artem Serovcfbe9132016-10-14 15:58:56 +01007992 break;
7993 }
7994
7995 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007996 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007997 // /* HeapReference<Class> */ out = obj->klass_
7998 GenerateReferenceLoadTwoRegisters(instruction,
7999 out_loc,
8000 obj_loc,
8001 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00008002 maybe_temp_loc,
8003 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008004 __ Cmp(out, cls);
8005 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008006 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008007 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008008 codegen_->AddSlowPath(slow_path);
8009 __ B(ne, slow_path->GetEntryLabel());
8010 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008011 break;
8012 }
8013
8014 case TypeCheckKind::kUnresolvedCheck:
8015 case TypeCheckKind::kInterfaceCheck: {
8016 // Note that we indeed only call on slow path, but we always go
8017 // into the slow path for the unresolved and interface check
8018 // cases.
8019 //
8020 // We cannot directly call the InstanceofNonTrivial runtime
8021 // entry point without resorting to a type checking slow path
8022 // here (i.e. by calling InvokeRuntime directly), as it would
8023 // require to assign fixed registers for the inputs of this
8024 // HInstanceOf instruction (following the runtime calling
8025 // convention), which might be cluttered by the potential first
8026 // read barrier emission at the beginning of this method.
8027 //
8028 // TODO: Introduce a new runtime entry point taking the object
8029 // to test (instead of its class) as argument, and let it deal
8030 // with the read barrier issues. This will let us refactor this
8031 // case of the `switch` code as it was previously (with a direct
8032 // call to the runtime not using a type checking slow path).
8033 // This should also be beneficial for the other cases above.
8034 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008035 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008036 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008037 codegen_->AddSlowPath(slow_path);
8038 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008039 break;
8040 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008041
8042 case TypeCheckKind::kBitstringCheck: {
8043 // /* HeapReference<Class> */ temp = obj->klass_
8044 GenerateReferenceLoadTwoRegisters(instruction,
8045 out_loc,
8046 obj_loc,
8047 class_offset,
8048 maybe_temp_loc,
8049 kWithoutReadBarrier);
8050
8051 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
8052 // If `out` is a low reg and we would have another low reg temp, we could
8053 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
8054 //
8055 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
8056 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
8057 // would be the same and we would have fewer direct data dependencies.
8058 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
8059 break;
8060 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008061 }
8062
Artem Serovcfbe9132016-10-14 15:58:56 +01008063 if (done.IsReferenced()) {
8064 __ Bind(&done);
8065 }
8066
8067 if (slow_path != nullptr) {
8068 __ Bind(slow_path->GetExitLabel());
8069 }
8070}
8071
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008072void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008073 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008074 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008075 LocationSummary* locations =
8076 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008077 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00008078 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
8079 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
8080 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
8081 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
8082 } else {
8083 locations->SetInAt(1, Location::RequiresRegister());
8084 }
Artem Serov657022c2016-11-23 14:19:38 +00008085 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008086}
8087
8088void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8089 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8090 LocationSummary* locations = instruction->GetLocations();
8091 Location obj_loc = locations->InAt(0);
8092 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00008093 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
8094 ? vixl32::Register()
8095 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008096 Location temp_loc = locations->GetTemp(0);
8097 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008098 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8099 DCHECK_LE(num_temps, 3u);
8100 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8101 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8102 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8103 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8104 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8105 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8106 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8107 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8108 const uint32_t object_array_data_offset =
8109 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008110
Vladimir Marko87584542017-12-12 17:47:52 +00008111 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008112 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008113 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8114 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008115 codegen_->AddSlowPath(type_check_slow_path);
8116
8117 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008118 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008119 // Avoid null check if we know obj is not null.
8120 if (instruction->MustDoNullCheck()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08008121 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008122 }
8123
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008124 switch (type_check_kind) {
8125 case TypeCheckKind::kExactCheck:
8126 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008127 // /* HeapReference<Class> */ temp = obj->klass_
8128 GenerateReferenceLoadTwoRegisters(instruction,
8129 temp_loc,
8130 obj_loc,
8131 class_offset,
8132 maybe_temp2_loc,
8133 kWithoutReadBarrier);
8134
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008135 __ Cmp(temp, cls);
8136 // Jump to slow path for throwing the exception or doing a
8137 // more involved array check.
8138 __ B(ne, type_check_slow_path->GetEntryLabel());
8139 break;
8140 }
8141
8142 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008143 // /* HeapReference<Class> */ temp = obj->klass_
8144 GenerateReferenceLoadTwoRegisters(instruction,
8145 temp_loc,
8146 obj_loc,
8147 class_offset,
8148 maybe_temp2_loc,
8149 kWithoutReadBarrier);
8150
Artem Serovcfbe9132016-10-14 15:58:56 +01008151 // If the class is abstract, we eagerly fetch the super class of the
8152 // object to avoid doing a comparison we know will fail.
8153 vixl32::Label loop;
8154 __ Bind(&loop);
8155 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008156 GenerateReferenceLoadOneRegister(instruction,
8157 temp_loc,
8158 super_offset,
8159 maybe_temp2_loc,
8160 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008161
8162 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8163 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008164 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008165
8166 // Otherwise, compare the classes.
8167 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008168 __ B(ne, &loop, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008169 break;
8170 }
8171
8172 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008173 // /* HeapReference<Class> */ temp = obj->klass_
8174 GenerateReferenceLoadTwoRegisters(instruction,
8175 temp_loc,
8176 obj_loc,
8177 class_offset,
8178 maybe_temp2_loc,
8179 kWithoutReadBarrier);
8180
Artem Serovcfbe9132016-10-14 15:58:56 +01008181 // Walk over the class hierarchy to find a match.
8182 vixl32::Label loop;
8183 __ Bind(&loop);
8184 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008185 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008186
8187 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008188 GenerateReferenceLoadOneRegister(instruction,
8189 temp_loc,
8190 super_offset,
8191 maybe_temp2_loc,
8192 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008193
8194 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8195 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008196 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008197 // Otherwise, jump to the beginning of the loop.
8198 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008199 break;
8200 }
8201
Artem Serovcfbe9132016-10-14 15:58:56 +01008202 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008203 // /* HeapReference<Class> */ temp = obj->klass_
8204 GenerateReferenceLoadTwoRegisters(instruction,
8205 temp_loc,
8206 obj_loc,
8207 class_offset,
8208 maybe_temp2_loc,
8209 kWithoutReadBarrier);
8210
Artem Serovcfbe9132016-10-14 15:58:56 +01008211 // Do an exact check.
8212 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008213 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008214
8215 // Otherwise, we need to check that the object's class is a non-primitive array.
8216 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008217 GenerateReferenceLoadOneRegister(instruction,
8218 temp_loc,
8219 component_offset,
8220 maybe_temp2_loc,
8221 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008222 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008223 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008224 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8225 // to further check that this component type is not a primitive type.
8226 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008227 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008228 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008229 break;
8230 }
8231
8232 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008233 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008234 // We cannot directly call the CheckCast runtime entry point
8235 // without resorting to a type checking slow path here (i.e. by
8236 // calling InvokeRuntime directly), as it would require to
8237 // assign fixed registers for the inputs of this HInstanceOf
8238 // instruction (following the runtime calling convention), which
8239 // might be cluttered by the potential first read barrier
8240 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008241
Artem Serovcfbe9132016-10-14 15:58:56 +01008242 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008243 break;
Artem Serov657022c2016-11-23 14:19:38 +00008244
8245 case TypeCheckKind::kInterfaceCheck: {
8246 // Avoid read barriers to improve performance of the fast path. We can not get false
8247 // positives by doing this.
8248 // /* HeapReference<Class> */ temp = obj->klass_
8249 GenerateReferenceLoadTwoRegisters(instruction,
8250 temp_loc,
8251 obj_loc,
8252 class_offset,
8253 maybe_temp2_loc,
8254 kWithoutReadBarrier);
8255
8256 // /* HeapReference<Class> */ temp = temp->iftable_
8257 GenerateReferenceLoadTwoRegisters(instruction,
8258 temp_loc,
8259 temp_loc,
8260 iftable_offset,
8261 maybe_temp2_loc,
8262 kWithoutReadBarrier);
8263 // Iftable is never null.
8264 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8265 // Loop through the iftable and check if any class matches.
8266 vixl32::Label start_loop;
8267 __ Bind(&start_loop);
8268 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8269 type_check_slow_path->GetEntryLabel());
8270 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8271 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8272 // Go to next interface.
8273 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8274 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8275 // Compare the classes and continue the loop if they do not match.
8276 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Andreas Gampe3db70682018-12-26 15:12:03 -08008277 __ B(ne, &start_loop, /* is_far_target= */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008278 break;
8279 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008280
8281 case TypeCheckKind::kBitstringCheck: {
8282 // /* HeapReference<Class> */ temp = obj->klass_
8283 GenerateReferenceLoadTwoRegisters(instruction,
8284 temp_loc,
8285 obj_loc,
8286 class_offset,
8287 maybe_temp2_loc,
8288 kWithoutReadBarrier);
8289
8290 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8291 __ B(ne, type_check_slow_path->GetEntryLabel());
8292 break;
8293 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008294 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008295 if (done.IsReferenced()) {
8296 __ Bind(&done);
8297 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008298
8299 __ Bind(type_check_slow_path->GetExitLabel());
8300}
8301
Artem Serov551b28f2016-10-18 19:11:30 +01008302void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008303 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8304 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008305 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8306 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8307}
8308
8309void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8310 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8311 instruction,
8312 instruction->GetDexPc());
8313 if (instruction->IsEnter()) {
8314 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8315 } else {
8316 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8317 }
Andra Danciua0130e82020-07-23 12:34:56 +00008318 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 19);
Artem Serov551b28f2016-10-18 19:11:30 +01008319}
8320
Artem Serov02109dd2016-09-23 17:17:54 +01008321void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8322 HandleBitwiseOperation(instruction, AND);
8323}
8324
8325void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8326 HandleBitwiseOperation(instruction, ORR);
8327}
8328
8329void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8330 HandleBitwiseOperation(instruction, EOR);
8331}
8332
8333void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8334 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008335 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008336 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8337 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008338 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8339 locations->SetInAt(0, Location::RequiresRegister());
8340 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8342}
8343
8344void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8345 HandleBitwiseOperation(instruction);
8346}
8347
8348void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8349 HandleBitwiseOperation(instruction);
8350}
8351
8352void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8353 HandleBitwiseOperation(instruction);
8354}
8355
Artem Serov2bbc9532016-10-21 11:51:50 +01008356void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8357 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008358 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008359 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8360 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008361
8362 locations->SetInAt(0, Location::RequiresRegister());
8363 locations->SetInAt(1, Location::RequiresRegister());
8364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8365}
8366
8367void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8368 LocationSummary* locations = instruction->GetLocations();
8369 Location first = locations->InAt(0);
8370 Location second = locations->InAt(1);
8371 Location out = locations->Out();
8372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008373 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008374 vixl32::Register first_reg = RegisterFrom(first);
8375 vixl32::Register second_reg = RegisterFrom(second);
8376 vixl32::Register out_reg = RegisterFrom(out);
8377
8378 switch (instruction->GetOpKind()) {
8379 case HInstruction::kAnd:
8380 __ Bic(out_reg, first_reg, second_reg);
8381 break;
8382 case HInstruction::kOr:
8383 __ Orn(out_reg, first_reg, second_reg);
8384 break;
8385 // There is no EON on arm.
8386 case HInstruction::kXor:
8387 default:
8388 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8389 UNREACHABLE();
8390 }
8391 return;
8392
8393 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008394 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008395 vixl32::Register first_low = LowRegisterFrom(first);
8396 vixl32::Register first_high = HighRegisterFrom(first);
8397 vixl32::Register second_low = LowRegisterFrom(second);
8398 vixl32::Register second_high = HighRegisterFrom(second);
8399 vixl32::Register out_low = LowRegisterFrom(out);
8400 vixl32::Register out_high = HighRegisterFrom(out);
8401
8402 switch (instruction->GetOpKind()) {
8403 case HInstruction::kAnd:
8404 __ Bic(out_low, first_low, second_low);
8405 __ Bic(out_high, first_high, second_high);
8406 break;
8407 case HInstruction::kOr:
8408 __ Orn(out_low, first_low, second_low);
8409 __ Orn(out_high, first_high, second_high);
8410 break;
8411 // There is no EON on arm.
8412 case HInstruction::kXor:
8413 default:
8414 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8415 UNREACHABLE();
8416 }
8417 }
8418}
8419
Anton Kirilov74234da2017-01-13 14:42:47 +00008420void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8421 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008422 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8423 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008424 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008425 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008426 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008427 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8428
8429 locations->SetInAt(0, Location::RequiresRegister());
8430 locations->SetInAt(1, Location::RequiresRegister());
8431 locations->SetOut(Location::RequiresRegister(),
8432 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8433}
8434
8435void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8436 HDataProcWithShifterOp* instruction) {
8437 const LocationSummary* const locations = instruction->GetLocations();
8438 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8439 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008441 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008442 const vixl32::Register first = InputRegisterAt(instruction, 0);
8443 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008444 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008445 ? LowRegisterFrom(locations->InAt(1))
8446 : InputRegisterAt(instruction, 1);
8447
Anton Kirilov420ee302017-02-21 18:10:26 +00008448 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8449 DCHECK_EQ(kind, HInstruction::kAdd);
8450
8451 switch (op_kind) {
8452 case HDataProcWithShifterOp::kUXTB:
8453 __ Uxtab(output, first, second);
8454 break;
8455 case HDataProcWithShifterOp::kUXTH:
8456 __ Uxtah(output, first, second);
8457 break;
8458 case HDataProcWithShifterOp::kSXTB:
8459 __ Sxtab(output, first, second);
8460 break;
8461 case HDataProcWithShifterOp::kSXTH:
8462 __ Sxtah(output, first, second);
8463 break;
8464 default:
8465 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8466 UNREACHABLE();
8467 }
8468 } else {
8469 GenerateDataProcInstruction(kind,
8470 output,
8471 first,
8472 Operand(second,
8473 ShiftFromOpKind(op_kind),
8474 instruction->GetShiftAmount()),
8475 codegen_);
8476 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008477 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008478 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008479
8480 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8481 const vixl32::Register second = InputRegisterAt(instruction, 1);
8482
8483 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8484 GenerateDataProc(kind,
8485 locations->Out(),
8486 locations->InAt(0),
8487 second,
8488 Operand(second, ShiftType::ASR, 31),
8489 codegen_);
8490 } else {
8491 GenerateLongDataProc(instruction, codegen_);
8492 }
8493 }
8494}
8495
Artem Serov02109dd2016-09-23 17:17:54 +01008496// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8497void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8498 vixl32::Register first,
8499 uint32_t value) {
8500 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8501 if (value == 0xffffffffu) {
8502 if (!out.Is(first)) {
8503 __ Mov(out, first);
8504 }
8505 return;
8506 }
8507 if (value == 0u) {
8508 __ Mov(out, 0);
8509 return;
8510 }
8511 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008512 __ And(out, first, value);
8513 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8514 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008515 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008516 DCHECK(IsPowerOfTwo(value + 1));
8517 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008518 }
8519}
8520
8521// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8522void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8523 vixl32::Register first,
8524 uint32_t value) {
8525 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8526 if (value == 0u) {
8527 if (!out.Is(first)) {
8528 __ Mov(out, first);
8529 }
8530 return;
8531 }
8532 if (value == 0xffffffffu) {
8533 __ Mvn(out, 0);
8534 return;
8535 }
8536 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8537 __ Orr(out, first, value);
8538 } else {
8539 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8540 __ Orn(out, first, ~value);
8541 }
8542}
8543
8544// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8545void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8546 vixl32::Register first,
8547 uint32_t value) {
8548 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8549 if (value == 0u) {
8550 if (!out.Is(first)) {
8551 __ Mov(out, first);
8552 }
8553 return;
8554 }
8555 __ Eor(out, first, value);
8556}
8557
Anton Kirilovdda43962016-11-21 19:55:20 +00008558void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8559 Location first,
8560 uint64_t value) {
8561 vixl32::Register out_low = LowRegisterFrom(out);
8562 vixl32::Register out_high = HighRegisterFrom(out);
8563 vixl32::Register first_low = LowRegisterFrom(first);
8564 vixl32::Register first_high = HighRegisterFrom(first);
8565 uint32_t value_low = Low32Bits(value);
8566 uint32_t value_high = High32Bits(value);
8567 if (value_low == 0u) {
8568 if (!out_low.Is(first_low)) {
8569 __ Mov(out_low, first_low);
8570 }
8571 __ Add(out_high, first_high, value_high);
8572 return;
8573 }
8574 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008575 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008576 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008577 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008578 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8579 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008580 }
8581}
8582
Artem Serov02109dd2016-09-23 17:17:54 +01008583void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8584 LocationSummary* locations = instruction->GetLocations();
8585 Location first = locations->InAt(0);
8586 Location second = locations->InAt(1);
8587 Location out = locations->Out();
8588
8589 if (second.IsConstant()) {
8590 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8591 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008592 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008593 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8594 vixl32::Register out_reg = OutputRegister(instruction);
8595 if (instruction->IsAnd()) {
8596 GenerateAndConst(out_reg, first_reg, value_low);
8597 } else if (instruction->IsOr()) {
8598 GenerateOrrConst(out_reg, first_reg, value_low);
8599 } else {
8600 DCHECK(instruction->IsXor());
8601 GenerateEorConst(out_reg, first_reg, value_low);
8602 }
8603 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008604 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008605 uint32_t value_high = High32Bits(value);
8606 vixl32::Register first_low = LowRegisterFrom(first);
8607 vixl32::Register first_high = HighRegisterFrom(first);
8608 vixl32::Register out_low = LowRegisterFrom(out);
8609 vixl32::Register out_high = HighRegisterFrom(out);
8610 if (instruction->IsAnd()) {
8611 GenerateAndConst(out_low, first_low, value_low);
8612 GenerateAndConst(out_high, first_high, value_high);
8613 } else if (instruction->IsOr()) {
8614 GenerateOrrConst(out_low, first_low, value_low);
8615 GenerateOrrConst(out_high, first_high, value_high);
8616 } else {
8617 DCHECK(instruction->IsXor());
8618 GenerateEorConst(out_low, first_low, value_low);
8619 GenerateEorConst(out_high, first_high, value_high);
8620 }
8621 }
8622 return;
8623 }
8624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008625 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008626 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8627 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8628 vixl32::Register out_reg = OutputRegister(instruction);
8629 if (instruction->IsAnd()) {
8630 __ And(out_reg, first_reg, second_reg);
8631 } else if (instruction->IsOr()) {
8632 __ Orr(out_reg, first_reg, second_reg);
8633 } else {
8634 DCHECK(instruction->IsXor());
8635 __ Eor(out_reg, first_reg, second_reg);
8636 }
8637 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008638 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008639 vixl32::Register first_low = LowRegisterFrom(first);
8640 vixl32::Register first_high = HighRegisterFrom(first);
8641 vixl32::Register second_low = LowRegisterFrom(second);
8642 vixl32::Register second_high = HighRegisterFrom(second);
8643 vixl32::Register out_low = LowRegisterFrom(out);
8644 vixl32::Register out_high = HighRegisterFrom(out);
8645 if (instruction->IsAnd()) {
8646 __ And(out_low, first_low, second_low);
8647 __ And(out_high, first_high, second_high);
8648 } else if (instruction->IsOr()) {
8649 __ Orr(out_low, first_low, second_low);
8650 __ Orr(out_high, first_high, second_high);
8651 } else {
8652 DCHECK(instruction->IsXor());
8653 __ Eor(out_low, first_low, second_low);
8654 __ Eor(out_high, first_high, second_high);
8655 }
8656 }
8657}
8658
Artem Serovcfbe9132016-10-14 15:58:56 +01008659void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008660 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008661 Location out,
8662 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008663 Location maybe_temp,
8664 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008665 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008666 if (read_barrier_option == kWithReadBarrier) {
8667 CHECK(kEmitCompilerReadBarrier);
8668 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8669 if (kUseBakerReadBarrier) {
8670 // Load with fast path based Baker's read barrier.
8671 // /* HeapReference<Object> */ out = *(out + offset)
8672 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008673 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008674 } else {
8675 // Load with slow path based read barrier.
8676 // Save the value of `out` into `maybe_temp` before overwriting it
8677 // in the following move operation, as we will need it for the
8678 // read barrier below.
8679 __ Mov(RegisterFrom(maybe_temp), out_reg);
8680 // /* HeapReference<Object> */ out = *(out + offset)
8681 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8682 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8683 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008684 } else {
8685 // Plain load with no read barrier.
8686 // /* HeapReference<Object> */ out = *(out + offset)
8687 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8688 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8689 }
8690}
8691
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008692void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008693 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008694 Location out,
8695 Location obj,
8696 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008697 Location maybe_temp,
8698 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008699 vixl32::Register out_reg = RegisterFrom(out);
8700 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008701 if (read_barrier_option == kWithReadBarrier) {
8702 CHECK(kEmitCompilerReadBarrier);
8703 if (kUseBakerReadBarrier) {
8704 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8705 // Load with fast path based Baker's read barrier.
8706 // /* HeapReference<Object> */ out = *(obj + offset)
8707 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008708 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008709 } else {
8710 // Load with slow path based read barrier.
8711 // /* HeapReference<Object> */ out = *(obj + offset)
8712 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8713 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8714 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008715 } else {
8716 // Plain load with no read barrier.
8717 // /* HeapReference<Object> */ out = *(obj + offset)
8718 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8719 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8720 }
8721}
8722
Vladimir Markoca1e0382018-04-11 09:58:41 +00008723void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008724 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008725 Location root,
8726 vixl32::Register obj,
8727 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008728 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008729 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008730 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008731 DCHECK(kEmitCompilerReadBarrier);
8732 if (kUseBakerReadBarrier) {
8733 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008734 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008735
Vladimir Marko008e09f32018-08-06 15:42:43 +01008736 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8737 // the Marking Register) to decide whether we need to enter
8738 // the slow path to mark the GC root.
8739 //
8740 // We use shared thunks for the slow path; shared within the method
8741 // for JIT, across methods for AOT. That thunk checks the reference
8742 // and jumps to the entrypoint if needed.
8743 //
8744 // lr = &return_address;
8745 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8746 // if (mr) { // Thread::Current()->GetIsGcMarking()
8747 // goto gc_root_thunk<root_reg>(lr)
8748 // }
8749 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008750
Vladimir Marko008e09f32018-08-06 15:42:43 +01008751 UseScratchRegisterScope temps(GetVIXLAssembler());
8752 temps.Exclude(ip);
8753 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8754 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008755
Vladimir Markod887ed82018-08-14 13:52:12 +00008756 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8757 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8758 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8759 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8760 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008761 vixl32::Label return_address;
8762 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8763 __ cmp(mr, Operand(0));
8764 // Currently the offset is always within range. If that changes,
8765 // we shall have to split the load the same way as for fields.
8766 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8767 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8768 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8769 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008770 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008771 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8772 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8773 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008774 } else {
8775 // GC root loaded through a slow path for read barriers other
8776 // than Baker's.
8777 // /* GcRoot<mirror::Object>* */ root = obj + offset
8778 __ Add(root_reg, obj, offset);
8779 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008780 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008781 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008782 } else {
8783 // Plain GC root load with no read barrier.
8784 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8785 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8786 // Note that GC roots are not affected by heap poisoning, thus we
8787 // do not have to unpoison `root_reg` here.
8788 }
Andra Danciua0130e82020-07-23 12:34:56 +00008789 MaybeGenerateMarkingRegisterCheck(/* code= */ 20);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008790}
8791
Vladimir Markod887ed82018-08-14 13:52:12 +00008792void CodeGeneratorARMVIXL::GenerateUnsafeCasOldValueAddWithBakerReadBarrier(
8793 vixl::aarch32::Register old_value,
8794 vixl::aarch32::Register adjusted_old_value,
8795 vixl::aarch32::Register expected) {
8796 DCHECK(kEmitCompilerReadBarrier);
8797 DCHECK(kUseBakerReadBarrier);
8798
8799 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with an ADD instead of LDR.
8800 uint32_t custom_data = EncodeBakerReadBarrierUnsafeCasData(old_value.GetCode());
8801
8802 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8803 size_t wide_instructions = /* ADR+CMP+ADD+BNE */ 4u - narrow_instructions;
8804 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8805 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8806 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8807 vixl32::Label return_address;
8808 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8809 __ cmp(mr, Operand(0));
8810 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8811 __ add(EncodingSize(Wide), old_value, adjusted_old_value, Operand(expected)); // Preserves flags.
8812 EmitBakerReadBarrierBne(custom_data);
8813 __ bind(&return_address);
8814 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8815 BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ADD_OFFSET);
8816}
8817
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008818void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8819 Location ref,
8820 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008821 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008822 bool needs_null_check) {
8823 DCHECK(kEmitCompilerReadBarrier);
8824 DCHECK(kUseBakerReadBarrier);
8825
Vladimir Marko008e09f32018-08-06 15:42:43 +01008826 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8827 // Marking Register) to decide whether we need to enter the slow
8828 // path to mark the reference. Then, in the slow path, check the
8829 // gray bit in the lock word of the reference's holder (`obj`) to
8830 // decide whether to mark `ref` or not.
8831 //
8832 // We use shared thunks for the slow path; shared within the method
8833 // for JIT, across methods for AOT. That thunk checks the holder
8834 // and jumps to the entrypoint if needed. If the holder is not gray,
8835 // it creates a fake dependency and returns to the LDR instruction.
8836 //
8837 // lr = &gray_return_address;
8838 // if (mr) { // Thread::Current()->GetIsGcMarking()
8839 // goto field_thunk<holder_reg, base_reg>(lr)
8840 // }
8841 // not_gray_return_address:
8842 // // Original reference load. If the offset is too large to fit
8843 // // into LDR, we use an adjusted base register here.
8844 // HeapReference<mirror::Object> reference = *(obj+offset);
8845 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008846
Vladimir Marko248141f2018-08-10 10:40:07 +01008847 DCHECK(src.GetAddrMode() == vixl32::Offset);
8848 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008849 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008850 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8851
Vladimir Marko008e09f32018-08-06 15:42:43 +01008852 UseScratchRegisterScope temps(GetVIXLAssembler());
8853 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008854 uint32_t custom_data =
8855 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008856
Vladimir Marko008e09f32018-08-06 15:42:43 +01008857 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008858 size_t narrow_instructions =
8859 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8860 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
8861 size_t wide_instructions =
8862 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8863 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8864 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8865 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008866 vixl32::Label return_address;
8867 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8868 __ cmp(mr, Operand(0));
8869 EmitBakerReadBarrierBne(custom_data);
8870 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01008871 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008872 if (needs_null_check) {
8873 MaybeRecordImplicitNullCheck(instruction);
8874 }
8875 // Note: We need a specific width for the unpoisoning NEG.
8876 if (kPoisonHeapReferences) {
8877 if (narrow) {
8878 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8879 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8880 } else {
8881 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8882 }
8883 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008884 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008885 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8886 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8887 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
8888 }
Andra Danciua0130e82020-07-23 12:34:56 +00008889 MaybeGenerateMarkingRegisterCheck(/* code= */ 21, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008890}
8891
Vladimir Marko248141f2018-08-10 10:40:07 +01008892void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8893 Location ref,
8894 vixl32::Register obj,
8895 uint32_t offset,
Vladimir Marko01b65522020-10-28 15:43:54 +00008896 Location maybe_temp,
Vladimir Marko248141f2018-08-10 10:40:07 +01008897 bool needs_null_check) {
8898 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8899 vixl32::Register base = obj;
8900 if (offset >= kReferenceLoadMinFarOffset) {
Vladimir Marko01b65522020-10-28 15:43:54 +00008901 base = RegisterFrom(maybe_temp);
Vladimir Marko248141f2018-08-10 10:40:07 +01008902 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8903 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8904 offset &= (kReferenceLoadMinFarOffset - 1u);
8905 }
8906 GenerateFieldLoadWithBakerReadBarrier(
8907 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
8908}
8909
Vladimir Marko008e09f32018-08-06 15:42:43 +01008910void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008911 vixl32::Register obj,
8912 uint32_t data_offset,
8913 Location index,
8914 Location temp,
8915 bool needs_null_check) {
8916 DCHECK(kEmitCompilerReadBarrier);
8917 DCHECK(kUseBakerReadBarrier);
8918
8919 static_assert(
8920 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8921 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008922 ScaleFactor scale_factor = TIMES_4;
8923
Vladimir Marko008e09f32018-08-06 15:42:43 +01008924 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8925 // Marking Register) to decide whether we need to enter the slow
8926 // path to mark the reference. Then, in the slow path, check the
8927 // gray bit in the lock word of the reference's holder (`obj`) to
8928 // decide whether to mark `ref` or not.
8929 //
8930 // We use shared thunks for the slow path; shared within the method
8931 // for JIT, across methods for AOT. That thunk checks the holder
8932 // and jumps to the entrypoint if needed. If the holder is not gray,
8933 // it creates a fake dependency and returns to the LDR instruction.
8934 //
8935 // lr = &gray_return_address;
8936 // if (mr) { // Thread::Current()->GetIsGcMarking()
8937 // goto array_thunk<base_reg>(lr)
8938 // }
8939 // not_gray_return_address:
8940 // // Original reference load. If the offset is too large to fit
8941 // // into LDR, we use an adjusted base register here.
8942 // HeapReference<mirror::Object> reference = data[index];
8943 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008944
Vladimir Marko008e09f32018-08-06 15:42:43 +01008945 DCHECK(index.IsValid());
8946 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8947 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8948 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008949
Vladimir Marko008e09f32018-08-06 15:42:43 +01008950 UseScratchRegisterScope temps(GetVIXLAssembler());
8951 temps.Exclude(ip);
8952 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008953
Vladimir Marko008e09f32018-08-06 15:42:43 +01008954 __ Add(data_reg, obj, Operand(data_offset));
8955 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008956 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8957 size_t wide_instructions =
8958 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8959 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8960 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8961 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008962 vixl32::Label return_address;
8963 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8964 __ cmp(mr, Operand(0));
8965 EmitBakerReadBarrierBne(custom_data);
8966 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8967 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8968 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8969 // Note: We need a Wide NEG for the unpoisoning.
8970 if (kPoisonHeapReferences) {
8971 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008972 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008973 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008974 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8975 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008976 }
Andra Danciua0130e82020-07-23 12:34:56 +00008977 MaybeGenerateMarkingRegisterCheck(/* code= */ 22, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008978}
8979
Roland Levillain5daa4952017-07-03 17:23:56 +01008980void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8981 // The following condition is a compile-time one, so it does not have a run-time cost.
8982 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8983 // The following condition is a run-time one; it is executed after the
8984 // previous compile-time test, to avoid penalizing non-debug builds.
8985 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8986 UseScratchRegisterScope temps(GetVIXLAssembler());
8987 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8988 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8989 kMarkingRegisterCheckBreakCodeBaseCode + code);
8990 }
8991 }
8992}
8993
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008994void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8995 Location out,
8996 Location ref,
8997 Location obj,
8998 uint32_t offset,
8999 Location index) {
9000 DCHECK(kEmitCompilerReadBarrier);
9001
9002 // Insert a slow path based read barrier *after* the reference load.
9003 //
9004 // If heap poisoning is enabled, the unpoisoning of the loaded
9005 // reference will be carried out by the runtime within the slow
9006 // path.
9007 //
9008 // Note that `ref` currently does not get unpoisoned (when heap
9009 // poisoning is enabled), which is alright as the `ref` argument is
9010 // not used by the artReadBarrierSlow entry point.
9011 //
9012 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01009013 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009014 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9015 AddSlowPath(slow_path);
9016
9017 __ B(slow_path->GetEntryLabel());
9018 __ Bind(slow_path->GetExitLabel());
9019}
9020
9021void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009022 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009023 Location ref,
9024 Location obj,
9025 uint32_t offset,
9026 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009027 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009028 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009029 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009030 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009031 // If heap poisoning is enabled, unpoisoning will be taken care of
9032 // by the runtime within the slow path.
9033 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009034 } else if (kPoisonHeapReferences) {
9035 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9036 }
9037}
9038
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009039void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9040 Location out,
9041 Location root) {
9042 DCHECK(kEmitCompilerReadBarrier);
9043
9044 // Insert a slow path based read barrier *after* the GC root load.
9045 //
9046 // Note that GC roots are not affected by heap poisoning, so we do
9047 // not need to do anything special for this here.
9048 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009049 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009050 AddSlowPath(slow_path);
9051
9052 __ B(slow_path->GetEntryLabel());
9053 __ Bind(slow_path->GetExitLabel());
9054}
9055
Artem Serov02d37832016-10-25 15:25:33 +01009056// Check if the desired_dispatch_info is supported. If it is, return it,
9057// otherwise return a fall-back info that should be used instead.
9058HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009059 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Vladimir Marko86c87522020-05-11 16:55:55 +01009060 ArtMethod* method) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009061 if (method->IsIntrinsic() &&
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009062 desired_dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009063 // As a work-around for soft-float native ABI interfering with type checks, we are
9064 // inserting fake calls to Float.floatToRawIntBits() or Double.doubleToRawLongBits()
9065 // when a float or double argument is passed in core registers but we cannot do that
9066 // for actual intrinsic implementations that expect them in FP registers. Therefore
9067 // we do not use `kCallCriticalNative` for intrinsics with FP arguments; if they are
9068 // properly intrinsified, the dispatch type does not matter anyway.
Vladimir Marko86c87522020-05-11 16:55:55 +01009069 ScopedObjectAccess soa(Thread::Current());
9070 uint32_t shorty_len;
9071 const char* shorty = method->GetShorty(&shorty_len);
Vladimir Marko86c87522020-05-11 16:55:55 +01009072 for (uint32_t i = 1; i != shorty_len; ++i) {
Vladimir Marko86c87522020-05-11 16:55:55 +01009073 if (shorty[i] == 'D' || shorty[i] == 'F') {
9074 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009075 dispatch_info.code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Marko86c87522020-05-11 16:55:55 +01009076 return dispatch_info;
9077 }
Vladimir Marko86c87522020-05-11 16:55:55 +01009078 }
9079 }
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009080 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009081}
9082
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009083
9084void CodeGeneratorARMVIXL::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
9085 switch (load_kind) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009086 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01009087 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009088 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01009089 vixl32::Register temp_reg = RegisterFrom(temp);
9090 EmitMovwMovtPlaceholder(labels, temp_reg);
9091 break;
9092 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009093 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009094 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00009095 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
9096 vixl32::Register temp_reg = RegisterFrom(temp);
9097 EmitMovwMovtPlaceholder(labels, temp_reg);
9098 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
9099 break;
9100 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009101 case MethodLoadKind::kBssEntry: {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009102 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009103 vixl32::Register temp_reg = RegisterFrom(temp);
9104 EmitMovwMovtPlaceholder(labels, temp_reg);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01009105 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009106 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009107 break;
9108 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009109 case MethodLoadKind::kJitDirectAddress: {
9110 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetResolvedMethod()));
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009111 break;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009112 }
9113 case MethodLoadKind::kRuntimeCall: {
9114 // Test situation, don't do anything.
9115 break;
9116 }
9117 default: {
9118 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
9119 UNREACHABLE();
9120 }
9121 }
9122}
9123
9124void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
9125 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
9126 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
9127 switch (invoke->GetMethodLoadKind()) {
9128 case MethodLoadKind::kStringInit: {
9129 uint32_t offset =
9130 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9131 // temp = thread->string_init_entrypoint
9132 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9133 break;
9134 }
9135 case MethodLoadKind::kRecursive: {
9136 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
9137 break;
9138 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009139 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009140 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9141 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009142 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01009143 case MethodLoadKind::kBootImageLinkTimePcRelative:
9144 // Note: Unlike arm64, x86 and x86-64, we do not avoid the materialization of method
9145 // pointer for kCallCriticalNative because it would not save us an instruction from
9146 // the current sequence MOVW+MOVT+ADD(pc)+LDR+BL. The ADD(pc) separates the patched
9147 // offset instructions MOVW+MOVT from the entrypoint load, so they cannot be fused.
9148 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01009149 default: {
9150 LoadMethod(invoke->GetMethodLoadKind(), temp, invoke);
9151 break;
9152 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009153 }
9154
Vladimir Marko86c87522020-05-11 16:55:55 +01009155 auto call_code_pointer_member = [&](MemberOffset offset) {
9156 // LR = callee_method->member;
9157 GetAssembler()->LoadFromOffset(kLoadWord, lr, RegisterFrom(callee_method), offset.Int32Value());
9158 {
9159 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9160 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9161 ExactAssemblyScope aas(GetVIXLAssembler(),
9162 vixl32::k16BitT32InstructionSizeInBytes,
9163 CodeBufferCheckScope::kExactSize);
9164 // LR()
9165 __ blx(lr);
9166 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9167 }
9168 };
Artem Serovd4cc5b22016-11-04 11:19:09 +00009169 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009170 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009171 {
9172 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9173 ExactAssemblyScope aas(GetVIXLAssembler(),
9174 vixl32::k32BitT32InstructionSizeInBytes,
9175 CodeBufferCheckScope::kMaximumSize);
9176 __ bl(GetFrameEntryLabel());
9177 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9178 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009179 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009180 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01009181 size_t out_frame_size =
9182 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARMVIXL,
9183 kAapcsStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01009184 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01009185 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArmPointerSize));
9186 // Move the result when needed due to native and managed ABI mismatch.
9187 switch (invoke->GetType()) {
9188 case DataType::Type::kFloat32:
9189 __ Vmov(s0, r0);
9190 break;
9191 case DataType::Type::kFloat64:
9192 __ Vmov(d0, r0, r1);
9193 break;
9194 case DataType::Type::kBool:
9195 case DataType::Type::kInt8:
9196 case DataType::Type::kUint16:
9197 case DataType::Type::kInt16:
9198 case DataType::Type::kInt32:
9199 case DataType::Type::kInt64:
9200 case DataType::Type::kVoid:
9201 break;
9202 default:
9203 DCHECK(false) << invoke->GetType();
9204 break;
9205 }
9206 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01009207 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01009208 }
9209 break;
9210 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009211 case CodePtrLocation::kCallArtMethod:
Vladimir Marko86c87522020-05-11 16:55:55 +01009212 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009213 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009214 }
9215
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009216 DCHECK(!IsLeafMethod());
9217}
9218
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009219void CodeGeneratorARMVIXL::GenerateVirtualCall(
9220 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009221 vixl32::Register temp = RegisterFrom(temp_location);
9222 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9223 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9224
9225 // Use the calling convention instead of the location of the receiver, as
9226 // intrinsics may have put the receiver in a different register. In the intrinsics
9227 // slow path, the arguments have been moved to the right place, so here we are
9228 // guaranteed that the receiver is the first register of the calling convention.
9229 InvokeDexCallingConventionARMVIXL calling_convention;
9230 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9231 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009232 {
9233 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009234 ExactAssemblyScope aas(GetVIXLAssembler(),
9235 vixl32::kMaxInstructionSizeInBytes,
9236 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009237 // /* HeapReference<Class> */ temp = receiver->klass_
9238 __ ldr(temp, MemOperand(receiver, class_offset));
9239 MaybeRecordImplicitNullCheck(invoke);
9240 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009241 // Instead of simply (possibly) unpoisoning `temp` here, we should
9242 // emit a read barrier for the previous class reference load.
9243 // However this is not required in practice, as this is an
9244 // intermediate/temporary reference and because the current
9245 // concurrent copying collector keeps the from-space memory
9246 // intact/accessible until the end of the marking phase (the
9247 // concurrent copying collector may not in the future).
9248 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9249
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00009250 // If we're compiling baseline, update the inline cache.
9251 MaybeGenerateInlineCacheCheck(invoke, temp);
9252
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009253 // temp = temp->GetMethodAt(method_offset);
9254 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9255 kArmPointerSize).Int32Value();
9256 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9257 // LR = temp->GetEntryPoint();
9258 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009259 {
9260 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9261 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9262 ExactAssemblyScope aas(GetVIXLAssembler(),
9263 vixl32::k16BitT32InstructionSizeInBytes,
9264 CodeBufferCheckScope::kExactSize);
9265 // LR();
9266 __ blx(lr);
9267 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9268 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009269}
9270
Vladimir Marko6fd16062018-06-26 11:02:04 +01009271CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
9272 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009273 return NewPcRelativePatch(/* dex_file= */ nullptr, intrinsic_data, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009274}
9275
Vladimir Markob066d432018-01-03 13:14:37 +00009276CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9277 uint32_t boot_image_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08009278 return NewPcRelativePatch(/* dex_file= */ nullptr,
Vladimir Markob066d432018-01-03 13:14:37 +00009279 boot_image_offset,
Vladimir Marko2d06e022019-07-08 15:45:19 +01009280 &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00009281}
9282
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009283CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009284 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009285 return NewPcRelativePatch(
9286 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009287}
9288
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009289CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9290 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009291 return NewPcRelativePatch(
9292 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009293}
9294
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009295CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009296 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009297 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009298}
9299
Vladimir Marko1998cd02017-01-13 13:02:58 +00009300CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01009301 HLoadClass* load_class) {
9302 const DexFile& dex_file = load_class->GetDexFile();
9303 dex::TypeIndex type_index = load_class->GetTypeIndex();
9304 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
9305 switch (load_class->GetLoadKind()) {
9306 case HLoadClass::LoadKind::kBssEntry:
9307 patches = &type_bss_entry_patches_;
9308 break;
9309 case HLoadClass::LoadKind::kBssEntryPublic:
9310 patches = &public_type_bss_entry_patches_;
9311 break;
9312 case HLoadClass::LoadKind::kBssEntryPackage:
9313 patches = &package_type_bss_entry_patches_;
9314 break;
9315 default:
9316 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
9317 UNREACHABLE();
9318 }
Vladimir Markobaade402020-09-30 14:45:39 +00009319 return NewPcRelativePatch(&dex_file, type_index.index_, patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009320}
9321
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009322CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009323 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009324 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009325}
9326
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009327CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9328 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009329 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009330}
9331
Artem Serovd4cc5b22016-11-04 11:19:09 +00009332CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009333 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009334 patches->emplace_back(dex_file, offset_or_index);
9335 return &patches->back();
9336}
9337
Vladimir Markof6675082019-05-17 12:05:28 +01009338void CodeGeneratorARMVIXL::EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset) {
9339 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009340 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01009341 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
9342 vixl::aarch32::Label* bl_label = &call_entrypoint_patches_.back().label;
9343 __ bind(bl_label);
9344 vixl32::Label placeholder_label;
9345 __ bl(&placeholder_label); // Placeholder, patched at link-time.
9346 __ bind(&placeholder_label);
9347}
9348
Vladimir Marko966b46f2018-08-03 10:20:19 +00009349void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00009350 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009351 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00009352 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
9353 vixl::aarch32::Label* slow_path_entry = &it->second.label;
9354 __ b(ne, EncodingSize(Wide), slow_path_entry);
9355 } else {
9356 baker_read_barrier_patches_.emplace_back(custom_data);
9357 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
9358 __ bind(patch_label);
9359 vixl32::Label placeholder_label;
9360 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
9361 __ bind(&placeholder_label);
9362 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009363}
9364
Artem Serovc5fcb442016-12-02 19:19:58 +00009365VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009366 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009367}
9368
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009369VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9370 const DexFile& dex_file,
9371 dex::StringIndex string_index,
9372 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009373 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009374 return jit_string_patches_.GetOrCreate(
9375 StringReference(&dex_file, string_index),
9376 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009377 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009378 });
9379}
9380
9381VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9382 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009383 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009384 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009385 return jit_class_patches_.GetOrCreate(
9386 TypeReference(&dex_file, type_index),
9387 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009388 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009389 });
9390}
9391
Vladimir Marko6fd16062018-06-26 11:02:04 +01009392void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
9393 uint32_t boot_image_reference) {
9394 if (GetCompilerOptions().IsBootImage()) {
9395 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9396 NewBootImageIntrinsicPatch(boot_image_reference);
9397 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01009398 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009399 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9400 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009401 EmitMovwMovtPlaceholder(labels, reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08009402 __ Ldr(reg, MemOperand(reg, /* offset= */ 0));
Vladimir Markoeebb8212018-06-05 14:57:24 +01009403 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01009404 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01009405 gc::Heap* heap = Runtime::Current()->GetHeap();
9406 DCHECK(!heap->GetBootImageSpaces().empty());
9407 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01009408 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009409 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
9410 }
9411}
9412
Vladimir Markode91ca92020-10-27 13:41:40 +00009413void CodeGeneratorARMVIXL::LoadIntrinsicDeclaringClass(vixl32::Register reg, HInvoke* invoke) {
9414 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009415 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009416 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009417 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01009418 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
9419 PcRelativePatchInfo* labels = NewBootImageTypePatch(*target_method.dex_file, type_idx);
Vladimir Markode91ca92020-10-27 13:41:40 +00009420 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009421 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00009422 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
9423 LoadBootImageAddress(reg, boot_image_offset);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009424 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01009425}
9426
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009427template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009428inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9429 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009430 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009431 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009432 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009433 size_t offset_or_index = info.offset_or_index;
9434 DCHECK(info.add_pc_label.IsBound());
9435 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9436 // Add MOVW patch.
9437 DCHECK(info.movw_label.IsBound());
9438 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009439 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009440 // Add MOVT patch.
9441 DCHECK(info.movt_label.IsBound());
9442 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009443 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009444 }
9445}
9446
Vladimir Marko6fd16062018-06-26 11:02:04 +01009447template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
9448linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
9449 const DexFile* target_dex_file,
9450 uint32_t pc_insn_offset,
9451 uint32_t boot_image_offset) {
9452 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
9453 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00009454}
9455
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009456void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009457 DCHECK(linker_patches->empty());
9458 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009459 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009460 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009461 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009462 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01009463 /* MOVW+MOVT for each entry */ 2u * public_type_bss_entry_patches_.size() +
9464 /* MOVW+MOVT for each entry */ 2u * package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009465 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009466 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01009467 /* MOVW+MOVT for each entry */ 2u * boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01009468 call_entrypoint_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009469 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009470 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01009471 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009472 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009473 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009474 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009475 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009476 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009477 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009478 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009479 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009480 DCHECK(boot_image_type_patches_.empty());
9481 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01009482 }
9483 if (GetCompilerOptions().IsBootImage()) {
9484 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
9485 boot_image_other_patches_, linker_patches);
9486 } else {
9487 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
9488 boot_image_other_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009489 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009490 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9491 method_bss_entry_patches_, linker_patches);
9492 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9493 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01009494 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
9495 public_type_bss_entry_patches_, linker_patches);
9496 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
9497 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009498 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9499 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01009500 for (const PatchInfo<vixl32::Label>& info : call_entrypoint_patches_) {
9501 DCHECK(info.target_dex_file == nullptr);
9502 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
9503 info.label.GetLocation(), info.offset_or_index));
9504 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009505 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009506 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9507 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009508 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009509 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009510}
9511
Vladimir Markoca1e0382018-04-11 09:58:41 +00009512bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01009513 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
9514 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00009515 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9516}
9517
9518void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9519 /*out*/ ArenaVector<uint8_t>* code,
9520 /*out*/ std::string* debug_name) {
9521 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9522 switch (patch.GetType()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009523 case linker::LinkerPatch::Type::kCallRelative: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009524 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9525 // to the generic JNI and interpreter trampolines.
Vladimir Markof6675082019-05-17 12:05:28 +01009526 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
9527 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, vixl32::r0, offset.Int32Value());
Vladimir Markoca1e0382018-04-11 09:58:41 +00009528 assembler.GetVIXLAssembler()->Bkpt(0);
9529 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9530 *debug_name = "MethodCallThunk";
9531 }
9532 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009533 }
9534 case linker::LinkerPatch::Type::kCallEntrypoint: {
9535 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, tr, patch.EntrypointOffset());
9536 assembler.GetVIXLAssembler()->Bkpt(0);
9537 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9538 *debug_name = "EntrypointCallThunk_" + std::to_string(patch.EntrypointOffset());
9539 }
9540 break;
9541 }
9542 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009543 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9544 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9545 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009546 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009547 default:
9548 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9549 UNREACHABLE();
9550 }
9551
9552 // Ensure we emit the literal pool if any.
9553 assembler.FinalizeCode();
9554 code->resize(assembler.CodeSize());
9555 MemoryRegion code_region(code->data(), code->size());
9556 assembler.FinalizeInstructions(code_region);
9557}
9558
Artem Serovc5fcb442016-12-02 19:19:58 +00009559VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9560 uint32_t value,
9561 Uint32ToLiteralMap* map) {
9562 return map->GetOrCreate(
9563 value,
9564 [this, value]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009565 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ value);
Artem Serovc5fcb442016-12-02 19:19:58 +00009566 });
9567}
9568
Artem Serov2bbc9532016-10-21 11:51:50 +01009569void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9570 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009571 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009572 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9573 Location::RequiresRegister());
9574 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9575 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9577}
9578
9579void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9580 vixl32::Register res = OutputRegister(instr);
9581 vixl32::Register accumulator =
9582 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9583 vixl32::Register mul_left =
9584 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9585 vixl32::Register mul_right =
9586 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9587
9588 if (instr->GetOpKind() == HInstruction::kAdd) {
9589 __ Mla(res, mul_left, mul_right, accumulator);
9590 } else {
9591 __ Mls(res, mul_left, mul_right, accumulator);
9592 }
9593}
9594
Artem Serov551b28f2016-10-18 19:11:30 +01009595void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9596 // Nothing to do, this should be removed during prepare for register allocator.
9597 LOG(FATAL) << "Unreachable";
9598}
9599
9600void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9601 // Nothing to do, this should be removed during prepare for register allocator.
9602 LOG(FATAL) << "Unreachable";
9603}
9604
9605// Simple implementation of packed switch - generate cascaded compare/jumps.
9606void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9607 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009608 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009609 locations->SetInAt(0, Location::RequiresRegister());
9610 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9611 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9612 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9613 if (switch_instr->GetStartValue() != 0) {
9614 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9615 }
9616 }
9617}
9618
9619// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9620void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9621 int32_t lower_bound = switch_instr->GetStartValue();
9622 uint32_t num_entries = switch_instr->GetNumEntries();
9623 LocationSummary* locations = switch_instr->GetLocations();
9624 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9625 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9626
9627 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9628 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9629 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009630 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009631 vixl32::Register temp_reg = temps.Acquire();
9632 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9633 // the immediate, because IP is used as the destination register. For the other
9634 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9635 // and they can be encoded in the instruction without making use of IP register.
9636 __ Adds(temp_reg, value_reg, -lower_bound);
9637
9638 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9639 // Jump to successors[0] if value == lower_bound.
9640 __ B(eq, codegen_->GetLabelOf(successors[0]));
9641 int32_t last_index = 0;
9642 for (; num_entries - last_index > 2; last_index += 2) {
9643 __ Adds(temp_reg, temp_reg, -2);
9644 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9645 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9646 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9647 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9648 }
9649 if (num_entries - last_index == 2) {
9650 // The last missing case_value.
9651 __ Cmp(temp_reg, 1);
9652 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9653 }
9654
9655 // And the default for any other value.
9656 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9657 __ B(codegen_->GetLabelOf(default_block));
9658 }
9659 } else {
9660 // Create a table lookup.
9661 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9662
9663 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9664
9665 // Remove the bias.
9666 vixl32::Register key_reg;
9667 if (lower_bound != 0) {
9668 key_reg = RegisterFrom(locations->GetTemp(1));
9669 __ Sub(key_reg, value_reg, lower_bound);
9670 } else {
9671 key_reg = value_reg;
9672 }
9673
9674 // Check whether the value is in the table, jump to default block if not.
9675 __ Cmp(key_reg, num_entries - 1);
9676 __ B(hi, codegen_->GetLabelOf(default_block));
9677
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009678 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009679 vixl32::Register jump_offset = temps.Acquire();
9680
9681 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009682 {
9683 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9684 ExactAssemblyScope aas(GetVIXLAssembler(),
9685 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9686 CodeBufferCheckScope::kMaximumSize);
9687 __ adr(table_base, jump_table->GetTableStartLabel());
9688 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009689
Scott Wakeling86e9d262017-01-18 15:59:24 +00009690 // Jump to target block by branching to table_base(pc related) + offset.
9691 vixl32::Register target_address = table_base;
9692 __ add(target_address, table_base, jump_offset);
9693 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009694
Scott Wakeling86e9d262017-01-18 15:59:24 +00009695 jump_table->EmitTable(codegen_);
9696 }
Artem Serov551b28f2016-10-18 19:11:30 +01009697 }
9698}
9699
Artem Serov02d37832016-10-25 15:25:33 +01009700// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009701void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009702 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009703 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009704 return;
9705 }
9706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009707 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009708
Artem Serovd4cc5b22016-11-04 11:19:09 +00009709 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009710 if (return_loc.Equals(trg)) {
9711 return;
9712 }
9713
9714 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9715 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009716 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009717 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009718 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009719 TODO_VIXL32(FATAL);
9720 } else {
9721 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009722 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009723 parallel_move.AddMove(return_loc, trg, type, nullptr);
9724 GetMoveResolver()->EmitNativeCode(&parallel_move);
9725 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009726}
Scott Wakelingfe885462016-09-22 10:24:38 +01009727
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009728void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9729 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009730 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009731 locations->SetInAt(0, Location::RequiresRegister());
9732 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009733}
9734
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009735void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9736 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9737 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9738 instruction->GetIndex(), kArmPointerSize).SizeValue();
9739 GetAssembler()->LoadFromOffset(kLoadWord,
9740 OutputRegister(instruction),
9741 InputRegisterAt(instruction, 0),
9742 method_offset);
9743 } else {
9744 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9745 instruction->GetIndex(), kArmPointerSize));
9746 GetAssembler()->LoadFromOffset(kLoadWord,
9747 OutputRegister(instruction),
9748 InputRegisterAt(instruction, 0),
9749 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9750 GetAssembler()->LoadFromOffset(kLoadWord,
9751 OutputRegister(instruction),
9752 OutputRegister(instruction),
9753 method_offset);
9754 }
Artem Serov551b28f2016-10-18 19:11:30 +01009755}
9756
Artem Serovc5fcb442016-12-02 19:19:58 +00009757static void PatchJitRootUse(uint8_t* code,
9758 const uint8_t* roots_data,
9759 VIXLUInt32Literal* literal,
9760 uint64_t index_in_table) {
9761 DCHECK(literal->IsBound());
9762 uint32_t literal_offset = literal->GetLocation();
9763 uintptr_t address =
9764 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9765 uint8_t* data = code + literal_offset;
9766 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9767}
9768
9769void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9770 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009771 const StringReference& string_reference = entry.first;
9772 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009773 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009774 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009775 }
9776 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009777 const TypeReference& type_reference = entry.first;
9778 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009779 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009780 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009781 }
9782}
9783
Artem Serovd4cc5b22016-11-04 11:19:09 +00009784void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9785 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9786 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009787 ExactAssemblyScope aas(GetVIXLAssembler(),
9788 3 * vixl32::kMaxInstructionSizeInBytes,
9789 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009790 // TODO(VIXL): Think about using mov instead of movw.
9791 __ bind(&labels->movw_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009792 __ movw(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009793 __ bind(&labels->movt_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009794 __ movt(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009795 __ bind(&labels->add_pc_label);
9796 __ add(out, out, pc);
9797}
9798
Scott Wakelingfe885462016-09-22 10:24:38 +01009799#undef __
9800#undef QUICK_ENTRY_POINT
9801#undef TODO_VIXL32
9802
Vladimir Markoca1e0382018-04-11 09:58:41 +00009803#define __ assembler.GetVIXLAssembler()->
9804
9805static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9806 vixl32::Register base_reg,
9807 vixl32::MemOperand& lock_word,
9808 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009809 int32_t raw_ldr_offset,
9810 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009811 // Load the lock word containing the rb_state.
9812 __ Ldr(ip, lock_word);
9813 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009814 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009815 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9816 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
Andreas Gampe3db70682018-12-26 15:12:03 -08009817 __ B(ne, slow_path, /* is_far_target= */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009818 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9819 if (throw_npe != nullptr) {
9820 __ Bind(throw_npe);
9821 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009822 __ Add(lr, lr, raw_ldr_offset);
9823 // Introduce a dependency on the lock_word including rb_state,
9824 // to prevent load-load reordering, and without using
9825 // a memory barrier (which would be more expensive).
9826 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9827 __ Bx(lr); // And return back to the function.
9828 // Note: The fake dependency is unnecessary for the slow path.
9829}
9830
9831// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009832static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009833 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009834 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9835 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009836 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9837 DCHECK_EQ(ip.GetCode(), 12u);
9838 const int32_t entry_point_offset =
9839 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9840 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +01009841 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009842}
9843
9844void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9845 uint32_t encoded_data,
9846 /*out*/ std::string* debug_name) {
9847 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9848 switch (kind) {
9849 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009850 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9851 CheckValidReg(base_reg.GetCode());
9852 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9853 CheckValidReg(holder_reg.GetCode());
9854 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9855 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9856 temps.Exclude(ip);
Roland Levillain988c3912019-09-25 19:33:35 +01009857 // In the case of a field load, if `base_reg` differs from
9858 // `holder_reg`, the offset was too large and we must have emitted (during the construction
9859 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
9860 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
9861 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
9862 // not necessarily do that check before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01009863 vixl32::Label throw_npe_label;
9864 vixl32::Label* throw_npe = nullptr;
9865 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9866 throw_npe = &throw_npe_label;
Andreas Gampe3db70682018-12-26 15:12:03 -08009867 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009868 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009869 // Check if the holder is gray and, if not, add fake dependency to the base register
9870 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9871 // to load the reference and call the entrypoint that performs further checks on the
9872 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009873 vixl32::Label slow_path;
9874 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9875 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9876 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9877 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009878 EmitGrayCheckAndFastPath(
9879 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009880 __ Bind(&slow_path);
9881 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9882 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +01009883 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009884 if (width == BakerReadBarrierWidth::kWide) {
9885 MemOperand ldr_half_address(lr, ldr_offset + 2);
9886 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9887 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9888 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9889 } else {
9890 MemOperand ldr_address(lr, ldr_offset);
9891 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9892 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9893 ep_reg, // for narrow LDR.
9894 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9895 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9896 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9897 }
9898 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9899 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009900 break;
9901 }
9902 case BakerReadBarrierKind::kArray: {
9903 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9904 CheckValidReg(base_reg.GetCode());
9905 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9906 BakerReadBarrierSecondRegField::Decode(encoded_data));
9907 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9908 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9909 temps.Exclude(ip);
9910 vixl32::Label slow_path;
9911 int32_t data_offset =
9912 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
9913 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
9914 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
9915 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
9916 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
9917 __ Bind(&slow_path);
9918 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9919 raw_ldr_offset;
9920 MemOperand ldr_address(lr, ldr_offset + 2);
9921 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
9922 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +01009923 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009924 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
9925 // a switch case target based on the index register.
9926 __ Mov(ip, base_reg); // Move the base register to ip0.
9927 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
9928 break;
9929 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009930 case BakerReadBarrierKind::kGcRoot:
9931 case BakerReadBarrierKind::kUnsafeCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009932 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
9933 // and it does not have a forwarding address), call the correct introspection entrypoint;
9934 // otherwise return the reference (or the extracted forwarding address).
9935 // There is no gray bit check for GC roots.
9936 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9937 CheckValidReg(root_reg.GetCode());
9938 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9939 BakerReadBarrierSecondRegField::Decode(encoded_data));
9940 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9941 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9942 temps.Exclude(ip);
9943 vixl32::Label return_label, not_marked, forwarding_address;
Andreas Gampe3db70682018-12-26 15:12:03 -08009944 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009945 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
9946 __ Ldr(ip, lock_word);
9947 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
9948 __ B(eq, &not_marked);
9949 __ Bind(&return_label);
9950 __ Bx(lr);
9951 __ Bind(&not_marked);
9952 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
9953 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
9954 " the highest bits and the 'forwarding address' state to have all bits set");
9955 __ Cmp(ip, Operand(0xc0000000));
9956 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +01009957 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009958 // Adjust the art_quick_read_barrier_mark_introspection address in kBakerCcEntrypointRegister
Vladimir Markod887ed82018-08-14 13:52:12 +00009959 // to one of art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},unsafe_cas}.
9960 DCHECK(kind != BakerReadBarrierKind::kUnsafeCas || width == BakerReadBarrierWidth::kWide);
9961 int32_t entrypoint_offset =
9962 (kind == BakerReadBarrierKind::kGcRoot)
9963 ? (width == BakerReadBarrierWidth::kWide)
9964 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
9965 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
9966 : BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009967 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
9968 __ Mov(ip, root_reg);
9969 __ Bx(ep_reg);
9970 __ Bind(&forwarding_address);
9971 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
9972 __ Bx(lr);
9973 break;
9974 }
9975 default:
9976 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
9977 UNREACHABLE();
9978 }
9979
Vladimir Marko966b46f2018-08-03 10:20:19 +00009980 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01009981 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01009982 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00009983 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009984 std::ostringstream oss;
9985 oss << "BakerReadBarrierThunk";
9986 switch (kind) {
9987 case BakerReadBarrierKind::kField:
9988 oss << "Field";
9989 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9990 oss << "Wide";
9991 }
9992 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
9993 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
9994 break;
9995 case BakerReadBarrierKind::kArray:
9996 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9997 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9998 BakerReadBarrierSecondRegField::Decode(encoded_data));
9999 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10000 break;
10001 case BakerReadBarrierKind::kGcRoot:
10002 oss << "GcRoot";
10003 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
10004 oss << "Wide";
10005 }
10006 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10007 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10008 BakerReadBarrierSecondRegField::Decode(encoded_data));
10009 break;
Vladimir Markod887ed82018-08-14 13:52:12 +000010010 case BakerReadBarrierKind::kUnsafeCas:
10011 oss << "UnsafeCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
10012 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
10013 BakerReadBarrierSecondRegField::Decode(encoded_data));
10014 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
10015 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +000010016 }
10017 *debug_name = oss.str();
10018 }
10019}
10020
10021#undef __
10022
Scott Wakelingfe885462016-09-22 10:24:38 +010010023} // namespace arm
10024} // namespace art