blob: 88551e090945465260547edfc18dc1044608d039 [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 Marko9d479252018-07-24 11:35:20 +0100533 arm_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
534 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100535 // If we also must_do_clinit, the resolved type is now in the correct register.
536 } else {
537 DCHECK(must_do_clinit);
538 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
539 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), source);
540 }
541 if (must_do_clinit) {
542 arm_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
543 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100544 }
545
546 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100547 if (out.IsValid()) {
548 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
549 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
550 }
551 RestoreLiveRegisters(codegen, locations);
552 __ B(GetExitLabel());
553 }
554
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100555 const char* GetDescription() const override { return "LoadClassSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100556
557 private:
558 // The class this slow path will load.
559 HLoadClass* const cls_;
560
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100561 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
562};
563
Artem Serovd4cc5b22016-11-04 11:19:09 +0000564class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
565 public:
566 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
567 : SlowPathCodeARMVIXL(instruction) {}
568
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100569 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000570 DCHECK(instruction_->IsLoadString());
571 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000572 LocationSummary* locations = instruction_->GetLocations();
573 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000574 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000575
576 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
577 __ Bind(GetEntryLabel());
578 SaveLiveRegisters(codegen, locations);
579
580 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000581 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000582 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
583 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
584
Artem Serovd4cc5b22016-11-04 11:19:09 +0000585 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
586 RestoreLiveRegisters(codegen, locations);
587
588 __ B(GetExitLabel());
589 }
590
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100591 const char* GetDescription() const override { return "LoadStringSlowPathARMVIXL"; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000592
593 private:
594 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
595};
596
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100597class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
598 public:
599 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
600 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100602 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100603 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100604 DCHECK(instruction_->IsCheckCast()
605 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
606
607 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
608 __ Bind(GetEntryLabel());
609
Vladimir Marko87584542017-12-12 17:47:52 +0000610 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100611 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100612 }
613
614 // We're moving two locations to locations that could overlap, so we need a parallel
615 // move resolver.
616 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100617
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800618 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800619 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100620 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800621 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800622 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100623 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100624 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100625 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
626 instruction_,
627 instruction_->GetDexPc(),
628 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800629 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100630 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100631 } else {
632 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800633 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
634 instruction_,
635 instruction_->GetDexPc(),
636 this);
637 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100638 }
639
640 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100641 RestoreLiveRegisters(codegen, locations);
642 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100643 }
644 }
645
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100646 const char* GetDescription() const override { return "TypeCheckSlowPathARMVIXL"; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100647
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100648 bool IsFatal() const override { return is_fatal_; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100649
650 private:
651 const bool is_fatal_;
652
653 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
654};
655
Scott Wakelingc34dba72016-10-03 10:14:44 +0100656class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
657 public:
658 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
659 : SlowPathCodeARMVIXL(instruction) {}
660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100661 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100662 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
663 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100664 LocationSummary* locations = instruction_->GetLocations();
665 SaveLiveRegisters(codegen, locations);
666 InvokeRuntimeCallingConventionARMVIXL calling_convention;
667 __ Mov(calling_convention.GetRegisterAt(0),
668 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
669
Scott Wakelingc34dba72016-10-03 10:14:44 +0100670 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100671 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100672 }
673
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100674 const char* GetDescription() const override { return "DeoptimizationSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100675
676 private:
677 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
678};
679
680class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
681 public:
682 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
683
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100684 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100685 LocationSummary* locations = instruction_->GetLocations();
686 __ Bind(GetEntryLabel());
687 SaveLiveRegisters(codegen, locations);
688
689 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100690 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100691 parallel_move.AddMove(
692 locations->InAt(0),
693 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100694 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100695 nullptr);
696 parallel_move.AddMove(
697 locations->InAt(1),
698 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100699 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100700 nullptr);
701 parallel_move.AddMove(
702 locations->InAt(2),
703 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100704 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100705 nullptr);
706 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
707
708 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
709 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
710 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
711 RestoreLiveRegisters(codegen, locations);
712 __ B(GetExitLabel());
713 }
714
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100715 const char* GetDescription() const override { return "ArraySetSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100716
717 private:
718 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
719};
720
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000721// Slow path generating a read barrier for a heap reference.
722class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
723 public:
724 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
725 Location out,
726 Location ref,
727 Location obj,
728 uint32_t offset,
729 Location index)
730 : SlowPathCodeARMVIXL(instruction),
731 out_(out),
732 ref_(ref),
733 obj_(obj),
734 offset_(offset),
735 index_(index) {
736 DCHECK(kEmitCompilerReadBarrier);
737 // If `obj` is equal to `out` or `ref`, it means the initial object
738 // has been overwritten by (or after) the heap object reference load
739 // to be instrumented, e.g.:
740 //
741 // __ LoadFromOffset(kLoadWord, out, out, offset);
742 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
743 //
744 // In that case, we have lost the information about the original
745 // object, and the emitted read barrier cannot work properly.
746 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
747 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
748 }
749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100750 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000751 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
752 LocationSummary* locations = instruction_->GetLocations();
753 vixl32::Register reg_out = RegisterFrom(out_);
754 DCHECK(locations->CanCall());
755 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
756 DCHECK(instruction_->IsInstanceFieldGet() ||
757 instruction_->IsStaticFieldGet() ||
758 instruction_->IsArrayGet() ||
759 instruction_->IsInstanceOf() ||
760 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700761 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000762 << "Unexpected instruction in read barrier for heap reference slow path: "
763 << instruction_->DebugName();
764 // The read barrier instrumentation of object ArrayGet
765 // instructions does not support the HIntermediateAddress
766 // instruction.
767 DCHECK(!(instruction_->IsArrayGet() &&
768 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
769
770 __ Bind(GetEntryLabel());
771 SaveLiveRegisters(codegen, locations);
772
773 // We may have to change the index's value, but as `index_` is a
774 // constant member (like other "inputs" of this slow path),
775 // introduce a copy of it, `index`.
776 Location index = index_;
777 if (index_.IsValid()) {
778 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
779 if (instruction_->IsArrayGet()) {
780 // Compute the actual memory offset and store it in `index`.
781 vixl32::Register index_reg = RegisterFrom(index_);
782 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
783 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
784 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +0100785 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
786 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000787 // not been saved by the previous call to
788 // art::SlowPathCode::SaveLiveRegisters, as it is a
789 // callee-save register --
790 // art::SlowPathCode::SaveLiveRegisters does not consider
791 // callee-save registers, as it has been designed with the
792 // assumption that callee-save registers are supposed to be
793 // handled by the called function. So, as a callee-save
794 // register, `index_reg` _would_ eventually be saved onto
795 // the stack, but it would be too late: we would have
796 // changed its value earlier. Therefore, we manually save
797 // it here into another freely available register,
798 // `free_reg`, chosen of course among the caller-save
799 // registers (as a callee-save `free_reg` register would
800 // exhibit the same problem).
801 //
802 // Note we could have requested a temporary register from
803 // the register allocator instead; but we prefer not to, as
804 // this is a slow path, and we know we can find a
805 // caller-save register that is available.
806 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
807 __ Mov(free_reg, index_reg);
808 index_reg = free_reg;
809 index = LocationFrom(index_reg);
810 } else {
811 // The initial register stored in `index_` has already been
812 // saved in the call to art::SlowPathCode::SaveLiveRegisters
813 // (as it is not a callee-save register), so we can freely
814 // use it.
815 }
816 // Shifting the index value contained in `index_reg` by the scale
817 // factor (2) cannot overflow in practice, as the runtime is
818 // unable to allocate object arrays with a size larger than
819 // 2^26 - 1 (that is, 2^28 - 4 bytes).
820 __ Lsl(index_reg, index_reg, TIMES_4);
821 static_assert(
822 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
823 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
824 __ Add(index_reg, index_reg, offset_);
825 } else {
826 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
827 // intrinsics, `index_` is not shifted by a scale factor of 2
828 // (as in the case of ArrayGet), as it is actually an offset
829 // to an object field within an object.
830 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
831 DCHECK(instruction_->GetLocations()->Intrinsified());
832 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
833 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
834 << instruction_->AsInvoke()->GetIntrinsic();
835 DCHECK_EQ(offset_, 0U);
836 DCHECK(index_.IsRegisterPair());
837 // UnsafeGet's offset location is a register pair, the low
838 // part contains the correct offset.
839 index = index_.ToLow();
840 }
841 }
842
843 // We're moving two or three locations to locations that could
844 // overlap, so we need a parallel move resolver.
845 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100846 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000847 parallel_move.AddMove(ref_,
848 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100849 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000850 nullptr);
851 parallel_move.AddMove(obj_,
852 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100853 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000854 nullptr);
855 if (index.IsValid()) {
856 parallel_move.AddMove(index,
857 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100858 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000859 nullptr);
860 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
861 } else {
862 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
863 __ Mov(calling_convention.GetRegisterAt(2), offset_);
864 }
865 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
866 CheckEntrypointTypes<
867 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
868 arm_codegen->Move32(out_, LocationFrom(r0));
869
870 RestoreLiveRegisters(codegen, locations);
871 __ B(GetExitLabel());
872 }
873
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100874 const char* GetDescription() const override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000875 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
876 }
877
878 private:
879 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
880 uint32_t ref = RegisterFrom(ref_).GetCode();
881 uint32_t obj = RegisterFrom(obj_).GetCode();
882 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
883 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
884 return vixl32::Register(i);
885 }
886 }
887 // We shall never fail to find a free caller-save register, as
888 // there are more than two core caller-save registers on ARM
889 // (meaning it is possible to find one which is different from
890 // `ref` and `obj`).
891 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
892 LOG(FATAL) << "Could not find a free caller-save register";
893 UNREACHABLE();
894 }
895
896 const Location out_;
897 const Location ref_;
898 const Location obj_;
899 const uint32_t offset_;
900 // An additional location containing an index to an array.
901 // Only used for HArrayGet and the UnsafeGetObject &
902 // UnsafeGetObjectVolatile intrinsics.
903 const Location index_;
904
905 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
906};
907
908// Slow path generating a read barrier for a GC root.
909class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
910 public:
911 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
912 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
913 DCHECK(kEmitCompilerReadBarrier);
914 }
915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100916 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000917 LocationSummary* locations = instruction_->GetLocations();
918 vixl32::Register reg_out = RegisterFrom(out_);
919 DCHECK(locations->CanCall());
920 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
921 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
922 << "Unexpected instruction in read barrier for GC root slow path: "
923 << instruction_->DebugName();
924
925 __ Bind(GetEntryLabel());
926 SaveLiveRegisters(codegen, locations);
927
928 InvokeRuntimeCallingConventionARMVIXL calling_convention;
929 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
930 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
931 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
932 instruction_,
933 instruction_->GetDexPc(),
934 this);
935 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
936 arm_codegen->Move32(out_, LocationFrom(r0));
937
938 RestoreLiveRegisters(codegen, locations);
939 __ B(GetExitLabel());
940 }
941
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100942 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARMVIXL"; }
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000943
944 private:
945 const Location out_;
946 const Location root_;
947
948 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
949};
Scott Wakelingc34dba72016-10-03 10:14:44 +0100950
Scott Wakelingfe885462016-09-22 10:24:38 +0100951inline vixl32::Condition ARMCondition(IfCondition cond) {
952 switch (cond) {
953 case kCondEQ: return eq;
954 case kCondNE: return ne;
955 case kCondLT: return lt;
956 case kCondLE: return le;
957 case kCondGT: return gt;
958 case kCondGE: return ge;
959 case kCondB: return lo;
960 case kCondBE: return ls;
961 case kCondA: return hi;
962 case kCondAE: return hs;
963 }
964 LOG(FATAL) << "Unreachable";
965 UNREACHABLE();
966}
967
968// Maps signed condition to unsigned condition.
969inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
970 switch (cond) {
971 case kCondEQ: return eq;
972 case kCondNE: return ne;
973 // Signed to unsigned.
974 case kCondLT: return lo;
975 case kCondLE: return ls;
976 case kCondGT: return hi;
977 case kCondGE: return hs;
978 // Unsigned remain unchanged.
979 case kCondB: return lo;
980 case kCondBE: return ls;
981 case kCondA: return hi;
982 case kCondAE: return hs;
983 }
984 LOG(FATAL) << "Unreachable";
985 UNREACHABLE();
986}
987
988inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
989 // The ARM condition codes can express all the necessary branches, see the
990 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
991 // There is no dex instruction or HIR that would need the missing conditions
992 // "equal or unordered" or "not equal".
993 switch (cond) {
994 case kCondEQ: return eq;
995 case kCondNE: return ne /* unordered */;
996 case kCondLT: return gt_bias ? cc : lt /* unordered */;
997 case kCondLE: return gt_bias ? ls : le /* unordered */;
998 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
999 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1000 default:
1001 LOG(FATAL) << "UNREACHABLE";
1002 UNREACHABLE();
1003 }
1004}
1005
Anton Kirilov74234da2017-01-13 14:42:47 +00001006inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1007 switch (op_kind) {
1008 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1009 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1010 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1011 default:
1012 LOG(FATAL) << "Unexpected op kind " << op_kind;
1013 UNREACHABLE();
1014 }
1015}
1016
Scott Wakelingfe885462016-09-22 10:24:38 +01001017void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1018 stream << vixl32::Register(reg);
1019}
1020
1021void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1022 stream << vixl32::SRegister(reg);
1023}
1024
Vladimir Markoa0431112018-06-25 09:32:54 +01001025const ArmInstructionSetFeatures& CodeGeneratorARMVIXL::GetInstructionSetFeatures() const {
1026 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArmInstructionSetFeatures();
1027}
1028
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001029static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001030 uint32_t mask = 0;
1031 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1032 i <= regs.GetLastSRegister().GetCode();
1033 ++i) {
1034 mask |= (1 << i);
1035 }
1036 return mask;
1037}
1038
Artem Serovd4cc5b22016-11-04 11:19:09 +00001039// Saves the register in the stack. Returns the size taken on stack.
1040size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1041 uint32_t reg_id ATTRIBUTE_UNUSED) {
1042 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001043 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001044}
1045
1046// Restores the register from the stack. Returns the size taken on stack.
1047size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1048 uint32_t reg_id ATTRIBUTE_UNUSED) {
1049 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001050 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001051}
1052
1053size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1054 uint32_t reg_id ATTRIBUTE_UNUSED) {
1055 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001056 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001057}
1058
1059size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1060 uint32_t reg_id ATTRIBUTE_UNUSED) {
1061 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001062 UNREACHABLE();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001063}
1064
Anton Kirilov74234da2017-01-13 14:42:47 +00001065static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1066 vixl32::Register out,
1067 vixl32::Register first,
1068 const Operand& second,
1069 CodeGeneratorARMVIXL* codegen) {
1070 if (second.IsImmediate() && second.GetImmediate() == 0) {
1071 const Operand in = kind == HInstruction::kAnd
1072 ? Operand(0)
1073 : Operand(first);
1074
1075 __ Mov(out, in);
1076 } else {
1077 switch (kind) {
1078 case HInstruction::kAdd:
1079 __ Add(out, first, second);
1080 break;
1081 case HInstruction::kAnd:
1082 __ And(out, first, second);
1083 break;
1084 case HInstruction::kOr:
1085 __ Orr(out, first, second);
1086 break;
1087 case HInstruction::kSub:
1088 __ Sub(out, first, second);
1089 break;
1090 case HInstruction::kXor:
1091 __ Eor(out, first, second);
1092 break;
1093 default:
1094 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1095 UNREACHABLE();
1096 }
1097 }
1098}
1099
1100static void GenerateDataProc(HInstruction::InstructionKind kind,
1101 const Location& out,
1102 const Location& first,
1103 const Operand& second_lo,
1104 const Operand& second_hi,
1105 CodeGeneratorARMVIXL* codegen) {
1106 const vixl32::Register first_hi = HighRegisterFrom(first);
1107 const vixl32::Register first_lo = LowRegisterFrom(first);
1108 const vixl32::Register out_hi = HighRegisterFrom(out);
1109 const vixl32::Register out_lo = LowRegisterFrom(out);
1110
1111 if (kind == HInstruction::kAdd) {
1112 __ Adds(out_lo, first_lo, second_lo);
1113 __ Adc(out_hi, first_hi, second_hi);
1114 } else if (kind == HInstruction::kSub) {
1115 __ Subs(out_lo, first_lo, second_lo);
1116 __ Sbc(out_hi, first_hi, second_hi);
1117 } else {
1118 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1119 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1120 }
1121}
1122
1123static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1124 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1125}
1126
1127static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1128 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001129 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001130 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1131
1132 const LocationSummary* const locations = instruction->GetLocations();
1133 const uint32_t shift_value = instruction->GetShiftAmount();
1134 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1135 const Location first = locations->InAt(0);
1136 const Location second = locations->InAt(1);
1137 const Location out = locations->Out();
1138 const vixl32::Register first_hi = HighRegisterFrom(first);
1139 const vixl32::Register first_lo = LowRegisterFrom(first);
1140 const vixl32::Register out_hi = HighRegisterFrom(out);
1141 const vixl32::Register out_lo = LowRegisterFrom(out);
1142 const vixl32::Register second_hi = HighRegisterFrom(second);
1143 const vixl32::Register second_lo = LowRegisterFrom(second);
1144 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1145
1146 if (shift_value >= 32) {
1147 if (shift == ShiftType::LSL) {
1148 GenerateDataProcInstruction(kind,
1149 out_hi,
1150 first_hi,
1151 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1152 codegen);
1153 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1154 } else if (shift == ShiftType::ASR) {
1155 GenerateDataProc(kind,
1156 out,
1157 first,
1158 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1159 Operand(second_hi, ShiftType::ASR, 31),
1160 codegen);
1161 } else {
1162 DCHECK_EQ(shift, ShiftType::LSR);
1163 GenerateDataProc(kind,
1164 out,
1165 first,
1166 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1167 0,
1168 codegen);
1169 }
1170 } else {
1171 DCHECK_GT(shift_value, 1U);
1172 DCHECK_LT(shift_value, 32U);
1173
1174 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1175
1176 if (shift == ShiftType::LSL) {
1177 // We are not doing this for HInstruction::kAdd because the output will require
1178 // Location::kOutputOverlap; not applicable to other cases.
1179 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1180 GenerateDataProcInstruction(kind,
1181 out_hi,
1182 first_hi,
1183 Operand(second_hi, ShiftType::LSL, shift_value),
1184 codegen);
1185 GenerateDataProcInstruction(kind,
1186 out_hi,
1187 out_hi,
1188 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1189 codegen);
1190 GenerateDataProcInstruction(kind,
1191 out_lo,
1192 first_lo,
1193 Operand(second_lo, ShiftType::LSL, shift_value),
1194 codegen);
1195 } else {
1196 const vixl32::Register temp = temps.Acquire();
1197
1198 __ Lsl(temp, second_hi, shift_value);
1199 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1200 GenerateDataProc(kind,
1201 out,
1202 first,
1203 Operand(second_lo, ShiftType::LSL, shift_value),
1204 temp,
1205 codegen);
1206 }
1207 } else {
1208 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1209
1210 // We are not doing this for HInstruction::kAdd because the output will require
1211 // Location::kOutputOverlap; not applicable to other cases.
1212 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1213 GenerateDataProcInstruction(kind,
1214 out_lo,
1215 first_lo,
1216 Operand(second_lo, ShiftType::LSR, shift_value),
1217 codegen);
1218 GenerateDataProcInstruction(kind,
1219 out_lo,
1220 out_lo,
1221 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1222 codegen);
1223 GenerateDataProcInstruction(kind,
1224 out_hi,
1225 first_hi,
1226 Operand(second_hi, shift, shift_value),
1227 codegen);
1228 } else {
1229 const vixl32::Register temp = temps.Acquire();
1230
1231 __ Lsr(temp, second_lo, shift_value);
1232 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1233 GenerateDataProc(kind,
1234 out,
1235 first,
1236 temp,
1237 Operand(second_hi, shift, shift_value),
1238 codegen);
1239 }
1240 }
1241 }
1242}
1243
Donghui Bai426b49c2016-11-08 14:55:38 +08001244static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1245 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1246 if (rhs_loc.IsConstant()) {
1247 // 0.0 is the only immediate that can be encoded directly in
1248 // a VCMP instruction.
1249 //
1250 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1251 // specify that in a floating-point comparison, positive zero
1252 // and negative zero are considered equal, so we can use the
1253 // literal 0.0 for both cases here.
1254 //
1255 // Note however that some methods (Float.equal, Float.compare,
1256 // Float.compareTo, Double.equal, Double.compare,
1257 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1258 // StrictMath.min) consider 0.0 to be (strictly) greater than
1259 // -0.0. So if we ever translate calls to these methods into a
1260 // HCompare instruction, we must handle the -0.0 case with
1261 // care here.
1262 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001264 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001265
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001266 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001267 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1268 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001270 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1271 }
1272 } else {
1273 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1274 }
1275}
1276
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001277static int64_t AdjustConstantForCondition(int64_t value,
1278 IfCondition* condition,
1279 IfCondition* opposite) {
1280 if (value == 1) {
1281 if (*condition == kCondB) {
1282 value = 0;
1283 *condition = kCondEQ;
1284 *opposite = kCondNE;
1285 } else if (*condition == kCondAE) {
1286 value = 0;
1287 *condition = kCondNE;
1288 *opposite = kCondEQ;
1289 }
1290 } else if (value == -1) {
1291 if (*condition == kCondGT) {
1292 value = 0;
1293 *condition = kCondGE;
1294 *opposite = kCondLT;
1295 } else if (*condition == kCondLE) {
1296 value = 0;
1297 *condition = kCondLT;
1298 *opposite = kCondGE;
1299 }
1300 }
1301
1302 return value;
1303}
1304
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001305static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1306 HCondition* condition,
1307 bool invert,
1308 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001309 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001310
1311 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001312 IfCondition cond = condition->GetCondition();
1313 IfCondition opposite = condition->GetOppositeCondition();
1314
1315 if (invert) {
1316 std::swap(cond, opposite);
1317 }
1318
1319 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001320 const Location left = locations->InAt(0);
1321 const Location right = locations->InAt(1);
1322
1323 DCHECK(right.IsConstant());
1324
1325 const vixl32::Register left_high = HighRegisterFrom(left);
1326 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001327 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1328 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1329
1330 // Comparisons against 0 are common enough to deserve special attention.
1331 if (value == 0) {
1332 switch (cond) {
1333 case kCondNE:
1334 // x > 0 iff x != 0 when the comparison is unsigned.
1335 case kCondA:
1336 ret = std::make_pair(ne, eq);
1337 FALLTHROUGH_INTENDED;
1338 case kCondEQ:
1339 // x <= 0 iff x == 0 when the comparison is unsigned.
1340 case kCondBE:
1341 __ Orrs(temps.Acquire(), left_low, left_high);
1342 return ret;
1343 case kCondLT:
1344 case kCondGE:
1345 __ Cmp(left_high, 0);
1346 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1347 // Trivially true or false.
1348 case kCondB:
1349 ret = std::make_pair(ne, eq);
1350 FALLTHROUGH_INTENDED;
1351 case kCondAE:
1352 __ Cmp(left_low, left_low);
1353 return ret;
1354 default:
1355 break;
1356 }
1357 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001358
1359 switch (cond) {
1360 case kCondEQ:
1361 case kCondNE:
1362 case kCondB:
1363 case kCondBE:
1364 case kCondA:
1365 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001366 const uint32_t value_low = Low32Bits(value);
1367 Operand operand_low(value_low);
1368
Donghui Bai426b49c2016-11-08 14:55:38 +08001369 __ Cmp(left_high, High32Bits(value));
1370
Anton Kirilov23b752b2017-07-20 14:40:44 +01001371 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1372 // we must ensure that the operands corresponding to the least significant
1373 // halves of the inputs fit into a 16-bit CMP encoding.
1374 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1375 operand_low = Operand(temps.Acquire());
1376 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1377 }
1378
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001379 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001380 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1381 2 * vixl32::k16BitT32InstructionSizeInBytes,
1382 CodeBufferCheckScope::kExactSize);
1383
1384 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001385 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001386 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001387 break;
1388 }
1389 case kCondLE:
1390 case kCondGT:
1391 // Trivially true or false.
1392 if (value == std::numeric_limits<int64_t>::max()) {
1393 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001394 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001395 break;
1396 }
1397
1398 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001399 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001400 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001401 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001402 } else {
1403 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001404 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001405 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001406 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001407 }
1408
1409 value++;
1410 FALLTHROUGH_INTENDED;
1411 case kCondGE:
1412 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001413 __ Cmp(left_low, Low32Bits(value));
1414 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001415 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001416 break;
1417 }
1418 default:
1419 LOG(FATAL) << "Unreachable";
1420 UNREACHABLE();
1421 }
1422
1423 return ret;
1424}
1425
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001426static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1427 HCondition* condition,
1428 bool invert,
1429 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001430 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001431
1432 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001433 IfCondition cond = condition->GetCondition();
1434 IfCondition opposite = condition->GetOppositeCondition();
1435
1436 if (invert) {
1437 std::swap(cond, opposite);
1438 }
1439
1440 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001441 Location left = locations->InAt(0);
1442 Location right = locations->InAt(1);
1443
1444 DCHECK(right.IsRegisterPair());
1445
1446 switch (cond) {
1447 case kCondEQ:
1448 case kCondNE:
1449 case kCondB:
1450 case kCondBE:
1451 case kCondA:
1452 case kCondAE: {
1453 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1454
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001455 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001456 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1457 2 * vixl32::k16BitT32InstructionSizeInBytes,
1458 CodeBufferCheckScope::kExactSize);
1459
1460 __ it(eq);
1461 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001462 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001463 break;
1464 }
1465 case kCondLE:
1466 case kCondGT:
1467 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001468 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001469 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001470 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001471 } else {
1472 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001473 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001474 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001475 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001476 }
1477
1478 std::swap(left, right);
1479 FALLTHROUGH_INTENDED;
1480 case kCondGE:
1481 case kCondLT: {
1482 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1483
1484 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1485 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001486 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001487 break;
1488 }
1489 default:
1490 LOG(FATAL) << "Unreachable";
1491 UNREACHABLE();
1492 }
1493
1494 return ret;
1495}
1496
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001497static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1498 bool invert,
1499 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001500 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001501 IfCondition cond = condition->GetCondition();
1502 IfCondition opposite = condition->GetOppositeCondition();
1503 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001504
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001505 if (invert) {
1506 std::swap(cond, opposite);
1507 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001509 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001510 ret = condition->GetLocations()->InAt(1).IsConstant()
1511 ? GenerateLongTestConstant(condition, invert, codegen)
1512 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001513 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001514 GenerateVcmp(condition, codegen);
1515 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1516 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1517 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001518 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001519 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001520 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1521 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001522 }
1523
1524 return ret;
1525}
1526
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001527static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001528 const vixl32::Register out = OutputRegister(cond);
1529 const auto condition = GenerateTest(cond, false, codegen);
1530
1531 __ Mov(LeaveFlags, out, 0);
1532
1533 if (out.IsLow()) {
1534 // We use the scope because of the IT block that follows.
1535 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1536 2 * vixl32::k16BitT32InstructionSizeInBytes,
1537 CodeBufferCheckScope::kExactSize);
1538
1539 __ it(condition.first);
1540 __ mov(condition.first, out, 1);
1541 } else {
1542 vixl32::Label done_label;
1543 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1544
Andreas Gampe3db70682018-12-26 15:12:03 -08001545 __ B(condition.second, final_label, /* is_far_target= */ false);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001546 __ Mov(out, 1);
1547
1548 if (done_label.IsReferenced()) {
1549 __ Bind(&done_label);
1550 }
1551 }
1552}
1553
1554static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001555 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001556
1557 const LocationSummary* const locations = cond->GetLocations();
1558 IfCondition condition = cond->GetCondition();
1559 const vixl32::Register out = OutputRegister(cond);
1560 const Location left = locations->InAt(0);
1561 const Location right = locations->InAt(1);
1562 vixl32::Register left_high = HighRegisterFrom(left);
1563 vixl32::Register left_low = LowRegisterFrom(left);
1564 vixl32::Register temp;
1565 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1566
1567 if (right.IsConstant()) {
1568 IfCondition opposite = cond->GetOppositeCondition();
1569 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
1570 &condition,
1571 &opposite);
1572 Operand right_high = High32Bits(value);
1573 Operand right_low = Low32Bits(value);
1574
1575 // The output uses Location::kNoOutputOverlap.
1576 if (out.Is(left_high)) {
1577 std::swap(left_low, left_high);
1578 std::swap(right_low, right_high);
1579 }
1580
1581 __ Sub(out, left_low, right_low);
1582 temp = temps.Acquire();
1583 __ Sub(temp, left_high, right_high);
1584 } else {
1585 DCHECK(right.IsRegisterPair());
1586 temp = temps.Acquire();
1587 __ Sub(temp, left_high, HighRegisterFrom(right));
1588 __ Sub(out, left_low, LowRegisterFrom(right));
1589 }
1590
1591 // Need to check after calling AdjustConstantForCondition().
1592 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
1593
1594 if (condition == kCondNE && out.IsLow()) {
1595 __ Orrs(out, out, temp);
1596
1597 // We use the scope because of the IT block that follows.
1598 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1599 2 * vixl32::k16BitT32InstructionSizeInBytes,
1600 CodeBufferCheckScope::kExactSize);
1601
1602 __ it(ne);
1603 __ mov(ne, out, 1);
1604 } else {
1605 __ Orr(out, out, temp);
1606 codegen->GenerateConditionWithZero(condition, out, out, temp);
1607 }
1608}
1609
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001610static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001612
1613 const LocationSummary* const locations = cond->GetLocations();
1614 IfCondition condition = cond->GetCondition();
1615 const vixl32::Register out = OutputRegister(cond);
1616 const Location left = locations->InAt(0);
1617 const Location right = locations->InAt(1);
1618
1619 if (right.IsConstant()) {
1620 IfCondition opposite = cond->GetOppositeCondition();
1621
1622 // Comparisons against 0 are common enough to deserve special attention.
1623 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
1624 switch (condition) {
1625 case kCondNE:
1626 case kCondA:
1627 if (out.IsLow()) {
1628 // We only care if both input registers are 0 or not.
1629 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
1630
1631 // We use the scope because of the IT block that follows.
1632 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1633 2 * vixl32::k16BitT32InstructionSizeInBytes,
1634 CodeBufferCheckScope::kExactSize);
1635
1636 __ it(ne);
1637 __ mov(ne, out, 1);
1638 return;
1639 }
1640
1641 FALLTHROUGH_INTENDED;
1642 case kCondEQ:
1643 case kCondBE:
1644 // We only care if both input registers are 0 or not.
1645 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
1646 codegen->GenerateConditionWithZero(condition, out, out);
1647 return;
1648 case kCondLT:
1649 case kCondGE:
1650 // We only care about the sign bit.
1651 FALLTHROUGH_INTENDED;
1652 case kCondAE:
1653 case kCondB:
1654 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
1655 return;
1656 case kCondLE:
1657 case kCondGT:
1658 default:
1659 break;
1660 }
1661 }
1662 }
1663
Anton Kirilov23b752b2017-07-20 14:40:44 +01001664 // If `out` is a low register, then the GenerateConditionGeneric()
1665 // function generates a shorter code sequence that is still branchless.
1666 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001667 GenerateEqualLong(cond, codegen);
1668 return;
1669 }
1670
Anton Kirilov23b752b2017-07-20 14:40:44 +01001671 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001672}
1673
Roland Levillain6d729a72017-06-30 18:34:01 +01001674static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
1675 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001681 GenerateConditionLong(cond, codegen);
1682 return;
1683 }
1684
1685 IfCondition condition = cond->GetCondition();
1686 vixl32::Register in = InputRegisterAt(cond, 0);
1687 const vixl32::Register out = OutputRegister(cond);
1688 const Location right = cond->GetLocations()->InAt(1);
1689 int64_t value;
1690
1691 if (right.IsConstant()) {
1692 IfCondition opposite = cond->GetOppositeCondition();
1693
1694 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
1695
1696 // Comparisons against 0 are common enough to deserve special attention.
1697 if (value == 0) {
1698 switch (condition) {
1699 case kCondNE:
1700 case kCondA:
1701 if (out.IsLow() && out.Is(in)) {
1702 __ Cmp(out, 0);
1703
1704 // We use the scope because of the IT block that follows.
1705 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1706 2 * vixl32::k16BitT32InstructionSizeInBytes,
1707 CodeBufferCheckScope::kExactSize);
1708
1709 __ it(ne);
1710 __ mov(ne, out, 1);
1711 return;
1712 }
1713
1714 FALLTHROUGH_INTENDED;
1715 case kCondEQ:
1716 case kCondBE:
1717 case kCondLT:
1718 case kCondGE:
1719 case kCondAE:
1720 case kCondB:
1721 codegen->GenerateConditionWithZero(condition, out, in);
1722 return;
1723 case kCondLE:
1724 case kCondGT:
1725 default:
1726 break;
1727 }
1728 }
1729 }
1730
1731 if (condition == kCondEQ || condition == kCondNE) {
1732 Operand operand(0);
1733
1734 if (right.IsConstant()) {
1735 operand = Operand::From(value);
1736 } else if (out.Is(RegisterFrom(right))) {
1737 // Avoid 32-bit instructions if possible.
1738 operand = InputOperandAt(cond, 0);
1739 in = RegisterFrom(right);
1740 } else {
1741 operand = InputOperandAt(cond, 1);
1742 }
1743
1744 if (condition == kCondNE && out.IsLow()) {
1745 __ Subs(out, in, operand);
1746
1747 // We use the scope because of the IT block that follows.
1748 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1749 2 * vixl32::k16BitT32InstructionSizeInBytes,
1750 CodeBufferCheckScope::kExactSize);
1751
1752 __ it(ne);
1753 __ mov(ne, out, 1);
1754 } else {
1755 __ Sub(out, in, operand);
1756 codegen->GenerateConditionWithZero(condition, out, out);
1757 }
1758
1759 return;
1760 }
1761
1762 GenerateConditionGeneric(cond, codegen);
1763}
1764
Donghui Bai426b49c2016-11-08 14:55:38 +08001765static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001766 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001767 bool ret = false;
1768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001769 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001772 const uint64_t value = Uint64ConstantFrom(constant);
1773
1774 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1775 } else {
1776 ret = IsUint<8>(Int32ConstantFrom(constant));
1777 }
1778
1779 return ret;
1780}
1781
1782static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001783 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001784
1785 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1786 return Location::ConstantLocation(constant->AsConstant());
1787 }
1788
1789 return Location::RequiresRegister();
1790}
1791
1792static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1793 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1794 // we check that we are not dealing with floating-point output (there is no
1795 // 16-bit VMOV encoding).
1796 if (!out.IsRegister() && !out.IsRegisterPair()) {
1797 return false;
1798 }
1799
1800 // For constants, we also check that the output is in one or two low registers,
1801 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1802 // MOV encoding can be used.
1803 if (src.IsConstant()) {
1804 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1805 return false;
1806 }
1807
1808 if (out.IsRegister()) {
1809 if (!RegisterFrom(out).IsLow()) {
1810 return false;
1811 }
1812 } else {
1813 DCHECK(out.IsRegisterPair());
1814
1815 if (!HighRegisterFrom(out).IsLow()) {
1816 return false;
1817 }
1818 }
1819 }
1820
1821 return true;
1822}
1823
Scott Wakelingfe885462016-09-22 10:24:38 +01001824#undef __
1825
Donghui Bai426b49c2016-11-08 14:55:38 +08001826vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1827 vixl32::Label* final_label) {
1828 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001829 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001830
1831 const HBasicBlock* const block = instruction->GetBlock();
1832 const HLoopInformation* const info = block->GetLoopInformation();
1833 HInstruction* const next = instruction->GetNext();
1834
1835 // Avoid a branch to a branch.
1836 if (next->IsGoto() && (info == nullptr ||
1837 !info->IsBackEdge(*block) ||
1838 !info->HasSuspendCheck())) {
1839 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1840 }
1841
1842 return final_label;
1843}
1844
Scott Wakelingfe885462016-09-22 10:24:38 +01001845CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +01001846 const CompilerOptions& compiler_options,
1847 OptimizingCompilerStats* stats)
1848 : CodeGenerator(graph,
1849 kNumberOfCoreRegisters,
1850 kNumberOfSRegisters,
1851 kNumberOfRegisterPairs,
1852 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001853 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001854 compiler_options,
1855 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001856 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1857 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001858 location_builder_(graph, this),
1859 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001860 move_resolver_(graph->GetAllocator(), this),
1861 assembler_(graph->GetAllocator()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001862 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001863 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001864 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001865 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001866 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001867 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001868 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001869 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001870 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001871 uint32_literals_(std::less<uint32_t>(),
1872 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001873 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001874 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001875 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001876 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1877 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1878 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001879 // Always save the LR register to mimic Quick.
1880 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00001881 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
1882 // S0-S31, which alias to D0-D15.
1883 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
1884 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01001885}
1886
Artem Serov551b28f2016-10-18 19:11:30 +01001887void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1888 uint32_t num_entries = switch_instr_->GetNumEntries();
1889 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1890
1891 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001892 // underlying code buffer and we have generated a jump table of the right size, using
1893 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001894 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1895 num_entries * sizeof(int32_t),
1896 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001897 // TODO(VIXL): Check that using lower case bind is fine here.
1898 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001899 for (uint32_t i = 0; i < num_entries; i++) {
1900 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1901 }
1902}
1903
1904void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1905 uint32_t num_entries = switch_instr_->GetNumEntries();
1906 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1907
Artem Serov551b28f2016-10-18 19:11:30 +01001908 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1909 for (uint32_t i = 0; i < num_entries; i++) {
1910 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1911 DCHECK(target_label->IsBound());
1912 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1913 // When doing BX to address we need to have lower bit set to 1 in T32.
1914 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1915 jump_offset++;
1916 }
1917 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1918 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001919
Scott Wakelingb77051e2016-11-21 19:46:00 +00001920 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001921 }
1922}
1923
Artem Serov09a940d2016-11-11 16:15:11 +00001924void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001925 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001926 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001927 }
1928}
1929
Andreas Gampeca620d72016-11-08 08:09:33 -08001930#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001931
1932void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001933 FixJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001934
1935 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001936 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001937 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1938 uint32_t encoded_data = entry.first;
1939 vixl::aarch32::Label* slow_path_entry = &entry.second.label;
1940 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001941 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001942 }
1943
Scott Wakelingfe885462016-09-22 10:24:38 +01001944 GetAssembler()->FinalizeCode();
1945 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001946
1947 // Verify Baker read barrier linker patches.
1948 if (kIsDebugBuild) {
1949 ArrayRef<const uint8_t> code = allocator->GetMemory();
1950 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1951 DCHECK(info.label.IsBound());
1952 uint32_t literal_offset = info.label.GetLocation();
1953 DCHECK_ALIGNED(literal_offset, 2u);
1954
1955 auto GetInsn16 = [&code](uint32_t offset) {
1956 DCHECK_ALIGNED(offset, 2u);
1957 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
1958 (static_cast<uint32_t>(code[offset + 1]) << 8);
1959 };
1960 auto GetInsn32 = [=](uint32_t offset) {
1961 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
1962 };
1963
1964 uint32_t encoded_data = info.custom_data;
1965 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1966 // Check that the next instruction matches the expected LDR.
1967 switch (kind) {
1968 case BakerReadBarrierKind::kField: {
1969 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1970 if (width == BakerReadBarrierWidth::kWide) {
1971 DCHECK_GE(code.size() - literal_offset, 8u);
1972 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1973 // LDR (immediate), encoding T3, with correct base_reg.
1974 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1975 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1976 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
1977 } else {
1978 DCHECK_GE(code.size() - literal_offset, 6u);
1979 uint32_t next_insn = GetInsn16(literal_offset + 4u);
1980 // LDR (immediate), encoding T1, with correct base_reg.
1981 CheckValidReg(next_insn & 0x7u); // Check destination register.
1982 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1983 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
1984 }
1985 break;
1986 }
1987 case BakerReadBarrierKind::kArray: {
1988 DCHECK_GE(code.size() - literal_offset, 8u);
1989 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1990 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
1991 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1992 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1993 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
1994 CheckValidReg(next_insn & 0xf); // Check index register
1995 break;
1996 }
1997 case BakerReadBarrierKind::kGcRoot: {
1998 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1999 if (width == BakerReadBarrierWidth::kWide) {
2000 DCHECK_GE(literal_offset, 4u);
2001 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Markof28be432018-08-14 12:20:51 +00002002 // LDR (immediate), encoding T3, with correct root_reg.
Vladimir Markoca1e0382018-04-11 09:58:41 +00002003 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markof28be432018-08-14 12:20:51 +00002004 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
Vladimir Markoca1e0382018-04-11 09:58:41 +00002005 } else {
2006 DCHECK_GE(literal_offset, 2u);
2007 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
2008 // LDR (immediate), encoding T1, with correct root_reg.
2009 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2010 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2011 }
2012 break;
2013 }
Vladimir Markod887ed82018-08-14 13:52:12 +00002014 case BakerReadBarrierKind::kUnsafeCas: {
2015 DCHECK_GE(literal_offset, 4u);
2016 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
2017 // ADD (register), encoding T3, with correct root_reg.
2018 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2019 CHECK_EQ(prev_insn & 0xfff0fff0u, 0xeb000000u | (root_reg << 8));
2020 break;
2021 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002022 default:
2023 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2024 UNREACHABLE();
2025 }
2026 }
2027 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002028}
2029
2030void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002031 // Stack register, LR and PC are always reserved.
2032 blocked_core_registers_[SP] = true;
2033 blocked_core_registers_[LR] = true;
2034 blocked_core_registers_[PC] = true;
2035
Roland Levillain6d729a72017-06-30 18:34:01 +01002036 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2037 // Reserve marking register.
2038 blocked_core_registers_[MR] = true;
2039 }
2040
Scott Wakelingfe885462016-09-22 10:24:38 +01002041 // Reserve thread register.
2042 blocked_core_registers_[TR] = true;
2043
2044 // Reserve temp register.
2045 blocked_core_registers_[IP] = true;
2046
2047 if (GetGraph()->IsDebuggable()) {
2048 // Stubs do not save callee-save floating point registers. If the graph
2049 // is debuggable, we need to deal with these registers differently. For
2050 // now, just block them.
2051 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2052 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2053 ++i) {
2054 blocked_fpu_registers_[i] = true;
2055 }
2056 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002057}
2058
Scott Wakelingfe885462016-09-22 10:24:38 +01002059InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2060 CodeGeneratorARMVIXL* codegen)
2061 : InstructionCodeGenerator(graph, codegen),
2062 assembler_(codegen->GetAssembler()),
2063 codegen_(codegen) {}
2064
2065void CodeGeneratorARMVIXL::ComputeSpillMask() {
2066 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Vladimir Marko460f0542019-07-04 14:02:08 +01002067 DCHECK_NE(core_spill_mask_ & (1u << kLrCode), 0u)
2068 << "At least the return address register must be saved";
2069 // 16-bit PUSH/POP (T1) can save/restore just the LR/PC.
2070 DCHECK(GetVIXLAssembler()->IsUsingT32());
Scott Wakelingfe885462016-09-22 10:24:38 +01002071 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2072 // We use vpush and vpop for saving and restoring floating point registers, which take
2073 // a SRegister and the number of registers to save/restore after that SRegister. We
2074 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2075 // but in the range.
2076 if (fpu_spill_mask_ != 0) {
2077 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2078 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2079 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2080 fpu_spill_mask_ |= (1 << i);
2081 }
2082 }
2083}
2084
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002085void CodeGeneratorARMVIXL::MaybeIncrementHotness(bool is_frame_entry) {
2086 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2087 UseScratchRegisterScope temps(GetVIXLAssembler());
2088 vixl32::Register temp = temps.Acquire();
2089 static_assert(ArtMethod::MaxCounter() == 0xFFFF, "asm is probably wrong");
2090 if (!is_frame_entry) {
2091 __ Push(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002092 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002093 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2094 }
2095 // Load with zero extend to clear the high bits for integer overflow check.
2096 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2097 __ Add(temp, temp, 1);
2098 // Subtract one if the counter would overflow.
2099 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
2100 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2101 if (!is_frame_entry) {
2102 __ Pop(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002103 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002104 }
2105 }
2106
2107 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002108 ScopedProfilingInfoUse spiu(
2109 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2110 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002111 if (info != nullptr) {
2112 uint32_t address = reinterpret_cast32<uint32_t>(info);
2113 vixl::aarch32::Label done;
2114 UseScratchRegisterScope temps(GetVIXLAssembler());
2115 temps.Exclude(ip);
2116 if (!is_frame_entry) {
2117 __ Push(r4); // Will be used as temporary. For frame entry, r4 is always available.
Vladimir Markodec78172020-06-19 15:31:23 +01002118 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002119 }
2120 __ Mov(r4, address);
2121 __ Ldrh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2122 __ Add(ip, ip, 1);
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01002123 instruction_visitor_.GenerateAndConst(ip, ip, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002124 __ Strh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2125 if (!is_frame_entry) {
2126 __ Pop(r4);
Vladimir Markodec78172020-06-19 15:31:23 +01002127 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002128 }
2129 __ Lsls(ip, ip, 16);
2130 __ B(ne, &done);
2131 uint32_t entry_point_offset =
2132 GetThreadOffset<kArmPointerSize>(kQuickCompileOptimized).Int32Value();
2133 if (HasEmptyFrame()) {
2134 CHECK(is_frame_entry);
2135 // For leaf methods, we need to spill lr and r0. Also spill r1 and r2 for
2136 // alignment.
2137 uint32_t core_spill_mask =
2138 (1 << lr.GetCode()) | (1 << r0.GetCode()) | (1 << r1.GetCode()) | (1 << r2.GetCode());
2139 __ Push(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002140 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002141 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2142 __ Blx(lr);
2143 __ Pop(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002144 GetAssembler()->cfi().AdjustCFAOffset(
2145 -static_cast<int>(kArmWordSize) * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002146 } else {
2147 if (!RequiresCurrentMethod()) {
2148 CHECK(is_frame_entry);
2149 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2150 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002151 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2152 __ Blx(lr);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002153 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002154 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002155 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002156 }
2157}
2158
Scott Wakelingfe885462016-09-22 10:24:38 +01002159void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2160 bool skip_overflow_check =
2161 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2162 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2163 __ Bind(&frame_entry_label_);
2164
2165 if (HasEmptyFrame()) {
David Srbecky30021842019-02-13 14:19:36 +00002166 // Ensure that the CFI opcode list is not empty.
2167 GetAssembler()->cfi().Nop();
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002168 MaybeIncrementHotness(/* is_frame_entry= */ true);
Scott Wakelingfe885462016-09-22 10:24:38 +01002169 return;
2170 }
2171
Scott Wakelingfe885462016-09-22 10:24:38 +01002172 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002173 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002174 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002175 vixl32::Register temp;
2176 // TODO: Remove this check when R4 is made a callee-save register
2177 // in ART compiled code (b/72801708). Currently we need to make
2178 // sure r4 is not blocked, e.g. in special purpose
2179 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2180 // here.
2181 if (!blocked_core_registers_[R4]) {
2182 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2183 DCHECK(!reg.Is(r4));
2184 }
2185 DCHECK(!kCoreCalleeSaves.Includes(r4));
2186 temp = r4;
2187 } else {
2188 temp = temps.Acquire();
2189 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002190 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002191 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002192 ExactAssemblyScope aas(GetVIXLAssembler(),
2193 vixl32::kMaxInstructionSizeInBytes,
2194 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002195 __ ldr(temp, MemOperand(temp));
2196 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002197 }
2198
Vladimir Marko1a225a72019-07-05 13:37:42 +01002199 uint32_t frame_size = GetFrameSize();
2200 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2201 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2202 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2203 core_spills_offset <= 3u * kArmWordSize) {
2204 // Do a single PUSH for core registers including the method and up to two
2205 // filler registers. Then store the single FP spill if any.
2206 // (The worst case is when the method is not required and we actually
2207 // store 3 extra registers but they are stored in the same properly
2208 // aligned 16-byte chunk where we're already writing anyway.)
2209 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2210 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize);
2211 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(core_spill_mask_));
2212 __ Push(RegisterList(core_spill_mask_ | extra_regs));
2213 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
2214 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2215 core_spills_offset,
2216 core_spill_mask_,
2217 kArmWordSize);
2218 if (fpu_spill_mask_ != 0u) {
2219 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2220 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2221 GetAssembler()->StoreSToOffset(sreg, sp, fp_spills_offset);
2222 GetAssembler()->cfi().RelOffset(DWARFReg(sreg), /*offset=*/ fp_spills_offset);
2223 }
2224 } else {
2225 __ Push(RegisterList(core_spill_mask_));
2226 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2227 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2228 /*offset=*/ 0,
2229 core_spill_mask_,
2230 kArmWordSize);
2231 if (fpu_spill_mask_ != 0) {
2232 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002233
Vladimir Marko1a225a72019-07-05 13:37:42 +01002234 // Check that list is contiguous.
2235 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002236
Vladimir Marko1a225a72019-07-05 13:37:42 +01002237 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2238 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
2239 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0),
2240 /*offset=*/ 0,
2241 fpu_spill_mask_,
2242 kArmWordSize);
2243 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002244
Vladimir Marko1a225a72019-07-05 13:37:42 +01002245 // Adjust SP and save the current method if we need it. Note that we do
2246 // not save the method in HCurrentMethod, as the instruction might have
2247 // been removed in the SSA graph.
2248 if (RequiresCurrentMethod() && fp_spills_offset <= 3 * kArmWordSize) {
2249 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2250 __ Push(RegisterList(MaxInt<uint32_t>(fp_spills_offset / kArmWordSize)));
2251 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2252 } else {
Vladimir Markodec78172020-06-19 15:31:23 +01002253 IncreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002254 if (RequiresCurrentMethod()) {
2255 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2256 }
2257 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002258 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002259
2260 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2261 UseScratchRegisterScope temps(GetVIXLAssembler());
2262 vixl32::Register temp = temps.Acquire();
2263 // Initialize should_deoptimize flag to 0.
2264 __ Mov(temp, 0);
2265 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2266 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002267
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002268 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08002269 MaybeGenerateMarkingRegisterCheck(/* code= */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002270}
2271
2272void CodeGeneratorARMVIXL::GenerateFrameExit() {
2273 if (HasEmptyFrame()) {
2274 __ Bx(lr);
2275 return;
2276 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002277
Scott Wakelingfe885462016-09-22 10:24:38 +01002278 // Pop LR into PC to return.
2279 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2280 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
Vladimir Marko1a225a72019-07-05 13:37:42 +01002281
2282 uint32_t frame_size = GetFrameSize();
2283 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2284 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2285 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2286 // r4 is blocked by TestCodeGeneratorARMVIXL used by some tests.
2287 core_spills_offset <= (blocked_core_registers_[r4.GetCode()] ? 2u : 3u) * kArmWordSize) {
2288 // Load the FP spill if any and then do a single POP including the method
2289 // and up to two filler registers. If we have no FP spills, this also has
2290 // the advantage that we do not need to emit CFI directives.
2291 if (fpu_spill_mask_ != 0u) {
2292 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2293 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2294 GetAssembler()->cfi().RememberState();
2295 GetAssembler()->LoadSFromOffset(sreg, sp, fp_spills_offset);
2296 GetAssembler()->cfi().Restore(DWARFReg(sreg));
2297 }
2298 // Clobber registers r2-r4 as they are caller-save in ART managed ABI and
2299 // never hold the return value.
2300 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize) << r2.GetCode();
2301 DCHECK_EQ(extra_regs & kCoreCalleeSaves.GetList(), 0u);
2302 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(pop_mask));
2303 __ Pop(RegisterList(pop_mask | extra_regs));
2304 if (fpu_spill_mask_ != 0u) {
2305 GetAssembler()->cfi().RestoreState();
2306 }
2307 } else {
2308 GetAssembler()->cfi().RememberState();
Vladimir Markodec78172020-06-19 15:31:23 +01002309 DecreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002310 if (fpu_spill_mask_ != 0) {
2311 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2312
2313 // Check that list is contiguous.
2314 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2315
2316 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2317 GetAssembler()->cfi().AdjustCFAOffset(
2318 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
2319 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
2320 }
2321 __ Pop(RegisterList(pop_mask));
2322 GetAssembler()->cfi().RestoreState();
2323 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2324 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002325}
2326
2327void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2328 __ Bind(GetLabelOf(block));
2329}
2330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002332 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002333 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002334 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002335 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 case DataType::Type::kInt8:
2337 case DataType::Type::kUint16:
2338 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002339 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002340 uint32_t index = gp_index_++;
2341 uint32_t stack_index = stack_index_++;
2342 if (index < calling_convention.GetNumberOfRegisters()) {
2343 return LocationFrom(calling_convention.GetRegisterAt(index));
2344 } else {
2345 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2346 }
2347 }
2348
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002350 uint32_t index = gp_index_;
2351 uint32_t stack_index = stack_index_;
2352 gp_index_ += 2;
2353 stack_index_ += 2;
2354 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2355 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2356 // Skip R1, and use R2_R3 instead.
2357 gp_index_++;
2358 index++;
2359 }
2360 }
2361 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2362 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2363 calling_convention.GetRegisterAt(index + 1).GetCode());
2364
2365 return LocationFrom(calling_convention.GetRegisterAt(index),
2366 calling_convention.GetRegisterAt(index + 1));
2367 } else {
2368 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2369 }
2370 }
2371
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002373 uint32_t stack_index = stack_index_++;
2374 if (float_index_ % 2 == 0) {
2375 float_index_ = std::max(double_index_, float_index_);
2376 }
2377 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2378 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2379 } else {
2380 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2381 }
2382 }
2383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002385 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2386 uint32_t stack_index = stack_index_;
2387 stack_index_ += 2;
2388 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2389 uint32_t index = double_index_;
2390 double_index_ += 2;
2391 Location result = LocationFrom(
2392 calling_convention.GetFpuRegisterAt(index),
2393 calling_convention.GetFpuRegisterAt(index + 1));
2394 DCHECK(ExpectedPairLayout(result));
2395 return result;
2396 } else {
2397 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2398 }
2399 }
2400
Aart Bik66c158e2018-01-31 12:55:04 -08002401 case DataType::Type::kUint32:
2402 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002404 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002405 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002406 }
2407 return Location::NoLocation();
2408}
2409
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002411 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002412 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002413 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002414 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 case DataType::Type::kInt8:
2416 case DataType::Type::kUint16:
2417 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002418 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002419 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002420 return LocationFrom(r0);
2421 }
2422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002424 return LocationFrom(s0);
2425 }
2426
Aart Bik66c158e2018-01-31 12:55:04 -08002427 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002428 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002429 return LocationFrom(r0, r1);
2430 }
2431
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002432 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002433 return LocationFrom(s0, s1);
2434 }
2435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002437 return Location::NoLocation();
2438 }
2439
2440 UNREACHABLE();
2441}
2442
2443Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2444 return LocationFrom(kMethodRegister);
2445}
2446
Vladimir Marko86c87522020-05-11 16:55:55 +01002447Location CriticalNativeCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
2448 DCHECK_NE(type, DataType::Type::kReference);
2449
2450 // Native ABI uses the same registers as managed, except that the method register r0
2451 // is a normal argument.
2452 Location location = Location::NoLocation();
2453 if (DataType::Is64BitType(type)) {
2454 gpr_index_ = RoundUp(gpr_index_, 2u);
2455 stack_offset_ = RoundUp(stack_offset_, 2 * kFramePointerSize);
2456 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2457 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u],
2458 kParameterCoreRegistersVIXL[gpr_index_]);
2459 gpr_index_ += 2u;
2460 }
2461 } else {
2462 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2463 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u]);
2464 ++gpr_index_;
2465 }
2466 }
2467 if (location.IsInvalid()) {
2468 if (DataType::Is64BitType(type)) {
2469 location = Location::DoubleStackSlot(stack_offset_);
2470 stack_offset_ += 2 * kFramePointerSize;
2471 } else {
2472 location = Location::StackSlot(stack_offset_);
2473 stack_offset_ += kFramePointerSize;
2474 }
2475
2476 if (for_register_allocation_) {
2477 location = Location::Any();
2478 }
2479 }
2480 return location;
2481}
2482
2483Location CriticalNativeCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type)
2484 const {
2485 // We perform conversion to the managed ABI return register after the call if needed.
2486 InvokeDexCallingConventionVisitorARMVIXL dex_calling_convention;
2487 return dex_calling_convention.GetReturnLocation(type);
2488}
2489
2490Location CriticalNativeCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2491 // Pass the method in the hidden argument R4.
2492 return Location::RegisterLocation(R4);
2493}
2494
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002495void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2496 if (source.Equals(destination)) {
2497 return;
2498 }
2499 if (destination.IsRegister()) {
2500 if (source.IsRegister()) {
2501 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2502 } else if (source.IsFpuRegister()) {
2503 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2504 } else {
2505 GetAssembler()->LoadFromOffset(kLoadWord,
2506 RegisterFrom(destination),
2507 sp,
2508 source.GetStackIndex());
2509 }
2510 } else if (destination.IsFpuRegister()) {
2511 if (source.IsRegister()) {
2512 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2513 } else if (source.IsFpuRegister()) {
2514 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2515 } else {
2516 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2517 }
2518 } else {
2519 DCHECK(destination.IsStackSlot()) << destination;
2520 if (source.IsRegister()) {
2521 GetAssembler()->StoreToOffset(kStoreWord,
2522 RegisterFrom(source),
2523 sp,
2524 destination.GetStackIndex());
2525 } else if (source.IsFpuRegister()) {
2526 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2527 } else {
2528 DCHECK(source.IsStackSlot()) << source;
2529 UseScratchRegisterScope temps(GetVIXLAssembler());
2530 vixl32::Register temp = temps.Acquire();
2531 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2532 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2533 }
2534 }
2535}
2536
Artem Serovcfbe9132016-10-14 15:58:56 +01002537void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2538 DCHECK(location.IsRegister());
2539 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002540}
2541
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002542void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002543 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2544 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002545 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002546 move.AddMove(src, dst, dst_type, nullptr);
2547 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002548}
2549
Artem Serovcfbe9132016-10-14 15:58:56 +01002550void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2551 if (location.IsRegister()) {
2552 locations->AddTemp(location);
2553 } else if (location.IsRegisterPair()) {
2554 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2555 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2556 } else {
2557 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2558 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002559}
2560
2561void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2562 HInstruction* instruction,
2563 uint32_t dex_pc,
2564 SlowPathCode* slow_path) {
2565 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Vladimir Markof6675082019-05-17 12:05:28 +01002566
2567 ThreadOffset32 entrypoint_offset = GetThreadOffset<kArmPointerSize>(entrypoint);
2568 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
2569 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
2570 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01002571 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01002572 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
2573 // Ensure the pc position is recorded immediately after the `blx` instruction.
2574 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
2575 ExactAssemblyScope aas(GetVIXLAssembler(),
2576 vixl32::k16BitT32InstructionSizeInBytes,
2577 CodeBufferCheckScope::kExactSize);
2578 __ blx(lr);
2579 if (EntrypointRequiresStackMap(entrypoint)) {
2580 RecordPcInfo(instruction, dex_pc, slow_path);
2581 }
2582 } else {
2583 // Ensure the pc position is recorded immediately after the `bl` instruction.
2584 ExactAssemblyScope aas(GetVIXLAssembler(),
2585 vixl32::k32BitT32InstructionSizeInBytes,
2586 CodeBufferCheckScope::kExactSize);
2587 EmitEntrypointThunkCall(entrypoint_offset);
2588 if (EntrypointRequiresStackMap(entrypoint)) {
2589 RecordPcInfo(instruction, dex_pc, slow_path);
2590 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002591 }
2592}
2593
2594void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2595 HInstruction* instruction,
2596 SlowPathCode* slow_path) {
2597 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002598 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002599 __ Blx(lr);
2600}
2601
Scott Wakelingfe885462016-09-22 10:24:38 +01002602void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002603 if (successor->IsExitBlock()) {
2604 DCHECK(got->GetPrevious()->AlwaysThrows());
2605 return; // no code needed
2606 }
2607
Scott Wakelingfe885462016-09-22 10:24:38 +01002608 HBasicBlock* block = got->GetBlock();
2609 HInstruction* previous = got->GetPrevious();
2610 HLoopInformation* info = block->GetLoopInformation();
2611
2612 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002613 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Scott Wakelingfe885462016-09-22 10:24:38 +01002614 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2615 return;
2616 }
2617 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2618 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08002619 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002620 }
2621 if (!codegen_->GoesToNextBlock(block, successor)) {
2622 __ B(codegen_->GetLabelOf(successor));
2623 }
2624}
2625
2626void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2627 got->SetLocations(nullptr);
2628}
2629
2630void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2631 HandleGoto(got, got->GetSuccessor());
2632}
2633
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002634void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2635 try_boundary->SetLocations(nullptr);
2636}
2637
2638void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2639 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2640 if (!successor->IsExitBlock()) {
2641 HandleGoto(try_boundary, successor);
2642 }
2643}
2644
Scott Wakelingfe885462016-09-22 10:24:38 +01002645void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2646 exit->SetLocations(nullptr);
2647}
2648
2649void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2650}
2651
Scott Wakelingfe885462016-09-22 10:24:38 +01002652void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002653 vixl32::Label* true_target,
2654 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002655 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002656 if (true_target == false_target) {
2657 DCHECK(true_target != nullptr);
2658 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002659 return;
2660 }
2661
Anton Kirilov23b752b2017-07-20 14:40:44 +01002662 vixl32::Label* non_fallthrough_target;
2663 bool invert;
2664 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002665
Anton Kirilov23b752b2017-07-20 14:40:44 +01002666 if (true_target == nullptr) {
2667 // The true target is fallthrough.
2668 DCHECK(false_target != nullptr);
2669 non_fallthrough_target = false_target;
2670 invert = true;
2671 emit_both_branches = false;
2672 } else {
2673 non_fallthrough_target = true_target;
2674 invert = false;
2675 // Either the false target is fallthrough, or there is no fallthrough
2676 // and both branches must be emitted.
2677 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002678 }
2679
Anton Kirilov23b752b2017-07-20 14:40:44 +01002680 const auto cond = GenerateTest(condition, invert, codegen_);
2681
2682 __ B(cond.first, non_fallthrough_target, is_far_target);
2683
2684 if (emit_both_branches) {
2685 // No target falls through, we need to branch.
2686 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002687 }
2688}
2689
2690void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2691 size_t condition_input_index,
2692 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002693 vixl32::Label* false_target,
2694 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002695 HInstruction* cond = instruction->InputAt(condition_input_index);
2696
2697 if (true_target == nullptr && false_target == nullptr) {
2698 // Nothing to do. The code always falls through.
2699 return;
2700 } else if (cond->IsIntConstant()) {
2701 // Constant condition, statically compared against "true" (integer value 1).
2702 if (cond->AsIntConstant()->IsTrue()) {
2703 if (true_target != nullptr) {
2704 __ B(true_target);
2705 }
2706 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002707 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002708 if (false_target != nullptr) {
2709 __ B(false_target);
2710 }
2711 }
2712 return;
2713 }
2714
2715 // The following code generates these patterns:
2716 // (1) true_target == nullptr && false_target != nullptr
2717 // - opposite condition true => branch to false_target
2718 // (2) true_target != nullptr && false_target == nullptr
2719 // - condition true => branch to true_target
2720 // (3) true_target != nullptr && false_target != nullptr
2721 // - condition true => branch to true_target
2722 // - branch to false_target
2723 if (IsBooleanValueOrMaterializedCondition(cond)) {
2724 // Condition has been materialized, compare the output to 0.
2725 if (kIsDebugBuild) {
2726 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2727 DCHECK(cond_val.IsRegister());
2728 }
2729 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002730 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2731 false_target,
2732 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002733 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002734 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2735 true_target,
2736 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002737 }
2738 } else {
2739 // Condition has not been materialized. Use its inputs as the comparison and
2740 // its condition as the branch condition.
2741 HCondition* condition = cond->AsCondition();
2742
2743 // If this is a long or FP comparison that has been folded into
2744 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 DataType::Type type = condition->InputAt(0)->GetType();
2746 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002747 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002748 return;
2749 }
2750
Donghui Bai426b49c2016-11-08 14:55:38 +08002751 vixl32::Label* non_fallthrough_target;
2752 vixl32::Condition arm_cond = vixl32::Condition::None();
2753 const vixl32::Register left = InputRegisterAt(cond, 0);
2754 const Operand right = InputOperandAt(cond, 1);
2755
Scott Wakelingfe885462016-09-22 10:24:38 +01002756 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002757 arm_cond = ARMCondition(condition->GetOppositeCondition());
2758 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002759 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002760 arm_cond = ARMCondition(condition->GetCondition());
2761 non_fallthrough_target = true_target;
2762 }
2763
2764 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2765 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002766 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002767 } else {
2768 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002769 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002770 }
2771 } else {
2772 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002773 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002774 }
2775 }
2776
2777 // If neither branch falls through (case 3), the conditional branch to `true_target`
2778 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2779 if (true_target != nullptr && false_target != nullptr) {
2780 __ B(false_target);
2781 }
2782}
2783
2784void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002785 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002786 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2787 locations->SetInAt(0, Location::RequiresRegister());
2788 }
2789}
2790
2791void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2792 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2793 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002794 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2795 nullptr : codegen_->GetLabelOf(true_successor);
2796 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2797 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002798 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002799}
2800
Scott Wakelingc34dba72016-10-03 10:14:44 +01002801void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002802 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002803 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002804 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2805 RegisterSet caller_saves = RegisterSet::Empty();
2806 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2807 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002808 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2809 locations->SetInAt(0, Location::RequiresRegister());
2810 }
2811}
2812
2813void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2814 SlowPathCodeARMVIXL* slow_path =
2815 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2816 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002817 /* condition_input_index= */ 0,
Scott Wakelingc34dba72016-10-03 10:14:44 +01002818 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002819 /* false_target= */ nullptr);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002820}
2821
Artem Serovd4cc5b22016-11-04 11:19:09 +00002822void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002823 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002824 LocationSummary(flag, LocationSummary::kNoCall);
2825 locations->SetOut(Location::RequiresRegister());
2826}
2827
2828void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2829 GetAssembler()->LoadFromOffset(kLoadWord,
2830 OutputRegister(flag),
2831 sp,
2832 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2833}
2834
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002835void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002836 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002838
2839 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002840 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002841 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002842 } else {
2843 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002844 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002845 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002846
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002847 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002848 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2849 // The code generator handles overlap with the values, but not with the condition.
2850 locations->SetOut(Location::SameAsFirstInput());
2851 } else if (is_floating_point) {
2852 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2853 } else {
2854 if (!locations->InAt(1).IsConstant()) {
2855 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2856 }
2857
2858 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002859 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002860}
2861
2862void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002863 HInstruction* const condition = select->GetCondition();
2864 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002866 const Location first = locations->InAt(0);
2867 const Location out = locations->Out();
2868 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002869
2870 // In the unlucky case the output of this instruction overlaps
2871 // with an input of an "emitted-at-use-site" condition, and
2872 // the output of this instruction is not one of its inputs, we'll
2873 // need to fallback to branches instead of conditional ARM instructions.
2874 bool output_overlaps_with_condition_inputs =
2875 !IsBooleanValueOrMaterializedCondition(condition) &&
2876 !out.Equals(first) &&
2877 !out.Equals(second) &&
2878 (condition->GetLocations()->InAt(0).Equals(out) ||
2879 condition->GetLocations()->InAt(1).Equals(out));
2880 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002881 Location src;
2882
2883 if (condition->IsIntConstant()) {
2884 if (condition->AsIntConstant()->IsFalse()) {
2885 src = first;
2886 } else {
2887 src = second;
2888 }
2889
2890 codegen_->MoveLocation(out, src, type);
2891 return;
2892 }
2893
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002894 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002895 bool invert = false;
2896
2897 if (out.Equals(second)) {
2898 src = first;
2899 invert = true;
2900 } else if (out.Equals(first)) {
2901 src = second;
2902 } else if (second.IsConstant()) {
2903 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2904 src = second;
2905 } else if (first.IsConstant()) {
2906 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2907 src = first;
2908 invert = true;
2909 } else {
2910 src = second;
2911 }
2912
2913 if (CanGenerateConditionalMove(out, src)) {
2914 if (!out.Equals(first) && !out.Equals(second)) {
2915 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2916 }
2917
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002918 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2919
2920 if (IsBooleanValueOrMaterializedCondition(condition)) {
2921 __ Cmp(InputRegisterAt(select, 2), 0);
2922 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2923 } else {
2924 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2925 }
2926
Donghui Bai426b49c2016-11-08 14:55:38 +08002927 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002928 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002929 ExactAssemblyScope guard(GetVIXLAssembler(),
2930 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2931 CodeBufferCheckScope::kExactSize);
2932
2933 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002934 __ it(cond.first);
2935 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002936 } else {
2937 DCHECK(out.IsRegisterPair());
2938
2939 Operand operand_high(0);
2940 Operand operand_low(0);
2941
2942 if (src.IsConstant()) {
2943 const int64_t value = Int64ConstantFrom(src);
2944
2945 operand_high = High32Bits(value);
2946 operand_low = Low32Bits(value);
2947 } else {
2948 DCHECK(src.IsRegisterPair());
2949 operand_high = HighRegisterFrom(src);
2950 operand_low = LowRegisterFrom(src);
2951 }
2952
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002953 __ it(cond.first);
2954 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2955 __ it(cond.first);
2956 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002957 }
2958
2959 return;
2960 }
2961 }
2962
2963 vixl32::Label* false_target = nullptr;
2964 vixl32::Label* true_target = nullptr;
2965 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002966 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08002967 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2968
2969 if (out.Equals(second)) {
2970 true_target = target;
2971 src = first;
2972 } else {
2973 false_target = target;
2974 src = second;
2975
2976 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002977 if (output_overlaps_with_condition_inputs) {
2978 false_target = &other_case;
2979 } else {
2980 codegen_->MoveLocation(out, first, type);
2981 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002982 }
2983 }
2984
Andreas Gampe3db70682018-12-26 15:12:03 -08002985 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target= */ false);
Donghui Bai426b49c2016-11-08 14:55:38 +08002986 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002987 if (output_overlaps_with_condition_inputs) {
2988 __ B(target);
2989 __ Bind(&other_case);
2990 codegen_->MoveLocation(out, first, type);
2991 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002992
2993 if (select_end.IsReferenced()) {
2994 __ Bind(&select_end);
2995 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002996}
2997
Artem Serov551b28f2016-10-18 19:11:30 +01002998void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002999 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003000}
3001
3002void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3003 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3004}
3005
Vladimir Markodec78172020-06-19 15:31:23 +01003006void CodeGeneratorARMVIXL::IncreaseFrame(size_t adjustment) {
3007 __ Claim(adjustment);
3008 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3009}
3010
3011void CodeGeneratorARMVIXL::DecreaseFrame(size_t adjustment) {
3012 __ Drop(adjustment);
3013 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3014}
3015
Scott Wakelingfe885462016-09-22 10:24:38 +01003016void CodeGeneratorARMVIXL::GenerateNop() {
3017 __ Nop();
3018}
3019
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003020// `temp` is an extra temporary register that is used for some conditions;
3021// callers may not specify it, in which case the method will use a scratch
3022// register instead.
3023void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3024 vixl32::Register out,
3025 vixl32::Register in,
3026 vixl32::Register temp) {
3027 switch (condition) {
3028 case kCondEQ:
3029 // x <= 0 iff x == 0 when the comparison is unsigned.
3030 case kCondBE:
3031 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3032 temp = out;
3033 }
3034
3035 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3036 // different as well.
3037 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3038 // temp = - in; only 0 sets the carry flag.
3039 __ Rsbs(temp, in, 0);
3040
3041 if (out.Is(in)) {
3042 std::swap(in, temp);
3043 }
3044
3045 // out = - in + in + carry = carry
3046 __ Adc(out, temp, in);
3047 } else {
3048 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3049 __ Clz(out, in);
3050 // Any number less than 32 logically shifted right by 5 bits results in 0;
3051 // the same operation on 32 yields 1.
3052 __ Lsr(out, out, 5);
3053 }
3054
3055 break;
3056 case kCondNE:
3057 // x > 0 iff x != 0 when the comparison is unsigned.
3058 case kCondA: {
3059 UseScratchRegisterScope temps(GetVIXLAssembler());
3060
3061 if (out.Is(in)) {
3062 if (!temp.IsValid() || in.Is(temp)) {
3063 temp = temps.Acquire();
3064 }
3065 } else if (!temp.IsValid() || !temp.IsLow()) {
3066 temp = out;
3067 }
3068
3069 // temp = in - 1; only 0 does not set the carry flag.
3070 __ Subs(temp, in, 1);
3071 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3072 __ Sbc(out, in, temp);
3073 break;
3074 }
3075 case kCondGE:
3076 __ Mvn(out, in);
3077 in = out;
3078 FALLTHROUGH_INTENDED;
3079 case kCondLT:
3080 // We only care about the sign bit.
3081 __ Lsr(out, in, 31);
3082 break;
3083 case kCondAE:
3084 // Trivially true.
3085 __ Mov(out, 1);
3086 break;
3087 case kCondB:
3088 // Trivially false.
3089 __ Mov(out, 0);
3090 break;
3091 default:
3092 LOG(FATAL) << "Unexpected condition " << condition;
3093 UNREACHABLE();
3094 }
3095}
3096
Scott Wakelingfe885462016-09-22 10:24:38 +01003097void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3098 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003099 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003100 const DataType::Type type = cond->InputAt(0)->GetType();
3101 if (DataType::IsFloatingPointType(type)) {
3102 locations->SetInAt(0, Location::RequiresFpuRegister());
3103 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
3104 } else {
3105 locations->SetInAt(0, Location::RequiresRegister());
3106 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3107 }
3108 if (!cond->IsEmittedAtUseSite()) {
3109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003110 }
3111}
3112
3113void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3114 if (cond->IsEmittedAtUseSite()) {
3115 return;
3116 }
3117
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003118 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003121 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003122 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003126
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003127 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003128
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003129 // A condition with only one boolean input, or two boolean inputs without being equality or
3130 // inequality results from transformations done by the instruction simplifier, and is handled
3131 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 if (type == DataType::Type::kBool &&
3133 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003134 (condition == kCondEQ || condition == kCondNE)) {
3135 vixl32::Register left = InputRegisterAt(cond, 0);
3136 const vixl32::Register out = OutputRegister(cond);
3137 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003138
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003139 // The constant case is handled by the instruction simplifier.
3140 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003141
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003142 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003143
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003144 // Avoid 32-bit instructions if possible.
3145 if (out.Is(right)) {
3146 std::swap(left, right);
3147 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003148
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003149 __ Eor(out, left, right);
3150
3151 if (condition == kCondEQ) {
3152 __ Eor(out, out, 1);
3153 }
3154
3155 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003156 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003157
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003158 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003159}
3160
3161void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3162 HandleCondition(comp);
3163}
3164
3165void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3166 HandleCondition(comp);
3167}
3168
3169void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3170 HandleCondition(comp);
3171}
3172
3173void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3174 HandleCondition(comp);
3175}
3176
3177void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3178 HandleCondition(comp);
3179}
3180
3181void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3182 HandleCondition(comp);
3183}
3184
3185void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3186 HandleCondition(comp);
3187}
3188
3189void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3190 HandleCondition(comp);
3191}
3192
3193void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3194 HandleCondition(comp);
3195}
3196
3197void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3198 HandleCondition(comp);
3199}
3200
3201void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3202 HandleCondition(comp);
3203}
3204
3205void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3206 HandleCondition(comp);
3207}
3208
3209void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3210 HandleCondition(comp);
3211}
3212
3213void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3214 HandleCondition(comp);
3215}
3216
3217void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3218 HandleCondition(comp);
3219}
3220
3221void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3222 HandleCondition(comp);
3223}
3224
3225void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3226 HandleCondition(comp);
3227}
3228
3229void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3230 HandleCondition(comp);
3231}
3232
3233void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3234 HandleCondition(comp);
3235}
3236
3237void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3238 HandleCondition(comp);
3239}
3240
3241void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3242 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003243 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003244 locations->SetOut(Location::ConstantLocation(constant));
3245}
3246
3247void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3248 // Will be generated at use site.
3249}
3250
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003251void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3252 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003253 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003254 locations->SetOut(Location::ConstantLocation(constant));
3255}
3256
3257void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3258 // Will be generated at use site.
3259}
3260
Scott Wakelingfe885462016-09-22 10:24:38 +01003261void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3262 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003263 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003264 locations->SetOut(Location::ConstantLocation(constant));
3265}
3266
3267void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3268 // Will be generated at use site.
3269}
3270
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003271void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3272 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003273 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003274 locations->SetOut(Location::ConstantLocation(constant));
3275}
3276
Scott Wakelingc34dba72016-10-03 10:14:44 +01003277void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3278 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003279 // Will be generated at use site.
3280}
3281
3282void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3283 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003284 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003285 locations->SetOut(Location::ConstantLocation(constant));
3286}
3287
Scott Wakelingc34dba72016-10-03 10:14:44 +01003288void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3289 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003290 // Will be generated at use site.
3291}
3292
Igor Murashkind01745e2017-04-05 16:40:31 -07003293void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3294 constructor_fence->SetLocations(nullptr);
3295}
3296
3297void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3298 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3299 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3300}
3301
Scott Wakelingfe885462016-09-22 10:24:38 +01003302void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3303 memory_barrier->SetLocations(nullptr);
3304}
3305
3306void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3307 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3308}
3309
3310void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3311 ret->SetLocations(nullptr);
3312}
3313
3314void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3315 codegen_->GenerateFrameExit();
3316}
3317
3318void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3319 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003320 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003321 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3322}
3323
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00003324void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret) {
3325 if (GetGraph()->IsCompilingOsr()) {
3326 // To simplify callers of an OSR method, we put the return value in both
3327 // floating point and core registers.
3328 switch (ret->InputAt(0)->GetType()) {
3329 case DataType::Type::kFloat32:
3330 __ Vmov(r0, s0);
3331 break;
3332 case DataType::Type::kFloat64:
3333 __ Vmov(r0, r1, d0);
3334 break;
3335 default:
3336 break;
3337 }
3338 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003339 codegen_->GenerateFrameExit();
3340}
3341
Artem Serovcfbe9132016-10-14 15:58:56 +01003342void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3343 // The trampoline uses the same calling convention as dex calling conventions,
3344 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3345 // the method_idx.
3346 HandleInvoke(invoke);
3347}
3348
3349void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3350 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003351 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003352}
3353
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003354void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3355 // Explicit clinit checks triggered by static invokes must have been pruned by
3356 // art::PrepareForRegisterAllocation.
3357 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3358
Anton Kirilov5ec62182016-10-13 20:16:02 +01003359 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3360 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003361 return;
3362 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003363
Vladimir Marko86c87522020-05-11 16:55:55 +01003364 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
3365 CriticalNativeCallingConventionVisitorARMVIXL calling_convention_visitor(
3366 /*for_register_allocation=*/ true);
3367 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3368 } else {
3369 HandleInvoke(invoke);
3370 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003371}
3372
Anton Kirilov5ec62182016-10-13 20:16:02 +01003373static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3374 if (invoke->GetLocations()->Intrinsified()) {
3375 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3376 intrinsic.Dispatch(invoke);
3377 return true;
3378 }
3379 return false;
3380}
3381
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003382void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3383 // Explicit clinit checks triggered by static invokes must have been pruned by
3384 // art::PrepareForRegisterAllocation.
3385 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3386
Anton Kirilov5ec62182016-10-13 20:16:02 +01003387 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003388 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003389 return;
3390 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003391
3392 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003393 codegen_->GenerateStaticOrDirectCall(
3394 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003395
Andreas Gampe3db70682018-12-26 15:12:03 -08003396 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003397}
3398
3399void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003400 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003401 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3402}
3403
3404void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003405 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3406 if (intrinsic.TryDispatch(invoke)) {
3407 return;
3408 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003409
3410 HandleInvoke(invoke);
3411}
3412
3413void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003414 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003415 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003416 return;
3417 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003418
3419 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003420 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003421
Andreas Gampe3db70682018-12-26 15:12:03 -08003422 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003423}
3424
Artem Serovcfbe9132016-10-14 15:58:56 +01003425void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3426 HandleInvoke(invoke);
3427 // Add the hidden argument.
3428 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3429}
3430
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003431void CodeGeneratorARMVIXL::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
3432 vixl32::Register klass) {
3433 DCHECK_EQ(r0.GetCode(), klass.GetCode());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003434 // We know the destination of an intrinsic, so no need to record inline
3435 // caches.
3436 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00003437 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003438 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003439 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01003440 ScopedProfilingInfoUse spiu(
3441 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
3442 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00003443 if (info != nullptr) {
3444 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
3445 uint32_t address = reinterpret_cast32<uint32_t>(cache);
3446 vixl32::Label done;
3447 UseScratchRegisterScope temps(GetVIXLAssembler());
3448 temps.Exclude(ip);
3449 __ Mov(r4, address);
3450 __ Ldr(ip, MemOperand(r4, InlineCache::ClassesOffset().Int32Value()));
3451 // Fast path for a monomorphic cache.
3452 __ Cmp(klass, ip);
3453 __ B(eq, &done, /* is_far_target= */ false);
3454 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
3455 __ Bind(&done);
3456 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003457 }
3458}
3459
Artem Serovcfbe9132016-10-14 15:58:56 +01003460void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3461 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3462 LocationSummary* locations = invoke->GetLocations();
3463 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3464 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3465 Location receiver = locations->InAt(0);
3466 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3467
3468 DCHECK(!receiver.IsStackSlot());
3469
Alexandre Rames374ddf32016-11-04 10:40:49 +00003470 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3471 {
Artem Serov0fb37192016-12-06 18:13:40 +00003472 ExactAssemblyScope aas(GetVIXLAssembler(),
3473 vixl32::kMaxInstructionSizeInBytes,
3474 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003475 // /* HeapReference<Class> */ temp = receiver->klass_
3476 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3477 codegen_->MaybeRecordImplicitNullCheck(invoke);
3478 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003479 // Instead of simply (possibly) unpoisoning `temp` here, we should
3480 // emit a read barrier for the previous class reference load.
3481 // However this is not required in practice, as this is an
3482 // intermediate/temporary reference and because the current
3483 // concurrent copying collector keeps the from-space memory
3484 // intact/accessible until the end of the marking phase (the
3485 // concurrent copying collector may not in the future).
3486 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003487
3488 // If we're compiling baseline, update the inline cache.
3489 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
3490
Artem Serovcfbe9132016-10-14 15:58:56 +01003491 GetAssembler()->LoadFromOffset(kLoadWord,
3492 temp,
3493 temp,
3494 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003495
Artem Serovcfbe9132016-10-14 15:58:56 +01003496 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3497 invoke->GetImtIndex(), kArmPointerSize));
3498 // temp = temp->GetImtEntryAt(method_offset);
3499 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3500 uint32_t entry_point =
3501 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3502 // LR = temp->GetEntryPoint();
3503 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3504
3505 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3506 // instruction from clobbering it as they might use r12 as a scratch register.
3507 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003508
3509 {
3510 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3511 // so it checks if the application is using them (by passing them to the macro assembler
3512 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3513 // what is available, and is the opposite of the standard usage: Instead of requesting a
3514 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3515 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3516 // (to materialize the constant), since the destination register becomes available for such use
3517 // internally for the duration of the macro instruction.
3518 UseScratchRegisterScope temps(GetVIXLAssembler());
3519 temps.Exclude(hidden_reg);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01003520 __ Mov(hidden_reg, invoke->GetMethodReference().index);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003521 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003522 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003523 // Ensure the pc position is recorded immediately after the `blx` instruction.
3524 // 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 +00003525 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003526 vixl32::k16BitT32InstructionSizeInBytes,
3527 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003528 // LR();
3529 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003530 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003531 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003532 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003533
Andreas Gampe3db70682018-12-26 15:12:03 -08003534 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003535}
3536
Orion Hodsonac141392017-01-13 11:53:47 +00003537void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003538 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3539 if (intrinsic.TryDispatch(invoke)) {
3540 return;
3541 }
Orion Hodsonac141392017-01-13 11:53:47 +00003542 HandleInvoke(invoke);
3543}
3544
3545void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003546 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3547 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 9);
3548 return;
3549 }
Orion Hodsonac141392017-01-13 11:53:47 +00003550 codegen_->GenerateInvokePolymorphicCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003551 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 10);
Orion Hodsonac141392017-01-13 11:53:47 +00003552}
3553
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003554void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3555 HandleInvoke(invoke);
3556}
3557
3558void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3559 codegen_->GenerateInvokeCustomCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003560 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 11);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003561}
3562
Artem Serov02109dd2016-09-23 17:17:54 +01003563void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3564 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003565 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003566 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003568 locations->SetInAt(0, Location::RequiresRegister());
3569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3570 break;
3571 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003572 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003573 locations->SetInAt(0, Location::RequiresRegister());
3574 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3575 break;
3576 }
3577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 case DataType::Type::kFloat32:
3579 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003580 locations->SetInAt(0, Location::RequiresFpuRegister());
3581 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3582 break;
3583
3584 default:
3585 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3586 }
3587}
3588
3589void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3590 LocationSummary* locations = neg->GetLocations();
3591 Location out = locations->Out();
3592 Location in = locations->InAt(0);
3593 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003595 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3596 break;
3597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003598 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003599 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3600 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3601 // We cannot emit an RSC (Reverse Subtract with Carry)
3602 // instruction here, as it does not exist in the Thumb-2
3603 // instruction set. We use the following approach
3604 // using SBC and SUB instead.
3605 //
3606 // out.hi = -C
3607 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3608 // out.hi = out.hi - in.hi
3609 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3610 break;
3611
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003612 case DataType::Type::kFloat32:
3613 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003614 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003615 break;
3616
3617 default:
3618 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3619 }
3620}
3621
Scott Wakelingfe885462016-09-22 10:24:38 +01003622void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003623 DataType::Type result_type = conversion->GetResultType();
3624 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003625 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3626 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003627
3628 // The float-to-long, double-to-long and long-to-float type conversions
3629 // rely on a call to the runtime.
3630 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3632 && result_type == DataType::Type::kInt64)
3633 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003634 ? LocationSummary::kCallOnMainOnly
3635 : LocationSummary::kNoCall;
3636 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003637 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003638
Scott Wakelingfe885462016-09-22 10:24:38 +01003639 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003640 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003641 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003642 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003643 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003644 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3645 locations->SetInAt(0, Location::RequiresRegister());
3646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003647 break;
3648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003650 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003651 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003652 locations->SetInAt(0, Location::Any());
3653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3654 break;
3655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003657 locations->SetInAt(0, Location::RequiresFpuRegister());
3658 locations->SetOut(Location::RequiresRegister());
3659 locations->AddTemp(Location::RequiresFpuRegister());
3660 break;
3661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003663 locations->SetInAt(0, Location::RequiresFpuRegister());
3664 locations->SetOut(Location::RequiresRegister());
3665 locations->AddTemp(Location::RequiresFpuRegister());
3666 break;
3667
3668 default:
3669 LOG(FATAL) << "Unexpected type conversion from " << input_type
3670 << " to " << result_type;
3671 }
3672 break;
3673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003674 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003675 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003677 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003679 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 case DataType::Type::kInt16:
3681 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003682 locations->SetInAt(0, Location::RequiresRegister());
3683 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3684 break;
3685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003687 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3688 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3689 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003690 break;
3691 }
3692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003694 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3695 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3696 calling_convention.GetFpuRegisterAt(1)));
3697 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003698 break;
3699 }
3700
3701 default:
3702 LOG(FATAL) << "Unexpected type conversion from " << input_type
3703 << " to " << result_type;
3704 }
3705 break;
3706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003707 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003708 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003709 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003710 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003712 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003713 case DataType::Type::kInt16:
3714 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003715 locations->SetInAt(0, Location::RequiresRegister());
3716 locations->SetOut(Location::RequiresFpuRegister());
3717 break;
3718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003719 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003720 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3721 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3722 calling_convention.GetRegisterAt(1)));
3723 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003724 break;
3725 }
3726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003728 locations->SetInAt(0, Location::RequiresFpuRegister());
3729 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3730 break;
3731
3732 default:
3733 LOG(FATAL) << "Unexpected type conversion from " << input_type
3734 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003735 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003736 break;
3737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003739 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003740 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003741 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003742 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003743 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003744 case DataType::Type::kInt16:
3745 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003746 locations->SetInAt(0, Location::RequiresRegister());
3747 locations->SetOut(Location::RequiresFpuRegister());
3748 break;
3749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003750 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003751 locations->SetInAt(0, Location::RequiresRegister());
3752 locations->SetOut(Location::RequiresFpuRegister());
3753 locations->AddTemp(Location::RequiresFpuRegister());
3754 locations->AddTemp(Location::RequiresFpuRegister());
3755 break;
3756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 locations->SetInAt(0, Location::RequiresFpuRegister());
3759 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3760 break;
3761
3762 default:
3763 LOG(FATAL) << "Unexpected type conversion from " << input_type
3764 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003765 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003766 break;
3767
3768 default:
3769 LOG(FATAL) << "Unexpected type conversion from " << input_type
3770 << " to " << result_type;
3771 }
3772}
3773
3774void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3775 LocationSummary* locations = conversion->GetLocations();
3776 Location out = locations->Out();
3777 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003778 DataType::Type result_type = conversion->GetResultType();
3779 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003780 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3781 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003782 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003783 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003784 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003785 case DataType::Type::kInt8:
3786 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kInt16:
3788 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003789 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3790 break;
3791 case DataType::Type::kInt64:
3792 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3793 break;
3794
3795 default:
3796 LOG(FATAL) << "Unexpected type conversion from " << input_type
3797 << " to " << result_type;
3798 }
3799 break;
3800
3801 case DataType::Type::kInt8:
3802 switch (input_type) {
3803 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003805 case DataType::Type::kInt16:
3806 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003807 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3808 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003809 case DataType::Type::kInt64:
3810 __ Sbfx(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::kUint16:
3820 switch (input_type) {
3821 case DataType::Type::kInt8:
3822 case DataType::Type::kInt16:
3823 case DataType::Type::kInt32:
3824 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3825 break;
3826 case DataType::Type::kInt64:
3827 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3828 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003829
3830 default:
3831 LOG(FATAL) << "Unexpected type conversion from " << input_type
3832 << " to " << result_type;
3833 }
3834 break;
3835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003836 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003837 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003839 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003840 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3841 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003842 case DataType::Type::kInt64:
3843 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3844 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003845
3846 default:
3847 LOG(FATAL) << "Unexpected type conversion from " << input_type
3848 << " to " << result_type;
3849 }
3850 break;
3851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003853 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003855 DCHECK(out.IsRegister());
3856 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003857 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003858 } else if (in.IsDoubleStackSlot()) {
3859 GetAssembler()->LoadFromOffset(kLoadWord,
3860 OutputRegister(conversion),
3861 sp,
3862 in.GetStackIndex());
3863 } else {
3864 DCHECK(in.IsConstant());
3865 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003866 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3867 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003868 }
3869 break;
3870
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003871 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003872 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003873 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003874 __ Vmov(OutputRegister(conversion), temp);
3875 break;
3876 }
3877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003878 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003879 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003880 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003881 __ Vmov(OutputRegister(conversion), temp_s);
3882 break;
3883 }
3884
3885 default:
3886 LOG(FATAL) << "Unexpected type conversion from " << input_type
3887 << " to " << result_type;
3888 }
3889 break;
3890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003892 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003893 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003894 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003895 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003896 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003897 case DataType::Type::kInt16:
3898 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003899 DCHECK(out.IsRegisterPair());
3900 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003901 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003902 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003903 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003904 break;
3905
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003906 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003907 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3908 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3909 break;
3910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003912 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3913 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3914 break;
3915
3916 default:
3917 LOG(FATAL) << "Unexpected type conversion from " << input_type
3918 << " to " << result_type;
3919 }
3920 break;
3921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003922 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003923 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003924 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003925 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003926 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003927 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kInt16:
3929 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003930 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003931 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003932 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003935 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3936 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3937 break;
3938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003939 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003940 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003941 break;
3942
3943 default:
3944 LOG(FATAL) << "Unexpected type conversion from " << input_type
3945 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003946 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003947 break;
3948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003949 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003950 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003951 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003952 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003954 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003955 case DataType::Type::kInt16:
3956 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003957 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003958 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003959 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003961 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003962 vixl32::Register low = LowRegisterFrom(in);
3963 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003964 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003965 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003966 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003967 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003968 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003969
3970 // temp_d = int-to-double(high)
3971 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003972 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003973 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003974 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003975 // out_d = unsigned-to-double(low)
3976 __ Vmov(out_s, low);
3977 __ Vcvt(F64, U32, out_d, out_s);
3978 // out_d += temp_d * constant_d
3979 __ Vmla(F64, out_d, temp_d, constant_d);
3980 break;
3981 }
3982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003983 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003984 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003985 break;
3986
3987 default:
3988 LOG(FATAL) << "Unexpected type conversion from " << input_type
3989 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003990 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003991 break;
3992
3993 default:
3994 LOG(FATAL) << "Unexpected type conversion from " << input_type
3995 << " to " << result_type;
3996 }
3997}
3998
3999void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4000 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004001 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004002 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004003 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004004 locations->SetInAt(0, Location::RequiresRegister());
4005 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4006 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4007 break;
4008 }
4009
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004010 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004011 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004012 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4014 break;
4015 }
4016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kFloat32:
4018 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004019 locations->SetInAt(0, Location::RequiresFpuRegister());
4020 locations->SetInAt(1, Location::RequiresFpuRegister());
4021 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4022 break;
4023 }
4024
4025 default:
4026 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4027 }
4028}
4029
4030void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4031 LocationSummary* locations = add->GetLocations();
4032 Location out = locations->Out();
4033 Location first = locations->InAt(0);
4034 Location second = locations->InAt(1);
4035
4036 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004037 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004038 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4039 }
4040 break;
4041
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004042 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004043 if (second.IsConstant()) {
4044 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4045 GenerateAddLongConst(out, first, value);
4046 } else {
4047 DCHECK(second.IsRegisterPair());
4048 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4049 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4050 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004051 break;
4052 }
4053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 case DataType::Type::kFloat32:
4055 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004056 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004057 break;
4058
4059 default:
4060 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4061 }
4062}
4063
4064void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4065 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004066 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004067 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004069 locations->SetInAt(0, Location::RequiresRegister());
4070 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4072 break;
4073 }
4074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004076 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004077 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4079 break;
4080 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004081 case DataType::Type::kFloat32:
4082 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004083 locations->SetInAt(0, Location::RequiresFpuRegister());
4084 locations->SetInAt(1, Location::RequiresFpuRegister());
4085 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4086 break;
4087 }
4088 default:
4089 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4090 }
4091}
4092
4093void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4094 LocationSummary* locations = sub->GetLocations();
4095 Location out = locations->Out();
4096 Location first = locations->InAt(0);
4097 Location second = locations->InAt(1);
4098 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004099 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004100 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004101 break;
4102 }
4103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004104 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004105 if (second.IsConstant()) {
4106 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4107 GenerateAddLongConst(out, first, -value);
4108 } else {
4109 DCHECK(second.IsRegisterPair());
4110 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4111 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4112 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004113 break;
4114 }
4115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004116 case DataType::Type::kFloat32:
4117 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004118 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004119 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004120
4121 default:
4122 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4123 }
4124}
4125
4126void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4127 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004128 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004129 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004130 case DataType::Type::kInt32:
4131 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004132 locations->SetInAt(0, Location::RequiresRegister());
4133 locations->SetInAt(1, Location::RequiresRegister());
4134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4135 break;
4136 }
4137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004138 case DataType::Type::kFloat32:
4139 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004140 locations->SetInAt(0, Location::RequiresFpuRegister());
4141 locations->SetInAt(1, Location::RequiresFpuRegister());
4142 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4143 break;
4144 }
4145
4146 default:
4147 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4148 }
4149}
4150
4151void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4152 LocationSummary* locations = mul->GetLocations();
4153 Location out = locations->Out();
4154 Location first = locations->InAt(0);
4155 Location second = locations->InAt(1);
4156 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004157 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004158 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4159 break;
4160 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004161 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004162 vixl32::Register out_hi = HighRegisterFrom(out);
4163 vixl32::Register out_lo = LowRegisterFrom(out);
4164 vixl32::Register in1_hi = HighRegisterFrom(first);
4165 vixl32::Register in1_lo = LowRegisterFrom(first);
4166 vixl32::Register in2_hi = HighRegisterFrom(second);
4167 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004168
4169 // Extra checks to protect caused by the existence of R1_R2.
4170 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4171 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004172 DCHECK(!out_hi.Is(in1_lo));
4173 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004174
4175 // input: in1 - 64 bits, in2 - 64 bits
4176 // output: out
4177 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4178 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4179 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4180
4181 UseScratchRegisterScope temps(GetVIXLAssembler());
4182 vixl32::Register temp = temps.Acquire();
4183 // temp <- in1.lo * in2.hi
4184 __ Mul(temp, in1_lo, in2_hi);
4185 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4186 __ Mla(out_hi, in1_hi, in2_lo, temp);
4187 // out.lo <- (in1.lo * in2.lo)[31:0];
4188 __ Umull(out_lo, temp, in1_lo, in2_lo);
4189 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004190 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004191 break;
4192 }
4193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004194 case DataType::Type::kFloat32:
4195 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004196 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004197 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004198
4199 default:
4200 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4201 }
4202}
4203
Scott Wakelingfe885462016-09-22 10:24:38 +01004204void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4205 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004206 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004207
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004208 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004209 DCHECK(second.IsConstant());
4210
4211 vixl32::Register out = OutputRegister(instruction);
4212 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004213 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004214 DCHECK(imm == 1 || imm == -1);
4215
4216 if (instruction->IsRem()) {
4217 __ Mov(out, 0);
4218 } else {
4219 if (imm == 1) {
4220 __ Mov(out, dividend);
4221 } else {
4222 __ Rsb(out, dividend, 0);
4223 }
4224 }
4225}
4226
4227void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4228 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004229 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004230
4231 LocationSummary* locations = instruction->GetLocations();
4232 Location second = locations->InAt(1);
4233 DCHECK(second.IsConstant());
4234
4235 vixl32::Register out = OutputRegister(instruction);
4236 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004237 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004238 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4239 int ctz_imm = CTZ(abs_imm);
4240
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004241 auto generate_div_code = [this, imm, ctz_imm](vixl32::Register out, vixl32::Register in) {
4242 __ Asr(out, in, ctz_imm);
Scott Wakelingfe885462016-09-22 10:24:38 +01004243 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004244 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004245 }
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004246 };
4247
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004248 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004249 // No need to adjust the result for non-negative dividends or the INT32_MIN dividend.
4250 // NOTE: The generated code for HDiv/HRem correctly works for the INT32_MIN dividend:
4251 // imm == 2
4252 // HDiv
4253 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
4254 // asr out, out(0x80000001), #1 => out = 0xc0000000
4255 // This is the same as 'asr out, dividend(0x80000000), #1'
4256 //
4257 // imm > 2
4258 // HDiv
4259 // asr out, dividend(0x80000000), #31 => out = -1
4260 // add out, dividend(0x80000000), out(-1), lsr #(32 - ctz_imm) => out = 0b10..01..1,
4261 // where the number of the rightmost 1s is ctz_imm.
4262 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
4263 // leftmost 1s is ctz_imm + 1.
4264 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
4265 //
4266 // imm == INT32_MIN
4267 // HDiv
4268 // asr out, dividend(0x80000000), #31 => out = -1
4269 // add out, dividend(0x80000000), out(-1), lsr #1 => out = 0xc0000000
4270 // asr out, out(0xc0000000), #31 => out = -1
4271 // rsb out, out(-1), #0 => out = 1
4272 // This is the same as
4273 // asr out, dividend(0x80000000), #31
4274 // rsb out, out, #0
4275 //
4276 //
4277 // INT_MIN % imm must be 0 for any imm of power 2. 'and' and 'ubfx' work only with bits
4278 // 0..30 of a dividend. For INT32_MIN those bits are zeros. So 'and' and 'ubfx' always
4279 // produce zero.
4280 if (instruction->IsDiv()) {
4281 generate_div_code(out, dividend);
4282 } else {
4283 if (GetVIXLAssembler()->IsModifiedImmediate(abs_imm - 1)) {
4284 __ And(out, dividend, abs_imm - 1);
4285 } else {
4286 __ Ubfx(out, dividend, 0, ctz_imm);
4287 }
4288 return;
4289 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004290 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004291 vixl32::Register add_right_input = dividend;
4292 if (ctz_imm > 1) {
4293 __ Asr(out, dividend, 31);
4294 add_right_input = out;
4295 }
4296 __ Add(out, dividend, Operand(add_right_input, vixl32::LSR, 32 - ctz_imm));
4297
4298 if (instruction->IsDiv()) {
4299 generate_div_code(out, out);
4300 } else {
4301 __ Bfc(out, 0, ctz_imm);
4302 __ Sub(out, dividend, out);
4303 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004304 }
4305}
4306
4307void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4308 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004309 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004310
4311 LocationSummary* locations = instruction->GetLocations();
4312 Location second = locations->InAt(1);
4313 DCHECK(second.IsConstant());
4314
4315 vixl32::Register out = OutputRegister(instruction);
4316 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004317 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4318 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004319 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004320
4321 int64_t magic;
4322 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004323 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Scott Wakelingfe885462016-09-22 10:24:38 +01004324
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004325 auto generate_unsigned_div_code =[this, magic, shift](vixl32::Register out,
4326 vixl32::Register dividend,
4327 vixl32::Register temp1,
4328 vixl32::Register temp2) {
4329 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4330 __ Mov(temp1, static_cast<int32_t>(magic));
4331 if (magic > 0 && shift == 0) {
4332 __ Smull(temp2, out, dividend, temp1);
4333 } else {
4334 __ Smull(temp2, temp1, dividend, temp1);
4335 if (magic < 0) {
4336 // The negative magic M = static_cast<int>(m) means that the multiplier m is greater
4337 // than INT32_MAX. In such a case shift is never 0.
4338 // Proof:
4339 // m = (2^p + d - 2^p % d) / d, where p = 32 + shift, d > 2
4340 //
4341 // If shift == 0, m = (2^32 + d - 2^32 % d) / d =
4342 // = (2^32 + d - (2^32 - (2^32 / d) * d)) / d =
4343 // = (d + (2^32 / d) * d) / d = 1 + (2^32 / d), here '/' is the integer division.
4344 //
4345 // 1 + (2^32 / d) is decreasing when d is increasing.
4346 // The maximum is 1 431 655 766, when d == 3. This value is less than INT32_MAX.
4347 // the minimum is 3, when d = 2^31 -1.
4348 // So for all values of d in [3, INT32_MAX] m with p == 32 is in [3, INT32_MAX) and
4349 // is never less than 0.
4350 __ Add(temp1, temp1, dividend);
4351 }
4352 DCHECK_NE(shift, 0);
4353 __ Lsr(out, temp1, shift);
4354 }
4355 };
Scott Wakelingfe885462016-09-22 10:24:38 +01004356
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004357 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004358 // No need to adjust the result for a non-negative dividend and a positive divisor.
4359 if (instruction->IsDiv()) {
4360 generate_unsigned_div_code(out, dividend, temp1, temp2);
4361 } else {
4362 generate_unsigned_div_code(temp1, dividend, temp1, temp2);
4363 __ Mov(temp2, imm);
4364 __ Mls(out, temp1, temp2, dividend);
4365 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004366 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004367 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4368 __ Mov(temp1, static_cast<int32_t>(magic));
4369 __ Smull(temp2, temp1, dividend, temp1);
4370
4371 if (imm > 0 && magic < 0) {
4372 __ Add(temp1, temp1, dividend);
4373 } else if (imm < 0 && magic > 0) {
4374 __ Sub(temp1, temp1, dividend);
4375 }
4376
4377 if (shift != 0) {
4378 __ Asr(temp1, temp1, shift);
4379 }
4380
4381 if (instruction->IsDiv()) {
4382 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4383 } else {
4384 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4385 // TODO: Strength reduction for mls.
4386 __ Mov(temp2, imm);
4387 __ Mls(out, temp1, temp2, dividend);
4388 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004389 }
4390}
4391
4392void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4393 HBinaryOperation* instruction) {
4394 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004395 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004396
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004397 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004398 DCHECK(second.IsConstant());
4399
Anton Kirilov644032c2016-12-06 17:51:43 +00004400 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004401 if (imm == 0) {
4402 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4403 } else if (imm == 1 || imm == -1) {
4404 DivRemOneOrMinusOne(instruction);
4405 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4406 DivRemByPowerOfTwo(instruction);
4407 } else {
4408 DCHECK(imm <= -2 || imm >= 2);
4409 GenerateDivRemWithAnyConstant(instruction);
4410 }
4411}
4412
4413void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4414 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004415 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004416 // pLdiv runtime call.
4417 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004418 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004419 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004420 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004421 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4422 // pIdivmod runtime call.
4423 call_kind = LocationSummary::kCallOnMainOnly;
4424 }
4425
Vladimir Markoca6fff82017-10-03 14:49:14 +01004426 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004427
4428 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004429 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004430 if (div->InputAt(1)->IsConstant()) {
4431 locations->SetInAt(0, Location::RequiresRegister());
4432 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004433 int32_t value = Int32ConstantFrom(div->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004434 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004435 if (value == 1 || value == 0 || value == -1) {
4436 // No temp register required.
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004437 } else if (IsPowerOfTwo(AbsOrMin(value)) &&
4438 value != 2 &&
4439 value != -2 &&
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004440 !HasNonNegativeOrMinIntInputAt(div, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004441 // The "out" register is used as a temporary, so it overlaps with the inputs.
4442 out_overlaps = Location::kOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004443 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004444 locations->AddRegisterTemps(2);
Scott Wakelingfe885462016-09-22 10:24:38 +01004445 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004446 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Scott Wakelingfe885462016-09-22 10:24:38 +01004447 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4448 locations->SetInAt(0, Location::RequiresRegister());
4449 locations->SetInAt(1, Location::RequiresRegister());
4450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4451 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004452 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4453 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4454 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004455 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004456 // we only need the former.
4457 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004458 }
4459 break;
4460 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004461 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004462 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4463 locations->SetInAt(0, LocationFrom(
4464 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4465 locations->SetInAt(1, LocationFrom(
4466 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4467 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004468 break;
4469 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004470 case DataType::Type::kFloat32:
4471 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004472 locations->SetInAt(0, Location::RequiresFpuRegister());
4473 locations->SetInAt(1, Location::RequiresFpuRegister());
4474 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4475 break;
4476 }
4477
4478 default:
4479 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4480 }
4481}
4482
4483void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004484 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004485 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004486
4487 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004488 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004489 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004490 GenerateDivRemConstantIntegral(div);
4491 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4492 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4493 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004494 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4495 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4496 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4497 DCHECK(r0.Is(OutputRegister(div)));
4498
4499 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4500 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004501 }
4502 break;
4503 }
4504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004505 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004506 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4507 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4508 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4509 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4510 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4511 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4512 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4513
4514 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4515 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004516 break;
4517 }
4518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004519 case DataType::Type::kFloat32:
4520 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004521 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004522 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004523
4524 default:
4525 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4526 }
4527}
4528
Artem Serov551b28f2016-10-18 19:11:30 +01004529void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004530 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004531
4532 // Most remainders are implemented in the runtime.
4533 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004534 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004535 // sdiv will be replaced by other instruction sequence.
4536 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004537 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004538 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4539 // Have hardware divide instruction for int, do it with three instructions.
4540 call_kind = LocationSummary::kNoCall;
4541 }
4542
Vladimir Markoca6fff82017-10-03 14:49:14 +01004543 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004544
4545 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004546 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004547 if (rem->InputAt(1)->IsConstant()) {
4548 locations->SetInAt(0, Location::RequiresRegister());
4549 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004550 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004551 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004552 if (value == 1 || value == 0 || value == -1) {
4553 // No temp register required.
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004554 } else if (IsPowerOfTwo(AbsOrMin(value)) && !HasNonNegativeOrMinIntInputAt(rem, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004555 // The "out" register is used as a temporary, so it overlaps with the inputs.
4556 out_overlaps = Location::kOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004557 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004558 locations->AddRegisterTemps(2);
Artem Serov551b28f2016-10-18 19:11:30 +01004559 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004560 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Artem Serov551b28f2016-10-18 19:11:30 +01004561 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4562 locations->SetInAt(0, Location::RequiresRegister());
4563 locations->SetInAt(1, Location::RequiresRegister());
4564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4565 locations->AddTemp(Location::RequiresRegister());
4566 } else {
4567 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4568 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4569 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004570 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004571 // we only need the latter.
4572 locations->SetOut(LocationFrom(r1));
4573 }
4574 break;
4575 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004576 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004577 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4578 locations->SetInAt(0, LocationFrom(
4579 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4580 locations->SetInAt(1, LocationFrom(
4581 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4582 // The runtime helper puts the output in R2,R3.
4583 locations->SetOut(LocationFrom(r2, r3));
4584 break;
4585 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004586 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004587 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4588 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4589 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4590 locations->SetOut(LocationFrom(s0));
4591 break;
4592 }
4593
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004594 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004595 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4596 locations->SetInAt(0, LocationFrom(
4597 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4598 locations->SetInAt(1, LocationFrom(
4599 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4600 locations->SetOut(LocationFrom(s0, s1));
4601 break;
4602 }
4603
4604 default:
4605 LOG(FATAL) << "Unexpected rem type " << type;
4606 }
4607}
4608
4609void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4610 LocationSummary* locations = rem->GetLocations();
4611 Location second = locations->InAt(1);
4612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004613 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004614 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004615 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004616 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4617 vixl32::Register out_reg = OutputRegister(rem);
4618 if (second.IsConstant()) {
4619 GenerateDivRemConstantIntegral(rem);
4620 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4621 vixl32::Register reg2 = RegisterFrom(second);
4622 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4623
4624 // temp = reg1 / reg2 (integer division)
4625 // dest = reg1 - temp * reg2
4626 __ Sdiv(temp, reg1, reg2);
4627 __ Mls(out_reg, temp, reg2, reg1);
4628 } else {
4629 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4630 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4631 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4632 DCHECK(out_reg.Is(r1));
4633
4634 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4635 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4636 }
4637 break;
4638 }
4639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004640 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004641 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4642 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4643 break;
4644 }
4645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004646 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004647 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4648 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4649 break;
4650 }
4651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004652 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004653 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4654 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4655 break;
4656 }
4657
4658 default:
4659 LOG(FATAL) << "Unexpected rem type " << type;
4660 }
4661}
4662
Aart Bik1f8d51b2018-02-15 10:42:37 -08004663static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4664 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4665 switch (minmax->GetResultType()) {
4666 case DataType::Type::kInt32:
4667 locations->SetInAt(0, Location::RequiresRegister());
4668 locations->SetInAt(1, Location::RequiresRegister());
4669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4670 break;
4671 case DataType::Type::kInt64:
4672 locations->SetInAt(0, Location::RequiresRegister());
4673 locations->SetInAt(1, Location::RequiresRegister());
4674 locations->SetOut(Location::SameAsFirstInput());
4675 break;
4676 case DataType::Type::kFloat32:
4677 locations->SetInAt(0, Location::RequiresFpuRegister());
4678 locations->SetInAt(1, Location::RequiresFpuRegister());
4679 locations->SetOut(Location::SameAsFirstInput());
4680 locations->AddTemp(Location::RequiresRegister());
4681 break;
4682 case DataType::Type::kFloat64:
4683 locations->SetInAt(0, Location::RequiresFpuRegister());
4684 locations->SetInAt(1, Location::RequiresFpuRegister());
4685 locations->SetOut(Location::SameAsFirstInput());
4686 break;
4687 default:
4688 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4689 }
4690}
4691
Aart Bik351df3e2018-03-07 11:54:57 -08004692void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004693 Location op1_loc = locations->InAt(0);
4694 Location op2_loc = locations->InAt(1);
4695 Location out_loc = locations->Out();
4696
4697 vixl32::Register op1 = RegisterFrom(op1_loc);
4698 vixl32::Register op2 = RegisterFrom(op2_loc);
4699 vixl32::Register out = RegisterFrom(out_loc);
4700
4701 __ Cmp(op1, op2);
4702
4703 {
4704 ExactAssemblyScope aas(GetVIXLAssembler(),
4705 3 * kMaxInstructionSizeInBytes,
4706 CodeBufferCheckScope::kMaximumSize);
4707
4708 __ ite(is_min ? lt : gt);
4709 __ mov(is_min ? lt : gt, out, op1);
4710 __ mov(is_min ? ge : le, out, op2);
4711 }
4712}
4713
4714void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4715 Location op1_loc = locations->InAt(0);
4716 Location op2_loc = locations->InAt(1);
4717 Location out_loc = locations->Out();
4718
4719 // Optimization: don't generate any code if inputs are the same.
4720 if (op1_loc.Equals(op2_loc)) {
4721 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4722 return;
4723 }
4724
4725 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4726 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4727 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4728 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4729 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4730 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4731 UseScratchRegisterScope temps(GetVIXLAssembler());
4732 const vixl32::Register temp = temps.Acquire();
4733
4734 DCHECK(op1_lo.Is(out_lo));
4735 DCHECK(op1_hi.Is(out_hi));
4736
4737 // Compare op1 >= op2, or op1 < op2.
4738 __ Cmp(out_lo, op2_lo);
4739 __ Sbcs(temp, out_hi, op2_hi);
4740
4741 // Now GE/LT condition code is correct for the long comparison.
4742 {
4743 vixl32::ConditionType cond = is_min ? ge : lt;
4744 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4745 3 * kMaxInstructionSizeInBytes,
4746 CodeBufferCheckScope::kMaximumSize);
4747 __ itt(cond);
4748 __ mov(cond, out_lo, op2_lo);
4749 __ mov(cond, out_hi, op2_hi);
4750 }
4751}
4752
Aart Bik351df3e2018-03-07 11:54:57 -08004753void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4754 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004755 Location op1_loc = locations->InAt(0);
4756 Location op2_loc = locations->InAt(1);
4757 Location out_loc = locations->Out();
4758
4759 // Optimization: don't generate any code if inputs are the same.
4760 if (op1_loc.Equals(op2_loc)) {
4761 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4762 return;
4763 }
4764
4765 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4766 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4767 vixl32::SRegister out = SRegisterFrom(out_loc);
4768
4769 UseScratchRegisterScope temps(GetVIXLAssembler());
4770 const vixl32::Register temp1 = temps.Acquire();
4771 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4772 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004773 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004774
4775 DCHECK(op1.Is(out));
4776
4777 __ Vcmp(op1, op2);
4778 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004779 __ B(vs, &nan, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004780
4781 // op1 <> op2
4782 vixl32::ConditionType cond = is_min ? gt : lt;
4783 {
4784 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4785 2 * kMaxInstructionSizeInBytes,
4786 CodeBufferCheckScope::kMaximumSize);
4787 __ it(cond);
4788 __ vmov(cond, F32, out, op2);
4789 }
4790 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004791 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004792
4793 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4794 __ Vmov(temp1, op1);
4795 __ Vmov(temp2, op2);
4796 if (is_min) {
4797 __ Orr(temp1, temp1, temp2);
4798 } else {
4799 __ And(temp1, temp1, temp2);
4800 }
4801 __ Vmov(out, temp1);
4802 __ B(final_label);
4803
4804 // handle NaN input.
4805 __ Bind(&nan);
4806 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4807 __ Vmov(out, temp1);
4808
4809 if (done.IsReferenced()) {
4810 __ Bind(&done);
4811 }
4812}
4813
Aart Bik351df3e2018-03-07 11:54:57 -08004814void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4815 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004816 Location op1_loc = locations->InAt(0);
4817 Location op2_loc = locations->InAt(1);
4818 Location out_loc = locations->Out();
4819
4820 // Optimization: don't generate any code if inputs are the same.
4821 if (op1_loc.Equals(op2_loc)) {
4822 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4823 return;
4824 }
4825
4826 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4827 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4828 vixl32::DRegister out = DRegisterFrom(out_loc);
4829 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004830 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004831
4832 DCHECK(op1.Is(out));
4833
4834 __ Vcmp(op1, op2);
4835 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004836 __ B(vs, &handle_nan_eq, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004837
4838 // op1 <> op2
4839 vixl32::ConditionType cond = is_min ? gt : lt;
4840 {
4841 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4842 2 * kMaxInstructionSizeInBytes,
4843 CodeBufferCheckScope::kMaximumSize);
4844 __ it(cond);
4845 __ vmov(cond, F64, out, op2);
4846 }
4847 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004848 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004849
4850 // handle op1 == op2, max(+0.0,-0.0).
4851 if (!is_min) {
4852 __ Vand(F64, out, op1, op2);
4853 __ B(final_label);
4854 }
4855
4856 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4857 __ Bind(&handle_nan_eq);
4858 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4859
4860 if (done.IsReferenced()) {
4861 __ Bind(&done);
4862 }
4863}
4864
Aart Bik351df3e2018-03-07 11:54:57 -08004865void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4866 DataType::Type type = minmax->GetResultType();
4867 switch (type) {
4868 case DataType::Type::kInt32:
4869 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4870 break;
4871 case DataType::Type::kInt64:
4872 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4873 break;
4874 case DataType::Type::kFloat32:
4875 GenerateMinMaxFloat(minmax, is_min);
4876 break;
4877 case DataType::Type::kFloat64:
4878 GenerateMinMaxDouble(minmax, is_min);
4879 break;
4880 default:
4881 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4882 }
4883}
4884
Aart Bik1f8d51b2018-02-15 10:42:37 -08004885void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4886 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4887}
4888
4889void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004890 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004891}
4892
4893void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4894 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4895}
4896
4897void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004898 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004899}
4900
Aart Bik3dad3412018-02-28 12:01:46 -08004901void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4902 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4903 switch (abs->GetResultType()) {
4904 case DataType::Type::kInt32:
4905 case DataType::Type::kInt64:
4906 locations->SetInAt(0, Location::RequiresRegister());
4907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4908 locations->AddTemp(Location::RequiresRegister());
4909 break;
4910 case DataType::Type::kFloat32:
4911 case DataType::Type::kFloat64:
4912 locations->SetInAt(0, Location::RequiresFpuRegister());
4913 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4914 break;
4915 default:
4916 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4917 }
4918}
4919
4920void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4921 LocationSummary* locations = abs->GetLocations();
4922 switch (abs->GetResultType()) {
4923 case DataType::Type::kInt32: {
4924 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4925 vixl32::Register out_reg = RegisterFrom(locations->Out());
4926 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4927 __ Asr(mask, in_reg, 31);
4928 __ Add(out_reg, in_reg, mask);
4929 __ Eor(out_reg, out_reg, mask);
4930 break;
4931 }
4932 case DataType::Type::kInt64: {
4933 Location in = locations->InAt(0);
4934 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4935 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4936 Location output = locations->Out();
4937 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4938 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4939 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4940 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4941 __ Asr(mask, in_reg_hi, 31);
4942 __ Adds(out_reg_lo, in_reg_lo, mask);
4943 __ Adc(out_reg_hi, in_reg_hi, mask);
4944 __ Eor(out_reg_lo, out_reg_lo, mask);
4945 __ Eor(out_reg_hi, out_reg_hi, mask);
4946 break;
4947 }
4948 case DataType::Type::kFloat32:
4949 case DataType::Type::kFloat64:
4950 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4951 break;
4952 default:
4953 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4954 }
4955}
Artem Serov551b28f2016-10-18 19:11:30 +01004956
Scott Wakelingfe885462016-09-22 10:24:38 +01004957void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004958 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004959 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004960}
4961
4962void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4963 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004964 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004965 codegen_->AddSlowPath(slow_path);
4966
4967 LocationSummary* locations = instruction->GetLocations();
4968 Location value = locations->InAt(0);
4969
4970 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004972 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 case DataType::Type::kInt8:
4974 case DataType::Type::kUint16:
4975 case DataType::Type::kInt16:
4976 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004977 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004978 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004979 } else {
4980 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004981 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004982 __ B(slow_path->GetEntryLabel());
4983 }
4984 }
4985 break;
4986 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004987 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004988 if (value.IsRegisterPair()) {
4989 UseScratchRegisterScope temps(GetVIXLAssembler());
4990 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004991 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004992 __ B(eq, slow_path->GetEntryLabel());
4993 } else {
4994 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004995 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004996 __ B(slow_path->GetEntryLabel());
4997 }
4998 }
4999 break;
5000 }
5001 default:
5002 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5003 }
5004}
5005
Artem Serov02109dd2016-09-23 17:17:54 +01005006void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5007 LocationSummary* locations = ror->GetLocations();
5008 vixl32::Register in = InputRegisterAt(ror, 0);
5009 Location rhs = locations->InAt(1);
5010 vixl32::Register out = OutputRegister(ror);
5011
5012 if (rhs.IsConstant()) {
5013 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5014 // so map all rotations to a +ve. equivalent in that range.
5015 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5016 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5017 if (rot) {
5018 // Rotate, mapping left rotations to right equivalents if necessary.
5019 // (e.g. left by 2 bits == right by 30.)
5020 __ Ror(out, in, rot);
5021 } else if (!out.Is(in)) {
5022 __ Mov(out, in);
5023 }
5024 } else {
5025 __ Ror(out, in, RegisterFrom(rhs));
5026 }
5027}
5028
5029// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5030// rotates by swapping input regs (effectively rotating by the first 32-bits of
5031// a larger rotation) or flipping direction (thus treating larger right/left
5032// rotations as sub-word sized rotations in the other direction) as appropriate.
5033void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5034 LocationSummary* locations = ror->GetLocations();
5035 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5036 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5037 Location rhs = locations->InAt(1);
5038 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5039 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5040
5041 if (rhs.IsConstant()) {
5042 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5043 // Map all rotations to +ve. equivalents on the interval [0,63].
5044 rot &= kMaxLongShiftDistance;
5045 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5046 // logic below to a simple pair of binary orr.
5047 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5048 if (rot >= kArmBitsPerWord) {
5049 rot -= kArmBitsPerWord;
5050 std::swap(in_reg_hi, in_reg_lo);
5051 }
5052 // Rotate, or mov to out for zero or word size rotations.
5053 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005054 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005055 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005056 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005057 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5058 } else {
5059 __ Mov(out_reg_lo, in_reg_lo);
5060 __ Mov(out_reg_hi, in_reg_hi);
5061 }
5062 } else {
5063 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5064 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5065 vixl32::Label end;
5066 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005067 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005068
5069 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5070 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005071 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Andreas Gampe3db70682018-12-26 15:12:03 -08005072 __ B(cc, &shift_by_32_plus_shift_right, /* is_far_target= */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005073
5074 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5075 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5076 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5077 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5078 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5079 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5080 __ Lsr(shift_left, in_reg_hi, shift_right);
5081 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005082 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005083
5084 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5085 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5086 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5087 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5088 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5089 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5090 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5091 __ Lsl(shift_right, in_reg_hi, shift_left);
5092 __ Add(out_reg_lo, out_reg_lo, shift_right);
5093
Anton Kirilov6f644202017-02-27 18:29:45 +00005094 if (end.IsReferenced()) {
5095 __ Bind(&end);
5096 }
Artem Serov02109dd2016-09-23 17:17:54 +01005097 }
5098}
5099
5100void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5101 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005102 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005103 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005104 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005105 locations->SetInAt(0, Location::RequiresRegister());
5106 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5107 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5108 break;
5109 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005110 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005111 locations->SetInAt(0, Location::RequiresRegister());
5112 if (ror->InputAt(1)->IsConstant()) {
5113 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5114 } else {
5115 locations->SetInAt(1, Location::RequiresRegister());
5116 locations->AddTemp(Location::RequiresRegister());
5117 locations->AddTemp(Location::RequiresRegister());
5118 }
5119 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5120 break;
5121 }
5122 default:
5123 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5124 }
5125}
5126
5127void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005128 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005129 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005130 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005131 HandleIntegerRotate(ror);
5132 break;
5133 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005134 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005135 HandleLongRotate(ror);
5136 break;
5137 }
5138 default:
5139 LOG(FATAL) << "Unexpected operation type " << type;
5140 UNREACHABLE();
5141 }
5142}
5143
Artem Serov02d37832016-10-25 15:25:33 +01005144void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5145 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5146
5147 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005148 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005149
5150 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005151 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005152 locations->SetInAt(0, Location::RequiresRegister());
5153 if (op->InputAt(1)->IsConstant()) {
5154 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5155 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5156 } else {
5157 locations->SetInAt(1, Location::RequiresRegister());
5158 // Make the output overlap, as it will be used to hold the masked
5159 // second input.
5160 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5161 }
5162 break;
5163 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005164 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005165 locations->SetInAt(0, Location::RequiresRegister());
5166 if (op->InputAt(1)->IsConstant()) {
5167 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5168 // For simplicity, use kOutputOverlap even though we only require that low registers
5169 // don't clash with high registers which the register allocator currently guarantees.
5170 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5171 } else {
5172 locations->SetInAt(1, Location::RequiresRegister());
5173 locations->AddTemp(Location::RequiresRegister());
5174 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5175 }
5176 break;
5177 }
5178 default:
5179 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5180 }
5181}
5182
5183void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5184 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5185
5186 LocationSummary* locations = op->GetLocations();
5187 Location out = locations->Out();
5188 Location first = locations->InAt(0);
5189 Location second = locations->InAt(1);
5190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005191 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005192 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005193 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005194 vixl32::Register out_reg = OutputRegister(op);
5195 vixl32::Register first_reg = InputRegisterAt(op, 0);
5196 if (second.IsRegister()) {
5197 vixl32::Register second_reg = RegisterFrom(second);
5198 // ARM doesn't mask the shift count so we need to do it ourselves.
5199 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5200 if (op->IsShl()) {
5201 __ Lsl(out_reg, first_reg, out_reg);
5202 } else if (op->IsShr()) {
5203 __ Asr(out_reg, first_reg, out_reg);
5204 } else {
5205 __ Lsr(out_reg, first_reg, out_reg);
5206 }
5207 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005208 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005209 uint32_t shift_value = cst & kMaxIntShiftDistance;
5210 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5211 __ Mov(out_reg, first_reg);
5212 } else if (op->IsShl()) {
5213 __ Lsl(out_reg, first_reg, shift_value);
5214 } else if (op->IsShr()) {
5215 __ Asr(out_reg, first_reg, shift_value);
5216 } else {
5217 __ Lsr(out_reg, first_reg, shift_value);
5218 }
5219 }
5220 break;
5221 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005222 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005223 vixl32::Register o_h = HighRegisterFrom(out);
5224 vixl32::Register o_l = LowRegisterFrom(out);
5225
5226 vixl32::Register high = HighRegisterFrom(first);
5227 vixl32::Register low = LowRegisterFrom(first);
5228
5229 if (second.IsRegister()) {
5230 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5231
5232 vixl32::Register second_reg = RegisterFrom(second);
5233
5234 if (op->IsShl()) {
5235 __ And(o_l, second_reg, kMaxLongShiftDistance);
5236 // Shift the high part
5237 __ Lsl(o_h, high, o_l);
5238 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005239 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005240 __ Lsr(temp, low, temp);
5241 __ Orr(o_h, o_h, temp);
5242 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005243 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005244 {
Artem Serov0fb37192016-12-06 18:13:40 +00005245 ExactAssemblyScope guard(GetVIXLAssembler(),
5246 2 * vixl32::kMaxInstructionSizeInBytes,
5247 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005248 __ it(pl);
5249 __ lsl(pl, o_h, low, temp);
5250 }
5251 // Shift the low part
5252 __ Lsl(o_l, low, o_l);
5253 } else if (op->IsShr()) {
5254 __ And(o_h, second_reg, kMaxLongShiftDistance);
5255 // Shift the low part
5256 __ Lsr(o_l, low, o_h);
5257 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005258 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005259 __ Lsl(temp, high, temp);
5260 __ Orr(o_l, o_l, temp);
5261 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005262 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005263 {
Artem Serov0fb37192016-12-06 18:13:40 +00005264 ExactAssemblyScope guard(GetVIXLAssembler(),
5265 2 * vixl32::kMaxInstructionSizeInBytes,
5266 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005267 __ it(pl);
5268 __ asr(pl, o_l, high, temp);
5269 }
5270 // Shift the high part
5271 __ Asr(o_h, high, o_h);
5272 } else {
5273 __ And(o_h, second_reg, kMaxLongShiftDistance);
5274 // same as Shr except we use `Lsr`s and not `Asr`s
5275 __ Lsr(o_l, low, o_h);
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);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005279 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005280 {
Artem Serov0fb37192016-12-06 18:13:40 +00005281 ExactAssemblyScope guard(GetVIXLAssembler(),
5282 2 * vixl32::kMaxInstructionSizeInBytes,
5283 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005284 __ it(pl);
5285 __ lsr(pl, o_l, high, temp);
5286 }
5287 __ Lsr(o_h, high, o_h);
5288 }
5289 } else {
5290 // Register allocator doesn't create partial overlap.
5291 DCHECK(!o_l.Is(high));
5292 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005293 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005294 uint32_t shift_value = cst & kMaxLongShiftDistance;
5295 if (shift_value > 32) {
5296 if (op->IsShl()) {
5297 __ Lsl(o_h, low, shift_value - 32);
5298 __ Mov(o_l, 0);
5299 } else if (op->IsShr()) {
5300 __ Asr(o_l, high, shift_value - 32);
5301 __ Asr(o_h, high, 31);
5302 } else {
5303 __ Lsr(o_l, high, shift_value - 32);
5304 __ Mov(o_h, 0);
5305 }
5306 } else if (shift_value == 32) {
5307 if (op->IsShl()) {
5308 __ Mov(o_h, low);
5309 __ Mov(o_l, 0);
5310 } else if (op->IsShr()) {
5311 __ Mov(o_l, high);
5312 __ Asr(o_h, high, 31);
5313 } else {
5314 __ Mov(o_l, high);
5315 __ Mov(o_h, 0);
5316 }
5317 } else if (shift_value == 1) {
5318 if (op->IsShl()) {
5319 __ Lsls(o_l, low, 1);
5320 __ Adc(o_h, high, high);
5321 } else if (op->IsShr()) {
5322 __ Asrs(o_h, high, 1);
5323 __ Rrx(o_l, low);
5324 } else {
5325 __ Lsrs(o_h, high, 1);
5326 __ Rrx(o_l, low);
5327 }
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005328 } else if (shift_value == 0) {
5329 __ Mov(o_l, low);
5330 __ Mov(o_h, high);
Artem Serov02d37832016-10-25 15:25:33 +01005331 } else {
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005332 DCHECK(0 < shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01005333 if (op->IsShl()) {
5334 __ Lsl(o_h, high, shift_value);
5335 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5336 __ Lsl(o_l, low, shift_value);
5337 } else if (op->IsShr()) {
5338 __ Lsr(o_l, low, shift_value);
5339 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5340 __ Asr(o_h, high, shift_value);
5341 } else {
5342 __ Lsr(o_l, low, shift_value);
5343 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5344 __ Lsr(o_h, high, shift_value);
5345 }
5346 }
5347 }
5348 break;
5349 }
5350 default:
5351 LOG(FATAL) << "Unexpected operation type " << type;
5352 UNREACHABLE();
5353 }
5354}
5355
5356void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5357 HandleShift(shl);
5358}
5359
5360void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5361 HandleShift(shl);
5362}
5363
5364void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5365 HandleShift(shr);
5366}
5367
5368void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5369 HandleShift(shr);
5370}
5371
5372void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5373 HandleShift(ushr);
5374}
5375
5376void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5377 HandleShift(ushr);
5378}
5379
5380void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005381 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5382 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005383 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5384 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005385 locations->SetOut(LocationFrom(r0));
5386}
5387
5388void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005389 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5390 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andra Danciua0130e82020-07-23 12:34:56 +00005391 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005392}
5393
5394void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005395 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5396 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005397 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005398 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005399 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5400 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005401}
5402
5403void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005404 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5405 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005406 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005407 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005408 DCHECK(!codegen_->IsLeafMethod());
Andra Danciua0130e82020-07-23 12:34:56 +00005409 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 13);
Artem Serov02d37832016-10-25 15:25:33 +01005410}
5411
5412void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5413 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005414 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005415 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5416 if (location.IsStackSlot()) {
5417 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5418 } else if (location.IsDoubleStackSlot()) {
5419 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5420 }
5421 locations->SetOut(location);
5422}
5423
5424void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5425 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5426 // Nothing to do, the parameter is already at its location.
5427}
5428
5429void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5430 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005431 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005432 locations->SetOut(LocationFrom(kMethodRegister));
5433}
5434
5435void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5436 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5437 // Nothing to do, the method is already at its location.
5438}
5439
5440void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5441 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005442 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005443 locations->SetInAt(0, Location::RequiresRegister());
5444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5445}
5446
5447void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5448 LocationSummary* locations = not_->GetLocations();
5449 Location out = locations->Out();
5450 Location in = locations->InAt(0);
5451 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005452 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005453 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5454 break;
5455
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005456 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005457 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5458 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5459 break;
5460
5461 default:
5462 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5463 }
5464}
5465
Scott Wakelingc34dba72016-10-03 10:14:44 +01005466void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5467 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005468 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005469 locations->SetInAt(0, Location::RequiresRegister());
5470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5471}
5472
5473void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5474 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5475}
5476
Artem Serov02d37832016-10-25 15:25:33 +01005477void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5478 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005479 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005480 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005481 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005482 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005483 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005484 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005485 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005486 case DataType::Type::kInt32:
5487 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005488 locations->SetInAt(0, Location::RequiresRegister());
5489 locations->SetInAt(1, Location::RequiresRegister());
5490 // Output overlaps because it is written before doing the low comparison.
5491 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5492 break;
5493 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005494 case DataType::Type::kFloat32:
5495 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005496 locations->SetInAt(0, Location::RequiresFpuRegister());
5497 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5498 locations->SetOut(Location::RequiresRegister());
5499 break;
5500 }
5501 default:
5502 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5503 }
5504}
5505
5506void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5507 LocationSummary* locations = compare->GetLocations();
5508 vixl32::Register out = OutputRegister(compare);
5509 Location left = locations->InAt(0);
5510 Location right = locations->InAt(1);
5511
5512 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005513 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005514 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005515 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005516 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005517 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005518 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005520 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005521 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005522 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005523 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5524 __ Mov(out, 0);
5525 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5526 less_cond = lt;
5527 break;
5528 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005529 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005530 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Andreas Gampe3db70682018-12-26 15:12:03 -08005531 __ B(lt, &less, /* is_far_target= */ false);
5532 __ B(gt, &greater, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005533 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5534 __ Mov(out, 0);
5535 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5536 less_cond = lo;
5537 break;
5538 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005539 case DataType::Type::kFloat32:
5540 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005541 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005542 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005543 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5544 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5545 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5546 break;
5547 }
5548 default:
5549 LOG(FATAL) << "Unexpected compare type " << type;
5550 UNREACHABLE();
5551 }
5552
Andreas Gampe3db70682018-12-26 15:12:03 -08005553 __ B(eq, final_label, /* is_far_target= */ false);
5554 __ B(less_cond, &less, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005555
5556 __ Bind(&greater);
5557 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005558 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005559
5560 __ Bind(&less);
5561 __ Mov(out, -1);
5562
Anton Kirilov6f644202017-02-27 18:29:45 +00005563 if (done.IsReferenced()) {
5564 __ Bind(&done);
5565 }
Artem Serov02d37832016-10-25 15:25:33 +01005566}
5567
5568void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5569 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005570 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005571 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5572 locations->SetInAt(i, Location::Any());
5573 }
5574 locations->SetOut(Location::Any());
5575}
5576
5577void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5578 LOG(FATAL) << "Unreachable";
5579}
5580
5581void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5582 // TODO (ported from quick): revisit ARM barrier kinds.
5583 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5584 switch (kind) {
5585 case MemBarrierKind::kAnyStore:
5586 case MemBarrierKind::kLoadAny:
5587 case MemBarrierKind::kAnyAny: {
5588 flavor = DmbOptions::ISH;
5589 break;
5590 }
5591 case MemBarrierKind::kStoreStore: {
5592 flavor = DmbOptions::ISHST;
5593 break;
5594 }
5595 default:
5596 LOG(FATAL) << "Unexpected memory barrier " << kind;
5597 }
5598 __ Dmb(flavor);
5599}
5600
5601void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5602 uint32_t offset,
5603 vixl32::Register out_lo,
5604 vixl32::Register out_hi) {
5605 UseScratchRegisterScope temps(GetVIXLAssembler());
5606 if (offset != 0) {
5607 vixl32::Register temp = temps.Acquire();
5608 __ Add(temp, addr, offset);
5609 addr = temp;
5610 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005611 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005612}
5613
5614void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5615 uint32_t offset,
5616 vixl32::Register value_lo,
5617 vixl32::Register value_hi,
5618 vixl32::Register temp1,
5619 vixl32::Register temp2,
5620 HInstruction* instruction) {
5621 UseScratchRegisterScope temps(GetVIXLAssembler());
5622 vixl32::Label fail;
5623 if (offset != 0) {
5624 vixl32::Register temp = temps.Acquire();
5625 __ Add(temp, addr, offset);
5626 addr = temp;
5627 }
5628 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005629 {
5630 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005631 ExactAssemblyScope aas(GetVIXLAssembler(),
5632 vixl32::kMaxInstructionSizeInBytes,
5633 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005634 // We need a load followed by store. (The address used in a STREX instruction must
5635 // be the same as the address in the most recently executed LDREX instruction.)
5636 __ ldrexd(temp1, temp2, MemOperand(addr));
5637 codegen_->MaybeRecordImplicitNullCheck(instruction);
5638 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005639 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005640 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005641}
Artem Serov02109dd2016-09-23 17:17:54 +01005642
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005643void LocationsBuilderARMVIXL::HandleFieldSet(
5644 HInstruction* instruction, const FieldInfo& field_info) {
5645 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5646
5647 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005648 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005649 locations->SetInAt(0, Location::RequiresRegister());
5650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 DataType::Type field_type = field_info.GetFieldType();
5652 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005653 locations->SetInAt(1, Location::RequiresFpuRegister());
5654 } else {
5655 locations->SetInAt(1, Location::RequiresRegister());
5656 }
5657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005658 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005659 bool generate_volatile = field_info.IsVolatile()
5660 && is_wide
5661 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5662 bool needs_write_barrier =
5663 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5664 // Temporary registers for the write barrier.
5665 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5666 if (needs_write_barrier) {
5667 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5668 locations->AddTemp(Location::RequiresRegister());
5669 } else if (generate_volatile) {
5670 // ARM encoding have some additional constraints for ldrexd/strexd:
5671 // - registers need to be consecutive
5672 // - the first register should be even but not R14.
5673 // We don't test for ARM yet, and the assertion makes sure that we
5674 // revisit this if we ever enable ARM encoding.
5675 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5676
5677 locations->AddTemp(Location::RequiresRegister());
5678 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005680 // For doubles we need two more registers to copy the value.
5681 locations->AddTemp(LocationFrom(r2));
5682 locations->AddTemp(LocationFrom(r3));
5683 }
5684 }
5685}
5686
5687void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5688 const FieldInfo& field_info,
5689 bool value_can_be_null) {
5690 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5691
5692 LocationSummary* locations = instruction->GetLocations();
5693 vixl32::Register base = InputRegisterAt(instruction, 0);
5694 Location value = locations->InAt(1);
5695
5696 bool is_volatile = field_info.IsVolatile();
5697 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005698 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005699 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5700 bool needs_write_barrier =
5701 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5702
5703 if (is_volatile) {
5704 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5705 }
5706
5707 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005709 case DataType::Type::kUint8:
5710 case DataType::Type::kInt8:
5711 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005712 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005713 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005714 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5715 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005716 StoreOperandType operand_type = GetStoreOperandType(field_type);
5717 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005718 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005719 break;
5720 }
5721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 case DataType::Type::kReference: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005723 vixl32::Register value_reg = RegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005724 if (kPoisonHeapReferences && needs_write_barrier) {
5725 // Note that in the case where `value` is a null reference,
5726 // we do not enter this block, as a null reference does not
5727 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 DCHECK_EQ(field_type, DataType::Type::kReference);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005729 value_reg = RegisterFrom(locations->GetTemp(0));
5730 __ Mov(value_reg, RegisterFrom(value));
5731 GetAssembler()->PoisonHeapReference(value_reg);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005732 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005733 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5734 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5735 GetAssembler()->StoreToOffset(kStoreWord, value_reg, base, offset);
5736 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::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005741 if (is_volatile && !atomic_ldrd_strd) {
5742 GenerateWideAtomicStore(base,
5743 offset,
5744 LowRegisterFrom(value),
5745 HighRegisterFrom(value),
5746 RegisterFrom(locations->GetTemp(0)),
5747 RegisterFrom(locations->GetTemp(1)),
5748 instruction);
5749 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005750 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5751 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005752 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5753 codegen_->MaybeRecordImplicitNullCheck(instruction);
5754 }
5755 break;
5756 }
5757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005758 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005759 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5760 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005761 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005762 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005763 break;
5764 }
5765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005766 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005767 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005768 if (is_volatile && !atomic_ldrd_strd) {
5769 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5770 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5771
5772 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5773
5774 GenerateWideAtomicStore(base,
5775 offset,
5776 value_reg_lo,
5777 value_reg_hi,
5778 RegisterFrom(locations->GetTemp(2)),
5779 RegisterFrom(locations->GetTemp(3)),
5780 instruction);
5781 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005782 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5783 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005784 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5785 codegen_->MaybeRecordImplicitNullCheck(instruction);
5786 }
5787 break;
5788 }
5789
Aart Bik66c158e2018-01-31 12:55:04 -08005790 case DataType::Type::kUint32:
5791 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005792 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005793 LOG(FATAL) << "Unreachable type " << field_type;
5794 UNREACHABLE();
5795 }
5796
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005797 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5798 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5799 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5800 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5801 }
5802
5803 if (is_volatile) {
5804 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5805 }
5806}
5807
Artem Serov02d37832016-10-25 15:25:33 +01005808void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5809 const FieldInfo& field_info) {
5810 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5811
5812 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005813 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005814 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005815 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5816 object_field_get_with_read_barrier
5817 ? LocationSummary::kCallOnSlowPath
5818 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005819 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5820 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5821 }
5822 locations->SetInAt(0, Location::RequiresRegister());
5823
5824 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005826 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5827 // The output overlaps in case of volatile long: we don't want the
5828 // code generated by GenerateWideAtomicLoad to overwrite the
5829 // object's location. Likewise, in the case of an object field get
5830 // with read barriers enabled, we do not want the load to overwrite
5831 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 bool overlap =
5833 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005834 object_field_get_with_read_barrier;
5835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005836 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005837 locations->SetOut(Location::RequiresFpuRegister());
5838 } else {
5839 locations->SetOut(Location::RequiresRegister(),
5840 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5841 }
5842 if (volatile_for_double) {
5843 // ARM encoding have some additional constraints for ldrexd/strexd:
5844 // - registers need to be consecutive
5845 // - the first register should be even but not R14.
5846 // We don't test for ARM yet, and the assertion makes sure that we
5847 // revisit this if we ever enable ARM encoding.
5848 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5849 locations->AddTemp(Location::RequiresRegister());
5850 locations->AddTemp(Location::RequiresRegister());
5851 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005852 // We need a temporary register for the read barrier load in
5853 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5854 // only if the offset is too big.
5855 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005856 locations->AddTemp(Location::RequiresRegister());
5857 }
Artem Serov02d37832016-10-25 15:25:33 +01005858 }
5859}
5860
5861Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005862 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005863 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5864 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5865 return Location::ConstantLocation(input->AsConstant());
5866 } else {
5867 return Location::RequiresFpuRegister();
5868 }
5869}
5870
Artem Serov02109dd2016-09-23 17:17:54 +01005871Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5872 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005874 if (constant->IsConstant() &&
5875 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5876 return Location::ConstantLocation(constant->AsConstant());
5877 }
5878 return Location::RequiresRegister();
5879}
5880
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005881static bool CanEncode32BitConstantAsImmediate(
5882 CodeGeneratorARMVIXL* codegen,
5883 uint32_t value,
5884 Opcode opcode,
5885 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5886 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5887 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005888 return true;
5889 }
5890 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005891 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005892 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005893 case AND: neg_opcode = BIC; neg_value = ~value; break;
5894 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5895 case ADD: neg_opcode = SUB; neg_value = -value; break;
5896 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5897 case SUB: neg_opcode = ADD; neg_value = -value; break;
5898 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5899 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005900 default:
5901 return false;
5902 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005903
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005904 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005905 return true;
5906 }
5907
5908 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005909}
5910
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005911bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5912 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5913 if (DataType::Is64BitType(input_cst->GetType())) {
5914 Opcode high_opcode = opcode;
5915 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5916 switch (opcode) {
5917 case SUB:
5918 // Flip the operation to an ADD.
5919 value = -value;
5920 opcode = ADD;
5921 FALLTHROUGH_INTENDED;
5922 case ADD:
5923 if (Low32Bits(value) == 0u) {
5924 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5925 }
5926 high_opcode = ADC;
5927 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5928 break;
5929 default:
5930 break;
5931 }
5932 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5933 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5934 } else {
5935 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5936 }
5937}
5938
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005939void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5940 const FieldInfo& field_info) {
5941 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5942
5943 LocationSummary* locations = instruction->GetLocations();
5944 vixl32::Register base = InputRegisterAt(instruction, 0);
5945 Location out = locations->Out();
5946 bool is_volatile = field_info.IsVolatile();
5947 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005948 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5949 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005950 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5951
Vladimir Marko61b92282017-10-11 13:23:17 +01005952 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005954 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005955 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005956 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005957 case DataType::Type::kInt16:
5958 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005959 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5960 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01005961 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005962 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005963 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005964 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005965 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005968 // /* HeapReference<Object> */ out = *(base + offset)
5969 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01005970 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005971 // Note that a potential implicit null check is handled in this
5972 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5973 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005974 instruction, out, base, offset, maybe_temp, /* needs_null_check= */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005975 if (is_volatile) {
5976 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5977 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005978 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005979 {
5980 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5981 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5982 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5983 codegen_->MaybeRecordImplicitNullCheck(instruction);
5984 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005985 if (is_volatile) {
5986 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5987 }
5988 // If read barriers are enabled, emit read barriers other than
5989 // Baker's using a slow path (and also unpoison the loaded
5990 // reference, if heap poisoning is enabled).
5991 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5992 }
5993 break;
5994 }
5995
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005996 case DataType::Type::kInt64: {
5997 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5998 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005999 if (is_volatile && !atomic_ldrd_strd) {
6000 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6001 } else {
6002 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6003 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006004 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006005 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006006 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006007
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006008 case DataType::Type::kFloat32: {
6009 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6010 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006011 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006012 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006013 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006014 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006015
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006016 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006017 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6018 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006019 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006020 if (is_volatile && !atomic_ldrd_strd) {
6021 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6022 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6023 GenerateWideAtomicLoad(base, offset, lo, hi);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006024 codegen_->MaybeRecordImplicitNullCheck(instruction);
6025 __ Vmov(out_dreg, lo, hi);
6026 } else {
6027 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006028 codegen_->MaybeRecordImplicitNullCheck(instruction);
6029 }
6030 break;
6031 }
6032
Aart Bik66c158e2018-01-31 12:55:04 -08006033 case DataType::Type::kUint32:
6034 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006036 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006037 UNREACHABLE();
6038 }
6039
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006040 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006041 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006042 // Memory barriers, in the case of references, are also handled
6043 // in the previous switch statement.
6044 } else {
6045 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6046 }
6047 }
6048}
6049
6050void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6051 HandleFieldSet(instruction, instruction->GetFieldInfo());
6052}
6053
6054void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6055 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6056}
6057
6058void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6059 HandleFieldGet(instruction, instruction->GetFieldInfo());
6060}
6061
6062void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6063 HandleFieldGet(instruction, instruction->GetFieldInfo());
6064}
6065
6066void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6067 HandleFieldGet(instruction, instruction->GetFieldInfo());
6068}
6069
6070void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6071 HandleFieldGet(instruction, instruction->GetFieldInfo());
6072}
6073
Scott Wakelingc34dba72016-10-03 10:14:44 +01006074void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6075 HandleFieldSet(instruction, instruction->GetFieldInfo());
6076}
6077
6078void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6079 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6080}
6081
Vladimir Marko552a1342017-10-31 10:56:47 +00006082void LocationsBuilderARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6083 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(r0));
6084}
6085
6086void InstructionCodeGeneratorARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6087 __ Mov(r0, instruction->GetFormat()->GetValue());
6088 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6089}
6090
Artem Serovcfbe9132016-10-14 15:58:56 +01006091void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6092 HUnresolvedInstanceFieldGet* instruction) {
6093 FieldAccessCallingConventionARMVIXL calling_convention;
6094 codegen_->CreateUnresolvedFieldLocationSummary(
6095 instruction, instruction->GetFieldType(), calling_convention);
6096}
6097
6098void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6099 HUnresolvedInstanceFieldGet* instruction) {
6100 FieldAccessCallingConventionARMVIXL calling_convention;
6101 codegen_->GenerateUnresolvedFieldAccess(instruction,
6102 instruction->GetFieldType(),
6103 instruction->GetFieldIndex(),
6104 instruction->GetDexPc(),
6105 calling_convention);
6106}
6107
6108void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6109 HUnresolvedInstanceFieldSet* instruction) {
6110 FieldAccessCallingConventionARMVIXL calling_convention;
6111 codegen_->CreateUnresolvedFieldLocationSummary(
6112 instruction, instruction->GetFieldType(), calling_convention);
6113}
6114
6115void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6116 HUnresolvedInstanceFieldSet* instruction) {
6117 FieldAccessCallingConventionARMVIXL calling_convention;
6118 codegen_->GenerateUnresolvedFieldAccess(instruction,
6119 instruction->GetFieldType(),
6120 instruction->GetFieldIndex(),
6121 instruction->GetDexPc(),
6122 calling_convention);
6123}
6124
6125void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6126 HUnresolvedStaticFieldGet* instruction) {
6127 FieldAccessCallingConventionARMVIXL calling_convention;
6128 codegen_->CreateUnresolvedFieldLocationSummary(
6129 instruction, instruction->GetFieldType(), calling_convention);
6130}
6131
6132void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6133 HUnresolvedStaticFieldGet* instruction) {
6134 FieldAccessCallingConventionARMVIXL calling_convention;
6135 codegen_->GenerateUnresolvedFieldAccess(instruction,
6136 instruction->GetFieldType(),
6137 instruction->GetFieldIndex(),
6138 instruction->GetDexPc(),
6139 calling_convention);
6140}
6141
6142void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6143 HUnresolvedStaticFieldSet* instruction) {
6144 FieldAccessCallingConventionARMVIXL calling_convention;
6145 codegen_->CreateUnresolvedFieldLocationSummary(
6146 instruction, instruction->GetFieldType(), calling_convention);
6147}
6148
6149void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6150 HUnresolvedStaticFieldSet* instruction) {
6151 FieldAccessCallingConventionARMVIXL calling_convention;
6152 codegen_->GenerateUnresolvedFieldAccess(instruction,
6153 instruction->GetFieldType(),
6154 instruction->GetFieldIndex(),
6155 instruction->GetDexPc(),
6156 calling_convention);
6157}
6158
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006159void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006160 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006161 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006162}
6163
6164void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6165 if (CanMoveNullCheckToUser(instruction)) {
6166 return;
6167 }
6168
6169 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006170 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006171 ExactAssemblyScope aas(GetVIXLAssembler(),
6172 vixl32::kMaxInstructionSizeInBytes,
6173 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006174 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6175 RecordPcInfo(instruction, instruction->GetDexPc());
6176}
6177
6178void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6179 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006180 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006181 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006182 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006183}
6184
6185void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6186 codegen_->GenerateNullCheck(instruction);
6187}
6188
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006189void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006190 Location out_loc,
6191 vixl32::Register base,
6192 vixl32::Register reg_index,
6193 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006194 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006195 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6196
6197 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006198 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006199 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006200 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6201 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006202 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006203 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6204 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006205 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006206 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6207 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006208 case DataType::Type::kInt16:
6209 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6210 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006211 case DataType::Type::kReference:
6212 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006213 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6214 break;
6215 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 case DataType::Type::kInt64:
6217 case DataType::Type::kFloat32:
6218 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006219 default:
6220 LOG(FATAL) << "Unreachable type " << type;
6221 UNREACHABLE();
6222 }
6223}
6224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006225void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006226 Location loc,
6227 vixl32::Register base,
6228 vixl32::Register reg_index,
6229 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006230 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006231 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6232
6233 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006234 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006235 case DataType::Type::kUint8:
6236 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006237 __ Strb(cond, RegisterFrom(loc), mem_address);
6238 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006239 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006240 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006241 __ Strh(cond, RegisterFrom(loc), mem_address);
6242 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006243 case DataType::Type::kReference:
6244 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006245 __ Str(cond, RegisterFrom(loc), mem_address);
6246 break;
6247 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006248 case DataType::Type::kInt64:
6249 case DataType::Type::kFloat32:
6250 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006251 default:
6252 LOG(FATAL) << "Unreachable type " << type;
6253 UNREACHABLE();
6254 }
6255}
6256
6257void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6258 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006259 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006260 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006261 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6262 object_array_get_with_read_barrier
6263 ? LocationSummary::kCallOnSlowPath
6264 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006265 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006266 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006267 }
6268 locations->SetInAt(0, Location::RequiresRegister());
6269 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006270 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006271 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6272 } else {
6273 // The output overlaps in the case of an object array get with
6274 // read barriers enabled: we do not want the move to overwrite the
6275 // array's location, as we need it to emit the read barrier.
6276 locations->SetOut(
6277 Location::RequiresRegister(),
6278 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6279 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006280 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006281 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006282 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01006283 // We need a temporary register for the read barrier load in
6284 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
6285 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006286 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6287 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006289 if (offset >= kReferenceLoadMinFarOffset) {
6290 locations->AddTemp(Location::RequiresRegister());
6291 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006292 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006293 // We need a non-scratch temporary for the array data pointer in
6294 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006295 locations->AddTemp(Location::RequiresRegister());
6296 }
6297 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6298 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006299 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006300 }
6301}
6302
6303void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006304 LocationSummary* locations = instruction->GetLocations();
6305 Location obj_loc = locations->InAt(0);
6306 vixl32::Register obj = InputRegisterAt(instruction, 0);
6307 Location index = locations->InAt(1);
6308 Location out_loc = locations->Out();
6309 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006310 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006311 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6312 instruction->IsStringCharAt();
6313 HInstruction* array_instr = instruction->GetArray();
6314 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315
6316 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006318 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006319 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006320 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006321 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006323 vixl32::Register length;
6324 if (maybe_compressed_char_at) {
6325 length = RegisterFrom(locations->GetTemp(0));
6326 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006327 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6328 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006329 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6330 codegen_->MaybeRecordImplicitNullCheck(instruction);
6331 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006332 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006333 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006334 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006335 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006336 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006337 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6338 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6339 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006340 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006341 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6342 RegisterFrom(out_loc),
6343 obj,
6344 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006345 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006346 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006347 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006348 RegisterFrom(out_loc),
6349 obj,
6350 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006351 if (done.IsReferenced()) {
6352 __ Bind(&done);
6353 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006354 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006355 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006356
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006357 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6358 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006359 LoadOperandType load_type = GetLoadOperandType(type);
6360 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006361 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006362 }
6363 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006364 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006365 vixl32::Register temp = temps.Acquire();
6366
6367 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006368 // We do not need to compute the intermediate address from the array: the
6369 // input instruction has done it already. See the comment in
6370 // `TryExtractArrayAccessAddress()`.
6371 if (kIsDebugBuild) {
6372 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006373 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006374 }
6375 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006376 } else {
6377 __ Add(temp, obj, data_offset);
6378 }
6379 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006380 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006381 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006382 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6383 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6384 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006385 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006386 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006387 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006388 __ Bind(&uncompressed_load);
6389 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006390 if (done.IsReferenced()) {
6391 __ Bind(&done);
6392 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006393 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006394 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6395 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006396 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006397 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006398 }
6399 }
6400 break;
6401 }
6402
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006403 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006404 // The read barrier instrumentation of object ArrayGet
6405 // instructions does not support the HIntermediateAddress
6406 // instruction.
6407 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6408
Scott Wakelingc34dba72016-10-03 10:14:44 +01006409 static_assert(
6410 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6411 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6412 // /* HeapReference<Object> */ out =
6413 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6414 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006415 // Note that a potential implicit null check is handled in this
6416 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006417 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6418 if (index.IsConstant()) {
6419 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006420 Location maybe_temp =
6421 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006422 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006423 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6424 out_loc,
6425 obj,
6426 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006427 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006428 /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006429 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006430 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006431 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006432 out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006433 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006434 } else {
6435 vixl32::Register out = OutputRegister(instruction);
6436 if (index.IsConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006437 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6438 {
6439 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6440 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6441 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
6442 codegen_->MaybeRecordImplicitNullCheck(instruction);
6443 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006444 // If read barriers are enabled, emit read barriers other than
6445 // Baker's using a slow path (and also unpoison the loaded
6446 // reference, if heap poisoning is enabled).
6447 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6448 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006449 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006450 vixl32::Register temp = temps.Acquire();
6451
6452 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006453 // We do not need to compute the intermediate address from the array: the
6454 // input instruction has done it already. See the comment in
6455 // `TryExtractArrayAccessAddress()`.
6456 if (kIsDebugBuild) {
6457 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006458 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006459 }
6460 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006461 } else {
6462 __ Add(temp, obj, data_offset);
6463 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006464 {
6465 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6466 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6467 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6468 temps.Close();
6469 codegen_->MaybeRecordImplicitNullCheck(instruction);
6470 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006471 // If read barriers are enabled, emit read barriers other than
6472 // Baker's using a slow path (and also unpoison the loaded
6473 // reference, if heap poisoning is enabled).
6474 codegen_->MaybeGenerateReadBarrierSlow(
6475 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6476 }
6477 }
6478 break;
6479 }
6480
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006481 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006482 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6483 // As two macro instructions can be emitted the max size is doubled.
6484 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006485 if (index.IsConstant()) {
6486 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006487 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006488 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6489 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006490 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006491 vixl32::Register temp = temps.Acquire();
6492 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6493 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6494 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006495 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006496 break;
6497 }
6498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 case DataType::Type::kFloat32: {
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 vixl32::SRegister out = SRegisterFrom(out_loc);
6504 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006505 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006506 GetAssembler()->LoadSFromOffset(out, 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_4));
6511 GetAssembler()->LoadSFromOffset(out, 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::kFloat64: {
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 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006522 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006523 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6524 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006525 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006526 vixl32::Register temp = temps.Acquire();
6527 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6528 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6529 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006530 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006531 break;
6532 }
6533
Aart Bik66c158e2018-01-31 12:55:04 -08006534 case DataType::Type::kUint32:
6535 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006536 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006537 LOG(FATAL) << "Unreachable type " << type;
6538 UNREACHABLE();
6539 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006540}
6541
6542void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006543 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006544
6545 bool needs_write_barrier =
6546 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006547 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006548
Vladimir Markoca6fff82017-10-03 14:49:14 +01006549 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006550 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006551 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006552
6553 locations->SetInAt(0, Location::RequiresRegister());
6554 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006555 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006556 locations->SetInAt(2, Location::RequiresFpuRegister());
6557 } else {
6558 locations->SetInAt(2, Location::RequiresRegister());
6559 }
6560 if (needs_write_barrier) {
6561 // Temporary registers for the write barrier.
6562 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6563 locations->AddTemp(Location::RequiresRegister());
6564 }
6565}
6566
6567void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006568 LocationSummary* locations = instruction->GetLocations();
6569 vixl32::Register array = InputRegisterAt(instruction, 0);
6570 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006572 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006573 bool needs_write_barrier =
6574 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6575 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006576 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006577 Location value_loc = locations->InAt(2);
6578 HInstruction* array_instr = instruction->GetArray();
6579 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006580
6581 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006582 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006583 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006584 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006585 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006586 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006587 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006588 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006589 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006590 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006591 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006592 StoreOperandType store_type = GetStoreOperandType(value_type);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006593 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6594 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006595 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006596 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006597 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006598 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006599 vixl32::Register temp = temps.Acquire();
6600
6601 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006602 // We do not need to compute the intermediate address from the array: the
6603 // input instruction has done it already. See the comment in
6604 // `TryExtractArrayAccessAddress()`.
6605 if (kIsDebugBuild) {
6606 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006607 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006608 }
6609 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006610 } else {
6611 __ Add(temp, array, data_offset);
6612 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006613 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6614 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006615 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006616 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006617 }
6618 break;
6619 }
6620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006621 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006622 vixl32::Register value = RegisterFrom(value_loc);
6623 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6624 // See the comment in instruction_simplifier_shared.cc.
6625 DCHECK(!has_intermediate_address);
6626
6627 if (instruction->InputAt(2)->IsNullConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006628 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6629 // As two macro instructions can be emitted the max size is doubled.
6630 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006631 // Just setting null.
6632 if (index.IsConstant()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006633 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006634 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6635 } else {
6636 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006637 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006638 vixl32::Register temp = temps.Acquire();
6639 __ Add(temp, array, data_offset);
6640 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6641 }
6642 codegen_->MaybeRecordImplicitNullCheck(instruction);
6643 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006644 DCHECK(!needs_type_check);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006645 break;
6646 }
6647
6648 DCHECK(needs_write_barrier);
6649 Location temp1_loc = locations->GetTemp(0);
6650 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6651 Location temp2_loc = locations->GetTemp(1);
6652 vixl32::Register temp2 = RegisterFrom(temp2_loc);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006653
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006654 bool can_value_be_null = instruction->GetValueCanBeNull();
6655 vixl32::Label do_store;
6656 if (can_value_be_null) {
6657 __ CompareAndBranchIfZero(value, &do_store, /* is_far_target= */ false);
6658 }
6659
6660 SlowPathCodeARMVIXL* slow_path = nullptr;
6661 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006662 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006663 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006664
6665 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6666 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6667 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006668
6669 // Note that when read barriers are enabled, the type checks
6670 // are performed without read barriers. This is fine, even in
6671 // the case where a class object is in the from-space after
6672 // the flip, as a comparison involving such a type would not
6673 // produce a false positive; it may of course produce a false
6674 // negative, in which case we would take the ArraySet slow
6675 // path.
6676
Alexandre Rames374ddf32016-11-04 10:40:49 +00006677 {
6678 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006679 ExactAssemblyScope aas(GetVIXLAssembler(),
6680 vixl32::kMaxInstructionSizeInBytes,
6681 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006682 // /* HeapReference<Class> */ temp1 = array->klass_
6683 __ ldr(temp1, MemOperand(array, class_offset));
6684 codegen_->MaybeRecordImplicitNullCheck(instruction);
6685 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006686 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6687
6688 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6689 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6690 // /* HeapReference<Class> */ temp2 = value->klass_
6691 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6692 // If heap poisoning is enabled, no need to unpoison `temp1`
6693 // nor `temp2`, as we are comparing two poisoned references.
6694 __ Cmp(temp1, temp2);
6695
6696 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006697 vixl32::Label do_put;
6698 __ B(eq, &do_put, /* is_far_target= */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006699 // If heap poisoning is enabled, the `temp1` reference has
6700 // not been unpoisoned yet; unpoison it now.
6701 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6702
6703 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6704 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6705 // If heap poisoning is enabled, no need to unpoison
6706 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006707 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006708 __ Bind(&do_put);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006709 } else {
6710 __ B(ne, slow_path->GetEntryLabel());
6711 }
6712 }
6713
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006714 codegen_->MarkGCCard(temp1, temp2, array, value, /* can_be_null= */ false);
6715
6716 if (can_value_be_null) {
6717 DCHECK(do_store.IsReferenced());
6718 __ Bind(&do_store);
6719 }
6720
Scott Wakelingc34dba72016-10-03 10:14:44 +01006721 vixl32::Register source = value;
6722 if (kPoisonHeapReferences) {
6723 // Note that in the case where `value` is a null reference,
6724 // we do not enter this block, as a null reference does not
6725 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006726 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006727 __ Mov(temp1, value);
6728 GetAssembler()->PoisonHeapReference(temp1);
6729 source = temp1;
6730 }
6731
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006732 {
6733 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6734 // As two macro instructions can be emitted the max size is doubled.
6735 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
6736 if (index.IsConstant()) {
6737 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6738 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6739 } else {
6740 DCHECK(index.IsRegister()) << index;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006741
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006742 UseScratchRegisterScope temps(GetVIXLAssembler());
6743 vixl32::Register temp = temps.Acquire();
6744 __ Add(temp, array, data_offset);
6745 codegen_->StoreToShiftedRegOffset(value_type,
6746 LocationFrom(source),
6747 temp,
6748 RegisterFrom(index));
6749 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006750
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006751 if (can_value_be_null || !needs_type_check) {
6752 codegen_->MaybeRecordImplicitNullCheck(instruction);
6753 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006754 }
6755
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006756 if (slow_path != nullptr) {
6757 __ Bind(slow_path->GetExitLabel());
6758 }
6759
Scott Wakelingc34dba72016-10-03 10:14:44 +01006760 break;
6761 }
6762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006763 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006764 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6765 // As two macro instructions can be emitted the max size is doubled.
6766 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006767 Location value = locations->InAt(2);
6768 if (index.IsConstant()) {
6769 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006770 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006771 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6772 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006773 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006774 vixl32::Register temp = temps.Acquire();
6775 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6776 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6777 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006778 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006779 break;
6780 }
6781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006782 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006783 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6784 // As two macro instructions can be emitted the max size is doubled.
6785 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006786 Location value = locations->InAt(2);
6787 DCHECK(value.IsFpuRegister());
6788 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006789 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006790 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6791 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006792 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006793 vixl32::Register temp = temps.Acquire();
6794 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6795 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6796 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006797 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006798 break;
6799 }
6800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006801 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006802 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6803 // As two macro instructions can be emitted the max size is doubled.
6804 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006805 Location value = locations->InAt(2);
6806 DCHECK(value.IsFpuRegisterPair());
6807 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006808 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006809 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6810 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006811 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006812 vixl32::Register temp = temps.Acquire();
6813 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6814 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6815 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006816 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006817 break;
6818 }
6819
Aart Bik66c158e2018-01-31 12:55:04 -08006820 case DataType::Type::kUint32:
6821 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006822 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006823 LOG(FATAL) << "Unreachable type " << value_type;
6824 UNREACHABLE();
6825 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006826}
6827
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006828void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6829 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006830 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006831 locations->SetInAt(0, Location::RequiresRegister());
6832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6833}
6834
6835void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6836 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6837 vixl32::Register obj = InputRegisterAt(instruction, 0);
6838 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006839 {
Artem Serov0fb37192016-12-06 18:13:40 +00006840 ExactAssemblyScope aas(GetVIXLAssembler(),
6841 vixl32::kMaxInstructionSizeInBytes,
6842 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006843 __ ldr(out, MemOperand(obj, offset));
6844 codegen_->MaybeRecordImplicitNullCheck(instruction);
6845 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006846 // Mask out compression flag from String's array length.
6847 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006848 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006849 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006850}
6851
Artem Serov2bbc9532016-10-21 11:51:50 +01006852void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006853 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006854 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006855
6856 locations->SetInAt(0, Location::RequiresRegister());
6857 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6858 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6859}
6860
6861void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6862 vixl32::Register out = OutputRegister(instruction);
6863 vixl32::Register first = InputRegisterAt(instruction, 0);
6864 Location second = instruction->GetLocations()->InAt(1);
6865
Artem Serov2bbc9532016-10-21 11:51:50 +01006866 if (second.IsRegister()) {
6867 __ Add(out, first, RegisterFrom(second));
6868 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006869 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006870 }
6871}
6872
Artem Serove1811ed2017-04-27 16:50:47 +01006873void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6874 HIntermediateAddressIndex* instruction) {
6875 LOG(FATAL) << "Unreachable " << instruction->GetId();
6876}
6877
6878void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6879 HIntermediateAddressIndex* instruction) {
6880 LOG(FATAL) << "Unreachable " << instruction->GetId();
6881}
6882
Scott Wakelingc34dba72016-10-03 10:14:44 +01006883void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6884 RegisterSet caller_saves = RegisterSet::Empty();
6885 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6886 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6887 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6888 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006889
6890 HInstruction* index = instruction->InputAt(0);
6891 HInstruction* length = instruction->InputAt(1);
6892 // If both index and length are constants we can statically check the bounds. But if at least one
6893 // of them is not encodable ArmEncodableConstantOrRegister will create
6894 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6895 // locations.
6896 bool both_const = index->IsConstant() && length->IsConstant();
6897 locations->SetInAt(0, both_const
6898 ? Location::ConstantLocation(index->AsConstant())
6899 : ArmEncodableConstantOrRegister(index, CMP));
6900 locations->SetInAt(1, both_const
6901 ? Location::ConstantLocation(length->AsConstant())
6902 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006903}
6904
6905void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006906 LocationSummary* locations = instruction->GetLocations();
6907 Location index_loc = locations->InAt(0);
6908 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006909
Artem Serov2dd053d2017-03-08 14:54:06 +00006910 if (length_loc.IsConstant()) {
6911 int32_t length = Int32ConstantFrom(length_loc);
6912 if (index_loc.IsConstant()) {
6913 // BCE will remove the bounds check if we are guaranteed to pass.
6914 int32_t index = Int32ConstantFrom(index_loc);
6915 if (index < 0 || index >= length) {
6916 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006917 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006918 codegen_->AddSlowPath(slow_path);
6919 __ B(slow_path->GetEntryLabel());
6920 } else {
6921 // Some optimization after BCE may have generated this, and we should not
6922 // generate a bounds check if it is a valid range.
6923 }
6924 return;
6925 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006926
Artem Serov2dd053d2017-03-08 14:54:06 +00006927 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006928 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006929 __ Cmp(RegisterFrom(index_loc), length);
6930 codegen_->AddSlowPath(slow_path);
6931 __ B(hs, slow_path->GetEntryLabel());
6932 } else {
6933 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006934 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006935 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6936 codegen_->AddSlowPath(slow_path);
6937 __ B(ls, slow_path->GetEntryLabel());
6938 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006939}
6940
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006941void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6942 vixl32::Register card,
6943 vixl32::Register object,
6944 vixl32::Register value,
6945 bool can_be_null) {
6946 vixl32::Label is_null;
6947 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006948 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006949 }
Roland Levillainc73f0522018-08-14 15:16:50 +01006950 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006951 GetAssembler()->LoadFromOffset(
6952 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01006953 // Calculate the offset (in the card table) of the card corresponding to
6954 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00006955 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01006956 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
6957 // `object`'s card.
6958 //
6959 // Register `card` contains the address of the card table. Note that the card
6960 // table's base is biased during its creation so that it always starts at an
6961 // address whose least-significant byte is equal to `kCardDirty` (see
6962 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
6963 // below writes the `kCardDirty` (byte) value into the `object`'s card
6964 // (located at `card + object >> kCardShift`).
6965 //
6966 // This dual use of the value in register `card` (1. to calculate the location
6967 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
6968 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006969 __ Strb(card, MemOperand(card, temp));
6970 if (can_be_null) {
6971 __ Bind(&is_null);
6972 }
6973}
6974
Scott Wakelingfe885462016-09-22 10:24:38 +01006975void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6976 LOG(FATAL) << "Unreachable";
6977}
6978
6979void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006980 if (instruction->GetNext()->IsSuspendCheck() &&
6981 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6982 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6983 // The back edge will generate the suspend check.
6984 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6985 }
6986
Scott Wakelingfe885462016-09-22 10:24:38 +01006987 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6988}
6989
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006990void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006991 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6992 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006993 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006994}
6995
6996void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6997 HBasicBlock* block = instruction->GetBlock();
6998 if (block->GetLoopInformation() != nullptr) {
6999 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7000 // The back edge will generate the suspend check.
7001 return;
7002 }
7003 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7004 // The goto will generate the suspend check.
7005 return;
7006 }
7007 GenerateSuspendCheck(instruction, nullptr);
Andra Danciua0130e82020-07-23 12:34:56 +00007008 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007009}
7010
7011void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7012 HBasicBlock* successor) {
7013 SuspendCheckSlowPathARMVIXL* slow_path =
7014 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7015 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007016 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007017 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007018 instruction->SetSlowPath(slow_path);
7019 codegen_->AddSlowPath(slow_path);
7020 if (successor != nullptr) {
7021 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007022 }
7023 } else {
7024 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7025 }
7026
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007027 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007028 vixl32::Register temp = temps.Acquire();
7029 GetAssembler()->LoadFromOffset(
7030 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7031 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007032 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007033 __ Bind(slow_path->GetReturnLabel());
7034 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007035 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007036 __ B(slow_path->GetEntryLabel());
7037 }
7038}
7039
Scott Wakelingfe885462016-09-22 10:24:38 +01007040ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7041 return codegen_->GetAssembler();
7042}
7043
7044void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007045 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007046 MoveOperands* move = moves_[index];
7047 Location source = move->GetSource();
7048 Location destination = move->GetDestination();
7049
7050 if (source.IsRegister()) {
7051 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007052 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007053 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007054 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007055 } else {
7056 DCHECK(destination.IsStackSlot());
7057 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007058 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007059 sp,
7060 destination.GetStackIndex());
7061 }
7062 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007063 if (destination.IsRegister()) {
7064 GetAssembler()->LoadFromOffset(kLoadWord,
7065 RegisterFrom(destination),
7066 sp,
7067 source.GetStackIndex());
7068 } else if (destination.IsFpuRegister()) {
7069 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7070 } else {
7071 DCHECK(destination.IsStackSlot());
7072 vixl32::Register temp = temps.Acquire();
7073 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7074 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7075 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007076 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007077 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007078 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007079 } else if (destination.IsFpuRegister()) {
7080 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7081 } else {
7082 DCHECK(destination.IsStackSlot());
7083 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7084 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007085 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007086 if (destination.IsDoubleStackSlot()) {
7087 vixl32::DRegister temp = temps.AcquireD();
7088 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7089 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7090 } else if (destination.IsRegisterPair()) {
7091 DCHECK(ExpectedPairLayout(destination));
7092 GetAssembler()->LoadFromOffset(
7093 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7094 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007095 DCHECK(destination.IsFpuRegisterPair()) << destination;
7096 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007097 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007098 } else if (source.IsRegisterPair()) {
7099 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007100 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7101 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007102 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007103 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007104 } else {
7105 DCHECK(destination.IsDoubleStackSlot()) << destination;
7106 DCHECK(ExpectedPairLayout(source));
7107 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007108 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007109 sp,
7110 destination.GetStackIndex());
7111 }
7112 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007113 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007114 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007115 } else if (destination.IsFpuRegisterPair()) {
7116 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7117 } else {
7118 DCHECK(destination.IsDoubleStackSlot()) << destination;
7119 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7120 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007121 } else {
7122 DCHECK(source.IsConstant()) << source;
7123 HConstant* constant = source.GetConstant();
7124 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7125 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7126 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007127 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007128 } else {
7129 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007130 vixl32::Register temp = temps.Acquire();
7131 __ Mov(temp, value);
7132 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7133 }
7134 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007135 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007136 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007137 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7138 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007139 } else {
7140 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007141 vixl32::Register temp = temps.Acquire();
7142 __ Mov(temp, Low32Bits(value));
7143 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7144 __ Mov(temp, High32Bits(value));
7145 GetAssembler()->StoreToOffset(kStoreWord,
7146 temp,
7147 sp,
7148 destination.GetHighStackIndex(kArmWordSize));
7149 }
7150 } else if (constant->IsDoubleConstant()) {
7151 double value = constant->AsDoubleConstant()->GetValue();
7152 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007153 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007154 } else {
7155 DCHECK(destination.IsDoubleStackSlot()) << destination;
7156 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007157 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007158 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007159 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007160 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007161 GetAssembler()->StoreToOffset(kStoreWord,
7162 temp,
7163 sp,
7164 destination.GetHighStackIndex(kArmWordSize));
7165 }
7166 } else {
7167 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7168 float value = constant->AsFloatConstant()->GetValue();
7169 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007170 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007171 } else {
7172 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007173 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007174 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007175 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7176 }
7177 }
7178 }
7179}
7180
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007181void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7182 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7183 vixl32::Register temp = temps.Acquire();
7184 __ Mov(temp, reg);
7185 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7186 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007187}
7188
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007189void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7190 // TODO(VIXL32): Double check the performance of this implementation.
7191 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007192 vixl32::Register temp1 = temps.Acquire();
7193 ScratchRegisterScope ensure_scratch(
7194 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7195 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007196
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007197 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7198 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7199 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7200 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7201 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007202}
7203
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007204void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7205 MoveOperands* move = moves_[index];
7206 Location source = move->GetSource();
7207 Location destination = move->GetDestination();
7208 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7209
7210 if (source.IsRegister() && destination.IsRegister()) {
7211 vixl32::Register temp = temps.Acquire();
7212 DCHECK(!RegisterFrom(source).Is(temp));
7213 DCHECK(!RegisterFrom(destination).Is(temp));
7214 __ Mov(temp, RegisterFrom(destination));
7215 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7216 __ Mov(RegisterFrom(source), temp);
7217 } else if (source.IsRegister() && destination.IsStackSlot()) {
7218 Exchange(RegisterFrom(source), destination.GetStackIndex());
7219 } else if (source.IsStackSlot() && destination.IsRegister()) {
7220 Exchange(RegisterFrom(destination), source.GetStackIndex());
7221 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007222 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007223 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007224 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007225 __ Vmov(temp, SRegisterFrom(source));
7226 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7227 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007228 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7229 vixl32::DRegister temp = temps.AcquireD();
7230 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7231 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7232 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7233 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7234 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7235 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7236 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7237 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7238 vixl32::DRegister temp = temps.AcquireD();
7239 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7240 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7241 GetAssembler()->StoreDToOffset(temp, sp, mem);
7242 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007243 vixl32::DRegister first = DRegisterFrom(source);
7244 vixl32::DRegister second = DRegisterFrom(destination);
7245 vixl32::DRegister temp = temps.AcquireD();
7246 __ Vmov(temp, first);
7247 __ Vmov(first, second);
7248 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007249 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007250 vixl32::DRegister reg = source.IsFpuRegisterPair()
7251 ? DRegisterFrom(source)
7252 : DRegisterFrom(destination);
7253 int mem = source.IsFpuRegisterPair()
7254 ? destination.GetStackIndex()
7255 : source.GetStackIndex();
7256 vixl32::DRegister temp = temps.AcquireD();
7257 __ Vmov(temp, reg);
7258 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7259 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007260 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007261 vixl32::SRegister reg = source.IsFpuRegister()
7262 ? SRegisterFrom(source)
7263 : SRegisterFrom(destination);
7264 int mem = source.IsFpuRegister()
7265 ? destination.GetStackIndex()
7266 : source.GetStackIndex();
7267 vixl32::Register temp = temps.Acquire();
7268 __ Vmov(temp, reg);
7269 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7270 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007271 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7272 vixl32::DRegister temp1 = temps.AcquireD();
7273 vixl32::DRegister temp2 = temps.AcquireD();
7274 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7275 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7276 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7277 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7278 } else {
7279 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7280 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007281}
7282
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007283void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7284 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007285}
7286
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007287void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7288 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007289}
7290
Artem Serov02d37832016-10-25 15:25:33 +01007291HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007292 HLoadClass::LoadKind desired_class_load_kind) {
7293 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007294 case HLoadClass::LoadKind::kInvalid:
7295 LOG(FATAL) << "UNREACHABLE";
7296 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007297 case HLoadClass::LoadKind::kReferrersClass:
7298 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007299 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007300 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007301 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007302 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007303 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007304 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007305 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007306 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007307 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007308 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007309 break;
7310 }
7311 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007312}
7313
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007314void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007315 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007316 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007317 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007318 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007319 cls,
7320 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007321 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007322 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007323 return;
7324 }
Vladimir Marko41559982017-01-06 14:04:23 +00007325 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007326
Artem Serovd4cc5b22016-11-04 11:19:09 +00007327 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7328 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007329 ? LocationSummary::kCallOnSlowPath
7330 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007331 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007332 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007333 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007334 }
7335
Vladimir Marko41559982017-01-06 14:04:23 +00007336 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007337 locations->SetInAt(0, Location::RequiresRegister());
7338 }
7339 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007340 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7341 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7342 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007343 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007344 } else {
7345 // For non-Baker read barrier we have a temp-clobbering call.
7346 }
7347 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007348}
7349
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007350// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7351// move.
7352void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007353 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007354 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007355 codegen_->GenerateLoadClassRuntimeCall(cls);
Andra Danciua0130e82020-07-23 12:34:56 +00007356 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007357 return;
7358 }
Vladimir Marko41559982017-01-06 14:04:23 +00007359 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007360
Vladimir Marko41559982017-01-06 14:04:23 +00007361 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007362 Location out_loc = locations->Out();
7363 vixl32::Register out = OutputRegister(cls);
7364
Artem Serovd4cc5b22016-11-04 11:19:09 +00007365 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7366 ? kWithoutReadBarrier
7367 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007368 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007369 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007370 case HLoadClass::LoadKind::kReferrersClass: {
7371 DCHECK(!cls->CanCallRuntime());
7372 DCHECK(!cls->MustGenerateClinitCheck());
7373 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7374 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007375 codegen_->GenerateGcRootFieldLoad(cls,
7376 out_loc,
7377 current_method,
7378 ArtMethod::DeclaringClassOffset().Int32Value(),
7379 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007380 break;
7381 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007382 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007383 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7384 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007385 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7386 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007387 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007388 codegen_->EmitMovwMovtPlaceholder(labels, out);
7389 break;
7390 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007391 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007392 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7393 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007394 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007395 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007396 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007397 break;
7398 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007399 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007400 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007401 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007402 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007403 // All aligned loads are implicitly atomic consume operations on ARM.
Andreas Gampe3db70682018-12-26 15:12:03 -08007404 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007405 generate_null_check = true;
7406 break;
7407 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007408 case HLoadClass::LoadKind::kJitBootImageAddress: {
7409 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7410 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7411 DCHECK_NE(address, 0u);
7412 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7413 break;
7414 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007415 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007416 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7417 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007418 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007419 // /* GcRoot<mirror::Class> */ out = *out
Andreas Gampe3db70682018-12-26 15:12:03 -08007420 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007421 break;
7422 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007423 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007424 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007425 LOG(FATAL) << "UNREACHABLE";
7426 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007427 }
7428
7429 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7430 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007431 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007432 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007433 codegen_->AddSlowPath(slow_path);
7434 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007435 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007436 }
7437 if (cls->MustGenerateClinitCheck()) {
7438 GenerateClassInitializationCheck(slow_path, out);
7439 } else {
7440 __ Bind(slow_path->GetExitLabel());
7441 }
Andra Danciua0130e82020-07-23 12:34:56 +00007442 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 16);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007443 }
7444}
7445
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007446void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7447 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7448 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7449 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7450}
7451
7452void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7453 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7454}
7455
Orion Hodson18259d72018-04-12 11:18:23 +01007456void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7457 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7458 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7459 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7460}
7461
7462void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7463 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7464}
7465
Artem Serov02d37832016-10-25 15:25:33 +01007466void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7467 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007468 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007469 locations->SetInAt(0, Location::RequiresRegister());
7470 if (check->HasUses()) {
7471 locations->SetOut(Location::SameAsFirstInput());
7472 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007473 // Rely on the type initialization to save everything we need.
7474 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007475}
7476
7477void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7478 // We assume the class is not null.
7479 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007480 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007481 codegen_->AddSlowPath(slow_path);
7482 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7483}
7484
7485void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7486 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7487 UseScratchRegisterScope temps(GetVIXLAssembler());
7488 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007489 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Markobf121912019-06-04 13:49:05 +01007490 constexpr uint32_t shifted_visibly_initialized_value =
7491 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << status_lsb_position;
Vladimir Markodc682aa2018-01-04 18:42:57 +00007492
Vladimir Markobf121912019-06-04 13:49:05 +01007493 const size_t status_offset = mirror::Class::StatusOffset().SizeValue();
7494 GetAssembler()->LoadFromOffset(kLoadWord, temp, class_reg, status_offset);
7495 __ Cmp(temp, shifted_visibly_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007496 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007497 __ Bind(slow_path->GetExitLabel());
7498}
7499
Vladimir Marko175e7862018-03-27 09:03:13 +00007500void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7501 HTypeCheckInstruction* check,
7502 vixl32::Register temp,
7503 vixl32::FlagsUpdate flags_update) {
7504 uint32_t path_to_root = check->GetBitstringPathToRoot();
7505 uint32_t mask = check->GetBitstringMask();
7506 DCHECK(IsPowerOfTwo(mask + 1));
7507 size_t mask_bits = WhichPowerOf2(mask + 1);
7508
7509 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7510 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7511 if (mask_bits == 16u) {
7512 // Load only the bitstring part of the status word.
7513 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7514 // Check if the bitstring bits are equal to `path_to_root`.
7515 if (flags_update == SetFlags) {
7516 __ Cmp(temp, path_to_root);
7517 } else {
7518 __ Sub(temp, temp, path_to_root);
7519 }
7520 } else {
7521 // /* uint32_t */ temp = temp->status_
7522 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7523 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7524 // Compare the bitstring bits using SUB.
7525 __ Sub(temp, temp, path_to_root);
7526 // Shift out bits that do not contribute to the comparison.
7527 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7528 } else if (IsUint<16>(path_to_root)) {
7529 if (temp.IsLow()) {
7530 // Note: Optimized for size but contains one more dependent instruction than necessary.
7531 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7532 // macro assembler would use the high reg IP for the constant by default.
7533 // Compare the bitstring bits using SUB.
7534 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7535 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7536 // Shift out bits that do not contribute to the comparison.
7537 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7538 } else {
7539 // Extract the bitstring bits.
7540 __ Ubfx(temp, temp, 0, mask_bits);
7541 // Check if the bitstring bits are equal to `path_to_root`.
7542 if (flags_update == SetFlags) {
7543 __ Cmp(temp, path_to_root);
7544 } else {
7545 __ Sub(temp, temp, path_to_root);
7546 }
7547 }
7548 } else {
7549 // Shift out bits that do not contribute to the comparison.
7550 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7551 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7552 if (flags_update == SetFlags) {
7553 __ Cmp(temp, path_to_root << (32u - mask_bits));
7554 } else {
7555 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7556 }
7557 }
7558 }
7559}
7560
Artem Serov02d37832016-10-25 15:25:33 +01007561HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007562 HLoadString::LoadKind desired_string_load_kind) {
7563 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007564 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007565 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007566 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007567 DCHECK(!GetCompilerOptions().IsJitCompiler());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007568 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007569 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007570 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007571 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007572 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007573 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007574 break;
7575 }
7576 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007577}
7578
7579void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007580 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007581 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007582 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007583 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007584 locations->SetOut(LocationFrom(r0));
7585 } else {
7586 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007587 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7588 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007589 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007590 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007591 } else {
7592 // For non-Baker read barrier we have a temp-clobbering call.
7593 }
7594 }
Artem Serov02d37832016-10-25 15:25:33 +01007595 }
7596}
7597
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007598// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7599// move.
7600void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007601 LocationSummary* locations = load->GetLocations();
7602 Location out_loc = locations->Out();
7603 vixl32::Register out = OutputRegister(load);
7604 HLoadString::LoadKind load_kind = load->GetLoadKind();
7605
7606 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007607 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007608 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7609 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007610 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007611 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007612 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007613 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007614 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007615 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007616 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7617 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007618 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007619 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007620 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007621 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007622 }
7623 case HLoadString::LoadKind::kBssEntry: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007624 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007625 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007626 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007627 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007628 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007629 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007630 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007631 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007632 codegen_->AddSlowPath(slow_path);
7633 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7634 __ Bind(slow_path->GetExitLabel());
Andra Danciua0130e82020-07-23 12:34:56 +00007635 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 17);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007636 return;
7637 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007638 case HLoadString::LoadKind::kJitBootImageAddress: {
7639 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7640 DCHECK_NE(address, 0u);
7641 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7642 return;
7643 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007644 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007645 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007646 load->GetStringIndex(),
7647 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007648 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007649 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007650 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007651 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007652 }
7653 default:
7654 break;
7655 }
Artem Serov02d37832016-10-25 15:25:33 +01007656
7657 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007658 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007659 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007660 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007661 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7662 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andra Danciua0130e82020-07-23 12:34:56 +00007663 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 18);
Artem Serov02d37832016-10-25 15:25:33 +01007664}
7665
7666static int32_t GetExceptionTlsOffset() {
7667 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7668}
7669
7670void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7671 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007672 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007673 locations->SetOut(Location::RequiresRegister());
7674}
7675
7676void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7677 vixl32::Register out = OutputRegister(load);
7678 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7679}
7680
7681
7682void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007683 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007684}
7685
7686void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7687 UseScratchRegisterScope temps(GetVIXLAssembler());
7688 vixl32::Register temp = temps.Acquire();
7689 __ Mov(temp, 0);
7690 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7691}
7692
7693void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007694 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7695 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007696 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7697 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7698}
7699
7700void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7701 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7702 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7703}
7704
Artem Serov657022c2016-11-23 14:19:38 +00007705// Temp is used for read barrier.
7706static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7707 if (kEmitCompilerReadBarrier &&
7708 (kUseBakerReadBarrier ||
7709 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7710 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7711 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7712 return 1;
7713 }
7714 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007715}
7716
Artem Serov657022c2016-11-23 14:19:38 +00007717// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7718// interface pointer, one for loading the current interface.
7719// The other checks have one temp for loading the object's class.
7720static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7721 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7722 return 3;
7723 }
7724 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7725}
Artem Serovcfbe9132016-10-14 15:58:56 +01007726
7727void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7728 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7729 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7730 bool baker_read_barrier_slow_path = false;
7731 switch (type_check_kind) {
7732 case TypeCheckKind::kExactCheck:
7733 case TypeCheckKind::kAbstractClassCheck:
7734 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007735 case TypeCheckKind::kArrayObjectCheck: {
7736 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7737 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7738 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007739 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007740 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007741 case TypeCheckKind::kArrayCheck:
7742 case TypeCheckKind::kUnresolvedCheck:
7743 case TypeCheckKind::kInterfaceCheck:
7744 call_kind = LocationSummary::kCallOnSlowPath;
7745 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007746 case TypeCheckKind::kBitstringCheck:
7747 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007748 }
7749
Vladimir Markoca6fff82017-10-03 14:49:14 +01007750 LocationSummary* locations =
7751 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007752 if (baker_read_barrier_slow_path) {
7753 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7754 }
7755 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007756 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7757 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7758 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7759 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7760 } else {
7761 locations->SetInAt(1, Location::RequiresRegister());
7762 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007763 // The "out" register is used as a temporary, so it overlaps with the inputs.
7764 // Note that TypeCheckSlowPathARM uses this register too.
7765 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007766 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007767}
7768
7769void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7770 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7771 LocationSummary* locations = instruction->GetLocations();
7772 Location obj_loc = locations->InAt(0);
7773 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007774 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7775 ? vixl32::Register()
7776 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007777 Location out_loc = locations->Out();
7778 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007779 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7780 DCHECK_LE(num_temps, 1u);
7781 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007782 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7783 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7784 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7785 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007786 vixl32::Label done;
7787 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007788 SlowPathCodeARMVIXL* slow_path = nullptr;
7789
7790 // Return 0 if `obj` is null.
7791 // avoid null check if we know obj is not null.
7792 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007793 DCHECK(!out.Is(obj));
7794 __ Mov(out, 0);
Andreas Gampe3db70682018-12-26 15:12:03 -08007795 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007796 }
7797
Artem Serovcfbe9132016-10-14 15:58:56 +01007798 switch (type_check_kind) {
7799 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007800 ReadBarrierOption read_barrier_option =
7801 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007802 // /* HeapReference<Class> */ out = obj->klass_
7803 GenerateReferenceLoadTwoRegisters(instruction,
7804 out_loc,
7805 obj_loc,
7806 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007807 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007808 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007809 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007810 __ Cmp(out, cls);
7811 // We speculatively set the result to false without changing the condition
7812 // flags, which allows us to avoid some branching later.
7813 __ Mov(LeaveFlags, out, 0);
7814
7815 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7816 // we check that the output is in a low register, so that a 16-bit MOV
7817 // encoding can be used.
7818 if (out.IsLow()) {
7819 // We use the scope because of the IT block that follows.
7820 ExactAssemblyScope guard(GetVIXLAssembler(),
7821 2 * vixl32::k16BitT32InstructionSizeInBytes,
7822 CodeBufferCheckScope::kExactSize);
7823
7824 __ it(eq);
7825 __ mov(eq, out, 1);
7826 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007827 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007828 __ Mov(out, 1);
7829 }
7830
Artem Serovcfbe9132016-10-14 15:58:56 +01007831 break;
7832 }
7833
7834 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007835 ReadBarrierOption read_barrier_option =
7836 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007837 // /* HeapReference<Class> */ out = obj->klass_
7838 GenerateReferenceLoadTwoRegisters(instruction,
7839 out_loc,
7840 obj_loc,
7841 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007842 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007843 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007844 // If the class is abstract, we eagerly fetch the super class of the
7845 // object to avoid doing a comparison we know will fail.
7846 vixl32::Label loop;
7847 __ Bind(&loop);
7848 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007849 GenerateReferenceLoadOneRegister(instruction,
7850 out_loc,
7851 super_offset,
7852 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007853 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007854 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007855 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007856 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007857 __ B(ne, &loop, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007858 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007859 break;
7860 }
7861
7862 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007863 ReadBarrierOption read_barrier_option =
7864 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007865 // /* HeapReference<Class> */ out = obj->klass_
7866 GenerateReferenceLoadTwoRegisters(instruction,
7867 out_loc,
7868 obj_loc,
7869 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007870 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007871 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007872 // Walk over the class hierarchy to find a match.
7873 vixl32::Label loop, success;
7874 __ Bind(&loop);
7875 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007876 __ B(eq, &success, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007877 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007878 GenerateReferenceLoadOneRegister(instruction,
7879 out_loc,
7880 super_offset,
7881 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007882 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007883 // This is essentially a null check, but it sets the condition flags to the
7884 // proper value for the code that follows the loop, i.e. not `eq`.
7885 __ Cmp(out, 1);
Andreas Gampe3db70682018-12-26 15:12:03 -08007886 __ B(hs, &loop, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007887
7888 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7889 // we check that the output is in a low register, so that a 16-bit MOV
7890 // encoding can be used.
7891 if (out.IsLow()) {
7892 // If `out` is null, we use it for the result, and the condition flags
7893 // have already been set to `ne`, so the IT block that comes afterwards
7894 // (and which handles the successful case) turns into a NOP (instead of
7895 // overwriting `out`).
7896 __ Bind(&success);
7897
7898 // We use the scope because of the IT block that follows.
7899 ExactAssemblyScope guard(GetVIXLAssembler(),
7900 2 * vixl32::k16BitT32InstructionSizeInBytes,
7901 CodeBufferCheckScope::kExactSize);
7902
7903 // There is only one branch to the `success` label (which is bound to this
7904 // IT block), and it has the same condition, `eq`, so in that case the MOV
7905 // is executed.
7906 __ it(eq);
7907 __ mov(eq, out, 1);
7908 } else {
7909 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007910 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007911 __ Bind(&success);
7912 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007913 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007914
Artem Serovcfbe9132016-10-14 15:58:56 +01007915 break;
7916 }
7917
7918 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007919 ReadBarrierOption read_barrier_option =
7920 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007921 // /* HeapReference<Class> */ out = obj->klass_
7922 GenerateReferenceLoadTwoRegisters(instruction,
7923 out_loc,
7924 obj_loc,
7925 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007926 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007927 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007928 // Do an exact check.
7929 vixl32::Label exact_check;
7930 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007931 __ B(eq, &exact_check, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007932 // Otherwise, we need to check that the object's class is a non-primitive array.
7933 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007934 GenerateReferenceLoadOneRegister(instruction,
7935 out_loc,
7936 component_offset,
7937 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007938 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007939 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007940 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007941 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7942 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007943 __ Cmp(out, 0);
7944 // We speculatively set the result to false without changing the condition
7945 // flags, which allows us to avoid some branching later.
7946 __ Mov(LeaveFlags, out, 0);
7947
7948 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7949 // we check that the output is in a low register, so that a 16-bit MOV
7950 // encoding can be used.
7951 if (out.IsLow()) {
7952 __ Bind(&exact_check);
7953
7954 // We use the scope because of the IT block that follows.
7955 ExactAssemblyScope guard(GetVIXLAssembler(),
7956 2 * vixl32::k16BitT32InstructionSizeInBytes,
7957 CodeBufferCheckScope::kExactSize);
7958
7959 __ it(eq);
7960 __ mov(eq, out, 1);
7961 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007962 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007963 __ Bind(&exact_check);
7964 __ Mov(out, 1);
7965 }
7966
Artem Serovcfbe9132016-10-14 15:58:56 +01007967 break;
7968 }
7969
7970 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007971 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007972 // /* HeapReference<Class> */ out = obj->klass_
7973 GenerateReferenceLoadTwoRegisters(instruction,
7974 out_loc,
7975 obj_loc,
7976 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007977 maybe_temp_loc,
7978 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007979 __ Cmp(out, cls);
7980 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007981 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08007982 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007983 codegen_->AddSlowPath(slow_path);
7984 __ B(ne, slow_path->GetEntryLabel());
7985 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007986 break;
7987 }
7988
7989 case TypeCheckKind::kUnresolvedCheck:
7990 case TypeCheckKind::kInterfaceCheck: {
7991 // Note that we indeed only call on slow path, but we always go
7992 // into the slow path for the unresolved and interface check
7993 // cases.
7994 //
7995 // We cannot directly call the InstanceofNonTrivial runtime
7996 // entry point without resorting to a type checking slow path
7997 // here (i.e. by calling InvokeRuntime directly), as it would
7998 // require to assign fixed registers for the inputs of this
7999 // HInstanceOf instruction (following the runtime calling
8000 // convention), which might be cluttered by the potential first
8001 // read barrier emission at the beginning of this method.
8002 //
8003 // TODO: Introduce a new runtime entry point taking the object
8004 // to test (instead of its class) as argument, and let it deal
8005 // with the read barrier issues. This will let us refactor this
8006 // case of the `switch` code as it was previously (with a direct
8007 // call to the runtime not using a type checking slow path).
8008 // This should also be beneficial for the other cases above.
8009 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008010 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008011 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008012 codegen_->AddSlowPath(slow_path);
8013 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008014 break;
8015 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008016
8017 case TypeCheckKind::kBitstringCheck: {
8018 // /* HeapReference<Class> */ temp = obj->klass_
8019 GenerateReferenceLoadTwoRegisters(instruction,
8020 out_loc,
8021 obj_loc,
8022 class_offset,
8023 maybe_temp_loc,
8024 kWithoutReadBarrier);
8025
8026 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
8027 // If `out` is a low reg and we would have another low reg temp, we could
8028 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
8029 //
8030 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
8031 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
8032 // would be the same and we would have fewer direct data dependencies.
8033 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
8034 break;
8035 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008036 }
8037
Artem Serovcfbe9132016-10-14 15:58:56 +01008038 if (done.IsReferenced()) {
8039 __ Bind(&done);
8040 }
8041
8042 if (slow_path != nullptr) {
8043 __ Bind(slow_path->GetExitLabel());
8044 }
8045}
8046
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008047void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008048 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008049 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008050 LocationSummary* locations =
8051 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008052 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00008053 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
8054 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
8055 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
8056 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
8057 } else {
8058 locations->SetInAt(1, Location::RequiresRegister());
8059 }
Artem Serov657022c2016-11-23 14:19:38 +00008060 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008061}
8062
8063void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8064 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8065 LocationSummary* locations = instruction->GetLocations();
8066 Location obj_loc = locations->InAt(0);
8067 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00008068 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
8069 ? vixl32::Register()
8070 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008071 Location temp_loc = locations->GetTemp(0);
8072 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008073 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8074 DCHECK_LE(num_temps, 3u);
8075 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8076 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8077 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8078 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8079 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8080 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8081 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8082 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8083 const uint32_t object_array_data_offset =
8084 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008085
Vladimir Marko87584542017-12-12 17:47:52 +00008086 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008087 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008088 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8089 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008090 codegen_->AddSlowPath(type_check_slow_path);
8091
8092 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008093 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008094 // Avoid null check if we know obj is not null.
8095 if (instruction->MustDoNullCheck()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08008096 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008097 }
8098
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008099 switch (type_check_kind) {
8100 case TypeCheckKind::kExactCheck:
8101 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008102 // /* HeapReference<Class> */ temp = obj->klass_
8103 GenerateReferenceLoadTwoRegisters(instruction,
8104 temp_loc,
8105 obj_loc,
8106 class_offset,
8107 maybe_temp2_loc,
8108 kWithoutReadBarrier);
8109
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008110 __ Cmp(temp, cls);
8111 // Jump to slow path for throwing the exception or doing a
8112 // more involved array check.
8113 __ B(ne, type_check_slow_path->GetEntryLabel());
8114 break;
8115 }
8116
8117 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008118 // /* HeapReference<Class> */ temp = obj->klass_
8119 GenerateReferenceLoadTwoRegisters(instruction,
8120 temp_loc,
8121 obj_loc,
8122 class_offset,
8123 maybe_temp2_loc,
8124 kWithoutReadBarrier);
8125
Artem Serovcfbe9132016-10-14 15:58:56 +01008126 // If the class is abstract, we eagerly fetch the super class of the
8127 // object to avoid doing a comparison we know will fail.
8128 vixl32::Label loop;
8129 __ Bind(&loop);
8130 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008131 GenerateReferenceLoadOneRegister(instruction,
8132 temp_loc,
8133 super_offset,
8134 maybe_temp2_loc,
8135 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008136
8137 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8138 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008139 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008140
8141 // Otherwise, compare the classes.
8142 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008143 __ B(ne, &loop, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008144 break;
8145 }
8146
8147 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008148 // /* HeapReference<Class> */ temp = obj->klass_
8149 GenerateReferenceLoadTwoRegisters(instruction,
8150 temp_loc,
8151 obj_loc,
8152 class_offset,
8153 maybe_temp2_loc,
8154 kWithoutReadBarrier);
8155
Artem Serovcfbe9132016-10-14 15:58:56 +01008156 // Walk over the class hierarchy to find a match.
8157 vixl32::Label loop;
8158 __ Bind(&loop);
8159 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008160 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008161
8162 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008163 GenerateReferenceLoadOneRegister(instruction,
8164 temp_loc,
8165 super_offset,
8166 maybe_temp2_loc,
8167 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008168
8169 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8170 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008171 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008172 // Otherwise, jump to the beginning of the loop.
8173 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008174 break;
8175 }
8176
Artem Serovcfbe9132016-10-14 15:58:56 +01008177 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008178 // /* HeapReference<Class> */ temp = obj->klass_
8179 GenerateReferenceLoadTwoRegisters(instruction,
8180 temp_loc,
8181 obj_loc,
8182 class_offset,
8183 maybe_temp2_loc,
8184 kWithoutReadBarrier);
8185
Artem Serovcfbe9132016-10-14 15:58:56 +01008186 // Do an exact check.
8187 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008188 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008189
8190 // Otherwise, we need to check that the object's class is a non-primitive array.
8191 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008192 GenerateReferenceLoadOneRegister(instruction,
8193 temp_loc,
8194 component_offset,
8195 maybe_temp2_loc,
8196 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008197 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008198 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008199 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8200 // to further check that this component type is not a primitive type.
8201 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008202 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008203 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008204 break;
8205 }
8206
8207 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008208 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008209 // We cannot directly call the CheckCast runtime entry point
8210 // without resorting to a type checking slow path here (i.e. by
8211 // calling InvokeRuntime directly), as it would require to
8212 // assign fixed registers for the inputs of this HInstanceOf
8213 // instruction (following the runtime calling convention), which
8214 // might be cluttered by the potential first read barrier
8215 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008216
Artem Serovcfbe9132016-10-14 15:58:56 +01008217 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008218 break;
Artem Serov657022c2016-11-23 14:19:38 +00008219
8220 case TypeCheckKind::kInterfaceCheck: {
8221 // Avoid read barriers to improve performance of the fast path. We can not get false
8222 // positives by doing this.
8223 // /* HeapReference<Class> */ temp = obj->klass_
8224 GenerateReferenceLoadTwoRegisters(instruction,
8225 temp_loc,
8226 obj_loc,
8227 class_offset,
8228 maybe_temp2_loc,
8229 kWithoutReadBarrier);
8230
8231 // /* HeapReference<Class> */ temp = temp->iftable_
8232 GenerateReferenceLoadTwoRegisters(instruction,
8233 temp_loc,
8234 temp_loc,
8235 iftable_offset,
8236 maybe_temp2_loc,
8237 kWithoutReadBarrier);
8238 // Iftable is never null.
8239 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8240 // Loop through the iftable and check if any class matches.
8241 vixl32::Label start_loop;
8242 __ Bind(&start_loop);
8243 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8244 type_check_slow_path->GetEntryLabel());
8245 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8246 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8247 // Go to next interface.
8248 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8249 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8250 // Compare the classes and continue the loop if they do not match.
8251 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Andreas Gampe3db70682018-12-26 15:12:03 -08008252 __ B(ne, &start_loop, /* is_far_target= */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008253 break;
8254 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008255
8256 case TypeCheckKind::kBitstringCheck: {
8257 // /* HeapReference<Class> */ temp = obj->klass_
8258 GenerateReferenceLoadTwoRegisters(instruction,
8259 temp_loc,
8260 obj_loc,
8261 class_offset,
8262 maybe_temp2_loc,
8263 kWithoutReadBarrier);
8264
8265 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8266 __ B(ne, type_check_slow_path->GetEntryLabel());
8267 break;
8268 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008269 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008270 if (done.IsReferenced()) {
8271 __ Bind(&done);
8272 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008273
8274 __ Bind(type_check_slow_path->GetExitLabel());
8275}
8276
Artem Serov551b28f2016-10-18 19:11:30 +01008277void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008278 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8279 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008280 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8281 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8282}
8283
8284void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8285 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8286 instruction,
8287 instruction->GetDexPc());
8288 if (instruction->IsEnter()) {
8289 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8290 } else {
8291 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8292 }
Andra Danciua0130e82020-07-23 12:34:56 +00008293 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 19);
Artem Serov551b28f2016-10-18 19:11:30 +01008294}
8295
Artem Serov02109dd2016-09-23 17:17:54 +01008296void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8297 HandleBitwiseOperation(instruction, AND);
8298}
8299
8300void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8301 HandleBitwiseOperation(instruction, ORR);
8302}
8303
8304void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8305 HandleBitwiseOperation(instruction, EOR);
8306}
8307
8308void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8309 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008310 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008311 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8312 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008313 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8314 locations->SetInAt(0, Location::RequiresRegister());
8315 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8317}
8318
8319void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8320 HandleBitwiseOperation(instruction);
8321}
8322
8323void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8324 HandleBitwiseOperation(instruction);
8325}
8326
8327void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8328 HandleBitwiseOperation(instruction);
8329}
8330
Artem Serov2bbc9532016-10-21 11:51:50 +01008331void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8332 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008333 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008334 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8335 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008336
8337 locations->SetInAt(0, Location::RequiresRegister());
8338 locations->SetInAt(1, Location::RequiresRegister());
8339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8340}
8341
8342void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8343 LocationSummary* locations = instruction->GetLocations();
8344 Location first = locations->InAt(0);
8345 Location second = locations->InAt(1);
8346 Location out = locations->Out();
8347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008348 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008349 vixl32::Register first_reg = RegisterFrom(first);
8350 vixl32::Register second_reg = RegisterFrom(second);
8351 vixl32::Register out_reg = RegisterFrom(out);
8352
8353 switch (instruction->GetOpKind()) {
8354 case HInstruction::kAnd:
8355 __ Bic(out_reg, first_reg, second_reg);
8356 break;
8357 case HInstruction::kOr:
8358 __ Orn(out_reg, first_reg, second_reg);
8359 break;
8360 // There is no EON on arm.
8361 case HInstruction::kXor:
8362 default:
8363 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8364 UNREACHABLE();
8365 }
8366 return;
8367
8368 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008369 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008370 vixl32::Register first_low = LowRegisterFrom(first);
8371 vixl32::Register first_high = HighRegisterFrom(first);
8372 vixl32::Register second_low = LowRegisterFrom(second);
8373 vixl32::Register second_high = HighRegisterFrom(second);
8374 vixl32::Register out_low = LowRegisterFrom(out);
8375 vixl32::Register out_high = HighRegisterFrom(out);
8376
8377 switch (instruction->GetOpKind()) {
8378 case HInstruction::kAnd:
8379 __ Bic(out_low, first_low, second_low);
8380 __ Bic(out_high, first_high, second_high);
8381 break;
8382 case HInstruction::kOr:
8383 __ Orn(out_low, first_low, second_low);
8384 __ Orn(out_high, first_high, second_high);
8385 break;
8386 // There is no EON on arm.
8387 case HInstruction::kXor:
8388 default:
8389 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8390 UNREACHABLE();
8391 }
8392 }
8393}
8394
Anton Kirilov74234da2017-01-13 14:42:47 +00008395void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8396 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008397 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8398 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008399 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008400 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008401 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008402 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8403
8404 locations->SetInAt(0, Location::RequiresRegister());
8405 locations->SetInAt(1, Location::RequiresRegister());
8406 locations->SetOut(Location::RequiresRegister(),
8407 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8408}
8409
8410void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8411 HDataProcWithShifterOp* instruction) {
8412 const LocationSummary* const locations = instruction->GetLocations();
8413 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8414 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8415
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008416 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008417 const vixl32::Register first = InputRegisterAt(instruction, 0);
8418 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008419 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008420 ? LowRegisterFrom(locations->InAt(1))
8421 : InputRegisterAt(instruction, 1);
8422
Anton Kirilov420ee302017-02-21 18:10:26 +00008423 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8424 DCHECK_EQ(kind, HInstruction::kAdd);
8425
8426 switch (op_kind) {
8427 case HDataProcWithShifterOp::kUXTB:
8428 __ Uxtab(output, first, second);
8429 break;
8430 case HDataProcWithShifterOp::kUXTH:
8431 __ Uxtah(output, first, second);
8432 break;
8433 case HDataProcWithShifterOp::kSXTB:
8434 __ Sxtab(output, first, second);
8435 break;
8436 case HDataProcWithShifterOp::kSXTH:
8437 __ Sxtah(output, first, second);
8438 break;
8439 default:
8440 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8441 UNREACHABLE();
8442 }
8443 } else {
8444 GenerateDataProcInstruction(kind,
8445 output,
8446 first,
8447 Operand(second,
8448 ShiftFromOpKind(op_kind),
8449 instruction->GetShiftAmount()),
8450 codegen_);
8451 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008452 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008453 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008454
8455 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8456 const vixl32::Register second = InputRegisterAt(instruction, 1);
8457
8458 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8459 GenerateDataProc(kind,
8460 locations->Out(),
8461 locations->InAt(0),
8462 second,
8463 Operand(second, ShiftType::ASR, 31),
8464 codegen_);
8465 } else {
8466 GenerateLongDataProc(instruction, codegen_);
8467 }
8468 }
8469}
8470
Artem Serov02109dd2016-09-23 17:17:54 +01008471// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8472void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8473 vixl32::Register first,
8474 uint32_t value) {
8475 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8476 if (value == 0xffffffffu) {
8477 if (!out.Is(first)) {
8478 __ Mov(out, first);
8479 }
8480 return;
8481 }
8482 if (value == 0u) {
8483 __ Mov(out, 0);
8484 return;
8485 }
8486 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008487 __ And(out, first, value);
8488 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8489 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008490 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008491 DCHECK(IsPowerOfTwo(value + 1));
8492 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008493 }
8494}
8495
8496// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8497void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8498 vixl32::Register first,
8499 uint32_t value) {
8500 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8501 if (value == 0u) {
8502 if (!out.Is(first)) {
8503 __ Mov(out, first);
8504 }
8505 return;
8506 }
8507 if (value == 0xffffffffu) {
8508 __ Mvn(out, 0);
8509 return;
8510 }
8511 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8512 __ Orr(out, first, value);
8513 } else {
8514 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8515 __ Orn(out, first, ~value);
8516 }
8517}
8518
8519// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8520void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8521 vixl32::Register first,
8522 uint32_t value) {
8523 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8524 if (value == 0u) {
8525 if (!out.Is(first)) {
8526 __ Mov(out, first);
8527 }
8528 return;
8529 }
8530 __ Eor(out, first, value);
8531}
8532
Anton Kirilovdda43962016-11-21 19:55:20 +00008533void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8534 Location first,
8535 uint64_t value) {
8536 vixl32::Register out_low = LowRegisterFrom(out);
8537 vixl32::Register out_high = HighRegisterFrom(out);
8538 vixl32::Register first_low = LowRegisterFrom(first);
8539 vixl32::Register first_high = HighRegisterFrom(first);
8540 uint32_t value_low = Low32Bits(value);
8541 uint32_t value_high = High32Bits(value);
8542 if (value_low == 0u) {
8543 if (!out_low.Is(first_low)) {
8544 __ Mov(out_low, first_low);
8545 }
8546 __ Add(out_high, first_high, value_high);
8547 return;
8548 }
8549 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008550 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008551 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008552 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008553 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8554 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008555 }
8556}
8557
Artem Serov02109dd2016-09-23 17:17:54 +01008558void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8559 LocationSummary* locations = instruction->GetLocations();
8560 Location first = locations->InAt(0);
8561 Location second = locations->InAt(1);
8562 Location out = locations->Out();
8563
8564 if (second.IsConstant()) {
8565 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8566 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008567 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008568 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8569 vixl32::Register out_reg = OutputRegister(instruction);
8570 if (instruction->IsAnd()) {
8571 GenerateAndConst(out_reg, first_reg, value_low);
8572 } else if (instruction->IsOr()) {
8573 GenerateOrrConst(out_reg, first_reg, value_low);
8574 } else {
8575 DCHECK(instruction->IsXor());
8576 GenerateEorConst(out_reg, first_reg, value_low);
8577 }
8578 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008579 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008580 uint32_t value_high = High32Bits(value);
8581 vixl32::Register first_low = LowRegisterFrom(first);
8582 vixl32::Register first_high = HighRegisterFrom(first);
8583 vixl32::Register out_low = LowRegisterFrom(out);
8584 vixl32::Register out_high = HighRegisterFrom(out);
8585 if (instruction->IsAnd()) {
8586 GenerateAndConst(out_low, first_low, value_low);
8587 GenerateAndConst(out_high, first_high, value_high);
8588 } else if (instruction->IsOr()) {
8589 GenerateOrrConst(out_low, first_low, value_low);
8590 GenerateOrrConst(out_high, first_high, value_high);
8591 } else {
8592 DCHECK(instruction->IsXor());
8593 GenerateEorConst(out_low, first_low, value_low);
8594 GenerateEorConst(out_high, first_high, value_high);
8595 }
8596 }
8597 return;
8598 }
8599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008600 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008601 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8602 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8603 vixl32::Register out_reg = OutputRegister(instruction);
8604 if (instruction->IsAnd()) {
8605 __ And(out_reg, first_reg, second_reg);
8606 } else if (instruction->IsOr()) {
8607 __ Orr(out_reg, first_reg, second_reg);
8608 } else {
8609 DCHECK(instruction->IsXor());
8610 __ Eor(out_reg, first_reg, second_reg);
8611 }
8612 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008613 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008614 vixl32::Register first_low = LowRegisterFrom(first);
8615 vixl32::Register first_high = HighRegisterFrom(first);
8616 vixl32::Register second_low = LowRegisterFrom(second);
8617 vixl32::Register second_high = HighRegisterFrom(second);
8618 vixl32::Register out_low = LowRegisterFrom(out);
8619 vixl32::Register out_high = HighRegisterFrom(out);
8620 if (instruction->IsAnd()) {
8621 __ And(out_low, first_low, second_low);
8622 __ And(out_high, first_high, second_high);
8623 } else if (instruction->IsOr()) {
8624 __ Orr(out_low, first_low, second_low);
8625 __ Orr(out_high, first_high, second_high);
8626 } else {
8627 DCHECK(instruction->IsXor());
8628 __ Eor(out_low, first_low, second_low);
8629 __ Eor(out_high, first_high, second_high);
8630 }
8631 }
8632}
8633
Artem Serovcfbe9132016-10-14 15:58:56 +01008634void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008635 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008636 Location out,
8637 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008638 Location maybe_temp,
8639 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008640 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008641 if (read_barrier_option == kWithReadBarrier) {
8642 CHECK(kEmitCompilerReadBarrier);
8643 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8644 if (kUseBakerReadBarrier) {
8645 // Load with fast path based Baker's read barrier.
8646 // /* HeapReference<Object> */ out = *(out + offset)
8647 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008648 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008649 } else {
8650 // Load with slow path based read barrier.
8651 // Save the value of `out` into `maybe_temp` before overwriting it
8652 // in the following move operation, as we will need it for the
8653 // read barrier below.
8654 __ Mov(RegisterFrom(maybe_temp), out_reg);
8655 // /* HeapReference<Object> */ out = *(out + offset)
8656 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8657 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8658 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008659 } else {
8660 // Plain load with no read barrier.
8661 // /* HeapReference<Object> */ out = *(out + offset)
8662 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8663 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8664 }
8665}
8666
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008667void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008668 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008669 Location out,
8670 Location obj,
8671 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008672 Location maybe_temp,
8673 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008674 vixl32::Register out_reg = RegisterFrom(out);
8675 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008676 if (read_barrier_option == kWithReadBarrier) {
8677 CHECK(kEmitCompilerReadBarrier);
8678 if (kUseBakerReadBarrier) {
8679 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8680 // Load with fast path based Baker's read barrier.
8681 // /* HeapReference<Object> */ out = *(obj + offset)
8682 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008683 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008684 } else {
8685 // Load with slow path based read barrier.
8686 // /* HeapReference<Object> */ out = *(obj + offset)
8687 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8688 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8689 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008690 } else {
8691 // Plain load with no read barrier.
8692 // /* HeapReference<Object> */ out = *(obj + offset)
8693 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8694 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8695 }
8696}
8697
Vladimir Markoca1e0382018-04-11 09:58:41 +00008698void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008699 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008700 Location root,
8701 vixl32::Register obj,
8702 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008703 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008704 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008705 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008706 DCHECK(kEmitCompilerReadBarrier);
8707 if (kUseBakerReadBarrier) {
8708 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008709 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008710
Vladimir Marko008e09f32018-08-06 15:42:43 +01008711 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8712 // the Marking Register) to decide whether we need to enter
8713 // the slow path to mark the GC root.
8714 //
8715 // We use shared thunks for the slow path; shared within the method
8716 // for JIT, across methods for AOT. That thunk checks the reference
8717 // and jumps to the entrypoint if needed.
8718 //
8719 // lr = &return_address;
8720 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8721 // if (mr) { // Thread::Current()->GetIsGcMarking()
8722 // goto gc_root_thunk<root_reg>(lr)
8723 // }
8724 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008725
Vladimir Marko008e09f32018-08-06 15:42:43 +01008726 UseScratchRegisterScope temps(GetVIXLAssembler());
8727 temps.Exclude(ip);
8728 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8729 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008730
Vladimir Markod887ed82018-08-14 13:52:12 +00008731 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8732 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8733 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8734 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8735 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008736 vixl32::Label return_address;
8737 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8738 __ cmp(mr, Operand(0));
8739 // Currently the offset is always within range. If that changes,
8740 // we shall have to split the load the same way as for fields.
8741 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8742 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8743 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8744 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008745 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008746 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8747 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8748 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008749 } else {
8750 // GC root loaded through a slow path for read barriers other
8751 // than Baker's.
8752 // /* GcRoot<mirror::Object>* */ root = obj + offset
8753 __ Add(root_reg, obj, offset);
8754 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008755 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008756 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008757 } else {
8758 // Plain GC root load with no read barrier.
8759 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8760 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8761 // Note that GC roots are not affected by heap poisoning, thus we
8762 // do not have to unpoison `root_reg` here.
8763 }
Andra Danciua0130e82020-07-23 12:34:56 +00008764 MaybeGenerateMarkingRegisterCheck(/* code= */ 20);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008765}
8766
Vladimir Markod887ed82018-08-14 13:52:12 +00008767void CodeGeneratorARMVIXL::GenerateUnsafeCasOldValueAddWithBakerReadBarrier(
8768 vixl::aarch32::Register old_value,
8769 vixl::aarch32::Register adjusted_old_value,
8770 vixl::aarch32::Register expected) {
8771 DCHECK(kEmitCompilerReadBarrier);
8772 DCHECK(kUseBakerReadBarrier);
8773
8774 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with an ADD instead of LDR.
8775 uint32_t custom_data = EncodeBakerReadBarrierUnsafeCasData(old_value.GetCode());
8776
8777 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8778 size_t wide_instructions = /* ADR+CMP+ADD+BNE */ 4u - narrow_instructions;
8779 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8780 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8781 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8782 vixl32::Label return_address;
8783 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8784 __ cmp(mr, Operand(0));
8785 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8786 __ add(EncodingSize(Wide), old_value, adjusted_old_value, Operand(expected)); // Preserves flags.
8787 EmitBakerReadBarrierBne(custom_data);
8788 __ bind(&return_address);
8789 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8790 BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ADD_OFFSET);
8791}
8792
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008793void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8794 Location ref,
8795 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008796 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008797 bool needs_null_check) {
8798 DCHECK(kEmitCompilerReadBarrier);
8799 DCHECK(kUseBakerReadBarrier);
8800
Vladimir Marko008e09f32018-08-06 15:42:43 +01008801 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8802 // Marking Register) to decide whether we need to enter the slow
8803 // path to mark the reference. Then, in the slow path, check the
8804 // gray bit in the lock word of the reference's holder (`obj`) to
8805 // decide whether to mark `ref` or not.
8806 //
8807 // We use shared thunks for the slow path; shared within the method
8808 // for JIT, across methods for AOT. That thunk checks the holder
8809 // and jumps to the entrypoint if needed. If the holder is not gray,
8810 // it creates a fake dependency and returns to the LDR instruction.
8811 //
8812 // lr = &gray_return_address;
8813 // if (mr) { // Thread::Current()->GetIsGcMarking()
8814 // goto field_thunk<holder_reg, base_reg>(lr)
8815 // }
8816 // not_gray_return_address:
8817 // // Original reference load. If the offset is too large to fit
8818 // // into LDR, we use an adjusted base register here.
8819 // HeapReference<mirror::Object> reference = *(obj+offset);
8820 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008821
Vladimir Marko248141f2018-08-10 10:40:07 +01008822 DCHECK(src.GetAddrMode() == vixl32::Offset);
8823 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008824 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008825 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8826
Vladimir Marko008e09f32018-08-06 15:42:43 +01008827 UseScratchRegisterScope temps(GetVIXLAssembler());
8828 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008829 uint32_t custom_data =
8830 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008831
Vladimir Marko008e09f32018-08-06 15:42:43 +01008832 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008833 size_t narrow_instructions =
8834 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8835 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
8836 size_t wide_instructions =
8837 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8838 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8839 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8840 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008841 vixl32::Label return_address;
8842 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8843 __ cmp(mr, Operand(0));
8844 EmitBakerReadBarrierBne(custom_data);
8845 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01008846 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008847 if (needs_null_check) {
8848 MaybeRecordImplicitNullCheck(instruction);
8849 }
8850 // Note: We need a specific width for the unpoisoning NEG.
8851 if (kPoisonHeapReferences) {
8852 if (narrow) {
8853 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8854 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8855 } else {
8856 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8857 }
8858 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008859 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008860 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8861 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8862 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
8863 }
Andra Danciua0130e82020-07-23 12:34:56 +00008864 MaybeGenerateMarkingRegisterCheck(/* code= */ 21, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008865}
8866
Vladimir Marko248141f2018-08-10 10:40:07 +01008867void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8868 Location ref,
8869 vixl32::Register obj,
8870 uint32_t offset,
8871 Location temp,
8872 bool needs_null_check) {
8873 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8874 vixl32::Register base = obj;
8875 if (offset >= kReferenceLoadMinFarOffset) {
8876 base = RegisterFrom(temp);
8877 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8878 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8879 offset &= (kReferenceLoadMinFarOffset - 1u);
8880 }
8881 GenerateFieldLoadWithBakerReadBarrier(
8882 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
8883}
8884
Vladimir Marko008e09f32018-08-06 15:42:43 +01008885void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008886 vixl32::Register obj,
8887 uint32_t data_offset,
8888 Location index,
8889 Location temp,
8890 bool needs_null_check) {
8891 DCHECK(kEmitCompilerReadBarrier);
8892 DCHECK(kUseBakerReadBarrier);
8893
8894 static_assert(
8895 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8896 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008897 ScaleFactor scale_factor = TIMES_4;
8898
Vladimir Marko008e09f32018-08-06 15:42:43 +01008899 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8900 // Marking Register) to decide whether we need to enter the slow
8901 // path to mark the reference. Then, in the slow path, check the
8902 // gray bit in the lock word of the reference's holder (`obj`) to
8903 // decide whether to mark `ref` or not.
8904 //
8905 // We use shared thunks for the slow path; shared within the method
8906 // for JIT, across methods for AOT. That thunk checks the holder
8907 // and jumps to the entrypoint if needed. If the holder is not gray,
8908 // it creates a fake dependency and returns to the LDR instruction.
8909 //
8910 // lr = &gray_return_address;
8911 // if (mr) { // Thread::Current()->GetIsGcMarking()
8912 // goto array_thunk<base_reg>(lr)
8913 // }
8914 // not_gray_return_address:
8915 // // Original reference load. If the offset is too large to fit
8916 // // into LDR, we use an adjusted base register here.
8917 // HeapReference<mirror::Object> reference = data[index];
8918 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008919
Vladimir Marko008e09f32018-08-06 15:42:43 +01008920 DCHECK(index.IsValid());
8921 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8922 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8923 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008924
Vladimir Marko008e09f32018-08-06 15:42:43 +01008925 UseScratchRegisterScope temps(GetVIXLAssembler());
8926 temps.Exclude(ip);
8927 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008928
Vladimir Marko008e09f32018-08-06 15:42:43 +01008929 __ Add(data_reg, obj, Operand(data_offset));
8930 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008931 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8932 size_t wide_instructions =
8933 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8934 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8935 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8936 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008937 vixl32::Label return_address;
8938 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8939 __ cmp(mr, Operand(0));
8940 EmitBakerReadBarrierBne(custom_data);
8941 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8942 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8943 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8944 // Note: We need a Wide NEG for the unpoisoning.
8945 if (kPoisonHeapReferences) {
8946 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008947 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008948 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008949 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8950 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008951 }
Andra Danciua0130e82020-07-23 12:34:56 +00008952 MaybeGenerateMarkingRegisterCheck(/* code= */ 22, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008953}
8954
Roland Levillain5daa4952017-07-03 17:23:56 +01008955void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8956 // The following condition is a compile-time one, so it does not have a run-time cost.
8957 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8958 // The following condition is a run-time one; it is executed after the
8959 // previous compile-time test, to avoid penalizing non-debug builds.
8960 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8961 UseScratchRegisterScope temps(GetVIXLAssembler());
8962 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8963 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8964 kMarkingRegisterCheckBreakCodeBaseCode + code);
8965 }
8966 }
8967}
8968
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008969void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8970 Location out,
8971 Location ref,
8972 Location obj,
8973 uint32_t offset,
8974 Location index) {
8975 DCHECK(kEmitCompilerReadBarrier);
8976
8977 // Insert a slow path based read barrier *after* the reference load.
8978 //
8979 // If heap poisoning is enabled, the unpoisoning of the loaded
8980 // reference will be carried out by the runtime within the slow
8981 // path.
8982 //
8983 // Note that `ref` currently does not get unpoisoned (when heap
8984 // poisoning is enabled), which is alright as the `ref` argument is
8985 // not used by the artReadBarrierSlow entry point.
8986 //
8987 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008988 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008989 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8990 AddSlowPath(slow_path);
8991
8992 __ B(slow_path->GetEntryLabel());
8993 __ Bind(slow_path->GetExitLabel());
8994}
8995
8996void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008997 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008998 Location ref,
8999 Location obj,
9000 uint32_t offset,
9001 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009002 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009003 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009004 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009005 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009006 // If heap poisoning is enabled, unpoisoning will be taken care of
9007 // by the runtime within the slow path.
9008 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009009 } else if (kPoisonHeapReferences) {
9010 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9011 }
9012}
9013
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009014void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9015 Location out,
9016 Location root) {
9017 DCHECK(kEmitCompilerReadBarrier);
9018
9019 // Insert a slow path based read barrier *after* the GC root load.
9020 //
9021 // Note that GC roots are not affected by heap poisoning, so we do
9022 // not need to do anything special for this here.
9023 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009024 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009025 AddSlowPath(slow_path);
9026
9027 __ B(slow_path->GetEntryLabel());
9028 __ Bind(slow_path->GetExitLabel());
9029}
9030
Artem Serov02d37832016-10-25 15:25:33 +01009031// Check if the desired_dispatch_info is supported. If it is, return it,
9032// otherwise return a fall-back info that should be used instead.
9033HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009034 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Vladimir Marko86c87522020-05-11 16:55:55 +01009035 ArtMethod* method) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009036 if (method->IsIntrinsic() &&
9037 desired_dispatch_info.code_ptr_location ==
Vladimir Marko86c87522020-05-11 16:55:55 +01009038 HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009039 // As a work-around for soft-float native ABI interfering with type checks, we are
9040 // inserting fake calls to Float.floatToRawIntBits() or Double.doubleToRawLongBits()
9041 // when a float or double argument is passed in core registers but we cannot do that
9042 // for actual intrinsic implementations that expect them in FP registers. Therefore
9043 // we do not use `kCallCriticalNative` for intrinsics with FP arguments; if they are
9044 // properly intrinsified, the dispatch type does not matter anyway.
Vladimir Marko86c87522020-05-11 16:55:55 +01009045 ScopedObjectAccess soa(Thread::Current());
9046 uint32_t shorty_len;
9047 const char* shorty = method->GetShorty(&shorty_len);
Vladimir Marko86c87522020-05-11 16:55:55 +01009048 for (uint32_t i = 1; i != shorty_len; ++i) {
Vladimir Marko86c87522020-05-11 16:55:55 +01009049 if (shorty[i] == 'D' || shorty[i] == 'F') {
9050 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
9051 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
9052 return dispatch_info;
9053 }
Vladimir Marko86c87522020-05-11 16:55:55 +01009054 }
9055 }
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009056 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009057}
9058
Vladimir Markod254f5c2017-06-02 15:18:36 +00009059void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009060 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009061 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009062 switch (invoke->GetMethodLoadKind()) {
9063 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
9064 uint32_t offset =
9065 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9066 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00009067 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9068 break;
9069 }
9070 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01009071 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00009072 break;
Vladimir Marko65979462017-05-19 17:25:12 +01009073 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01009074 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009075 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01009076 vixl32::Register temp_reg = RegisterFrom(temp);
9077 EmitMovwMovtPlaceholder(labels, temp_reg);
9078 break;
9079 }
Vladimir Markob066d432018-01-03 13:14:37 +00009080 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009081 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00009082 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
9083 vixl32::Register temp_reg = RegisterFrom(temp);
9084 EmitMovwMovtPlaceholder(labels, temp_reg);
9085 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
9086 break;
9087 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009088 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009089 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009090 vixl32::Register temp_reg = RegisterFrom(temp);
9091 EmitMovwMovtPlaceholder(labels, temp_reg);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01009092 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009093 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009094 break;
9095 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009096 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
9097 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9098 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009099 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
9100 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9101 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009102 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009103 }
9104
Vladimir Marko86c87522020-05-11 16:55:55 +01009105 auto call_code_pointer_member = [&](MemberOffset offset) {
9106 // LR = callee_method->member;
9107 GetAssembler()->LoadFromOffset(kLoadWord, lr, RegisterFrom(callee_method), offset.Int32Value());
9108 {
9109 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9110 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9111 ExactAssemblyScope aas(GetVIXLAssembler(),
9112 vixl32::k16BitT32InstructionSizeInBytes,
9113 CodeBufferCheckScope::kExactSize);
9114 // LR()
9115 __ blx(lr);
9116 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9117 }
9118 };
Artem Serovd4cc5b22016-11-04 11:19:09 +00009119 switch (invoke->GetCodePtrLocation()) {
9120 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009121 {
9122 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9123 ExactAssemblyScope aas(GetVIXLAssembler(),
9124 vixl32::k32BitT32InstructionSizeInBytes,
9125 CodeBufferCheckScope::kMaximumSize);
9126 __ bl(GetFrameEntryLabel());
9127 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9128 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009129 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01009130 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01009131 size_t out_frame_size =
9132 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARMVIXL,
9133 kAapcsStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01009134 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01009135 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArmPointerSize));
9136 // Move the result when needed due to native and managed ABI mismatch.
9137 switch (invoke->GetType()) {
9138 case DataType::Type::kFloat32:
9139 __ Vmov(s0, r0);
9140 break;
9141 case DataType::Type::kFloat64:
9142 __ Vmov(d0, r0, r1);
9143 break;
9144 case DataType::Type::kBool:
9145 case DataType::Type::kInt8:
9146 case DataType::Type::kUint16:
9147 case DataType::Type::kInt16:
9148 case DataType::Type::kInt32:
9149 case DataType::Type::kInt64:
9150 case DataType::Type::kVoid:
9151 break;
9152 default:
9153 DCHECK(false) << invoke->GetType();
9154 break;
9155 }
9156 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01009157 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01009158 }
9159 break;
9160 }
9161 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9162 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009163 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009164 }
9165
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009166 DCHECK(!IsLeafMethod());
9167}
9168
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009169void CodeGeneratorARMVIXL::GenerateVirtualCall(
9170 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009171 vixl32::Register temp = RegisterFrom(temp_location);
9172 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9173 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9174
9175 // Use the calling convention instead of the location of the receiver, as
9176 // intrinsics may have put the receiver in a different register. In the intrinsics
9177 // slow path, the arguments have been moved to the right place, so here we are
9178 // guaranteed that the receiver is the first register of the calling convention.
9179 InvokeDexCallingConventionARMVIXL calling_convention;
9180 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9181 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009182 {
9183 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009184 ExactAssemblyScope aas(GetVIXLAssembler(),
9185 vixl32::kMaxInstructionSizeInBytes,
9186 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009187 // /* HeapReference<Class> */ temp = receiver->klass_
9188 __ ldr(temp, MemOperand(receiver, class_offset));
9189 MaybeRecordImplicitNullCheck(invoke);
9190 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009191 // Instead of simply (possibly) unpoisoning `temp` here, we should
9192 // emit a read barrier for the previous class reference load.
9193 // However this is not required in practice, as this is an
9194 // intermediate/temporary reference and because the current
9195 // concurrent copying collector keeps the from-space memory
9196 // intact/accessible until the end of the marking phase (the
9197 // concurrent copying collector may not in the future).
9198 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9199
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00009200 // If we're compiling baseline, update the inline cache.
9201 MaybeGenerateInlineCacheCheck(invoke, temp);
9202
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009203 // temp = temp->GetMethodAt(method_offset);
9204 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9205 kArmPointerSize).Int32Value();
9206 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9207 // LR = temp->GetEntryPoint();
9208 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009209 {
9210 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9211 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9212 ExactAssemblyScope aas(GetVIXLAssembler(),
9213 vixl32::k16BitT32InstructionSizeInBytes,
9214 CodeBufferCheckScope::kExactSize);
9215 // LR();
9216 __ blx(lr);
9217 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9218 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009219}
9220
Vladimir Marko6fd16062018-06-26 11:02:04 +01009221CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
9222 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009223 return NewPcRelativePatch(/* dex_file= */ nullptr, intrinsic_data, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009224}
9225
Vladimir Markob066d432018-01-03 13:14:37 +00009226CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9227 uint32_t boot_image_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08009228 return NewPcRelativePatch(/* dex_file= */ nullptr,
Vladimir Markob066d432018-01-03 13:14:37 +00009229 boot_image_offset,
Vladimir Marko2d06e022019-07-08 15:45:19 +01009230 &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00009231}
9232
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009233CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009234 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009235 return NewPcRelativePatch(
9236 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009237}
9238
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009239CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9240 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009241 return NewPcRelativePatch(
9242 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009243}
9244
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009245CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009246 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009247 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009248}
9249
Vladimir Marko1998cd02017-01-13 13:02:58 +00009250CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9251 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009252 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009253}
9254
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009255CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009256 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009257 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009258}
9259
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009260CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9261 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009262 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009263}
9264
Artem Serovd4cc5b22016-11-04 11:19:09 +00009265CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009266 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009267 patches->emplace_back(dex_file, offset_or_index);
9268 return &patches->back();
9269}
9270
Vladimir Markof6675082019-05-17 12:05:28 +01009271void CodeGeneratorARMVIXL::EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset) {
9272 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009273 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01009274 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
9275 vixl::aarch32::Label* bl_label = &call_entrypoint_patches_.back().label;
9276 __ bind(bl_label);
9277 vixl32::Label placeholder_label;
9278 __ bl(&placeholder_label); // Placeholder, patched at link-time.
9279 __ bind(&placeholder_label);
9280}
9281
Vladimir Marko966b46f2018-08-03 10:20:19 +00009282void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00009283 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009284 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00009285 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
9286 vixl::aarch32::Label* slow_path_entry = &it->second.label;
9287 __ b(ne, EncodingSize(Wide), slow_path_entry);
9288 } else {
9289 baker_read_barrier_patches_.emplace_back(custom_data);
9290 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
9291 __ bind(patch_label);
9292 vixl32::Label placeholder_label;
9293 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
9294 __ bind(&placeholder_label);
9295 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009296}
9297
Artem Serovc5fcb442016-12-02 19:19:58 +00009298VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009299 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009300}
9301
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009302VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9303 const DexFile& dex_file,
9304 dex::StringIndex string_index,
9305 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009306 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009307 return jit_string_patches_.GetOrCreate(
9308 StringReference(&dex_file, string_index),
9309 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009310 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009311 });
9312}
9313
9314VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9315 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009316 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009317 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009318 return jit_class_patches_.GetOrCreate(
9319 TypeReference(&dex_file, type_index),
9320 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009321 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009322 });
9323}
9324
Vladimir Marko6fd16062018-06-26 11:02:04 +01009325void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
9326 uint32_t boot_image_reference) {
9327 if (GetCompilerOptions().IsBootImage()) {
9328 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9329 NewBootImageIntrinsicPatch(boot_image_reference);
9330 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01009331 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009332 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9333 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009334 EmitMovwMovtPlaceholder(labels, reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08009335 __ Ldr(reg, MemOperand(reg, /* offset= */ 0));
Vladimir Markoeebb8212018-06-05 14:57:24 +01009336 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01009337 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01009338 gc::Heap* heap = Runtime::Current()->GetHeap();
9339 DCHECK(!heap->GetBootImageSpaces().empty());
9340 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01009341 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009342 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
9343 }
9344}
9345
Vladimir Marko6fd16062018-06-26 11:02:04 +01009346void CodeGeneratorARMVIXL::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
9347 uint32_t boot_image_offset) {
9348 DCHECK(invoke->IsStatic());
9349 InvokeRuntimeCallingConventionARMVIXL calling_convention;
9350 vixl32::Register argument = calling_convention.GetRegisterAt(0);
9351 if (GetCompilerOptions().IsBootImage()) {
9352 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
9353 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009354 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01009355 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
9356 PcRelativePatchInfo* labels = NewBootImageTypePatch(*target_method.dex_file, type_idx);
9357 EmitMovwMovtPlaceholder(labels, argument);
9358 } else {
9359 LoadBootImageAddress(argument, boot_image_offset);
9360 }
9361 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
9362 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
9363}
9364
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009365template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009366inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9367 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009368 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009369 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009370 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009371 size_t offset_or_index = info.offset_or_index;
9372 DCHECK(info.add_pc_label.IsBound());
9373 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9374 // Add MOVW patch.
9375 DCHECK(info.movw_label.IsBound());
9376 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009377 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009378 // Add MOVT patch.
9379 DCHECK(info.movt_label.IsBound());
9380 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009381 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009382 }
9383}
9384
Vladimir Marko6fd16062018-06-26 11:02:04 +01009385template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
9386linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
9387 const DexFile* target_dex_file,
9388 uint32_t pc_insn_offset,
9389 uint32_t boot_image_offset) {
9390 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
9391 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00009392}
9393
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009394void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009395 DCHECK(linker_patches->empty());
9396 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009397 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009398 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009399 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009400 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009401 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009402 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01009403 /* MOVW+MOVT for each entry */ 2u * boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01009404 call_entrypoint_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009405 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009406 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01009407 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009408 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009409 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009410 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009411 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009412 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009413 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009414 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009415 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009416 DCHECK(boot_image_type_patches_.empty());
9417 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01009418 }
9419 if (GetCompilerOptions().IsBootImage()) {
9420 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
9421 boot_image_other_patches_, linker_patches);
9422 } else {
9423 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
9424 boot_image_other_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009425 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009426 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9427 method_bss_entry_patches_, linker_patches);
9428 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9429 type_bss_entry_patches_, linker_patches);
9430 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9431 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01009432 for (const PatchInfo<vixl32::Label>& info : call_entrypoint_patches_) {
9433 DCHECK(info.target_dex_file == nullptr);
9434 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
9435 info.label.GetLocation(), info.offset_or_index));
9436 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009437 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009438 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9439 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009440 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009441 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009442}
9443
Vladimir Markoca1e0382018-04-11 09:58:41 +00009444bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01009445 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
9446 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00009447 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9448}
9449
9450void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9451 /*out*/ ArenaVector<uint8_t>* code,
9452 /*out*/ std::string* debug_name) {
9453 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9454 switch (patch.GetType()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009455 case linker::LinkerPatch::Type::kCallRelative: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009456 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9457 // to the generic JNI and interpreter trampolines.
Vladimir Markof6675082019-05-17 12:05:28 +01009458 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
9459 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, vixl32::r0, offset.Int32Value());
Vladimir Markoca1e0382018-04-11 09:58:41 +00009460 assembler.GetVIXLAssembler()->Bkpt(0);
9461 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9462 *debug_name = "MethodCallThunk";
9463 }
9464 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009465 }
9466 case linker::LinkerPatch::Type::kCallEntrypoint: {
9467 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, tr, patch.EntrypointOffset());
9468 assembler.GetVIXLAssembler()->Bkpt(0);
9469 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9470 *debug_name = "EntrypointCallThunk_" + std::to_string(patch.EntrypointOffset());
9471 }
9472 break;
9473 }
9474 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009475 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9476 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9477 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009478 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009479 default:
9480 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9481 UNREACHABLE();
9482 }
9483
9484 // Ensure we emit the literal pool if any.
9485 assembler.FinalizeCode();
9486 code->resize(assembler.CodeSize());
9487 MemoryRegion code_region(code->data(), code->size());
9488 assembler.FinalizeInstructions(code_region);
9489}
9490
Artem Serovc5fcb442016-12-02 19:19:58 +00009491VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9492 uint32_t value,
9493 Uint32ToLiteralMap* map) {
9494 return map->GetOrCreate(
9495 value,
9496 [this, value]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009497 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ value);
Artem Serovc5fcb442016-12-02 19:19:58 +00009498 });
9499}
9500
Artem Serov2bbc9532016-10-21 11:51:50 +01009501void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9502 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009503 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009504 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9505 Location::RequiresRegister());
9506 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9507 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9509}
9510
9511void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9512 vixl32::Register res = OutputRegister(instr);
9513 vixl32::Register accumulator =
9514 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9515 vixl32::Register mul_left =
9516 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9517 vixl32::Register mul_right =
9518 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9519
9520 if (instr->GetOpKind() == HInstruction::kAdd) {
9521 __ Mla(res, mul_left, mul_right, accumulator);
9522 } else {
9523 __ Mls(res, mul_left, mul_right, accumulator);
9524 }
9525}
9526
Artem Serov551b28f2016-10-18 19:11:30 +01009527void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9528 // Nothing to do, this should be removed during prepare for register allocator.
9529 LOG(FATAL) << "Unreachable";
9530}
9531
9532void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9533 // Nothing to do, this should be removed during prepare for register allocator.
9534 LOG(FATAL) << "Unreachable";
9535}
9536
9537// Simple implementation of packed switch - generate cascaded compare/jumps.
9538void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9539 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009540 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009541 locations->SetInAt(0, Location::RequiresRegister());
9542 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9543 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9544 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9545 if (switch_instr->GetStartValue() != 0) {
9546 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9547 }
9548 }
9549}
9550
9551// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9552void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9553 int32_t lower_bound = switch_instr->GetStartValue();
9554 uint32_t num_entries = switch_instr->GetNumEntries();
9555 LocationSummary* locations = switch_instr->GetLocations();
9556 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9557 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9558
9559 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9560 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9561 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009562 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009563 vixl32::Register temp_reg = temps.Acquire();
9564 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9565 // the immediate, because IP is used as the destination register. For the other
9566 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9567 // and they can be encoded in the instruction without making use of IP register.
9568 __ Adds(temp_reg, value_reg, -lower_bound);
9569
9570 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9571 // Jump to successors[0] if value == lower_bound.
9572 __ B(eq, codegen_->GetLabelOf(successors[0]));
9573 int32_t last_index = 0;
9574 for (; num_entries - last_index > 2; last_index += 2) {
9575 __ Adds(temp_reg, temp_reg, -2);
9576 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9577 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9578 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9579 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9580 }
9581 if (num_entries - last_index == 2) {
9582 // The last missing case_value.
9583 __ Cmp(temp_reg, 1);
9584 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9585 }
9586
9587 // And the default for any other value.
9588 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9589 __ B(codegen_->GetLabelOf(default_block));
9590 }
9591 } else {
9592 // Create a table lookup.
9593 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9594
9595 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9596
9597 // Remove the bias.
9598 vixl32::Register key_reg;
9599 if (lower_bound != 0) {
9600 key_reg = RegisterFrom(locations->GetTemp(1));
9601 __ Sub(key_reg, value_reg, lower_bound);
9602 } else {
9603 key_reg = value_reg;
9604 }
9605
9606 // Check whether the value is in the table, jump to default block if not.
9607 __ Cmp(key_reg, num_entries - 1);
9608 __ B(hi, codegen_->GetLabelOf(default_block));
9609
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009610 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009611 vixl32::Register jump_offset = temps.Acquire();
9612
9613 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009614 {
9615 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9616 ExactAssemblyScope aas(GetVIXLAssembler(),
9617 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9618 CodeBufferCheckScope::kMaximumSize);
9619 __ adr(table_base, jump_table->GetTableStartLabel());
9620 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009621
Scott Wakeling86e9d262017-01-18 15:59:24 +00009622 // Jump to target block by branching to table_base(pc related) + offset.
9623 vixl32::Register target_address = table_base;
9624 __ add(target_address, table_base, jump_offset);
9625 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009626
Scott Wakeling86e9d262017-01-18 15:59:24 +00009627 jump_table->EmitTable(codegen_);
9628 }
Artem Serov551b28f2016-10-18 19:11:30 +01009629 }
9630}
9631
Artem Serov02d37832016-10-25 15:25:33 +01009632// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009633void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009634 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009635 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009636 return;
9637 }
9638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009639 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009640
Artem Serovd4cc5b22016-11-04 11:19:09 +00009641 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009642 if (return_loc.Equals(trg)) {
9643 return;
9644 }
9645
9646 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9647 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009648 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009649 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009650 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009651 TODO_VIXL32(FATAL);
9652 } else {
9653 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009654 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009655 parallel_move.AddMove(return_loc, trg, type, nullptr);
9656 GetMoveResolver()->EmitNativeCode(&parallel_move);
9657 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009658}
Scott Wakelingfe885462016-09-22 10:24:38 +01009659
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009660void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9661 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009662 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009663 locations->SetInAt(0, Location::RequiresRegister());
9664 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009665}
9666
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009667void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9668 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9669 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9670 instruction->GetIndex(), kArmPointerSize).SizeValue();
9671 GetAssembler()->LoadFromOffset(kLoadWord,
9672 OutputRegister(instruction),
9673 InputRegisterAt(instruction, 0),
9674 method_offset);
9675 } else {
9676 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9677 instruction->GetIndex(), kArmPointerSize));
9678 GetAssembler()->LoadFromOffset(kLoadWord,
9679 OutputRegister(instruction),
9680 InputRegisterAt(instruction, 0),
9681 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9682 GetAssembler()->LoadFromOffset(kLoadWord,
9683 OutputRegister(instruction),
9684 OutputRegister(instruction),
9685 method_offset);
9686 }
Artem Serov551b28f2016-10-18 19:11:30 +01009687}
9688
Artem Serovc5fcb442016-12-02 19:19:58 +00009689static void PatchJitRootUse(uint8_t* code,
9690 const uint8_t* roots_data,
9691 VIXLUInt32Literal* literal,
9692 uint64_t index_in_table) {
9693 DCHECK(literal->IsBound());
9694 uint32_t literal_offset = literal->GetLocation();
9695 uintptr_t address =
9696 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9697 uint8_t* data = code + literal_offset;
9698 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9699}
9700
9701void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9702 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009703 const StringReference& string_reference = entry.first;
9704 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009705 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009706 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009707 }
9708 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009709 const TypeReference& type_reference = entry.first;
9710 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009711 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009712 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009713 }
9714}
9715
Artem Serovd4cc5b22016-11-04 11:19:09 +00009716void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9717 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9718 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009719 ExactAssemblyScope aas(GetVIXLAssembler(),
9720 3 * vixl32::kMaxInstructionSizeInBytes,
9721 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009722 // TODO(VIXL): Think about using mov instead of movw.
9723 __ bind(&labels->movw_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009724 __ movw(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009725 __ bind(&labels->movt_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009726 __ movt(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009727 __ bind(&labels->add_pc_label);
9728 __ add(out, out, pc);
9729}
9730
Scott Wakelingfe885462016-09-22 10:24:38 +01009731#undef __
9732#undef QUICK_ENTRY_POINT
9733#undef TODO_VIXL32
9734
Vladimir Markoca1e0382018-04-11 09:58:41 +00009735#define __ assembler.GetVIXLAssembler()->
9736
9737static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9738 vixl32::Register base_reg,
9739 vixl32::MemOperand& lock_word,
9740 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009741 int32_t raw_ldr_offset,
9742 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009743 // Load the lock word containing the rb_state.
9744 __ Ldr(ip, lock_word);
9745 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009746 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009747 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9748 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
Andreas Gampe3db70682018-12-26 15:12:03 -08009749 __ B(ne, slow_path, /* is_far_target= */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009750 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9751 if (throw_npe != nullptr) {
9752 __ Bind(throw_npe);
9753 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009754 __ Add(lr, lr, raw_ldr_offset);
9755 // Introduce a dependency on the lock_word including rb_state,
9756 // to prevent load-load reordering, and without using
9757 // a memory barrier (which would be more expensive).
9758 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9759 __ Bx(lr); // And return back to the function.
9760 // Note: The fake dependency is unnecessary for the slow path.
9761}
9762
9763// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009764static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009765 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009766 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9767 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009768 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9769 DCHECK_EQ(ip.GetCode(), 12u);
9770 const int32_t entry_point_offset =
9771 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9772 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +01009773 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009774}
9775
9776void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9777 uint32_t encoded_data,
9778 /*out*/ std::string* debug_name) {
9779 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9780 switch (kind) {
9781 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009782 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9783 CheckValidReg(base_reg.GetCode());
9784 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9785 CheckValidReg(holder_reg.GetCode());
9786 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9787 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9788 temps.Exclude(ip);
Roland Levillain988c3912019-09-25 19:33:35 +01009789 // In the case of a field load, if `base_reg` differs from
9790 // `holder_reg`, the offset was too large and we must have emitted (during the construction
9791 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
9792 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
9793 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
9794 // not necessarily do that check before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01009795 vixl32::Label throw_npe_label;
9796 vixl32::Label* throw_npe = nullptr;
9797 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9798 throw_npe = &throw_npe_label;
Andreas Gampe3db70682018-12-26 15:12:03 -08009799 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009800 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009801 // Check if the holder is gray and, if not, add fake dependency to the base register
9802 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9803 // to load the reference and call the entrypoint that performs further checks on the
9804 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009805 vixl32::Label slow_path;
9806 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9807 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9808 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9809 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009810 EmitGrayCheckAndFastPath(
9811 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009812 __ Bind(&slow_path);
9813 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9814 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +01009815 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009816 if (width == BakerReadBarrierWidth::kWide) {
9817 MemOperand ldr_half_address(lr, ldr_offset + 2);
9818 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9819 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9820 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9821 } else {
9822 MemOperand ldr_address(lr, ldr_offset);
9823 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9824 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9825 ep_reg, // for narrow LDR.
9826 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9827 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9828 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9829 }
9830 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9831 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009832 break;
9833 }
9834 case BakerReadBarrierKind::kArray: {
9835 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9836 CheckValidReg(base_reg.GetCode());
9837 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9838 BakerReadBarrierSecondRegField::Decode(encoded_data));
9839 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9840 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9841 temps.Exclude(ip);
9842 vixl32::Label slow_path;
9843 int32_t data_offset =
9844 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
9845 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
9846 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
9847 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
9848 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
9849 __ Bind(&slow_path);
9850 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9851 raw_ldr_offset;
9852 MemOperand ldr_address(lr, ldr_offset + 2);
9853 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
9854 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +01009855 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009856 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
9857 // a switch case target based on the index register.
9858 __ Mov(ip, base_reg); // Move the base register to ip0.
9859 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
9860 break;
9861 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009862 case BakerReadBarrierKind::kGcRoot:
9863 case BakerReadBarrierKind::kUnsafeCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009864 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
9865 // and it does not have a forwarding address), call the correct introspection entrypoint;
9866 // otherwise return the reference (or the extracted forwarding address).
9867 // There is no gray bit check for GC roots.
9868 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9869 CheckValidReg(root_reg.GetCode());
9870 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9871 BakerReadBarrierSecondRegField::Decode(encoded_data));
9872 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9873 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9874 temps.Exclude(ip);
9875 vixl32::Label return_label, not_marked, forwarding_address;
Andreas Gampe3db70682018-12-26 15:12:03 -08009876 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009877 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
9878 __ Ldr(ip, lock_word);
9879 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
9880 __ B(eq, &not_marked);
9881 __ Bind(&return_label);
9882 __ Bx(lr);
9883 __ Bind(&not_marked);
9884 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
9885 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
9886 " the highest bits and the 'forwarding address' state to have all bits set");
9887 __ Cmp(ip, Operand(0xc0000000));
9888 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +01009889 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009890 // Adjust the art_quick_read_barrier_mark_introspection address in kBakerCcEntrypointRegister
Vladimir Markod887ed82018-08-14 13:52:12 +00009891 // to one of art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},unsafe_cas}.
9892 DCHECK(kind != BakerReadBarrierKind::kUnsafeCas || width == BakerReadBarrierWidth::kWide);
9893 int32_t entrypoint_offset =
9894 (kind == BakerReadBarrierKind::kGcRoot)
9895 ? (width == BakerReadBarrierWidth::kWide)
9896 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
9897 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
9898 : BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009899 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
9900 __ Mov(ip, root_reg);
9901 __ Bx(ep_reg);
9902 __ Bind(&forwarding_address);
9903 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
9904 __ Bx(lr);
9905 break;
9906 }
9907 default:
9908 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
9909 UNREACHABLE();
9910 }
9911
Vladimir Marko966b46f2018-08-03 10:20:19 +00009912 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01009913 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01009914 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00009915 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009916 std::ostringstream oss;
9917 oss << "BakerReadBarrierThunk";
9918 switch (kind) {
9919 case BakerReadBarrierKind::kField:
9920 oss << "Field";
9921 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9922 oss << "Wide";
9923 }
9924 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
9925 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
9926 break;
9927 case BakerReadBarrierKind::kArray:
9928 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9929 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9930 BakerReadBarrierSecondRegField::Decode(encoded_data));
9931 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9932 break;
9933 case BakerReadBarrierKind::kGcRoot:
9934 oss << "GcRoot";
9935 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9936 oss << "Wide";
9937 }
9938 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9939 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9940 BakerReadBarrierSecondRegField::Decode(encoded_data));
9941 break;
Vladimir Markod887ed82018-08-14 13:52:12 +00009942 case BakerReadBarrierKind::kUnsafeCas:
9943 oss << "UnsafeCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9944 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9945 BakerReadBarrierSecondRegField::Decode(encoded_data));
9946 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9947 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009948 }
9949 *debug_name = oss.str();
9950 }
9951}
9952
9953#undef __
9954
Scott Wakelingfe885462016-09-22 10:24:38 +01009955} // namespace arm
9956} // namespace art