blob: 9aefdabdb41f63a0d0dec68b4f263e03a4e6e0c3 [file] [log] [blame]
Scott Wakelingfe885462016-09-22 10:24:38 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm_vixl.h"
18
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010020#include "arch/arm/instruction_set_features_arm.h"
Vladimir Marko86c87522020-05-11 16:55:55 +010021#include "arch/arm/jni_frame_arm.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000022#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/bit_utils.h"
24#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010025#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010026#include "code_generator_utils.h"
27#include "common_arm.h"
28#include "compiled_method.h"
29#include "entrypoints/quick/quick_entrypoints.h"
30#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010031#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070032#include "heap_poisoning.h"
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +010033#include "interpreter/mterp/nterp.h"
Vladimir Marko6fd16062018-06-26 11:02:04 +010034#include "intrinsics.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010035#include "intrinsics_arm_vixl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010036#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010037#include "mirror/array-inl.h"
38#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000039#include "scoped_thread_state_change-inl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010040#include "thread.h"
41#include "utils/arm/assembler_arm_vixl.h"
42#include "utils/arm/managed_register_arm.h"
43#include "utils/assembler.h"
44#include "utils/stack_checks.h"
45
Vladimir Marko0a516052019-10-14 13:00:44 +000046namespace art {
Scott Wakelingfe885462016-09-22 10:24:38 +010047namespace arm {
48
49namespace vixl32 = vixl::aarch32;
50using namespace vixl32; // NOLINT(build/namespaces)
51
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010052using helpers::DRegisterFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010053using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080054using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010055using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010056using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010057using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010058using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000059using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010060using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000061using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000062using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010063using helpers::LocationFrom;
64using helpers::LowRegisterFrom;
65using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080066using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010067using helpers::OutputRegister;
68using helpers::OutputSRegister;
69using helpers::OutputVRegister;
70using helpers::RegisterFrom;
71using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000072using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010073
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +010074using vixl::EmissionCheckScope;
Artem Serov0fb37192016-12-06 18:13:40 +000075using vixl::ExactAssemblyScope;
76using vixl::CodeBufferCheckScope;
77
Scott Wakelingfe885462016-09-22 10:24:38 +010078using RegisterList = vixl32::RegisterList;
79
80static bool ExpectedPairLayout(Location location) {
81 // We expected this for both core and fpu register pairs.
82 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
83}
Artem Serovd4cc5b22016-11-04 11:19:09 +000084// Use a local definition to prevent copying mistakes.
85static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
86static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010087static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010088
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010089// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
90// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010091// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010092// the offset explicitly.
93constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
94
Roland Levillain5daa4952017-07-03 17:23:56 +010095// Using a base helps identify when we hit Marking Register check breakpoints.
96constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
97
Scott Wakelingfe885462016-09-22 10:24:38 +010098#ifdef __
99#error "ARM Codegen VIXL macro-assembler macro already defined."
100#endif
101
Scott Wakelingfe885462016-09-22 10:24:38 +0100102// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
103#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
104#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
105
106// Marker that code is yet to be, and must, be implemented.
107#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
108
Vladimir Marko88abba22017-05-03 17:09:25 +0100109static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
110 return rt.IsLow() && rn.IsLow() && offset < 32u;
111}
112
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100113class EmitAdrCode {
114 public:
115 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
116 : assembler_(assembler), rd_(rd), label_(label) {
Vladimir Markod887ed82018-08-14 13:52:12 +0000117 DCHECK(!assembler->AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100118 adr_location_ = assembler->GetCursorOffset();
119 assembler->adr(EncodingSize(Wide), rd, label);
120 }
121
122 ~EmitAdrCode() {
123 DCHECK(label_->IsBound());
124 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
125 // TODO: Maybe extend VIXL to allow ADR for return address?
126 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
127 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
128 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
129 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
130 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
131 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
132 // Add the Thumb mode bit.
133 raw_adr[2] |= 0x01u;
134 }
135
136 private:
137 ArmVIXLMacroAssembler* const assembler_;
138 vixl32::Register rd_;
139 vixl32::Label* const label_;
140 int32_t adr_location_;
141};
142
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100143static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
144 InvokeRuntimeCallingConventionARMVIXL calling_convention;
145 RegisterSet caller_saves = RegisterSet::Empty();
146 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
147 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
148 // that the the kPrimNot result register is the same as the first argument register.
149 return caller_saves;
150}
151
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100152// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
153// for each live D registers they treat two corresponding S registers as live ones.
154//
155// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
156// from a list of contiguous S registers a list of contiguous D registers (processing first/last
157// S registers corner cases) and save/restore this new list treating them as D registers.
158// - decreasing code size
159// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
160// restored and then used in regular non SlowPath code as D register.
161//
162// For the following example (v means the S register is live):
163// D names: | D0 | D1 | D2 | D4 | ...
164// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
165// Live? | | v | v | v | v | v | v | | ...
166//
167// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
168// as D registers.
169//
170// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
171// for lists of floating-point registers.
172static size_t SaveContiguousSRegisterList(size_t first,
173 size_t last,
174 CodeGenerator* codegen,
175 size_t stack_offset) {
176 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
177 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
178 DCHECK_LE(first, last);
179 if ((first == last) && (first == 0)) {
180 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
181 return stack_offset + kSRegSizeInBytes;
182 }
183 if (first % 2 == 1) {
184 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
185 stack_offset += kSRegSizeInBytes;
186 }
187
188 bool save_last = false;
189 if (last % 2 == 0) {
190 save_last = true;
191 --last;
192 }
193
194 if (first < last) {
195 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
196 DCHECK_EQ((last - first + 1) % 2, 0u);
197 size_t number_of_d_regs = (last - first + 1) / 2;
198
199 if (number_of_d_regs == 1) {
200 __ Vstr(d_reg, MemOperand(sp, stack_offset));
201 } else if (number_of_d_regs > 1) {
202 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
203 vixl32::Register base = sp;
204 if (stack_offset != 0) {
205 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000206 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100207 }
208 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
209 }
210 stack_offset += number_of_d_regs * kDRegSizeInBytes;
211 }
212
213 if (save_last) {
214 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
215 stack_offset += kSRegSizeInBytes;
216 }
217
218 return stack_offset;
219}
220
221static size_t RestoreContiguousSRegisterList(size_t first,
222 size_t last,
223 CodeGenerator* codegen,
224 size_t stack_offset) {
225 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
226 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
227 DCHECK_LE(first, last);
228 if ((first == last) && (first == 0)) {
229 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
230 return stack_offset + kSRegSizeInBytes;
231 }
232 if (first % 2 == 1) {
233 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
234 stack_offset += kSRegSizeInBytes;
235 }
236
237 bool restore_last = false;
238 if (last % 2 == 0) {
239 restore_last = true;
240 --last;
241 }
242
243 if (first < last) {
244 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
245 DCHECK_EQ((last - first + 1) % 2, 0u);
246 size_t number_of_d_regs = (last - first + 1) / 2;
247 if (number_of_d_regs == 1) {
248 __ Vldr(d_reg, MemOperand(sp, stack_offset));
249 } else if (number_of_d_regs > 1) {
250 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
251 vixl32::Register base = sp;
252 if (stack_offset != 0) {
253 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000254 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100255 }
256 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
257 }
258 stack_offset += number_of_d_regs * kDRegSizeInBytes;
259 }
260
261 if (restore_last) {
262 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
263 stack_offset += kSRegSizeInBytes;
264 }
265
266 return stack_offset;
267}
268
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100269static LoadOperandType GetLoadOperandType(DataType::Type type) {
270 switch (type) {
271 case DataType::Type::kReference:
272 return kLoadWord;
273 case DataType::Type::kBool:
274 case DataType::Type::kUint8:
275 return kLoadUnsignedByte;
276 case DataType::Type::kInt8:
277 return kLoadSignedByte;
278 case DataType::Type::kUint16:
279 return kLoadUnsignedHalfword;
280 case DataType::Type::kInt16:
281 return kLoadSignedHalfword;
282 case DataType::Type::kInt32:
283 return kLoadWord;
284 case DataType::Type::kInt64:
285 return kLoadWordPair;
286 case DataType::Type::kFloat32:
287 return kLoadSWord;
288 case DataType::Type::kFloat64:
289 return kLoadDWord;
290 default:
291 LOG(FATAL) << "Unreachable type " << type;
292 UNREACHABLE();
293 }
294}
295
296static StoreOperandType GetStoreOperandType(DataType::Type type) {
297 switch (type) {
298 case DataType::Type::kReference:
299 return kStoreWord;
300 case DataType::Type::kBool:
301 case DataType::Type::kUint8:
302 case DataType::Type::kInt8:
303 return kStoreByte;
304 case DataType::Type::kUint16:
305 case DataType::Type::kInt16:
306 return kStoreHalfword;
307 case DataType::Type::kInt32:
308 return kStoreWord;
309 case DataType::Type::kInt64:
310 return kStoreWordPair;
311 case DataType::Type::kFloat32:
312 return kStoreSWord;
313 case DataType::Type::kFloat64:
314 return kStoreDWord;
315 default:
316 LOG(FATAL) << "Unreachable type " << type;
317 UNREACHABLE();
318 }
319}
320
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100321void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
322 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
323 size_t orig_offset = stack_offset;
324
Andreas Gampe3db70682018-12-26 15:12:03 -0800325 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100326 for (uint32_t i : LowToHighBits(core_spills)) {
327 // If the register holds an object, update the stack mask.
328 if (locations->RegisterContainsObject(i)) {
329 locations->SetStackBit(stack_offset / kVRegSize);
330 }
331 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
332 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
333 saved_core_stack_offsets_[i] = stack_offset;
334 stack_offset += kArmWordSize;
335 }
336
337 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
338 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
339
Andreas Gampe3db70682018-12-26 15:12:03 -0800340 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100341 orig_offset = stack_offset;
342 for (uint32_t i : LowToHighBits(fp_spills)) {
343 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
344 saved_fpu_stack_offsets_[i] = stack_offset;
345 stack_offset += kArmWordSize;
346 }
347
348 stack_offset = orig_offset;
349 while (fp_spills != 0u) {
350 uint32_t begin = CTZ(fp_spills);
351 uint32_t tmp = fp_spills + (1u << begin);
352 fp_spills &= tmp; // Clear the contiguous range of 1s.
353 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
354 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
355 }
356 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
357}
358
359void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
360 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
361 size_t orig_offset = stack_offset;
362
Andreas Gampe3db70682018-12-26 15:12:03 -0800363 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100364 for (uint32_t i : LowToHighBits(core_spills)) {
365 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
366 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
367 stack_offset += kArmWordSize;
368 }
369
370 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
371 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
372 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
373
Andreas Gampe3db70682018-12-26 15:12:03 -0800374 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100375 while (fp_spills != 0u) {
376 uint32_t begin = CTZ(fp_spills);
377 uint32_t tmp = fp_spills + (1u << begin);
378 fp_spills &= tmp; // Clear the contiguous range of 1s.
379 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
380 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
381 }
382 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
383}
384
385class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
386 public:
387 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100389 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100390 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
391 __ Bind(GetEntryLabel());
392 if (instruction_->CanThrowIntoCatchBlock()) {
393 // Live registers will be restored in the catch block if caught.
394 SaveLiveRegisters(codegen, instruction_->GetLocations());
395 }
396 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
397 instruction_,
398 instruction_->GetDexPc(),
399 this);
400 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
401 }
402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100403 bool IsFatal() const override { return true; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100405 const char* GetDescription() const override { return "NullCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100406
407 private:
408 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
409};
410
Scott Wakelingfe885462016-09-22 10:24:38 +0100411class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
412 public:
413 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
414 : SlowPathCodeARMVIXL(instruction) {}
415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100416 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100417 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100418 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100419 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100420 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
421 }
422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100423 bool IsFatal() const override { return true; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100425 const char* GetDescription() const override { return "DivZeroCheckSlowPathARMVIXL"; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100426
427 private:
428 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
429};
430
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100431class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
432 public:
433 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
434 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100436 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100437 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
438 __ Bind(GetEntryLabel());
439 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
440 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
441 if (successor_ == nullptr) {
442 __ B(GetReturnLabel());
443 } else {
444 __ B(arm_codegen->GetLabelOf(successor_));
445 }
446 }
447
448 vixl32::Label* GetReturnLabel() {
449 DCHECK(successor_ == nullptr);
450 return &return_label_;
451 }
452
453 HBasicBlock* GetSuccessor() const {
454 return successor_;
455 }
456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100457 const char* GetDescription() const override { return "SuspendCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100458
459 private:
460 // If not null, the block to branch to after the suspend check.
461 HBasicBlock* const successor_;
462
463 // If `successor_` is null, the label to branch to after the suspend check.
464 vixl32::Label return_label_;
465
466 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
467};
468
Scott Wakelingc34dba72016-10-03 10:14:44 +0100469class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
470 public:
471 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
472 : SlowPathCodeARMVIXL(instruction) {}
473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100474 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100475 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
476 LocationSummary* locations = instruction_->GetLocations();
477
478 __ Bind(GetEntryLabel());
479 if (instruction_->CanThrowIntoCatchBlock()) {
480 // Live registers will be restored in the catch block if caught.
481 SaveLiveRegisters(codegen, instruction_->GetLocations());
482 }
483 // We're moving two locations to locations that could overlap, so we need a parallel
484 // move resolver.
485 InvokeRuntimeCallingConventionARMVIXL calling_convention;
486 codegen->EmitParallelMoves(
487 locations->InAt(0),
488 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100489 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100490 locations->InAt(1),
491 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100493 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
494 ? kQuickThrowStringBounds
495 : kQuickThrowArrayBounds;
496 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
497 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
498 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
499 }
500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100501 bool IsFatal() const override { return true; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100503 const char* GetDescription() const override { return "BoundsCheckSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100504
505 private:
506 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
507};
508
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100509class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
510 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100511 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at)
512 : SlowPathCodeARMVIXL(at), cls_(cls) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100513 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100514 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100515 }
516
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100517 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000518 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000519 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100520 const uint32_t dex_pc = instruction_->GetDexPc();
521 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
522 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100523
524 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
525 __ Bind(GetEntryLabel());
526 SaveLiveRegisters(codegen, locations);
527
528 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100529 if (must_resolve_type) {
530 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm_codegen->GetGraph()->GetDexFile()));
531 dex::TypeIndex type_index = cls_->GetTypeIndex();
532 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +0100533 if (cls_->NeedsAccessCheck()) {
534 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
535 arm_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
536 } else {
537 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
538 arm_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
539 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100540 // If we also must_do_clinit, the resolved type is now in the correct register.
541 } else {
542 DCHECK(must_do_clinit);
543 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
544 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), source);
545 }
546 if (must_do_clinit) {
547 arm_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
548 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100549 }
550
551 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100552 if (out.IsValid()) {
553 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
554 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
555 }
556 RestoreLiveRegisters(codegen, locations);
557 __ B(GetExitLabel());
558 }
559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100560 const char* GetDescription() const override { return "LoadClassSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100561
562 private:
563 // The class this slow path will load.
564 HLoadClass* const cls_;
565
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100566 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
567};
568
Artem Serovd4cc5b22016-11-04 11:19:09 +0000569class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
570 public:
571 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
572 : SlowPathCodeARMVIXL(instruction) {}
573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100574 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000575 DCHECK(instruction_->IsLoadString());
576 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000577 LocationSummary* locations = instruction_->GetLocations();
578 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000579 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000580
581 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
582 __ Bind(GetEntryLabel());
583 SaveLiveRegisters(codegen, locations);
584
585 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000586 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000587 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
588 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
589
Artem Serovd4cc5b22016-11-04 11:19:09 +0000590 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
591 RestoreLiveRegisters(codegen, locations);
592
593 __ B(GetExitLabel());
594 }
595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100596 const char* GetDescription() const override { return "LoadStringSlowPathARMVIXL"; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000597
598 private:
599 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
600};
601
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100602class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
603 public:
604 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
605 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
606
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100607 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100608 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100609 DCHECK(instruction_->IsCheckCast()
610 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
611
612 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
613 __ Bind(GetEntryLabel());
614
Vladimir Marko87584542017-12-12 17:47:52 +0000615 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100616 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100617 }
618
619 // We're moving two locations to locations that could overlap, so we need a parallel
620 // move resolver.
621 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100622
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800623 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800624 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100625 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800626 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800627 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100628 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100630 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
631 instruction_,
632 instruction_->GetDexPc(),
633 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800634 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100635 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100636 } else {
637 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800638 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
639 instruction_,
640 instruction_->GetDexPc(),
641 this);
642 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100643 }
644
645 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100646 RestoreLiveRegisters(codegen, locations);
647 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100648 }
649 }
650
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100651 const char* GetDescription() const override { return "TypeCheckSlowPathARMVIXL"; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100652
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100653 bool IsFatal() const override { return is_fatal_; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100654
655 private:
656 const bool is_fatal_;
657
658 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
659};
660
Scott Wakelingc34dba72016-10-03 10:14:44 +0100661class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
662 public:
663 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
664 : SlowPathCodeARMVIXL(instruction) {}
665
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100666 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100667 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
668 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100669 LocationSummary* locations = instruction_->GetLocations();
670 SaveLiveRegisters(codegen, locations);
671 InvokeRuntimeCallingConventionARMVIXL calling_convention;
672 __ Mov(calling_convention.GetRegisterAt(0),
673 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
674
Scott Wakelingc34dba72016-10-03 10:14:44 +0100675 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100676 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100677 }
678
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100679 const char* GetDescription() const override { return "DeoptimizationSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100680
681 private:
682 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
683};
684
685class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
686 public:
687 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
688
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100689 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100690 LocationSummary* locations = instruction_->GetLocations();
691 __ Bind(GetEntryLabel());
692 SaveLiveRegisters(codegen, locations);
693
694 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100696 parallel_move.AddMove(
697 locations->InAt(0),
698 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100699 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100700 nullptr);
701 parallel_move.AddMove(
702 locations->InAt(1),
703 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100704 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100705 nullptr);
706 parallel_move.AddMove(
707 locations->InAt(2),
708 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100709 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100710 nullptr);
711 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
712
713 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
714 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
715 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
716 RestoreLiveRegisters(codegen, locations);
717 __ B(GetExitLabel());
718 }
719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100720 const char* GetDescription() const override { return "ArraySetSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100721
722 private:
723 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
724};
725
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000726// Slow path generating a read barrier for a heap reference.
727class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
728 public:
729 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
730 Location out,
731 Location ref,
732 Location obj,
733 uint32_t offset,
734 Location index)
735 : SlowPathCodeARMVIXL(instruction),
736 out_(out),
737 ref_(ref),
738 obj_(obj),
739 offset_(offset),
740 index_(index) {
741 DCHECK(kEmitCompilerReadBarrier);
742 // If `obj` is equal to `out` or `ref`, it means the initial object
743 // has been overwritten by (or after) the heap object reference load
744 // to be instrumented, e.g.:
745 //
746 // __ LoadFromOffset(kLoadWord, out, out, offset);
747 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
748 //
749 // In that case, we have lost the information about the original
750 // object, and the emitted read barrier cannot work properly.
751 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
752 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
753 }
754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100755 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000756 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
757 LocationSummary* locations = instruction_->GetLocations();
758 vixl32::Register reg_out = RegisterFrom(out_);
759 DCHECK(locations->CanCall());
760 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
761 DCHECK(instruction_->IsInstanceFieldGet() ||
762 instruction_->IsStaticFieldGet() ||
763 instruction_->IsArrayGet() ||
764 instruction_->IsInstanceOf() ||
765 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700766 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000767 << "Unexpected instruction in read barrier for heap reference slow path: "
768 << instruction_->DebugName();
769 // The read barrier instrumentation of object ArrayGet
770 // instructions does not support the HIntermediateAddress
771 // instruction.
772 DCHECK(!(instruction_->IsArrayGet() &&
773 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
774
775 __ Bind(GetEntryLabel());
776 SaveLiveRegisters(codegen, locations);
777
778 // We may have to change the index's value, but as `index_` is a
779 // constant member (like other "inputs" of this slow path),
780 // introduce a copy of it, `index`.
781 Location index = index_;
782 if (index_.IsValid()) {
783 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
784 if (instruction_->IsArrayGet()) {
785 // Compute the actual memory offset and store it in `index`.
786 vixl32::Register index_reg = RegisterFrom(index_);
787 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
788 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
789 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +0100790 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
791 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000792 // not been saved by the previous call to
793 // art::SlowPathCode::SaveLiveRegisters, as it is a
794 // callee-save register --
795 // art::SlowPathCode::SaveLiveRegisters does not consider
796 // callee-save registers, as it has been designed with the
797 // assumption that callee-save registers are supposed to be
798 // handled by the called function. So, as a callee-save
799 // register, `index_reg` _would_ eventually be saved onto
800 // the stack, but it would be too late: we would have
801 // changed its value earlier. Therefore, we manually save
802 // it here into another freely available register,
803 // `free_reg`, chosen of course among the caller-save
804 // registers (as a callee-save `free_reg` register would
805 // exhibit the same problem).
806 //
807 // Note we could have requested a temporary register from
808 // the register allocator instead; but we prefer not to, as
809 // this is a slow path, and we know we can find a
810 // caller-save register that is available.
811 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
812 __ Mov(free_reg, index_reg);
813 index_reg = free_reg;
814 index = LocationFrom(index_reg);
815 } else {
816 // The initial register stored in `index_` has already been
817 // saved in the call to art::SlowPathCode::SaveLiveRegisters
818 // (as it is not a callee-save register), so we can freely
819 // use it.
820 }
821 // Shifting the index value contained in `index_reg` by the scale
822 // factor (2) cannot overflow in practice, as the runtime is
823 // unable to allocate object arrays with a size larger than
824 // 2^26 - 1 (that is, 2^28 - 4 bytes).
825 __ Lsl(index_reg, index_reg, TIMES_4);
826 static_assert(
827 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
828 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
829 __ Add(index_reg, index_reg, offset_);
830 } else {
831 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
832 // intrinsics, `index_` is not shifted by a scale factor of 2
833 // (as in the case of ArrayGet), as it is actually an offset
834 // to an object field within an object.
835 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
836 DCHECK(instruction_->GetLocations()->Intrinsified());
837 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
838 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
839 << instruction_->AsInvoke()->GetIntrinsic();
840 DCHECK_EQ(offset_, 0U);
841 DCHECK(index_.IsRegisterPair());
842 // UnsafeGet's offset location is a register pair, the low
843 // part contains the correct offset.
844 index = index_.ToLow();
845 }
846 }
847
848 // We're moving two or three locations to locations that could
849 // overlap, so we need a parallel move resolver.
850 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100851 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000852 parallel_move.AddMove(ref_,
853 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000855 nullptr);
856 parallel_move.AddMove(obj_,
857 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100858 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000859 nullptr);
860 if (index.IsValid()) {
861 parallel_move.AddMove(index,
862 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100863 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000864 nullptr);
865 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
866 } else {
867 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
868 __ Mov(calling_convention.GetRegisterAt(2), offset_);
869 }
870 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
871 CheckEntrypointTypes<
872 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
873 arm_codegen->Move32(out_, LocationFrom(r0));
874
875 RestoreLiveRegisters(codegen, locations);
876 __ B(GetExitLabel());
877 }
878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100879 const char* GetDescription() const override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000880 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
881 }
882
883 private:
884 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
885 uint32_t ref = RegisterFrom(ref_).GetCode();
886 uint32_t obj = RegisterFrom(obj_).GetCode();
887 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
888 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
889 return vixl32::Register(i);
890 }
891 }
892 // We shall never fail to find a free caller-save register, as
893 // there are more than two core caller-save registers on ARM
894 // (meaning it is possible to find one which is different from
895 // `ref` and `obj`).
896 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
897 LOG(FATAL) << "Could not find a free caller-save register";
898 UNREACHABLE();
899 }
900
901 const Location out_;
902 const Location ref_;
903 const Location obj_;
904 const uint32_t offset_;
905 // An additional location containing an index to an array.
906 // Only used for HArrayGet and the UnsafeGetObject &
907 // UnsafeGetObjectVolatile intrinsics.
908 const Location index_;
909
910 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
911};
912
913// Slow path generating a read barrier for a GC root.
914class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
915 public:
916 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
917 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
918 DCHECK(kEmitCompilerReadBarrier);
919 }
920
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100921 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000922 LocationSummary* locations = instruction_->GetLocations();
923 vixl32::Register reg_out = RegisterFrom(out_);
924 DCHECK(locations->CanCall());
925 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
926 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
927 << "Unexpected instruction in read barrier for GC root slow path: "
928 << instruction_->DebugName();
929
930 __ Bind(GetEntryLabel());
931 SaveLiveRegisters(codegen, locations);
932
933 InvokeRuntimeCallingConventionARMVIXL calling_convention;
934 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
935 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
936 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
937 instruction_,
938 instruction_->GetDexPc(),
939 this);
940 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
941 arm_codegen->Move32(out_, LocationFrom(r0));
942
943 RestoreLiveRegisters(codegen, locations);
944 __ B(GetExitLabel());
945 }
946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100947 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARMVIXL"; }
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000948
949 private:
950 const Location out_;
951 const Location root_;
952
953 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
954};
Scott Wakelingc34dba72016-10-03 10:14:44 +0100955
Scott Wakelingfe885462016-09-22 10:24:38 +0100956inline vixl32::Condition ARMCondition(IfCondition cond) {
957 switch (cond) {
958 case kCondEQ: return eq;
959 case kCondNE: return ne;
960 case kCondLT: return lt;
961 case kCondLE: return le;
962 case kCondGT: return gt;
963 case kCondGE: return ge;
964 case kCondB: return lo;
965 case kCondBE: return ls;
966 case kCondA: return hi;
967 case kCondAE: return hs;
968 }
969 LOG(FATAL) << "Unreachable";
970 UNREACHABLE();
971}
972
973// Maps signed condition to unsigned condition.
974inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
975 switch (cond) {
976 case kCondEQ: return eq;
977 case kCondNE: return ne;
978 // Signed to unsigned.
979 case kCondLT: return lo;
980 case kCondLE: return ls;
981 case kCondGT: return hi;
982 case kCondGE: return hs;
983 // Unsigned remain unchanged.
984 case kCondB: return lo;
985 case kCondBE: return ls;
986 case kCondA: return hi;
987 case kCondAE: return hs;
988 }
989 LOG(FATAL) << "Unreachable";
990 UNREACHABLE();
991}
992
993inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
994 // The ARM condition codes can express all the necessary branches, see the
995 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
996 // There is no dex instruction or HIR that would need the missing conditions
997 // "equal or unordered" or "not equal".
998 switch (cond) {
999 case kCondEQ: return eq;
1000 case kCondNE: return ne /* unordered */;
1001 case kCondLT: return gt_bias ? cc : lt /* unordered */;
1002 case kCondLE: return gt_bias ? ls : le /* unordered */;
1003 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
1004 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
1005 default:
1006 LOG(FATAL) << "UNREACHABLE";
1007 UNREACHABLE();
1008 }
1009}
1010
Anton Kirilov74234da2017-01-13 14:42:47 +00001011inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1012 switch (op_kind) {
1013 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1014 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1015 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1016 default:
1017 LOG(FATAL) << "Unexpected op kind " << op_kind;
1018 UNREACHABLE();
1019 }
1020}
1021
Scott Wakelingfe885462016-09-22 10:24:38 +01001022void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1023 stream << vixl32::Register(reg);
1024}
1025
1026void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1027 stream << vixl32::SRegister(reg);
1028}
1029
Vladimir Markoa0431112018-06-25 09:32:54 +01001030const ArmInstructionSetFeatures& CodeGeneratorARMVIXL::GetInstructionSetFeatures() const {
1031 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArmInstructionSetFeatures();
1032}
1033
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001034static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001035 uint32_t mask = 0;
1036 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1037 i <= regs.GetLastSRegister().GetCode();
1038 ++i) {
1039 mask |= (1 << i);
1040 }
1041 return mask;
1042}
1043
Artem Serovd4cc5b22016-11-04 11:19:09 +00001044// Saves the register in the stack. Returns the size taken on stack.
1045size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1046 uint32_t reg_id ATTRIBUTE_UNUSED) {
1047 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001048 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001049}
1050
1051// Restores the register from the stack. Returns the size taken on stack.
1052size_t CodeGeneratorARMVIXL::RestoreCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1053 uint32_t reg_id ATTRIBUTE_UNUSED) {
1054 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001055 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001056}
1057
1058size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1059 uint32_t reg_id ATTRIBUTE_UNUSED) {
1060 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001061 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001062}
1063
1064size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1065 uint32_t reg_id ATTRIBUTE_UNUSED) {
1066 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001067 UNREACHABLE();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001068}
1069
Anton Kirilov74234da2017-01-13 14:42:47 +00001070static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1071 vixl32::Register out,
1072 vixl32::Register first,
1073 const Operand& second,
1074 CodeGeneratorARMVIXL* codegen) {
1075 if (second.IsImmediate() && second.GetImmediate() == 0) {
1076 const Operand in = kind == HInstruction::kAnd
1077 ? Operand(0)
1078 : Operand(first);
1079
1080 __ Mov(out, in);
1081 } else {
1082 switch (kind) {
1083 case HInstruction::kAdd:
1084 __ Add(out, first, second);
1085 break;
1086 case HInstruction::kAnd:
1087 __ And(out, first, second);
1088 break;
1089 case HInstruction::kOr:
1090 __ Orr(out, first, second);
1091 break;
1092 case HInstruction::kSub:
1093 __ Sub(out, first, second);
1094 break;
1095 case HInstruction::kXor:
1096 __ Eor(out, first, second);
1097 break;
1098 default:
1099 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1100 UNREACHABLE();
1101 }
1102 }
1103}
1104
1105static void GenerateDataProc(HInstruction::InstructionKind kind,
1106 const Location& out,
1107 const Location& first,
1108 const Operand& second_lo,
1109 const Operand& second_hi,
1110 CodeGeneratorARMVIXL* codegen) {
1111 const vixl32::Register first_hi = HighRegisterFrom(first);
1112 const vixl32::Register first_lo = LowRegisterFrom(first);
1113 const vixl32::Register out_hi = HighRegisterFrom(out);
1114 const vixl32::Register out_lo = LowRegisterFrom(out);
1115
1116 if (kind == HInstruction::kAdd) {
1117 __ Adds(out_lo, first_lo, second_lo);
1118 __ Adc(out_hi, first_hi, second_hi);
1119 } else if (kind == HInstruction::kSub) {
1120 __ Subs(out_lo, first_lo, second_lo);
1121 __ Sbc(out_hi, first_hi, second_hi);
1122 } else {
1123 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1124 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1125 }
1126}
1127
1128static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1129 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1130}
1131
1132static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1133 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001134 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001135 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1136
1137 const LocationSummary* const locations = instruction->GetLocations();
1138 const uint32_t shift_value = instruction->GetShiftAmount();
1139 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1140 const Location first = locations->InAt(0);
1141 const Location second = locations->InAt(1);
1142 const Location out = locations->Out();
1143 const vixl32::Register first_hi = HighRegisterFrom(first);
1144 const vixl32::Register first_lo = LowRegisterFrom(first);
1145 const vixl32::Register out_hi = HighRegisterFrom(out);
1146 const vixl32::Register out_lo = LowRegisterFrom(out);
1147 const vixl32::Register second_hi = HighRegisterFrom(second);
1148 const vixl32::Register second_lo = LowRegisterFrom(second);
1149 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1150
1151 if (shift_value >= 32) {
1152 if (shift == ShiftType::LSL) {
1153 GenerateDataProcInstruction(kind,
1154 out_hi,
1155 first_hi,
1156 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1157 codegen);
1158 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1159 } else if (shift == ShiftType::ASR) {
1160 GenerateDataProc(kind,
1161 out,
1162 first,
1163 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1164 Operand(second_hi, ShiftType::ASR, 31),
1165 codegen);
1166 } else {
1167 DCHECK_EQ(shift, ShiftType::LSR);
1168 GenerateDataProc(kind,
1169 out,
1170 first,
1171 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1172 0,
1173 codegen);
1174 }
1175 } else {
1176 DCHECK_GT(shift_value, 1U);
1177 DCHECK_LT(shift_value, 32U);
1178
1179 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1180
1181 if (shift == ShiftType::LSL) {
1182 // We are not doing this for HInstruction::kAdd because the output will require
1183 // Location::kOutputOverlap; not applicable to other cases.
1184 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1185 GenerateDataProcInstruction(kind,
1186 out_hi,
1187 first_hi,
1188 Operand(second_hi, ShiftType::LSL, shift_value),
1189 codegen);
1190 GenerateDataProcInstruction(kind,
1191 out_hi,
1192 out_hi,
1193 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1194 codegen);
1195 GenerateDataProcInstruction(kind,
1196 out_lo,
1197 first_lo,
1198 Operand(second_lo, ShiftType::LSL, shift_value),
1199 codegen);
1200 } else {
1201 const vixl32::Register temp = temps.Acquire();
1202
1203 __ Lsl(temp, second_hi, shift_value);
1204 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1205 GenerateDataProc(kind,
1206 out,
1207 first,
1208 Operand(second_lo, ShiftType::LSL, shift_value),
1209 temp,
1210 codegen);
1211 }
1212 } else {
1213 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1214
1215 // We are not doing this for HInstruction::kAdd because the output will require
1216 // Location::kOutputOverlap; not applicable to other cases.
1217 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1218 GenerateDataProcInstruction(kind,
1219 out_lo,
1220 first_lo,
1221 Operand(second_lo, ShiftType::LSR, shift_value),
1222 codegen);
1223 GenerateDataProcInstruction(kind,
1224 out_lo,
1225 out_lo,
1226 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1227 codegen);
1228 GenerateDataProcInstruction(kind,
1229 out_hi,
1230 first_hi,
1231 Operand(second_hi, shift, shift_value),
1232 codegen);
1233 } else {
1234 const vixl32::Register temp = temps.Acquire();
1235
1236 __ Lsr(temp, second_lo, shift_value);
1237 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1238 GenerateDataProc(kind,
1239 out,
1240 first,
1241 temp,
1242 Operand(second_hi, shift, shift_value),
1243 codegen);
1244 }
1245 }
1246 }
1247}
1248
Donghui Bai426b49c2016-11-08 14:55:38 +08001249static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1250 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1251 if (rhs_loc.IsConstant()) {
1252 // 0.0 is the only immediate that can be encoded directly in
1253 // a VCMP instruction.
1254 //
1255 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1256 // specify that in a floating-point comparison, positive zero
1257 // and negative zero are considered equal, so we can use the
1258 // literal 0.0 for both cases here.
1259 //
1260 // Note however that some methods (Float.equal, Float.compare,
1261 // Float.compareTo, Double.equal, Double.compare,
1262 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1263 // StrictMath.min) consider 0.0 to be (strictly) greater than
1264 // -0.0. So if we ever translate calls to these methods into a
1265 // HCompare instruction, we must handle the -0.0 case with
1266 // care here.
1267 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001271 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001272 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1273 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001275 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1276 }
1277 } else {
1278 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1279 }
1280}
1281
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001282static int64_t AdjustConstantForCondition(int64_t value,
1283 IfCondition* condition,
1284 IfCondition* opposite) {
1285 if (value == 1) {
1286 if (*condition == kCondB) {
1287 value = 0;
1288 *condition = kCondEQ;
1289 *opposite = kCondNE;
1290 } else if (*condition == kCondAE) {
1291 value = 0;
1292 *condition = kCondNE;
1293 *opposite = kCondEQ;
1294 }
1295 } else if (value == -1) {
1296 if (*condition == kCondGT) {
1297 value = 0;
1298 *condition = kCondGE;
1299 *opposite = kCondLT;
1300 } else if (*condition == kCondLE) {
1301 value = 0;
1302 *condition = kCondLT;
1303 *opposite = kCondGE;
1304 }
1305 }
1306
1307 return value;
1308}
1309
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001310static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1311 HCondition* condition,
1312 bool invert,
1313 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001315
1316 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001317 IfCondition cond = condition->GetCondition();
1318 IfCondition opposite = condition->GetOppositeCondition();
1319
1320 if (invert) {
1321 std::swap(cond, opposite);
1322 }
1323
1324 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001325 const Location left = locations->InAt(0);
1326 const Location right = locations->InAt(1);
1327
1328 DCHECK(right.IsConstant());
1329
1330 const vixl32::Register left_high = HighRegisterFrom(left);
1331 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001332 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1333 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1334
1335 // Comparisons against 0 are common enough to deserve special attention.
1336 if (value == 0) {
1337 switch (cond) {
1338 case kCondNE:
1339 // x > 0 iff x != 0 when the comparison is unsigned.
1340 case kCondA:
1341 ret = std::make_pair(ne, eq);
1342 FALLTHROUGH_INTENDED;
1343 case kCondEQ:
1344 // x <= 0 iff x == 0 when the comparison is unsigned.
1345 case kCondBE:
1346 __ Orrs(temps.Acquire(), left_low, left_high);
1347 return ret;
1348 case kCondLT:
1349 case kCondGE:
1350 __ Cmp(left_high, 0);
1351 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1352 // Trivially true or false.
1353 case kCondB:
1354 ret = std::make_pair(ne, eq);
1355 FALLTHROUGH_INTENDED;
1356 case kCondAE:
1357 __ Cmp(left_low, left_low);
1358 return ret;
1359 default:
1360 break;
1361 }
1362 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001363
1364 switch (cond) {
1365 case kCondEQ:
1366 case kCondNE:
1367 case kCondB:
1368 case kCondBE:
1369 case kCondA:
1370 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001371 const uint32_t value_low = Low32Bits(value);
1372 Operand operand_low(value_low);
1373
Donghui Bai426b49c2016-11-08 14:55:38 +08001374 __ Cmp(left_high, High32Bits(value));
1375
Anton Kirilov23b752b2017-07-20 14:40:44 +01001376 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1377 // we must ensure that the operands corresponding to the least significant
1378 // halves of the inputs fit into a 16-bit CMP encoding.
1379 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1380 operand_low = Operand(temps.Acquire());
1381 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1382 }
1383
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001384 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001385 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1386 2 * vixl32::k16BitT32InstructionSizeInBytes,
1387 CodeBufferCheckScope::kExactSize);
1388
1389 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001390 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001391 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001392 break;
1393 }
1394 case kCondLE:
1395 case kCondGT:
1396 // Trivially true or false.
1397 if (value == std::numeric_limits<int64_t>::max()) {
1398 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001399 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001400 break;
1401 }
1402
1403 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001404 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001405 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001406 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001407 } else {
1408 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001409 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001410 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001411 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001412 }
1413
1414 value++;
1415 FALLTHROUGH_INTENDED;
1416 case kCondGE:
1417 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001418 __ Cmp(left_low, Low32Bits(value));
1419 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001420 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001421 break;
1422 }
1423 default:
1424 LOG(FATAL) << "Unreachable";
1425 UNREACHABLE();
1426 }
1427
1428 return ret;
1429}
1430
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001431static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1432 HCondition* condition,
1433 bool invert,
1434 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001435 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001436
1437 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001438 IfCondition cond = condition->GetCondition();
1439 IfCondition opposite = condition->GetOppositeCondition();
1440
1441 if (invert) {
1442 std::swap(cond, opposite);
1443 }
1444
1445 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001446 Location left = locations->InAt(0);
1447 Location right = locations->InAt(1);
1448
1449 DCHECK(right.IsRegisterPair());
1450
1451 switch (cond) {
1452 case kCondEQ:
1453 case kCondNE:
1454 case kCondB:
1455 case kCondBE:
1456 case kCondA:
1457 case kCondAE: {
1458 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1459
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001460 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001461 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1462 2 * vixl32::k16BitT32InstructionSizeInBytes,
1463 CodeBufferCheckScope::kExactSize);
1464
1465 __ it(eq);
1466 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001467 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001468 break;
1469 }
1470 case kCondLE:
1471 case kCondGT:
1472 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001473 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001474 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001475 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001476 } else {
1477 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001478 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001479 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001480 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001481 }
1482
1483 std::swap(left, right);
1484 FALLTHROUGH_INTENDED;
1485 case kCondGE:
1486 case kCondLT: {
1487 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1488
1489 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1490 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001491 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001492 break;
1493 }
1494 default:
1495 LOG(FATAL) << "Unreachable";
1496 UNREACHABLE();
1497 }
1498
1499 return ret;
1500}
1501
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001502static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1503 bool invert,
1504 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001506 IfCondition cond = condition->GetCondition();
1507 IfCondition opposite = condition->GetOppositeCondition();
1508 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001509
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001510 if (invert) {
1511 std::swap(cond, opposite);
1512 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001514 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001515 ret = condition->GetLocations()->InAt(1).IsConstant()
1516 ? GenerateLongTestConstant(condition, invert, codegen)
1517 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001518 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001519 GenerateVcmp(condition, codegen);
1520 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1521 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1522 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001523 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001524 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001525 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1526 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001527 }
1528
1529 return ret;
1530}
1531
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001532static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001533 const vixl32::Register out = OutputRegister(cond);
1534 const auto condition = GenerateTest(cond, false, codegen);
1535
1536 __ Mov(LeaveFlags, out, 0);
1537
1538 if (out.IsLow()) {
1539 // We use the scope because of the IT block that follows.
1540 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1541 2 * vixl32::k16BitT32InstructionSizeInBytes,
1542 CodeBufferCheckScope::kExactSize);
1543
1544 __ it(condition.first);
1545 __ mov(condition.first, out, 1);
1546 } else {
1547 vixl32::Label done_label;
1548 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1549
Andreas Gampe3db70682018-12-26 15:12:03 -08001550 __ B(condition.second, final_label, /* is_far_target= */ false);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001551 __ Mov(out, 1);
1552
1553 if (done_label.IsReferenced()) {
1554 __ Bind(&done_label);
1555 }
1556 }
1557}
1558
1559static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001561
1562 const LocationSummary* const locations = cond->GetLocations();
1563 IfCondition condition = cond->GetCondition();
1564 const vixl32::Register out = OutputRegister(cond);
1565 const Location left = locations->InAt(0);
1566 const Location right = locations->InAt(1);
1567 vixl32::Register left_high = HighRegisterFrom(left);
1568 vixl32::Register left_low = LowRegisterFrom(left);
1569 vixl32::Register temp;
1570 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1571
1572 if (right.IsConstant()) {
1573 IfCondition opposite = cond->GetOppositeCondition();
1574 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
1575 &condition,
1576 &opposite);
1577 Operand right_high = High32Bits(value);
1578 Operand right_low = Low32Bits(value);
1579
1580 // The output uses Location::kNoOutputOverlap.
1581 if (out.Is(left_high)) {
1582 std::swap(left_low, left_high);
1583 std::swap(right_low, right_high);
1584 }
1585
1586 __ Sub(out, left_low, right_low);
1587 temp = temps.Acquire();
1588 __ Sub(temp, left_high, right_high);
1589 } else {
1590 DCHECK(right.IsRegisterPair());
1591 temp = temps.Acquire();
1592 __ Sub(temp, left_high, HighRegisterFrom(right));
1593 __ Sub(out, left_low, LowRegisterFrom(right));
1594 }
1595
1596 // Need to check after calling AdjustConstantForCondition().
1597 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
1598
1599 if (condition == kCondNE && out.IsLow()) {
1600 __ Orrs(out, out, temp);
1601
1602 // We use the scope because of the IT block that follows.
1603 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1604 2 * vixl32::k16BitT32InstructionSizeInBytes,
1605 CodeBufferCheckScope::kExactSize);
1606
1607 __ it(ne);
1608 __ mov(ne, out, 1);
1609 } else {
1610 __ Orr(out, out, temp);
1611 codegen->GenerateConditionWithZero(condition, out, out, temp);
1612 }
1613}
1614
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001615static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001616 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001617
1618 const LocationSummary* const locations = cond->GetLocations();
1619 IfCondition condition = cond->GetCondition();
1620 const vixl32::Register out = OutputRegister(cond);
1621 const Location left = locations->InAt(0);
1622 const Location right = locations->InAt(1);
1623
1624 if (right.IsConstant()) {
1625 IfCondition opposite = cond->GetOppositeCondition();
1626
1627 // Comparisons against 0 are common enough to deserve special attention.
1628 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
1629 switch (condition) {
1630 case kCondNE:
1631 case kCondA:
1632 if (out.IsLow()) {
1633 // We only care if both input registers are 0 or not.
1634 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
1635
1636 // We use the scope because of the IT block that follows.
1637 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1638 2 * vixl32::k16BitT32InstructionSizeInBytes,
1639 CodeBufferCheckScope::kExactSize);
1640
1641 __ it(ne);
1642 __ mov(ne, out, 1);
1643 return;
1644 }
1645
1646 FALLTHROUGH_INTENDED;
1647 case kCondEQ:
1648 case kCondBE:
1649 // We only care if both input registers are 0 or not.
1650 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
1651 codegen->GenerateConditionWithZero(condition, out, out);
1652 return;
1653 case kCondLT:
1654 case kCondGE:
1655 // We only care about the sign bit.
1656 FALLTHROUGH_INTENDED;
1657 case kCondAE:
1658 case kCondB:
1659 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
1660 return;
1661 case kCondLE:
1662 case kCondGT:
1663 default:
1664 break;
1665 }
1666 }
1667 }
1668
Anton Kirilov23b752b2017-07-20 14:40:44 +01001669 // If `out` is a low register, then the GenerateConditionGeneric()
1670 // function generates a shorter code sequence that is still branchless.
1671 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001672 GenerateEqualLong(cond, codegen);
1673 return;
1674 }
1675
Anton Kirilov23b752b2017-07-20 14:40:44 +01001676 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001677}
1678
Roland Levillain6d729a72017-06-30 18:34:01 +01001679static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
1680 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001685 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001686 GenerateConditionLong(cond, codegen);
1687 return;
1688 }
1689
1690 IfCondition condition = cond->GetCondition();
1691 vixl32::Register in = InputRegisterAt(cond, 0);
1692 const vixl32::Register out = OutputRegister(cond);
1693 const Location right = cond->GetLocations()->InAt(1);
1694 int64_t value;
1695
1696 if (right.IsConstant()) {
1697 IfCondition opposite = cond->GetOppositeCondition();
1698
1699 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
1700
1701 // Comparisons against 0 are common enough to deserve special attention.
1702 if (value == 0) {
1703 switch (condition) {
1704 case kCondNE:
1705 case kCondA:
1706 if (out.IsLow() && out.Is(in)) {
1707 __ Cmp(out, 0);
1708
1709 // We use the scope because of the IT block that follows.
1710 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1711 2 * vixl32::k16BitT32InstructionSizeInBytes,
1712 CodeBufferCheckScope::kExactSize);
1713
1714 __ it(ne);
1715 __ mov(ne, out, 1);
1716 return;
1717 }
1718
1719 FALLTHROUGH_INTENDED;
1720 case kCondEQ:
1721 case kCondBE:
1722 case kCondLT:
1723 case kCondGE:
1724 case kCondAE:
1725 case kCondB:
1726 codegen->GenerateConditionWithZero(condition, out, in);
1727 return;
1728 case kCondLE:
1729 case kCondGT:
1730 default:
1731 break;
1732 }
1733 }
1734 }
1735
1736 if (condition == kCondEQ || condition == kCondNE) {
1737 Operand operand(0);
1738
1739 if (right.IsConstant()) {
1740 operand = Operand::From(value);
1741 } else if (out.Is(RegisterFrom(right))) {
1742 // Avoid 32-bit instructions if possible.
1743 operand = InputOperandAt(cond, 0);
1744 in = RegisterFrom(right);
1745 } else {
1746 operand = InputOperandAt(cond, 1);
1747 }
1748
1749 if (condition == kCondNE && out.IsLow()) {
1750 __ Subs(out, in, operand);
1751
1752 // We use the scope because of the IT block that follows.
1753 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1754 2 * vixl32::k16BitT32InstructionSizeInBytes,
1755 CodeBufferCheckScope::kExactSize);
1756
1757 __ it(ne);
1758 __ mov(ne, out, 1);
1759 } else {
1760 __ Sub(out, in, operand);
1761 codegen->GenerateConditionWithZero(condition, out, out);
1762 }
1763
1764 return;
1765 }
1766
1767 GenerateConditionGeneric(cond, codegen);
1768}
1769
Donghui Bai426b49c2016-11-08 14:55:38 +08001770static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001772 bool ret = false;
1773
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001774 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001776 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001777 const uint64_t value = Uint64ConstantFrom(constant);
1778
1779 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1780 } else {
1781 ret = IsUint<8>(Int32ConstantFrom(constant));
1782 }
1783
1784 return ret;
1785}
1786
1787static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001788 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001789
1790 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1791 return Location::ConstantLocation(constant->AsConstant());
1792 }
1793
1794 return Location::RequiresRegister();
1795}
1796
1797static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1798 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1799 // we check that we are not dealing with floating-point output (there is no
1800 // 16-bit VMOV encoding).
1801 if (!out.IsRegister() && !out.IsRegisterPair()) {
1802 return false;
1803 }
1804
1805 // For constants, we also check that the output is in one or two low registers,
1806 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1807 // MOV encoding can be used.
1808 if (src.IsConstant()) {
1809 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1810 return false;
1811 }
1812
1813 if (out.IsRegister()) {
1814 if (!RegisterFrom(out).IsLow()) {
1815 return false;
1816 }
1817 } else {
1818 DCHECK(out.IsRegisterPair());
1819
1820 if (!HighRegisterFrom(out).IsLow()) {
1821 return false;
1822 }
1823 }
1824 }
1825
1826 return true;
1827}
1828
Scott Wakelingfe885462016-09-22 10:24:38 +01001829#undef __
1830
Donghui Bai426b49c2016-11-08 14:55:38 +08001831vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1832 vixl32::Label* final_label) {
1833 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001834 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001835
1836 const HBasicBlock* const block = instruction->GetBlock();
1837 const HLoopInformation* const info = block->GetLoopInformation();
1838 HInstruction* const next = instruction->GetNext();
1839
1840 // Avoid a branch to a branch.
1841 if (next->IsGoto() && (info == nullptr ||
1842 !info->IsBackEdge(*block) ||
1843 !info->HasSuspendCheck())) {
1844 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1845 }
1846
1847 return final_label;
1848}
1849
Scott Wakelingfe885462016-09-22 10:24:38 +01001850CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +01001851 const CompilerOptions& compiler_options,
1852 OptimizingCompilerStats* stats)
1853 : CodeGenerator(graph,
1854 kNumberOfCoreRegisters,
1855 kNumberOfSRegisters,
1856 kNumberOfRegisterPairs,
1857 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001858 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001859 compiler_options,
1860 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1862 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001863 location_builder_(graph, this),
1864 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001865 move_resolver_(graph->GetAllocator(), this),
1866 assembler_(graph->GetAllocator()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001867 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001869 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001870 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001871 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1872 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001873 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001874 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001875 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001876 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001877 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001878 uint32_literals_(std::less<uint32_t>(),
1879 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001880 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001881 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001882 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001883 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1884 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1885 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001886 // Always save the LR register to mimic Quick.
1887 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00001888 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
1889 // S0-S31, which alias to D0-D15.
1890 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
1891 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01001892}
1893
Artem Serov551b28f2016-10-18 19:11:30 +01001894void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1895 uint32_t num_entries = switch_instr_->GetNumEntries();
1896 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1897
1898 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001899 // underlying code buffer and we have generated a jump table of the right size, using
1900 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001901 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1902 num_entries * sizeof(int32_t),
1903 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001904 // TODO(VIXL): Check that using lower case bind is fine here.
1905 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001906 for (uint32_t i = 0; i < num_entries; i++) {
1907 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1908 }
1909}
1910
1911void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1912 uint32_t num_entries = switch_instr_->GetNumEntries();
1913 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1914
Artem Serov551b28f2016-10-18 19:11:30 +01001915 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1916 for (uint32_t i = 0; i < num_entries; i++) {
1917 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1918 DCHECK(target_label->IsBound());
1919 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1920 // When doing BX to address we need to have lower bit set to 1 in T32.
1921 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1922 jump_offset++;
1923 }
1924 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1925 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001926
Scott Wakelingb77051e2016-11-21 19:46:00 +00001927 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001928 }
1929}
1930
Artem Serov09a940d2016-11-11 16:15:11 +00001931void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001932 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001933 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001934 }
1935}
1936
Andreas Gampeca620d72016-11-08 08:09:33 -08001937#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001938
1939void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001940 FixJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001941
1942 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001943 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001944 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1945 uint32_t encoded_data = entry.first;
1946 vixl::aarch32::Label* slow_path_entry = &entry.second.label;
1947 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001948 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001949 }
1950
Scott Wakelingfe885462016-09-22 10:24:38 +01001951 GetAssembler()->FinalizeCode();
1952 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001953
1954 // Verify Baker read barrier linker patches.
1955 if (kIsDebugBuild) {
1956 ArrayRef<const uint8_t> code = allocator->GetMemory();
1957 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1958 DCHECK(info.label.IsBound());
1959 uint32_t literal_offset = info.label.GetLocation();
1960 DCHECK_ALIGNED(literal_offset, 2u);
1961
1962 auto GetInsn16 = [&code](uint32_t offset) {
1963 DCHECK_ALIGNED(offset, 2u);
1964 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
1965 (static_cast<uint32_t>(code[offset + 1]) << 8);
1966 };
1967 auto GetInsn32 = [=](uint32_t offset) {
1968 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
1969 };
1970
1971 uint32_t encoded_data = info.custom_data;
1972 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1973 // Check that the next instruction matches the expected LDR.
1974 switch (kind) {
1975 case BakerReadBarrierKind::kField: {
1976 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1977 if (width == BakerReadBarrierWidth::kWide) {
1978 DCHECK_GE(code.size() - literal_offset, 8u);
1979 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1980 // LDR (immediate), encoding T3, with correct base_reg.
1981 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1982 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1983 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
1984 } else {
1985 DCHECK_GE(code.size() - literal_offset, 6u);
1986 uint32_t next_insn = GetInsn16(literal_offset + 4u);
1987 // LDR (immediate), encoding T1, with correct base_reg.
1988 CheckValidReg(next_insn & 0x7u); // Check destination register.
1989 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1990 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
1991 }
1992 break;
1993 }
1994 case BakerReadBarrierKind::kArray: {
1995 DCHECK_GE(code.size() - literal_offset, 8u);
1996 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1997 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
1998 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1999 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2000 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
2001 CheckValidReg(next_insn & 0xf); // Check index register
2002 break;
2003 }
2004 case BakerReadBarrierKind::kGcRoot: {
2005 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
2006 if (width == BakerReadBarrierWidth::kWide) {
2007 DCHECK_GE(literal_offset, 4u);
2008 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Markof28be432018-08-14 12:20:51 +00002009 // LDR (immediate), encoding T3, with correct root_reg.
Vladimir Markoca1e0382018-04-11 09:58:41 +00002010 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markof28be432018-08-14 12:20:51 +00002011 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
Vladimir Markoca1e0382018-04-11 09:58:41 +00002012 } else {
2013 DCHECK_GE(literal_offset, 2u);
2014 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
2015 // LDR (immediate), encoding T1, with correct root_reg.
2016 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2017 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2018 }
2019 break;
2020 }
Vladimir Markod887ed82018-08-14 13:52:12 +00002021 case BakerReadBarrierKind::kUnsafeCas: {
2022 DCHECK_GE(literal_offset, 4u);
2023 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
2024 // ADD (register), encoding T3, with correct root_reg.
2025 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2026 CHECK_EQ(prev_insn & 0xfff0fff0u, 0xeb000000u | (root_reg << 8));
2027 break;
2028 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002029 default:
2030 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2031 UNREACHABLE();
2032 }
2033 }
2034 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002035}
2036
2037void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002038 // Stack register, LR and PC are always reserved.
2039 blocked_core_registers_[SP] = true;
2040 blocked_core_registers_[LR] = true;
2041 blocked_core_registers_[PC] = true;
2042
Roland Levillain6d729a72017-06-30 18:34:01 +01002043 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2044 // Reserve marking register.
2045 blocked_core_registers_[MR] = true;
2046 }
2047
Scott Wakelingfe885462016-09-22 10:24:38 +01002048 // Reserve thread register.
2049 blocked_core_registers_[TR] = true;
2050
2051 // Reserve temp register.
2052 blocked_core_registers_[IP] = true;
2053
2054 if (GetGraph()->IsDebuggable()) {
2055 // Stubs do not save callee-save floating point registers. If the graph
2056 // is debuggable, we need to deal with these registers differently. For
2057 // now, just block them.
2058 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2059 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2060 ++i) {
2061 blocked_fpu_registers_[i] = true;
2062 }
2063 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002064}
2065
Scott Wakelingfe885462016-09-22 10:24:38 +01002066InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2067 CodeGeneratorARMVIXL* codegen)
2068 : InstructionCodeGenerator(graph, codegen),
2069 assembler_(codegen->GetAssembler()),
2070 codegen_(codegen) {}
2071
2072void CodeGeneratorARMVIXL::ComputeSpillMask() {
2073 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Vladimir Marko460f0542019-07-04 14:02:08 +01002074 DCHECK_NE(core_spill_mask_ & (1u << kLrCode), 0u)
2075 << "At least the return address register must be saved";
2076 // 16-bit PUSH/POP (T1) can save/restore just the LR/PC.
2077 DCHECK(GetVIXLAssembler()->IsUsingT32());
Scott Wakelingfe885462016-09-22 10:24:38 +01002078 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2079 // We use vpush and vpop for saving and restoring floating point registers, which take
2080 // a SRegister and the number of registers to save/restore after that SRegister. We
2081 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2082 // but in the range.
2083 if (fpu_spill_mask_ != 0) {
2084 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2085 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2086 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2087 fpu_spill_mask_ |= (1 << i);
2088 }
2089 }
2090}
2091
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002092void CodeGeneratorARMVIXL::MaybeIncrementHotness(bool is_frame_entry) {
2093 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2094 UseScratchRegisterScope temps(GetVIXLAssembler());
2095 vixl32::Register temp = temps.Acquire();
2096 static_assert(ArtMethod::MaxCounter() == 0xFFFF, "asm is probably wrong");
2097 if (!is_frame_entry) {
2098 __ Push(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002099 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002100 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2101 }
2102 // Load with zero extend to clear the high bits for integer overflow check.
2103 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2104 __ Add(temp, temp, 1);
2105 // Subtract one if the counter would overflow.
2106 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
2107 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2108 if (!is_frame_entry) {
2109 __ Pop(vixl32::Register(kMethodRegister));
Vladimir Markodec78172020-06-19 15:31:23 +01002110 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002111 }
2112 }
2113
2114 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002115 ScopedProfilingInfoUse spiu(
2116 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2117 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002118 if (info != nullptr) {
2119 uint32_t address = reinterpret_cast32<uint32_t>(info);
2120 vixl::aarch32::Label done;
2121 UseScratchRegisterScope temps(GetVIXLAssembler());
2122 temps.Exclude(ip);
2123 if (!is_frame_entry) {
2124 __ Push(r4); // Will be used as temporary. For frame entry, r4 is always available.
Vladimir Markodec78172020-06-19 15:31:23 +01002125 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002126 }
2127 __ Mov(r4, address);
2128 __ Ldrh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2129 __ Add(ip, ip, 1);
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01002130 instruction_visitor_.GenerateAndConst(ip, ip, interpreter::kTieredHotnessMask);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002131 __ Strh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2132 if (!is_frame_entry) {
2133 __ Pop(r4);
Vladimir Markodec78172020-06-19 15:31:23 +01002134 GetAssembler()->cfi().AdjustCFAOffset(-static_cast<int>(kArmWordSize));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002135 }
2136 __ Lsls(ip, ip, 16);
2137 __ B(ne, &done);
2138 uint32_t entry_point_offset =
2139 GetThreadOffset<kArmPointerSize>(kQuickCompileOptimized).Int32Value();
2140 if (HasEmptyFrame()) {
2141 CHECK(is_frame_entry);
2142 // For leaf methods, we need to spill lr and r0. Also spill r1 and r2 for
2143 // alignment.
2144 uint32_t core_spill_mask =
2145 (1 << lr.GetCode()) | (1 << r0.GetCode()) | (1 << r1.GetCode()) | (1 << r2.GetCode());
2146 __ Push(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002147 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002148 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2149 __ Blx(lr);
2150 __ Pop(RegisterList(core_spill_mask));
Vladimir Markodec78172020-06-19 15:31:23 +01002151 GetAssembler()->cfi().AdjustCFAOffset(
2152 -static_cast<int>(kArmWordSize) * POPCOUNT(core_spill_mask));
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002153 } else {
2154 if (!RequiresCurrentMethod()) {
2155 CHECK(is_frame_entry);
2156 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2157 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002158 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2159 __ Blx(lr);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002160 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002161 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002162 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002163 }
2164}
2165
Scott Wakelingfe885462016-09-22 10:24:38 +01002166void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2167 bool skip_overflow_check =
2168 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2169 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2170 __ Bind(&frame_entry_label_);
2171
2172 if (HasEmptyFrame()) {
David Srbecky30021842019-02-13 14:19:36 +00002173 // Ensure that the CFI opcode list is not empty.
2174 GetAssembler()->cfi().Nop();
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002175 MaybeIncrementHotness(/* is_frame_entry= */ true);
Scott Wakelingfe885462016-09-22 10:24:38 +01002176 return;
2177 }
2178
Scott Wakelingfe885462016-09-22 10:24:38 +01002179 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002180 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002181 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002182 vixl32::Register temp;
2183 // TODO: Remove this check when R4 is made a callee-save register
2184 // in ART compiled code (b/72801708). Currently we need to make
2185 // sure r4 is not blocked, e.g. in special purpose
2186 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2187 // here.
2188 if (!blocked_core_registers_[R4]) {
2189 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2190 DCHECK(!reg.Is(r4));
2191 }
2192 DCHECK(!kCoreCalleeSaves.Includes(r4));
2193 temp = r4;
2194 } else {
2195 temp = temps.Acquire();
2196 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002197 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002198 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002199 ExactAssemblyScope aas(GetVIXLAssembler(),
2200 vixl32::kMaxInstructionSizeInBytes,
2201 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002202 __ ldr(temp, MemOperand(temp));
2203 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002204 }
2205
Vladimir Marko1a225a72019-07-05 13:37:42 +01002206 uint32_t frame_size = GetFrameSize();
2207 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2208 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2209 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2210 core_spills_offset <= 3u * kArmWordSize) {
2211 // Do a single PUSH for core registers including the method and up to two
2212 // filler registers. Then store the single FP spill if any.
2213 // (The worst case is when the method is not required and we actually
2214 // store 3 extra registers but they are stored in the same properly
2215 // aligned 16-byte chunk where we're already writing anyway.)
2216 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2217 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize);
2218 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(core_spill_mask_));
2219 __ Push(RegisterList(core_spill_mask_ | extra_regs));
2220 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
2221 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2222 core_spills_offset,
2223 core_spill_mask_,
2224 kArmWordSize);
2225 if (fpu_spill_mask_ != 0u) {
2226 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2227 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2228 GetAssembler()->StoreSToOffset(sreg, sp, fp_spills_offset);
2229 GetAssembler()->cfi().RelOffset(DWARFReg(sreg), /*offset=*/ fp_spills_offset);
2230 }
2231 } else {
2232 __ Push(RegisterList(core_spill_mask_));
2233 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2234 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2235 /*offset=*/ 0,
2236 core_spill_mask_,
2237 kArmWordSize);
2238 if (fpu_spill_mask_ != 0) {
2239 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002240
Vladimir Marko1a225a72019-07-05 13:37:42 +01002241 // Check that list is contiguous.
2242 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002243
Vladimir Marko1a225a72019-07-05 13:37:42 +01002244 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2245 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
2246 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0),
2247 /*offset=*/ 0,
2248 fpu_spill_mask_,
2249 kArmWordSize);
2250 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002251
Vladimir Marko1a225a72019-07-05 13:37:42 +01002252 // Adjust SP and save the current method if we need it. Note that we do
2253 // not save the method in HCurrentMethod, as the instruction might have
2254 // been removed in the SSA graph.
2255 if (RequiresCurrentMethod() && fp_spills_offset <= 3 * kArmWordSize) {
2256 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2257 __ Push(RegisterList(MaxInt<uint32_t>(fp_spills_offset / kArmWordSize)));
2258 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2259 } else {
Vladimir Markodec78172020-06-19 15:31:23 +01002260 IncreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002261 if (RequiresCurrentMethod()) {
2262 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2263 }
2264 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002265 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002266
2267 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2268 UseScratchRegisterScope temps(GetVIXLAssembler());
2269 vixl32::Register temp = temps.Acquire();
2270 // Initialize should_deoptimize flag to 0.
2271 __ Mov(temp, 0);
2272 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2273 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002274
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002275 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08002276 MaybeGenerateMarkingRegisterCheck(/* code= */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002277}
2278
2279void CodeGeneratorARMVIXL::GenerateFrameExit() {
2280 if (HasEmptyFrame()) {
2281 __ Bx(lr);
2282 return;
2283 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002284
Scott Wakelingfe885462016-09-22 10:24:38 +01002285 // Pop LR into PC to return.
2286 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2287 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
Vladimir Marko1a225a72019-07-05 13:37:42 +01002288
2289 uint32_t frame_size = GetFrameSize();
2290 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2291 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2292 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2293 // r4 is blocked by TestCodeGeneratorARMVIXL used by some tests.
2294 core_spills_offset <= (blocked_core_registers_[r4.GetCode()] ? 2u : 3u) * kArmWordSize) {
2295 // Load the FP spill if any and then do a single POP including the method
2296 // and up to two filler registers. If we have no FP spills, this also has
2297 // the advantage that we do not need to emit CFI directives.
2298 if (fpu_spill_mask_ != 0u) {
2299 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2300 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2301 GetAssembler()->cfi().RememberState();
2302 GetAssembler()->LoadSFromOffset(sreg, sp, fp_spills_offset);
2303 GetAssembler()->cfi().Restore(DWARFReg(sreg));
2304 }
2305 // Clobber registers r2-r4 as they are caller-save in ART managed ABI and
2306 // never hold the return value.
2307 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize) << r2.GetCode();
2308 DCHECK_EQ(extra_regs & kCoreCalleeSaves.GetList(), 0u);
2309 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(pop_mask));
2310 __ Pop(RegisterList(pop_mask | extra_regs));
2311 if (fpu_spill_mask_ != 0u) {
2312 GetAssembler()->cfi().RestoreState();
2313 }
2314 } else {
2315 GetAssembler()->cfi().RememberState();
Vladimir Markodec78172020-06-19 15:31:23 +01002316 DecreaseFrame(fp_spills_offset);
Vladimir Marko1a225a72019-07-05 13:37:42 +01002317 if (fpu_spill_mask_ != 0) {
2318 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2319
2320 // Check that list is contiguous.
2321 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2322
2323 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2324 GetAssembler()->cfi().AdjustCFAOffset(
2325 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
2326 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
2327 }
2328 __ Pop(RegisterList(pop_mask));
2329 GetAssembler()->cfi().RestoreState();
2330 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2331 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002332}
2333
2334void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2335 __ Bind(GetLabelOf(block));
2336}
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002339 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002340 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002342 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 case DataType::Type::kInt8:
2344 case DataType::Type::kUint16:
2345 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002346 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002347 uint32_t index = gp_index_++;
2348 uint32_t stack_index = stack_index_++;
2349 if (index < calling_convention.GetNumberOfRegisters()) {
2350 return LocationFrom(calling_convention.GetRegisterAt(index));
2351 } else {
2352 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2353 }
2354 }
2355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002357 uint32_t index = gp_index_;
2358 uint32_t stack_index = stack_index_;
2359 gp_index_ += 2;
2360 stack_index_ += 2;
2361 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2362 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2363 // Skip R1, and use R2_R3 instead.
2364 gp_index_++;
2365 index++;
2366 }
2367 }
2368 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2369 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2370 calling_convention.GetRegisterAt(index + 1).GetCode());
2371
2372 return LocationFrom(calling_convention.GetRegisterAt(index),
2373 calling_convention.GetRegisterAt(index + 1));
2374 } else {
2375 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2376 }
2377 }
2378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002380 uint32_t stack_index = stack_index_++;
2381 if (float_index_ % 2 == 0) {
2382 float_index_ = std::max(double_index_, float_index_);
2383 }
2384 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2385 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2386 } else {
2387 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2388 }
2389 }
2390
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002391 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002392 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2393 uint32_t stack_index = stack_index_;
2394 stack_index_ += 2;
2395 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2396 uint32_t index = double_index_;
2397 double_index_ += 2;
2398 Location result = LocationFrom(
2399 calling_convention.GetFpuRegisterAt(index),
2400 calling_convention.GetFpuRegisterAt(index + 1));
2401 DCHECK(ExpectedPairLayout(result));
2402 return result;
2403 } else {
2404 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2405 }
2406 }
2407
Aart Bik66c158e2018-01-31 12:55:04 -08002408 case DataType::Type::kUint32:
2409 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002411 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002412 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002413 }
2414 return Location::NoLocation();
2415}
2416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002418 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002419 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002421 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 case DataType::Type::kInt8:
2423 case DataType::Type::kUint16:
2424 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002425 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002426 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002427 return LocationFrom(r0);
2428 }
2429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002430 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002431 return LocationFrom(s0);
2432 }
2433
Aart Bik66c158e2018-01-31 12:55:04 -08002434 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002435 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002436 return LocationFrom(r0, r1);
2437 }
2438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002440 return LocationFrom(s0, s1);
2441 }
2442
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002444 return Location::NoLocation();
2445 }
2446
2447 UNREACHABLE();
2448}
2449
2450Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2451 return LocationFrom(kMethodRegister);
2452}
2453
Vladimir Marko86c87522020-05-11 16:55:55 +01002454Location CriticalNativeCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
2455 DCHECK_NE(type, DataType::Type::kReference);
2456
2457 // Native ABI uses the same registers as managed, except that the method register r0
2458 // is a normal argument.
2459 Location location = Location::NoLocation();
2460 if (DataType::Is64BitType(type)) {
2461 gpr_index_ = RoundUp(gpr_index_, 2u);
2462 stack_offset_ = RoundUp(stack_offset_, 2 * kFramePointerSize);
2463 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2464 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u],
2465 kParameterCoreRegistersVIXL[gpr_index_]);
2466 gpr_index_ += 2u;
2467 }
2468 } else {
2469 if (gpr_index_ < 1u + kParameterCoreRegistersLengthVIXL) {
2470 location = LocationFrom(gpr_index_ == 0u ? r0 : kParameterCoreRegistersVIXL[gpr_index_ - 1u]);
2471 ++gpr_index_;
2472 }
2473 }
2474 if (location.IsInvalid()) {
2475 if (DataType::Is64BitType(type)) {
2476 location = Location::DoubleStackSlot(stack_offset_);
2477 stack_offset_ += 2 * kFramePointerSize;
2478 } else {
2479 location = Location::StackSlot(stack_offset_);
2480 stack_offset_ += kFramePointerSize;
2481 }
2482
2483 if (for_register_allocation_) {
2484 location = Location::Any();
2485 }
2486 }
2487 return location;
2488}
2489
2490Location CriticalNativeCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type)
2491 const {
2492 // We perform conversion to the managed ABI return register after the call if needed.
2493 InvokeDexCallingConventionVisitorARMVIXL dex_calling_convention;
2494 return dex_calling_convention.GetReturnLocation(type);
2495}
2496
2497Location CriticalNativeCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2498 // Pass the method in the hidden argument R4.
2499 return Location::RegisterLocation(R4);
2500}
2501
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002502void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2503 if (source.Equals(destination)) {
2504 return;
2505 }
2506 if (destination.IsRegister()) {
2507 if (source.IsRegister()) {
2508 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2509 } else if (source.IsFpuRegister()) {
2510 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2511 } else {
2512 GetAssembler()->LoadFromOffset(kLoadWord,
2513 RegisterFrom(destination),
2514 sp,
2515 source.GetStackIndex());
2516 }
2517 } else if (destination.IsFpuRegister()) {
2518 if (source.IsRegister()) {
2519 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2520 } else if (source.IsFpuRegister()) {
2521 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2522 } else {
2523 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2524 }
2525 } else {
2526 DCHECK(destination.IsStackSlot()) << destination;
2527 if (source.IsRegister()) {
2528 GetAssembler()->StoreToOffset(kStoreWord,
2529 RegisterFrom(source),
2530 sp,
2531 destination.GetStackIndex());
2532 } else if (source.IsFpuRegister()) {
2533 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2534 } else {
2535 DCHECK(source.IsStackSlot()) << source;
2536 UseScratchRegisterScope temps(GetVIXLAssembler());
2537 vixl32::Register temp = temps.Acquire();
2538 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2539 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2540 }
2541 }
2542}
2543
Artem Serovcfbe9132016-10-14 15:58:56 +01002544void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2545 DCHECK(location.IsRegister());
2546 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002547}
2548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002550 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2551 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002552 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002553 move.AddMove(src, dst, dst_type, nullptr);
2554 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002555}
2556
Artem Serovcfbe9132016-10-14 15:58:56 +01002557void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2558 if (location.IsRegister()) {
2559 locations->AddTemp(location);
2560 } else if (location.IsRegisterPair()) {
2561 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2562 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2563 } else {
2564 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2565 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002566}
2567
2568void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2569 HInstruction* instruction,
2570 uint32_t dex_pc,
2571 SlowPathCode* slow_path) {
2572 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Vladimir Markof6675082019-05-17 12:05:28 +01002573
2574 ThreadOffset32 entrypoint_offset = GetThreadOffset<kArmPointerSize>(entrypoint);
2575 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
2576 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
2577 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01002578 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01002579 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
2580 // Ensure the pc position is recorded immediately after the `blx` instruction.
2581 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
2582 ExactAssemblyScope aas(GetVIXLAssembler(),
2583 vixl32::k16BitT32InstructionSizeInBytes,
2584 CodeBufferCheckScope::kExactSize);
2585 __ blx(lr);
2586 if (EntrypointRequiresStackMap(entrypoint)) {
2587 RecordPcInfo(instruction, dex_pc, slow_path);
2588 }
2589 } else {
2590 // Ensure the pc position is recorded immediately after the `bl` instruction.
2591 ExactAssemblyScope aas(GetVIXLAssembler(),
2592 vixl32::k32BitT32InstructionSizeInBytes,
2593 CodeBufferCheckScope::kExactSize);
2594 EmitEntrypointThunkCall(entrypoint_offset);
2595 if (EntrypointRequiresStackMap(entrypoint)) {
2596 RecordPcInfo(instruction, dex_pc, slow_path);
2597 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002598 }
2599}
2600
2601void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2602 HInstruction* instruction,
2603 SlowPathCode* slow_path) {
2604 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002605 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002606 __ Blx(lr);
2607}
2608
Scott Wakelingfe885462016-09-22 10:24:38 +01002609void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002610 if (successor->IsExitBlock()) {
2611 DCHECK(got->GetPrevious()->AlwaysThrows());
2612 return; // no code needed
2613 }
2614
Scott Wakelingfe885462016-09-22 10:24:38 +01002615 HBasicBlock* block = got->GetBlock();
2616 HInstruction* previous = got->GetPrevious();
2617 HLoopInformation* info = block->GetLoopInformation();
2618
2619 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002620 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Scott Wakelingfe885462016-09-22 10:24:38 +01002621 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2622 return;
2623 }
2624 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2625 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08002626 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002627 }
2628 if (!codegen_->GoesToNextBlock(block, successor)) {
2629 __ B(codegen_->GetLabelOf(successor));
2630 }
2631}
2632
2633void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2634 got->SetLocations(nullptr);
2635}
2636
2637void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2638 HandleGoto(got, got->GetSuccessor());
2639}
2640
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002641void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2642 try_boundary->SetLocations(nullptr);
2643}
2644
2645void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2646 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2647 if (!successor->IsExitBlock()) {
2648 HandleGoto(try_boundary, successor);
2649 }
2650}
2651
Scott Wakelingfe885462016-09-22 10:24:38 +01002652void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2653 exit->SetLocations(nullptr);
2654}
2655
2656void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2657}
2658
Scott Wakelingfe885462016-09-22 10:24:38 +01002659void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002660 vixl32::Label* true_target,
2661 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002662 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002663 if (true_target == false_target) {
2664 DCHECK(true_target != nullptr);
2665 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002666 return;
2667 }
2668
Anton Kirilov23b752b2017-07-20 14:40:44 +01002669 vixl32::Label* non_fallthrough_target;
2670 bool invert;
2671 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002672
Anton Kirilov23b752b2017-07-20 14:40:44 +01002673 if (true_target == nullptr) {
2674 // The true target is fallthrough.
2675 DCHECK(false_target != nullptr);
2676 non_fallthrough_target = false_target;
2677 invert = true;
2678 emit_both_branches = false;
2679 } else {
2680 non_fallthrough_target = true_target;
2681 invert = false;
2682 // Either the false target is fallthrough, or there is no fallthrough
2683 // and both branches must be emitted.
2684 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002685 }
2686
Anton Kirilov23b752b2017-07-20 14:40:44 +01002687 const auto cond = GenerateTest(condition, invert, codegen_);
2688
2689 __ B(cond.first, non_fallthrough_target, is_far_target);
2690
2691 if (emit_both_branches) {
2692 // No target falls through, we need to branch.
2693 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002694 }
2695}
2696
2697void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2698 size_t condition_input_index,
2699 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002700 vixl32::Label* false_target,
2701 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002702 HInstruction* cond = instruction->InputAt(condition_input_index);
2703
2704 if (true_target == nullptr && false_target == nullptr) {
2705 // Nothing to do. The code always falls through.
2706 return;
2707 } else if (cond->IsIntConstant()) {
2708 // Constant condition, statically compared against "true" (integer value 1).
2709 if (cond->AsIntConstant()->IsTrue()) {
2710 if (true_target != nullptr) {
2711 __ B(true_target);
2712 }
2713 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002714 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002715 if (false_target != nullptr) {
2716 __ B(false_target);
2717 }
2718 }
2719 return;
2720 }
2721
2722 // The following code generates these patterns:
2723 // (1) true_target == nullptr && false_target != nullptr
2724 // - opposite condition true => branch to false_target
2725 // (2) true_target != nullptr && false_target == nullptr
2726 // - condition true => branch to true_target
2727 // (3) true_target != nullptr && false_target != nullptr
2728 // - condition true => branch to true_target
2729 // - branch to false_target
2730 if (IsBooleanValueOrMaterializedCondition(cond)) {
2731 // Condition has been materialized, compare the output to 0.
2732 if (kIsDebugBuild) {
2733 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2734 DCHECK(cond_val.IsRegister());
2735 }
2736 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002737 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2738 false_target,
2739 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002740 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002741 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2742 true_target,
2743 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002744 }
2745 } else {
2746 // Condition has not been materialized. Use its inputs as the comparison and
2747 // its condition as the branch condition.
2748 HCondition* condition = cond->AsCondition();
2749
2750 // If this is a long or FP comparison that has been folded into
2751 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 DataType::Type type = condition->InputAt(0)->GetType();
2753 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002754 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002755 return;
2756 }
2757
Donghui Bai426b49c2016-11-08 14:55:38 +08002758 vixl32::Label* non_fallthrough_target;
2759 vixl32::Condition arm_cond = vixl32::Condition::None();
2760 const vixl32::Register left = InputRegisterAt(cond, 0);
2761 const Operand right = InputOperandAt(cond, 1);
2762
Scott Wakelingfe885462016-09-22 10:24:38 +01002763 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002764 arm_cond = ARMCondition(condition->GetOppositeCondition());
2765 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002766 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002767 arm_cond = ARMCondition(condition->GetCondition());
2768 non_fallthrough_target = true_target;
2769 }
2770
2771 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2772 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002773 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002774 } else {
2775 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002776 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002777 }
2778 } else {
2779 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002780 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002781 }
2782 }
2783
2784 // If neither branch falls through (case 3), the conditional branch to `true_target`
2785 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2786 if (true_target != nullptr && false_target != nullptr) {
2787 __ B(false_target);
2788 }
2789}
2790
2791void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002792 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002793 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2794 locations->SetInAt(0, Location::RequiresRegister());
2795 }
2796}
2797
2798void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2799 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2800 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002801 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2802 nullptr : codegen_->GetLabelOf(true_successor);
2803 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2804 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002805 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002806}
2807
Scott Wakelingc34dba72016-10-03 10:14:44 +01002808void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002809 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002810 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002811 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2812 RegisterSet caller_saves = RegisterSet::Empty();
2813 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2814 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002815 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2816 locations->SetInAt(0, Location::RequiresRegister());
2817 }
2818}
2819
2820void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2821 SlowPathCodeARMVIXL* slow_path =
2822 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2823 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002824 /* condition_input_index= */ 0,
Scott Wakelingc34dba72016-10-03 10:14:44 +01002825 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002826 /* false_target= */ nullptr);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002827}
2828
Artem Serovd4cc5b22016-11-04 11:19:09 +00002829void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002830 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002831 LocationSummary(flag, LocationSummary::kNoCall);
2832 locations->SetOut(Location::RequiresRegister());
2833}
2834
2835void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2836 GetAssembler()->LoadFromOffset(kLoadWord,
2837 OutputRegister(flag),
2838 sp,
2839 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2840}
2841
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002842void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002843 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002845
2846 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002847 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002848 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002849 } else {
2850 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002851 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002852 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002853
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002854 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002855 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2856 // The code generator handles overlap with the values, but not with the condition.
2857 locations->SetOut(Location::SameAsFirstInput());
2858 } else if (is_floating_point) {
2859 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2860 } else {
2861 if (!locations->InAt(1).IsConstant()) {
2862 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2863 }
2864
2865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002866 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002867}
2868
2869void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002870 HInstruction* const condition = select->GetCondition();
2871 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002873 const Location first = locations->InAt(0);
2874 const Location out = locations->Out();
2875 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002876
2877 // In the unlucky case the output of this instruction overlaps
2878 // with an input of an "emitted-at-use-site" condition, and
2879 // the output of this instruction is not one of its inputs, we'll
2880 // need to fallback to branches instead of conditional ARM instructions.
2881 bool output_overlaps_with_condition_inputs =
2882 !IsBooleanValueOrMaterializedCondition(condition) &&
2883 !out.Equals(first) &&
2884 !out.Equals(second) &&
2885 (condition->GetLocations()->InAt(0).Equals(out) ||
2886 condition->GetLocations()->InAt(1).Equals(out));
2887 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002888 Location src;
2889
2890 if (condition->IsIntConstant()) {
2891 if (condition->AsIntConstant()->IsFalse()) {
2892 src = first;
2893 } else {
2894 src = second;
2895 }
2896
2897 codegen_->MoveLocation(out, src, type);
2898 return;
2899 }
2900
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002901 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002902 bool invert = false;
2903
2904 if (out.Equals(second)) {
2905 src = first;
2906 invert = true;
2907 } else if (out.Equals(first)) {
2908 src = second;
2909 } else if (second.IsConstant()) {
2910 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2911 src = second;
2912 } else if (first.IsConstant()) {
2913 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2914 src = first;
2915 invert = true;
2916 } else {
2917 src = second;
2918 }
2919
2920 if (CanGenerateConditionalMove(out, src)) {
2921 if (!out.Equals(first) && !out.Equals(second)) {
2922 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2923 }
2924
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002925 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2926
2927 if (IsBooleanValueOrMaterializedCondition(condition)) {
2928 __ Cmp(InputRegisterAt(select, 2), 0);
2929 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2930 } else {
2931 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2932 }
2933
Donghui Bai426b49c2016-11-08 14:55:38 +08002934 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002935 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002936 ExactAssemblyScope guard(GetVIXLAssembler(),
2937 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2938 CodeBufferCheckScope::kExactSize);
2939
2940 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002941 __ it(cond.first);
2942 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002943 } else {
2944 DCHECK(out.IsRegisterPair());
2945
2946 Operand operand_high(0);
2947 Operand operand_low(0);
2948
2949 if (src.IsConstant()) {
2950 const int64_t value = Int64ConstantFrom(src);
2951
2952 operand_high = High32Bits(value);
2953 operand_low = Low32Bits(value);
2954 } else {
2955 DCHECK(src.IsRegisterPair());
2956 operand_high = HighRegisterFrom(src);
2957 operand_low = LowRegisterFrom(src);
2958 }
2959
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002960 __ it(cond.first);
2961 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2962 __ it(cond.first);
2963 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002964 }
2965
2966 return;
2967 }
2968 }
2969
2970 vixl32::Label* false_target = nullptr;
2971 vixl32::Label* true_target = nullptr;
2972 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002973 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08002974 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2975
2976 if (out.Equals(second)) {
2977 true_target = target;
2978 src = first;
2979 } else {
2980 false_target = target;
2981 src = second;
2982
2983 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002984 if (output_overlaps_with_condition_inputs) {
2985 false_target = &other_case;
2986 } else {
2987 codegen_->MoveLocation(out, first, type);
2988 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002989 }
2990 }
2991
Andreas Gampe3db70682018-12-26 15:12:03 -08002992 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target= */ false);
Donghui Bai426b49c2016-11-08 14:55:38 +08002993 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002994 if (output_overlaps_with_condition_inputs) {
2995 __ B(target);
2996 __ Bind(&other_case);
2997 codegen_->MoveLocation(out, first, type);
2998 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002999
3000 if (select_end.IsReferenced()) {
3001 __ Bind(&select_end);
3002 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003003}
3004
Artem Serov551b28f2016-10-18 19:11:30 +01003005void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003006 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01003007}
3008
3009void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
3010 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
3011}
3012
Vladimir Markodec78172020-06-19 15:31:23 +01003013void CodeGeneratorARMVIXL::IncreaseFrame(size_t adjustment) {
3014 __ Claim(adjustment);
3015 GetAssembler()->cfi().AdjustCFAOffset(adjustment);
3016}
3017
3018void CodeGeneratorARMVIXL::DecreaseFrame(size_t adjustment) {
3019 __ Drop(adjustment);
3020 GetAssembler()->cfi().AdjustCFAOffset(-adjustment);
3021}
3022
Scott Wakelingfe885462016-09-22 10:24:38 +01003023void CodeGeneratorARMVIXL::GenerateNop() {
3024 __ Nop();
3025}
3026
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003027// `temp` is an extra temporary register that is used for some conditions;
3028// callers may not specify it, in which case the method will use a scratch
3029// register instead.
3030void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
3031 vixl32::Register out,
3032 vixl32::Register in,
3033 vixl32::Register temp) {
3034 switch (condition) {
3035 case kCondEQ:
3036 // x <= 0 iff x == 0 when the comparison is unsigned.
3037 case kCondBE:
3038 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
3039 temp = out;
3040 }
3041
3042 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3043 // different as well.
3044 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
3045 // temp = - in; only 0 sets the carry flag.
3046 __ Rsbs(temp, in, 0);
3047
3048 if (out.Is(in)) {
3049 std::swap(in, temp);
3050 }
3051
3052 // out = - in + in + carry = carry
3053 __ Adc(out, temp, in);
3054 } else {
3055 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3056 __ Clz(out, in);
3057 // Any number less than 32 logically shifted right by 5 bits results in 0;
3058 // the same operation on 32 yields 1.
3059 __ Lsr(out, out, 5);
3060 }
3061
3062 break;
3063 case kCondNE:
3064 // x > 0 iff x != 0 when the comparison is unsigned.
3065 case kCondA: {
3066 UseScratchRegisterScope temps(GetVIXLAssembler());
3067
3068 if (out.Is(in)) {
3069 if (!temp.IsValid() || in.Is(temp)) {
3070 temp = temps.Acquire();
3071 }
3072 } else if (!temp.IsValid() || !temp.IsLow()) {
3073 temp = out;
3074 }
3075
3076 // temp = in - 1; only 0 does not set the carry flag.
3077 __ Subs(temp, in, 1);
3078 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3079 __ Sbc(out, in, temp);
3080 break;
3081 }
3082 case kCondGE:
3083 __ Mvn(out, in);
3084 in = out;
3085 FALLTHROUGH_INTENDED;
3086 case kCondLT:
3087 // We only care about the sign bit.
3088 __ Lsr(out, in, 31);
3089 break;
3090 case kCondAE:
3091 // Trivially true.
3092 __ Mov(out, 1);
3093 break;
3094 case kCondB:
3095 // Trivially false.
3096 __ Mov(out, 0);
3097 break;
3098 default:
3099 LOG(FATAL) << "Unexpected condition " << condition;
3100 UNREACHABLE();
3101 }
3102}
3103
Scott Wakelingfe885462016-09-22 10:24:38 +01003104void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3105 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003106 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003107 const DataType::Type type = cond->InputAt(0)->GetType();
3108 if (DataType::IsFloatingPointType(type)) {
3109 locations->SetInAt(0, Location::RequiresFpuRegister());
3110 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
3111 } else {
3112 locations->SetInAt(0, Location::RequiresRegister());
3113 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3114 }
3115 if (!cond->IsEmittedAtUseSite()) {
3116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003117 }
3118}
3119
3120void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3121 if (cond->IsEmittedAtUseSite()) {
3122 return;
3123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003128 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003129 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003130 }
3131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003133
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003134 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003135
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003136 // A condition with only one boolean input, or two boolean inputs without being equality or
3137 // inequality results from transformations done by the instruction simplifier, and is handled
3138 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 if (type == DataType::Type::kBool &&
3140 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003141 (condition == kCondEQ || condition == kCondNE)) {
3142 vixl32::Register left = InputRegisterAt(cond, 0);
3143 const vixl32::Register out = OutputRegister(cond);
3144 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003145
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003146 // The constant case is handled by the instruction simplifier.
3147 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003148
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003149 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003150
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003151 // Avoid 32-bit instructions if possible.
3152 if (out.Is(right)) {
3153 std::swap(left, right);
3154 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003155
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003156 __ Eor(out, left, right);
3157
3158 if (condition == kCondEQ) {
3159 __ Eor(out, out, 1);
3160 }
3161
3162 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003163 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003164
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003165 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003166}
3167
3168void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3169 HandleCondition(comp);
3170}
3171
3172void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3173 HandleCondition(comp);
3174}
3175
3176void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3177 HandleCondition(comp);
3178}
3179
3180void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3181 HandleCondition(comp);
3182}
3183
3184void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3185 HandleCondition(comp);
3186}
3187
3188void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3189 HandleCondition(comp);
3190}
3191
3192void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3193 HandleCondition(comp);
3194}
3195
3196void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3197 HandleCondition(comp);
3198}
3199
3200void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3201 HandleCondition(comp);
3202}
3203
3204void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3205 HandleCondition(comp);
3206}
3207
3208void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3209 HandleCondition(comp);
3210}
3211
3212void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3213 HandleCondition(comp);
3214}
3215
3216void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3217 HandleCondition(comp);
3218}
3219
3220void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3221 HandleCondition(comp);
3222}
3223
3224void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3225 HandleCondition(comp);
3226}
3227
3228void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3229 HandleCondition(comp);
3230}
3231
3232void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3233 HandleCondition(comp);
3234}
3235
3236void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3237 HandleCondition(comp);
3238}
3239
3240void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3241 HandleCondition(comp);
3242}
3243
3244void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3245 HandleCondition(comp);
3246}
3247
3248void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3249 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003250 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003251 locations->SetOut(Location::ConstantLocation(constant));
3252}
3253
3254void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3255 // Will be generated at use site.
3256}
3257
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003258void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3259 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003260 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003261 locations->SetOut(Location::ConstantLocation(constant));
3262}
3263
3264void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3265 // Will be generated at use site.
3266}
3267
Scott Wakelingfe885462016-09-22 10:24:38 +01003268void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3269 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003270 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003271 locations->SetOut(Location::ConstantLocation(constant));
3272}
3273
3274void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3275 // Will be generated at use site.
3276}
3277
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003278void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3279 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003280 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003281 locations->SetOut(Location::ConstantLocation(constant));
3282}
3283
Scott Wakelingc34dba72016-10-03 10:14:44 +01003284void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3285 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003286 // Will be generated at use site.
3287}
3288
3289void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3290 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003291 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003292 locations->SetOut(Location::ConstantLocation(constant));
3293}
3294
Scott Wakelingc34dba72016-10-03 10:14:44 +01003295void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3296 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003297 // Will be generated at use site.
3298}
3299
Igor Murashkind01745e2017-04-05 16:40:31 -07003300void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3301 constructor_fence->SetLocations(nullptr);
3302}
3303
3304void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3305 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3306 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3307}
3308
Scott Wakelingfe885462016-09-22 10:24:38 +01003309void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3310 memory_barrier->SetLocations(nullptr);
3311}
3312
3313void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3314 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3315}
3316
3317void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3318 ret->SetLocations(nullptr);
3319}
3320
3321void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3322 codegen_->GenerateFrameExit();
3323}
3324
3325void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3326 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003327 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003328 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3329}
3330
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00003331void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret) {
3332 if (GetGraph()->IsCompilingOsr()) {
3333 // To simplify callers of an OSR method, we put the return value in both
3334 // floating point and core registers.
3335 switch (ret->InputAt(0)->GetType()) {
3336 case DataType::Type::kFloat32:
3337 __ Vmov(r0, s0);
3338 break;
3339 case DataType::Type::kFloat64:
3340 __ Vmov(r0, r1, d0);
3341 break;
3342 default:
3343 break;
3344 }
3345 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003346 codegen_->GenerateFrameExit();
3347}
3348
Artem Serovcfbe9132016-10-14 15:58:56 +01003349void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3350 // The trampoline uses the same calling convention as dex calling conventions,
3351 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3352 // the method_idx.
3353 HandleInvoke(invoke);
3354}
3355
3356void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3357 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003358 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003359}
3360
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003361void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3362 // Explicit clinit checks triggered by static invokes must have been pruned by
3363 // art::PrepareForRegisterAllocation.
3364 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3365
Anton Kirilov5ec62182016-10-13 20:16:02 +01003366 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3367 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003368 return;
3369 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003370
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01003371 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01003372 CriticalNativeCallingConventionVisitorARMVIXL calling_convention_visitor(
3373 /*for_register_allocation=*/ true);
3374 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3375 } else {
3376 HandleInvoke(invoke);
3377 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003378}
3379
Anton Kirilov5ec62182016-10-13 20:16:02 +01003380static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3381 if (invoke->GetLocations()->Intrinsified()) {
3382 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3383 intrinsic.Dispatch(invoke);
3384 return true;
3385 }
3386 return false;
3387}
3388
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003389void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3390 // Explicit clinit checks triggered by static invokes must have been pruned by
3391 // art::PrepareForRegisterAllocation.
3392 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3393
Anton Kirilov5ec62182016-10-13 20:16:02 +01003394 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003395 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003396 return;
3397 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003398
3399 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003400 codegen_->GenerateStaticOrDirectCall(
3401 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003402
Andreas Gampe3db70682018-12-26 15:12:03 -08003403 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003404}
3405
3406void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003407 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003408 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3409}
3410
3411void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003412 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3413 if (intrinsic.TryDispatch(invoke)) {
3414 return;
3415 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003416
3417 HandleInvoke(invoke);
3418}
3419
3420void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003421 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003422 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003423 return;
3424 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003425
3426 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003427 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003428
Andreas Gampe3db70682018-12-26 15:12:03 -08003429 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003430}
3431
Artem Serovcfbe9132016-10-14 15:58:56 +01003432void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3433 HandleInvoke(invoke);
3434 // Add the hidden argument.
3435 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3436}
3437
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003438void CodeGeneratorARMVIXL::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
3439 vixl32::Register klass) {
3440 DCHECK_EQ(r0.GetCode(), klass.GetCode());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003441 // We know the destination of an intrinsic, so no need to record inline
3442 // caches.
3443 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00003444 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003445 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003446 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01003447 ScopedProfilingInfoUse spiu(
3448 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
3449 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00003450 if (info != nullptr) {
3451 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
3452 uint32_t address = reinterpret_cast32<uint32_t>(cache);
3453 vixl32::Label done;
3454 UseScratchRegisterScope temps(GetVIXLAssembler());
3455 temps.Exclude(ip);
3456 __ Mov(r4, address);
3457 __ Ldr(ip, MemOperand(r4, InlineCache::ClassesOffset().Int32Value()));
3458 // Fast path for a monomorphic cache.
3459 __ Cmp(klass, ip);
3460 __ B(eq, &done, /* is_far_target= */ false);
3461 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
3462 __ Bind(&done);
3463 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003464 }
3465}
3466
Artem Serovcfbe9132016-10-14 15:58:56 +01003467void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3468 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3469 LocationSummary* locations = invoke->GetLocations();
3470 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3471 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3472 Location receiver = locations->InAt(0);
3473 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3474
3475 DCHECK(!receiver.IsStackSlot());
3476
Alexandre Rames374ddf32016-11-04 10:40:49 +00003477 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3478 {
Artem Serov0fb37192016-12-06 18:13:40 +00003479 ExactAssemblyScope aas(GetVIXLAssembler(),
3480 vixl32::kMaxInstructionSizeInBytes,
3481 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003482 // /* HeapReference<Class> */ temp = receiver->klass_
3483 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3484 codegen_->MaybeRecordImplicitNullCheck(invoke);
3485 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003486 // Instead of simply (possibly) unpoisoning `temp` here, we should
3487 // emit a read barrier for the previous class reference load.
3488 // However this is not required in practice, as this is an
3489 // intermediate/temporary reference and because the current
3490 // concurrent copying collector keeps the from-space memory
3491 // intact/accessible until the end of the marking phase (the
3492 // concurrent copying collector may not in the future).
3493 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003494
3495 // If we're compiling baseline, update the inline cache.
3496 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
3497
Artem Serovcfbe9132016-10-14 15:58:56 +01003498 GetAssembler()->LoadFromOffset(kLoadWord,
3499 temp,
3500 temp,
3501 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003502
Artem Serovcfbe9132016-10-14 15:58:56 +01003503 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3504 invoke->GetImtIndex(), kArmPointerSize));
3505 // temp = temp->GetImtEntryAt(method_offset);
3506 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3507 uint32_t entry_point =
3508 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3509 // LR = temp->GetEntryPoint();
3510 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3511
3512 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3513 // instruction from clobbering it as they might use r12 as a scratch register.
3514 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003515
3516 {
3517 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3518 // so it checks if the application is using them (by passing them to the macro assembler
3519 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3520 // what is available, and is the opposite of the standard usage: Instead of requesting a
3521 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3522 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3523 // (to materialize the constant), since the destination register becomes available for such use
3524 // internally for the duration of the macro instruction.
3525 UseScratchRegisterScope temps(GetVIXLAssembler());
3526 temps.Exclude(hidden_reg);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01003527 __ Mov(hidden_reg, invoke->GetMethodReference().index);
Scott Wakelingb77051e2016-11-21 19:46:00 +00003528 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003529 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003530 // Ensure the pc position is recorded immediately after the `blx` instruction.
3531 // 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 +00003532 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003533 vixl32::k16BitT32InstructionSizeInBytes,
3534 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003535 // LR();
3536 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003537 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003538 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003539 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003540
Andreas Gampe3db70682018-12-26 15:12:03 -08003541 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003542}
3543
Orion Hodsonac141392017-01-13 11:53:47 +00003544void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003545 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3546 if (intrinsic.TryDispatch(invoke)) {
3547 return;
3548 }
Orion Hodsonac141392017-01-13 11:53:47 +00003549 HandleInvoke(invoke);
3550}
3551
3552void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00003553 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3554 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 9);
3555 return;
3556 }
Orion Hodsonac141392017-01-13 11:53:47 +00003557 codegen_->GenerateInvokePolymorphicCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003558 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 10);
Orion Hodsonac141392017-01-13 11:53:47 +00003559}
3560
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003561void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3562 HandleInvoke(invoke);
3563}
3564
3565void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3566 codegen_->GenerateInvokeCustomCall(invoke);
Andra Danciua0130e82020-07-23 12:34:56 +00003567 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 11);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003568}
3569
Artem Serov02109dd2016-09-23 17:17:54 +01003570void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3571 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003572 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003573 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003574 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003575 locations->SetInAt(0, Location::RequiresRegister());
3576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3577 break;
3578 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003579 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003580 locations->SetInAt(0, Location::RequiresRegister());
3581 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3582 break;
3583 }
3584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 case DataType::Type::kFloat32:
3586 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003587 locations->SetInAt(0, Location::RequiresFpuRegister());
3588 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3589 break;
3590
3591 default:
3592 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3593 }
3594}
3595
3596void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3597 LocationSummary* locations = neg->GetLocations();
3598 Location out = locations->Out();
3599 Location in = locations->InAt(0);
3600 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003601 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003602 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3603 break;
3604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003605 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003606 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3607 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3608 // We cannot emit an RSC (Reverse Subtract with Carry)
3609 // instruction here, as it does not exist in the Thumb-2
3610 // instruction set. We use the following approach
3611 // using SBC and SUB instead.
3612 //
3613 // out.hi = -C
3614 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3615 // out.hi = out.hi - in.hi
3616 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3617 break;
3618
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003619 case DataType::Type::kFloat32:
3620 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003621 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003622 break;
3623
3624 default:
3625 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3626 }
3627}
3628
Scott Wakelingfe885462016-09-22 10:24:38 +01003629void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003630 DataType::Type result_type = conversion->GetResultType();
3631 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003632 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3633 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003634
3635 // The float-to-long, double-to-long and long-to-float type conversions
3636 // rely on a call to the runtime.
3637 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003638 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3639 && result_type == DataType::Type::kInt64)
3640 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003641 ? LocationSummary::kCallOnMainOnly
3642 : LocationSummary::kNoCall;
3643 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003644 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003645
Scott Wakelingfe885462016-09-22 10:24:38 +01003646 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003647 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003648 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003649 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003650 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003651 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3652 locations->SetInAt(0, Location::RequiresRegister());
3653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003654 break;
3655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003657 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003659 locations->SetInAt(0, Location::Any());
3660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3661 break;
3662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003664 locations->SetInAt(0, Location::RequiresFpuRegister());
3665 locations->SetOut(Location::RequiresRegister());
3666 locations->AddTemp(Location::RequiresFpuRegister());
3667 break;
3668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003669 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003670 locations->SetInAt(0, Location::RequiresFpuRegister());
3671 locations->SetOut(Location::RequiresRegister());
3672 locations->AddTemp(Location::RequiresFpuRegister());
3673 break;
3674
3675 default:
3676 LOG(FATAL) << "Unexpected type conversion from " << input_type
3677 << " to " << result_type;
3678 }
3679 break;
3680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003681 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003682 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003683 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003684 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003685 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003686 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 case DataType::Type::kInt16:
3688 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003689 locations->SetInAt(0, Location::RequiresRegister());
3690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3691 break;
3692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003694 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3695 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3696 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003697 break;
3698 }
3699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003700 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003701 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3702 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3703 calling_convention.GetFpuRegisterAt(1)));
3704 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003705 break;
3706 }
3707
3708 default:
3709 LOG(FATAL) << "Unexpected type conversion from " << input_type
3710 << " to " << result_type;
3711 }
3712 break;
3713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003715 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003716 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003717 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003719 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003720 case DataType::Type::kInt16:
3721 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003722 locations->SetInAt(0, Location::RequiresRegister());
3723 locations->SetOut(Location::RequiresFpuRegister());
3724 break;
3725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003727 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3728 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3729 calling_convention.GetRegisterAt(1)));
3730 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003731 break;
3732 }
3733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003734 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003735 locations->SetInAt(0, Location::RequiresFpuRegister());
3736 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3737 break;
3738
3739 default:
3740 LOG(FATAL) << "Unexpected type conversion from " << input_type
3741 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003742 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003743 break;
3744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003745 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003746 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003747 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003748 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003749 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003750 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003751 case DataType::Type::kInt16:
3752 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003753 locations->SetInAt(0, Location::RequiresRegister());
3754 locations->SetOut(Location::RequiresFpuRegister());
3755 break;
3756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 locations->SetInAt(0, Location::RequiresRegister());
3759 locations->SetOut(Location::RequiresFpuRegister());
3760 locations->AddTemp(Location::RequiresFpuRegister());
3761 locations->AddTemp(Location::RequiresFpuRegister());
3762 break;
3763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003765 locations->SetInAt(0, Location::RequiresFpuRegister());
3766 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3767 break;
3768
3769 default:
3770 LOG(FATAL) << "Unexpected type conversion from " << input_type
3771 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003772 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003773 break;
3774
3775 default:
3776 LOG(FATAL) << "Unexpected type conversion from " << input_type
3777 << " to " << result_type;
3778 }
3779}
3780
3781void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3782 LocationSummary* locations = conversion->GetLocations();
3783 Location out = locations->Out();
3784 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003785 DataType::Type result_type = conversion->GetResultType();
3786 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003787 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3788 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003789 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003790 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003791 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003792 case DataType::Type::kInt8:
3793 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 case DataType::Type::kInt16:
3795 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003796 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3797 break;
3798 case DataType::Type::kInt64:
3799 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3800 break;
3801
3802 default:
3803 LOG(FATAL) << "Unexpected type conversion from " << input_type
3804 << " to " << result_type;
3805 }
3806 break;
3807
3808 case DataType::Type::kInt8:
3809 switch (input_type) {
3810 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003812 case DataType::Type::kInt16:
3813 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003814 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3815 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003816 case DataType::Type::kInt64:
3817 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3818 break;
3819
3820 default:
3821 LOG(FATAL) << "Unexpected type conversion from " << input_type
3822 << " to " << result_type;
3823 }
3824 break;
3825
3826 case DataType::Type::kUint16:
3827 switch (input_type) {
3828 case DataType::Type::kInt8:
3829 case DataType::Type::kInt16:
3830 case DataType::Type::kInt32:
3831 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3832 break;
3833 case DataType::Type::kInt64:
3834 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3835 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003836
3837 default:
3838 LOG(FATAL) << "Unexpected type conversion from " << input_type
3839 << " to " << result_type;
3840 }
3841 break;
3842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003843 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003844 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003845 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003846 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003847 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3848 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003849 case DataType::Type::kInt64:
3850 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3851 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003852
3853 default:
3854 LOG(FATAL) << "Unexpected type conversion from " << input_type
3855 << " to " << result_type;
3856 }
3857 break;
3858
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003859 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003860 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003861 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003862 DCHECK(out.IsRegister());
3863 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003864 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003865 } else if (in.IsDoubleStackSlot()) {
3866 GetAssembler()->LoadFromOffset(kLoadWord,
3867 OutputRegister(conversion),
3868 sp,
3869 in.GetStackIndex());
3870 } else {
3871 DCHECK(in.IsConstant());
3872 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003873 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3874 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003875 }
3876 break;
3877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003878 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003879 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003880 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003881 __ Vmov(OutputRegister(conversion), temp);
3882 break;
3883 }
3884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003885 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003886 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003887 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003888 __ Vmov(OutputRegister(conversion), temp_s);
3889 break;
3890 }
3891
3892 default:
3893 LOG(FATAL) << "Unexpected type conversion from " << input_type
3894 << " to " << result_type;
3895 }
3896 break;
3897
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003898 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003899 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003900 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003901 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003902 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003903 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003904 case DataType::Type::kInt16:
3905 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003906 DCHECK(out.IsRegisterPair());
3907 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003908 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003909 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003910 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003911 break;
3912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003913 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003914 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3915 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3916 break;
3917
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003918 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003919 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3920 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3921 break;
3922
3923 default:
3924 LOG(FATAL) << "Unexpected type conversion from " << input_type
3925 << " to " << result_type;
3926 }
3927 break;
3928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003929 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003930 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003931 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003932 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003933 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003934 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003935 case DataType::Type::kInt16:
3936 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003937 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003938 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003939 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003940
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003941 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003942 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3943 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3944 break;
3945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003946 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003947 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003948 break;
3949
3950 default:
3951 LOG(FATAL) << "Unexpected type conversion from " << input_type
3952 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003953 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003954 break;
3955
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003956 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003957 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003958 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003959 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003961 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 case DataType::Type::kInt16:
3963 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003964 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003965 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003966 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003967
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003968 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003969 vixl32::Register low = LowRegisterFrom(in);
3970 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003971 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003972 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003973 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003974 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003975 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003976
3977 // temp_d = int-to-double(high)
3978 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003979 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003980 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003981 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003982 // out_d = unsigned-to-double(low)
3983 __ Vmov(out_s, low);
3984 __ Vcvt(F64, U32, out_d, out_s);
3985 // out_d += temp_d * constant_d
3986 __ Vmla(F64, out_d, temp_d, constant_d);
3987 break;
3988 }
3989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003990 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003991 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003992 break;
3993
3994 default:
3995 LOG(FATAL) << "Unexpected type conversion from " << input_type
3996 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003997 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003998 break;
3999
4000 default:
4001 LOG(FATAL) << "Unexpected type conversion from " << input_type
4002 << " to " << result_type;
4003 }
4004}
4005
4006void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
4007 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004008 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004009 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004010 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004011 locations->SetInAt(0, Location::RequiresRegister());
4012 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
4013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4014 break;
4015 }
4016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004018 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004019 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01004020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4021 break;
4022 }
4023
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004024 case DataType::Type::kFloat32:
4025 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004026 locations->SetInAt(0, Location::RequiresFpuRegister());
4027 locations->SetInAt(1, Location::RequiresFpuRegister());
4028 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4029 break;
4030 }
4031
4032 default:
4033 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4034 }
4035}
4036
4037void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
4038 LocationSummary* locations = add->GetLocations();
4039 Location out = locations->Out();
4040 Location first = locations->InAt(0);
4041 Location second = locations->InAt(1);
4042
4043 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004044 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004045 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
4046 }
4047 break;
4048
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004049 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004050 if (second.IsConstant()) {
4051 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4052 GenerateAddLongConst(out, first, value);
4053 } else {
4054 DCHECK(second.IsRegisterPair());
4055 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4056 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4057 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004058 break;
4059 }
4060
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004061 case DataType::Type::kFloat32:
4062 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004063 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004064 break;
4065
4066 default:
4067 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
4068 }
4069}
4070
4071void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
4072 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004073 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004074 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004076 locations->SetInAt(0, Location::RequiresRegister());
4077 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
4078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4079 break;
4080 }
4081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004083 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00004084 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01004085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4086 break;
4087 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004088 case DataType::Type::kFloat32:
4089 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004090 locations->SetInAt(0, Location::RequiresFpuRegister());
4091 locations->SetInAt(1, Location::RequiresFpuRegister());
4092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4093 break;
4094 }
4095 default:
4096 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4097 }
4098}
4099
4100void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4101 LocationSummary* locations = sub->GetLocations();
4102 Location out = locations->Out();
4103 Location first = locations->InAt(0);
4104 Location second = locations->InAt(1);
4105 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004106 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004107 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004108 break;
4109 }
4110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004112 if (second.IsConstant()) {
4113 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4114 GenerateAddLongConst(out, first, -value);
4115 } else {
4116 DCHECK(second.IsRegisterPair());
4117 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4118 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4119 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004120 break;
4121 }
4122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004123 case DataType::Type::kFloat32:
4124 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004125 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004126 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004127
4128 default:
4129 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4130 }
4131}
4132
4133void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4134 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004135 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004136 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004137 case DataType::Type::kInt32:
4138 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004139 locations->SetInAt(0, Location::RequiresRegister());
4140 locations->SetInAt(1, Location::RequiresRegister());
4141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4142 break;
4143 }
4144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004145 case DataType::Type::kFloat32:
4146 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004147 locations->SetInAt(0, Location::RequiresFpuRegister());
4148 locations->SetInAt(1, Location::RequiresFpuRegister());
4149 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4150 break;
4151 }
4152
4153 default:
4154 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4155 }
4156}
4157
4158void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4159 LocationSummary* locations = mul->GetLocations();
4160 Location out = locations->Out();
4161 Location first = locations->InAt(0);
4162 Location second = locations->InAt(1);
4163 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004164 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004165 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4166 break;
4167 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004168 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004169 vixl32::Register out_hi = HighRegisterFrom(out);
4170 vixl32::Register out_lo = LowRegisterFrom(out);
4171 vixl32::Register in1_hi = HighRegisterFrom(first);
4172 vixl32::Register in1_lo = LowRegisterFrom(first);
4173 vixl32::Register in2_hi = HighRegisterFrom(second);
4174 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004175
4176 // Extra checks to protect caused by the existence of R1_R2.
4177 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4178 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004179 DCHECK(!out_hi.Is(in1_lo));
4180 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004181
4182 // input: in1 - 64 bits, in2 - 64 bits
4183 // output: out
4184 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4185 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4186 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4187
4188 UseScratchRegisterScope temps(GetVIXLAssembler());
4189 vixl32::Register temp = temps.Acquire();
4190 // temp <- in1.lo * in2.hi
4191 __ Mul(temp, in1_lo, in2_hi);
4192 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4193 __ Mla(out_hi, in1_hi, in2_lo, temp);
4194 // out.lo <- (in1.lo * in2.lo)[31:0];
4195 __ Umull(out_lo, temp, in1_lo, in2_lo);
4196 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004197 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004198 break;
4199 }
4200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004201 case DataType::Type::kFloat32:
4202 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004203 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004204 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004205
4206 default:
4207 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4208 }
4209}
4210
Scott Wakelingfe885462016-09-22 10:24:38 +01004211void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4212 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004214
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004215 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004216 DCHECK(second.IsConstant());
4217
4218 vixl32::Register out = OutputRegister(instruction);
4219 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004220 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004221 DCHECK(imm == 1 || imm == -1);
4222
4223 if (instruction->IsRem()) {
4224 __ Mov(out, 0);
4225 } else {
4226 if (imm == 1) {
4227 __ Mov(out, dividend);
4228 } else {
4229 __ Rsb(out, dividend, 0);
4230 }
4231 }
4232}
4233
4234void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4235 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004236 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004237
4238 LocationSummary* locations = instruction->GetLocations();
4239 Location second = locations->InAt(1);
4240 DCHECK(second.IsConstant());
4241
4242 vixl32::Register out = OutputRegister(instruction);
4243 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004244 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004245 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4246 int ctz_imm = CTZ(abs_imm);
4247
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004248 auto generate_div_code = [this, imm, ctz_imm](vixl32::Register out, vixl32::Register in) {
4249 __ Asr(out, in, ctz_imm);
Scott Wakelingfe885462016-09-22 10:24:38 +01004250 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004251 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004252 }
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004253 };
4254
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004255 if (HasNonNegativeOrMinIntInputAt(instruction, 0)) {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004256 // No need to adjust the result for non-negative dividends or the INT32_MIN dividend.
4257 // NOTE: The generated code for HDiv/HRem correctly works for the INT32_MIN dividend:
4258 // imm == 2
4259 // HDiv
4260 // add out, dividend(0x80000000), dividend(0x80000000), lsr #31 => out = 0x80000001
4261 // asr out, out(0x80000001), #1 => out = 0xc0000000
4262 // This is the same as 'asr out, dividend(0x80000000), #1'
4263 //
4264 // imm > 2
4265 // HDiv
4266 // asr out, dividend(0x80000000), #31 => out = -1
4267 // add out, dividend(0x80000000), out(-1), lsr #(32 - ctz_imm) => out = 0b10..01..1,
4268 // where the number of the rightmost 1s is ctz_imm.
4269 // asr out, out(0b10..01..1), #ctz_imm => out = 0b1..10..0, where the number of the
4270 // leftmost 1s is ctz_imm + 1.
4271 // This is the same as 'asr out, dividend(0x80000000), #ctz_imm'.
4272 //
4273 // imm == INT32_MIN
4274 // HDiv
4275 // asr out, dividend(0x80000000), #31 => out = -1
4276 // add out, dividend(0x80000000), out(-1), lsr #1 => out = 0xc0000000
4277 // asr out, out(0xc0000000), #31 => out = -1
4278 // rsb out, out(-1), #0 => out = 1
4279 // This is the same as
4280 // asr out, dividend(0x80000000), #31
4281 // rsb out, out, #0
4282 //
4283 //
4284 // INT_MIN % imm must be 0 for any imm of power 2. 'and' and 'ubfx' work only with bits
4285 // 0..30 of a dividend. For INT32_MIN those bits are zeros. So 'and' and 'ubfx' always
4286 // produce zero.
4287 if (instruction->IsDiv()) {
4288 generate_div_code(out, dividend);
4289 } else {
4290 if (GetVIXLAssembler()->IsModifiedImmediate(abs_imm - 1)) {
4291 __ And(out, dividend, abs_imm - 1);
4292 } else {
4293 __ Ubfx(out, dividend, 0, ctz_imm);
4294 }
4295 return;
4296 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004297 } else {
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004298 vixl32::Register add_right_input = dividend;
4299 if (ctz_imm > 1) {
4300 __ Asr(out, dividend, 31);
4301 add_right_input = out;
4302 }
4303 __ Add(out, dividend, Operand(add_right_input, vixl32::LSR, 32 - ctz_imm));
4304
4305 if (instruction->IsDiv()) {
4306 generate_div_code(out, out);
4307 } else {
4308 __ Bfc(out, 0, ctz_imm);
4309 __ Sub(out, dividend, out);
4310 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004311 }
4312}
4313
4314void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4315 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004316 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004317
4318 LocationSummary* locations = instruction->GetLocations();
4319 Location second = locations->InAt(1);
4320 DCHECK(second.IsConstant());
4321
4322 vixl32::Register out = OutputRegister(instruction);
4323 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004324 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4325 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004326 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004327
4328 int64_t magic;
4329 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004330 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Scott Wakelingfe885462016-09-22 10:24:38 +01004331
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004332 auto generate_unsigned_div_code =[this, magic, shift](vixl32::Register out,
4333 vixl32::Register dividend,
4334 vixl32::Register temp1,
4335 vixl32::Register temp2) {
4336 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4337 __ Mov(temp1, static_cast<int32_t>(magic));
4338 if (magic > 0 && shift == 0) {
4339 __ Smull(temp2, out, dividend, temp1);
4340 } else {
4341 __ Smull(temp2, temp1, dividend, temp1);
4342 if (magic < 0) {
4343 // The negative magic M = static_cast<int>(m) means that the multiplier m is greater
4344 // than INT32_MAX. In such a case shift is never 0.
4345 // Proof:
4346 // m = (2^p + d - 2^p % d) / d, where p = 32 + shift, d > 2
4347 //
4348 // If shift == 0, m = (2^32 + d - 2^32 % d) / d =
4349 // = (2^32 + d - (2^32 - (2^32 / d) * d)) / d =
4350 // = (d + (2^32 / d) * d) / d = 1 + (2^32 / d), here '/' is the integer division.
4351 //
4352 // 1 + (2^32 / d) is decreasing when d is increasing.
4353 // The maximum is 1 431 655 766, when d == 3. This value is less than INT32_MAX.
4354 // the minimum is 3, when d = 2^31 -1.
4355 // So for all values of d in [3, INT32_MAX] m with p == 32 is in [3, INT32_MAX) and
4356 // is never less than 0.
4357 __ Add(temp1, temp1, dividend);
4358 }
4359 DCHECK_NE(shift, 0);
4360 __ Lsr(out, temp1, shift);
4361 }
4362 };
Scott Wakelingfe885462016-09-22 10:24:38 +01004363
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004364 if (imm > 0 && HasNonNegativeInputAt(instruction, 0)) {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004365 // No need to adjust the result for a non-negative dividend and a positive divisor.
4366 if (instruction->IsDiv()) {
4367 generate_unsigned_div_code(out, dividend, temp1, temp2);
4368 } else {
4369 generate_unsigned_div_code(temp1, dividend, temp1, temp2);
4370 __ Mov(temp2, imm);
4371 __ Mls(out, temp1, temp2, dividend);
4372 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004373 } else {
Evgeny Astigeevichf9388412020-07-02 15:25:13 +01004374 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4375 __ Mov(temp1, static_cast<int32_t>(magic));
4376 __ Smull(temp2, temp1, dividend, temp1);
4377
4378 if (imm > 0 && magic < 0) {
4379 __ Add(temp1, temp1, dividend);
4380 } else if (imm < 0 && magic > 0) {
4381 __ Sub(temp1, temp1, dividend);
4382 }
4383
4384 if (shift != 0) {
4385 __ Asr(temp1, temp1, shift);
4386 }
4387
4388 if (instruction->IsDiv()) {
4389 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4390 } else {
4391 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4392 // TODO: Strength reduction for mls.
4393 __ Mov(temp2, imm);
4394 __ Mls(out, temp1, temp2, dividend);
4395 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004396 }
4397}
4398
4399void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4400 HBinaryOperation* instruction) {
4401 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004402 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004403
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004404 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004405 DCHECK(second.IsConstant());
4406
Anton Kirilov644032c2016-12-06 17:51:43 +00004407 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004408 if (imm == 0) {
4409 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4410 } else if (imm == 1 || imm == -1) {
4411 DivRemOneOrMinusOne(instruction);
4412 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4413 DivRemByPowerOfTwo(instruction);
4414 } else {
4415 DCHECK(imm <= -2 || imm >= 2);
4416 GenerateDivRemWithAnyConstant(instruction);
4417 }
4418}
4419
4420void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4421 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004422 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004423 // pLdiv runtime call.
4424 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004425 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004426 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004427 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004428 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4429 // pIdivmod runtime call.
4430 call_kind = LocationSummary::kCallOnMainOnly;
4431 }
4432
Vladimir Markoca6fff82017-10-03 14:49:14 +01004433 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004434
4435 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004436 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004437 if (div->InputAt(1)->IsConstant()) {
4438 locations->SetInAt(0, Location::RequiresRegister());
4439 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004440 int32_t value = Int32ConstantFrom(div->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004441 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004442 if (value == 1 || value == 0 || value == -1) {
4443 // No temp register required.
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01004444 } else if (IsPowerOfTwo(AbsOrMin(value)) &&
4445 value != 2 &&
4446 value != -2 &&
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004447 !HasNonNegativeOrMinIntInputAt(div, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004448 // The "out" register is used as a temporary, so it overlaps with the inputs.
4449 out_overlaps = Location::kOutputOverlap;
Scott Wakelingfe885462016-09-22 10:24:38 +01004450 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004451 locations->AddRegisterTemps(2);
Scott Wakelingfe885462016-09-22 10:24:38 +01004452 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004453 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Scott Wakelingfe885462016-09-22 10:24:38 +01004454 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4455 locations->SetInAt(0, Location::RequiresRegister());
4456 locations->SetInAt(1, Location::RequiresRegister());
4457 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4458 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004459 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4460 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4461 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004462 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004463 // we only need the former.
4464 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004465 }
4466 break;
4467 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004468 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004469 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4470 locations->SetInAt(0, LocationFrom(
4471 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4472 locations->SetInAt(1, LocationFrom(
4473 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4474 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004475 break;
4476 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004477 case DataType::Type::kFloat32:
4478 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004479 locations->SetInAt(0, Location::RequiresFpuRegister());
4480 locations->SetInAt(1, Location::RequiresFpuRegister());
4481 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4482 break;
4483 }
4484
4485 default:
4486 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4487 }
4488}
4489
4490void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004491 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004492 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004493
4494 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004495 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004496 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004497 GenerateDivRemConstantIntegral(div);
4498 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4499 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4500 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004501 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4502 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4503 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4504 DCHECK(r0.Is(OutputRegister(div)));
4505
4506 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4507 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004508 }
4509 break;
4510 }
4511
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004512 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004513 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4514 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4515 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4516 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4517 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4518 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4519 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4520
4521 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4522 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004523 break;
4524 }
4525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004526 case DataType::Type::kFloat32:
4527 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004528 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004529 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004530
4531 default:
4532 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4533 }
4534}
4535
Artem Serov551b28f2016-10-18 19:11:30 +01004536void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004537 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004538
4539 // Most remainders are implemented in the runtime.
4540 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004541 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004542 // sdiv will be replaced by other instruction sequence.
4543 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004544 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004545 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4546 // Have hardware divide instruction for int, do it with three instructions.
4547 call_kind = LocationSummary::kNoCall;
4548 }
4549
Vladimir Markoca6fff82017-10-03 14:49:14 +01004550 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004551
4552 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004553 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004554 if (rem->InputAt(1)->IsConstant()) {
4555 locations->SetInAt(0, Location::RequiresRegister());
4556 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Anton Kirilov644032c2016-12-06 17:51:43 +00004557 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004558 Location::OutputOverlap out_overlaps = Location::kNoOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004559 if (value == 1 || value == 0 || value == -1) {
4560 // No temp register required.
Evgeny Astigeevich0f3d7ac2020-08-06 16:28:37 +01004561 } else if (IsPowerOfTwo(AbsOrMin(value)) && !HasNonNegativeOrMinIntInputAt(rem, 0)) {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004562 // The "out" register is used as a temporary, so it overlaps with the inputs.
4563 out_overlaps = Location::kOutputOverlap;
Artem Serov551b28f2016-10-18 19:11:30 +01004564 } else {
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004565 locations->AddRegisterTemps(2);
Artem Serov551b28f2016-10-18 19:11:30 +01004566 }
Evgeny Astigeevich2d101172020-06-25 16:52:03 +01004567 locations->SetOut(Location::RequiresRegister(), out_overlaps);
Artem Serov551b28f2016-10-18 19:11:30 +01004568 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4569 locations->SetInAt(0, Location::RequiresRegister());
4570 locations->SetInAt(1, Location::RequiresRegister());
4571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4572 locations->AddTemp(Location::RequiresRegister());
4573 } else {
4574 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4575 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4576 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004577 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004578 // we only need the latter.
4579 locations->SetOut(LocationFrom(r1));
4580 }
4581 break;
4582 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004583 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004584 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4585 locations->SetInAt(0, LocationFrom(
4586 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4587 locations->SetInAt(1, LocationFrom(
4588 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4589 // The runtime helper puts the output in R2,R3.
4590 locations->SetOut(LocationFrom(r2, r3));
4591 break;
4592 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004593 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004594 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4595 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4596 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4597 locations->SetOut(LocationFrom(s0));
4598 break;
4599 }
4600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004601 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004602 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4603 locations->SetInAt(0, LocationFrom(
4604 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4605 locations->SetInAt(1, LocationFrom(
4606 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4607 locations->SetOut(LocationFrom(s0, s1));
4608 break;
4609 }
4610
4611 default:
4612 LOG(FATAL) << "Unexpected rem type " << type;
4613 }
4614}
4615
4616void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4617 LocationSummary* locations = rem->GetLocations();
4618 Location second = locations->InAt(1);
4619
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004620 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004621 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004622 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004623 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4624 vixl32::Register out_reg = OutputRegister(rem);
4625 if (second.IsConstant()) {
4626 GenerateDivRemConstantIntegral(rem);
4627 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4628 vixl32::Register reg2 = RegisterFrom(second);
4629 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4630
4631 // temp = reg1 / reg2 (integer division)
4632 // dest = reg1 - temp * reg2
4633 __ Sdiv(temp, reg1, reg2);
4634 __ Mls(out_reg, temp, reg2, reg1);
4635 } else {
4636 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4637 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4638 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4639 DCHECK(out_reg.Is(r1));
4640
4641 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4642 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4643 }
4644 break;
4645 }
4646
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004647 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004648 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4649 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4650 break;
4651 }
4652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004653 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004654 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4655 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4656 break;
4657 }
4658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004659 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004660 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4661 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4662 break;
4663 }
4664
4665 default:
4666 LOG(FATAL) << "Unexpected rem type " << type;
4667 }
4668}
4669
Aart Bik1f8d51b2018-02-15 10:42:37 -08004670static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4671 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4672 switch (minmax->GetResultType()) {
4673 case DataType::Type::kInt32:
4674 locations->SetInAt(0, Location::RequiresRegister());
4675 locations->SetInAt(1, Location::RequiresRegister());
4676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4677 break;
4678 case DataType::Type::kInt64:
4679 locations->SetInAt(0, Location::RequiresRegister());
4680 locations->SetInAt(1, Location::RequiresRegister());
4681 locations->SetOut(Location::SameAsFirstInput());
4682 break;
4683 case DataType::Type::kFloat32:
4684 locations->SetInAt(0, Location::RequiresFpuRegister());
4685 locations->SetInAt(1, Location::RequiresFpuRegister());
4686 locations->SetOut(Location::SameAsFirstInput());
4687 locations->AddTemp(Location::RequiresRegister());
4688 break;
4689 case DataType::Type::kFloat64:
4690 locations->SetInAt(0, Location::RequiresFpuRegister());
4691 locations->SetInAt(1, Location::RequiresFpuRegister());
4692 locations->SetOut(Location::SameAsFirstInput());
4693 break;
4694 default:
4695 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4696 }
4697}
4698
Aart Bik351df3e2018-03-07 11:54:57 -08004699void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004700 Location op1_loc = locations->InAt(0);
4701 Location op2_loc = locations->InAt(1);
4702 Location out_loc = locations->Out();
4703
4704 vixl32::Register op1 = RegisterFrom(op1_loc);
4705 vixl32::Register op2 = RegisterFrom(op2_loc);
4706 vixl32::Register out = RegisterFrom(out_loc);
4707
4708 __ Cmp(op1, op2);
4709
4710 {
4711 ExactAssemblyScope aas(GetVIXLAssembler(),
4712 3 * kMaxInstructionSizeInBytes,
4713 CodeBufferCheckScope::kMaximumSize);
4714
4715 __ ite(is_min ? lt : gt);
4716 __ mov(is_min ? lt : gt, out, op1);
4717 __ mov(is_min ? ge : le, out, op2);
4718 }
4719}
4720
4721void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4722 Location op1_loc = locations->InAt(0);
4723 Location op2_loc = locations->InAt(1);
4724 Location out_loc = locations->Out();
4725
4726 // Optimization: don't generate any code if inputs are the same.
4727 if (op1_loc.Equals(op2_loc)) {
4728 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4729 return;
4730 }
4731
4732 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4733 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4734 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4735 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4736 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4737 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4738 UseScratchRegisterScope temps(GetVIXLAssembler());
4739 const vixl32::Register temp = temps.Acquire();
4740
4741 DCHECK(op1_lo.Is(out_lo));
4742 DCHECK(op1_hi.Is(out_hi));
4743
4744 // Compare op1 >= op2, or op1 < op2.
4745 __ Cmp(out_lo, op2_lo);
4746 __ Sbcs(temp, out_hi, op2_hi);
4747
4748 // Now GE/LT condition code is correct for the long comparison.
4749 {
4750 vixl32::ConditionType cond = is_min ? ge : lt;
4751 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4752 3 * kMaxInstructionSizeInBytes,
4753 CodeBufferCheckScope::kMaximumSize);
4754 __ itt(cond);
4755 __ mov(cond, out_lo, op2_lo);
4756 __ mov(cond, out_hi, op2_hi);
4757 }
4758}
4759
Aart Bik351df3e2018-03-07 11:54:57 -08004760void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4761 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004762 Location op1_loc = locations->InAt(0);
4763 Location op2_loc = locations->InAt(1);
4764 Location out_loc = locations->Out();
4765
4766 // Optimization: don't generate any code if inputs are the same.
4767 if (op1_loc.Equals(op2_loc)) {
4768 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4769 return;
4770 }
4771
4772 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4773 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4774 vixl32::SRegister out = SRegisterFrom(out_loc);
4775
4776 UseScratchRegisterScope temps(GetVIXLAssembler());
4777 const vixl32::Register temp1 = temps.Acquire();
4778 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4779 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004780 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004781
4782 DCHECK(op1.Is(out));
4783
4784 __ Vcmp(op1, op2);
4785 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004786 __ B(vs, &nan, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004787
4788 // op1 <> op2
4789 vixl32::ConditionType cond = is_min ? gt : lt;
4790 {
4791 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4792 2 * kMaxInstructionSizeInBytes,
4793 CodeBufferCheckScope::kMaximumSize);
4794 __ it(cond);
4795 __ vmov(cond, F32, out, op2);
4796 }
4797 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004798 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004799
4800 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4801 __ Vmov(temp1, op1);
4802 __ Vmov(temp2, op2);
4803 if (is_min) {
4804 __ Orr(temp1, temp1, temp2);
4805 } else {
4806 __ And(temp1, temp1, temp2);
4807 }
4808 __ Vmov(out, temp1);
4809 __ B(final_label);
4810
4811 // handle NaN input.
4812 __ Bind(&nan);
4813 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4814 __ Vmov(out, temp1);
4815
4816 if (done.IsReferenced()) {
4817 __ Bind(&done);
4818 }
4819}
4820
Aart Bik351df3e2018-03-07 11:54:57 -08004821void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4822 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004823 Location op1_loc = locations->InAt(0);
4824 Location op2_loc = locations->InAt(1);
4825 Location out_loc = locations->Out();
4826
4827 // Optimization: don't generate any code if inputs are the same.
4828 if (op1_loc.Equals(op2_loc)) {
4829 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4830 return;
4831 }
4832
4833 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4834 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4835 vixl32::DRegister out = DRegisterFrom(out_loc);
4836 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004837 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004838
4839 DCHECK(op1.Is(out));
4840
4841 __ Vcmp(op1, op2);
4842 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004843 __ B(vs, &handle_nan_eq, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004844
4845 // op1 <> op2
4846 vixl32::ConditionType cond = is_min ? gt : lt;
4847 {
4848 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4849 2 * kMaxInstructionSizeInBytes,
4850 CodeBufferCheckScope::kMaximumSize);
4851 __ it(cond);
4852 __ vmov(cond, F64, out, op2);
4853 }
4854 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004855 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004856
4857 // handle op1 == op2, max(+0.0,-0.0).
4858 if (!is_min) {
4859 __ Vand(F64, out, op1, op2);
4860 __ B(final_label);
4861 }
4862
4863 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4864 __ Bind(&handle_nan_eq);
4865 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4866
4867 if (done.IsReferenced()) {
4868 __ Bind(&done);
4869 }
4870}
4871
Aart Bik351df3e2018-03-07 11:54:57 -08004872void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4873 DataType::Type type = minmax->GetResultType();
4874 switch (type) {
4875 case DataType::Type::kInt32:
4876 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4877 break;
4878 case DataType::Type::kInt64:
4879 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4880 break;
4881 case DataType::Type::kFloat32:
4882 GenerateMinMaxFloat(minmax, is_min);
4883 break;
4884 case DataType::Type::kFloat64:
4885 GenerateMinMaxDouble(minmax, is_min);
4886 break;
4887 default:
4888 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4889 }
4890}
4891
Aart Bik1f8d51b2018-02-15 10:42:37 -08004892void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4893 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4894}
4895
4896void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004897 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004898}
4899
4900void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4901 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4902}
4903
4904void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004905 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004906}
4907
Aart Bik3dad3412018-02-28 12:01:46 -08004908void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4909 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4910 switch (abs->GetResultType()) {
4911 case DataType::Type::kInt32:
4912 case DataType::Type::kInt64:
4913 locations->SetInAt(0, Location::RequiresRegister());
4914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4915 locations->AddTemp(Location::RequiresRegister());
4916 break;
4917 case DataType::Type::kFloat32:
4918 case DataType::Type::kFloat64:
4919 locations->SetInAt(0, Location::RequiresFpuRegister());
4920 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4921 break;
4922 default:
4923 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4924 }
4925}
4926
4927void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4928 LocationSummary* locations = abs->GetLocations();
4929 switch (abs->GetResultType()) {
4930 case DataType::Type::kInt32: {
4931 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4932 vixl32::Register out_reg = RegisterFrom(locations->Out());
4933 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4934 __ Asr(mask, in_reg, 31);
4935 __ Add(out_reg, in_reg, mask);
4936 __ Eor(out_reg, out_reg, mask);
4937 break;
4938 }
4939 case DataType::Type::kInt64: {
4940 Location in = locations->InAt(0);
4941 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4942 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4943 Location output = locations->Out();
4944 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4945 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4946 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4947 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4948 __ Asr(mask, in_reg_hi, 31);
4949 __ Adds(out_reg_lo, in_reg_lo, mask);
4950 __ Adc(out_reg_hi, in_reg_hi, mask);
4951 __ Eor(out_reg_lo, out_reg_lo, mask);
4952 __ Eor(out_reg_hi, out_reg_hi, mask);
4953 break;
4954 }
4955 case DataType::Type::kFloat32:
4956 case DataType::Type::kFloat64:
4957 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4958 break;
4959 default:
4960 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4961 }
4962}
Artem Serov551b28f2016-10-18 19:11:30 +01004963
Scott Wakelingfe885462016-09-22 10:24:38 +01004964void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004965 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004966 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004967}
4968
4969void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4970 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004971 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004972 codegen_->AddSlowPath(slow_path);
4973
4974 LocationSummary* locations = instruction->GetLocations();
4975 Location value = locations->InAt(0);
4976
4977 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004978 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004979 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004980 case DataType::Type::kInt8:
4981 case DataType::Type::kUint16:
4982 case DataType::Type::kInt16:
4983 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004984 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004985 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004986 } else {
4987 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004988 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004989 __ B(slow_path->GetEntryLabel());
4990 }
4991 }
4992 break;
4993 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004994 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004995 if (value.IsRegisterPair()) {
4996 UseScratchRegisterScope temps(GetVIXLAssembler());
4997 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004998 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004999 __ B(eq, slow_path->GetEntryLabel());
5000 } else {
5001 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00005002 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01005003 __ B(slow_path->GetEntryLabel());
5004 }
5005 }
5006 break;
5007 }
5008 default:
5009 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
5010 }
5011}
5012
Artem Serov02109dd2016-09-23 17:17:54 +01005013void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
5014 LocationSummary* locations = ror->GetLocations();
5015 vixl32::Register in = InputRegisterAt(ror, 0);
5016 Location rhs = locations->InAt(1);
5017 vixl32::Register out = OutputRegister(ror);
5018
5019 if (rhs.IsConstant()) {
5020 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
5021 // so map all rotations to a +ve. equivalent in that range.
5022 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
5023 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
5024 if (rot) {
5025 // Rotate, mapping left rotations to right equivalents if necessary.
5026 // (e.g. left by 2 bits == right by 30.)
5027 __ Ror(out, in, rot);
5028 } else if (!out.Is(in)) {
5029 __ Mov(out, in);
5030 }
5031 } else {
5032 __ Ror(out, in, RegisterFrom(rhs));
5033 }
5034}
5035
5036// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
5037// rotates by swapping input regs (effectively rotating by the first 32-bits of
5038// a larger rotation) or flipping direction (thus treating larger right/left
5039// rotations as sub-word sized rotations in the other direction) as appropriate.
5040void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
5041 LocationSummary* locations = ror->GetLocations();
5042 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
5043 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
5044 Location rhs = locations->InAt(1);
5045 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
5046 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
5047
5048 if (rhs.IsConstant()) {
5049 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
5050 // Map all rotations to +ve. equivalents on the interval [0,63].
5051 rot &= kMaxLongShiftDistance;
5052 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
5053 // logic below to a simple pair of binary orr.
5054 // (e.g. 34 bits == in_reg swap + 2 bits right.)
5055 if (rot >= kArmBitsPerWord) {
5056 rot -= kArmBitsPerWord;
5057 std::swap(in_reg_hi, in_reg_lo);
5058 }
5059 // Rotate, or mov to out for zero or word size rotations.
5060 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00005061 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005062 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00005063 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01005064 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
5065 } else {
5066 __ Mov(out_reg_lo, in_reg_lo);
5067 __ Mov(out_reg_hi, in_reg_hi);
5068 }
5069 } else {
5070 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
5071 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
5072 vixl32::Label end;
5073 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00005074 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01005075
5076 __ And(shift_right, RegisterFrom(rhs), 0x1F);
5077 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00005078 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Andreas Gampe3db70682018-12-26 15:12:03 -08005079 __ B(cc, &shift_by_32_plus_shift_right, /* is_far_target= */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01005080
5081 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
5082 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
5083 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
5084 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5085 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5086 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5087 __ Lsr(shift_left, in_reg_hi, shift_right);
5088 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00005089 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01005090
5091 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
5092 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
5093 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
5094 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
5095 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
5096 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
5097 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
5098 __ Lsl(shift_right, in_reg_hi, shift_left);
5099 __ Add(out_reg_lo, out_reg_lo, shift_right);
5100
Anton Kirilov6f644202017-02-27 18:29:45 +00005101 if (end.IsReferenced()) {
5102 __ Bind(&end);
5103 }
Artem Serov02109dd2016-09-23 17:17:54 +01005104 }
5105}
5106
5107void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
5108 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005109 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01005110 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005111 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005112 locations->SetInAt(0, Location::RequiresRegister());
5113 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
5114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5115 break;
5116 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005117 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005118 locations->SetInAt(0, Location::RequiresRegister());
5119 if (ror->InputAt(1)->IsConstant()) {
5120 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
5121 } else {
5122 locations->SetInAt(1, Location::RequiresRegister());
5123 locations->AddTemp(Location::RequiresRegister());
5124 locations->AddTemp(Location::RequiresRegister());
5125 }
5126 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5127 break;
5128 }
5129 default:
5130 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5131 }
5132}
5133
5134void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01005136 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005137 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01005138 HandleIntegerRotate(ror);
5139 break;
5140 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005141 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01005142 HandleLongRotate(ror);
5143 break;
5144 }
5145 default:
5146 LOG(FATAL) << "Unexpected operation type " << type;
5147 UNREACHABLE();
5148 }
5149}
5150
Artem Serov02d37832016-10-25 15:25:33 +01005151void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
5152 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5153
5154 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005155 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005156
5157 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005158 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005159 locations->SetInAt(0, Location::RequiresRegister());
5160 if (op->InputAt(1)->IsConstant()) {
5161 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5163 } else {
5164 locations->SetInAt(1, Location::RequiresRegister());
5165 // Make the output overlap, as it will be used to hold the masked
5166 // second input.
5167 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5168 }
5169 break;
5170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005171 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005172 locations->SetInAt(0, Location::RequiresRegister());
5173 if (op->InputAt(1)->IsConstant()) {
5174 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5175 // For simplicity, use kOutputOverlap even though we only require that low registers
5176 // don't clash with high registers which the register allocator currently guarantees.
5177 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5178 } else {
5179 locations->SetInAt(1, Location::RequiresRegister());
5180 locations->AddTemp(Location::RequiresRegister());
5181 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5182 }
5183 break;
5184 }
5185 default:
5186 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5187 }
5188}
5189
5190void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5191 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5192
5193 LocationSummary* locations = op->GetLocations();
5194 Location out = locations->Out();
5195 Location first = locations->InAt(0);
5196 Location second = locations->InAt(1);
5197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005198 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005199 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005200 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005201 vixl32::Register out_reg = OutputRegister(op);
5202 vixl32::Register first_reg = InputRegisterAt(op, 0);
5203 if (second.IsRegister()) {
5204 vixl32::Register second_reg = RegisterFrom(second);
5205 // ARM doesn't mask the shift count so we need to do it ourselves.
5206 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5207 if (op->IsShl()) {
5208 __ Lsl(out_reg, first_reg, out_reg);
5209 } else if (op->IsShr()) {
5210 __ Asr(out_reg, first_reg, out_reg);
5211 } else {
5212 __ Lsr(out_reg, first_reg, out_reg);
5213 }
5214 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005215 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005216 uint32_t shift_value = cst & kMaxIntShiftDistance;
5217 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5218 __ Mov(out_reg, first_reg);
5219 } else if (op->IsShl()) {
5220 __ Lsl(out_reg, first_reg, shift_value);
5221 } else if (op->IsShr()) {
5222 __ Asr(out_reg, first_reg, shift_value);
5223 } else {
5224 __ Lsr(out_reg, first_reg, shift_value);
5225 }
5226 }
5227 break;
5228 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005230 vixl32::Register o_h = HighRegisterFrom(out);
5231 vixl32::Register o_l = LowRegisterFrom(out);
5232
5233 vixl32::Register high = HighRegisterFrom(first);
5234 vixl32::Register low = LowRegisterFrom(first);
5235
5236 if (second.IsRegister()) {
5237 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5238
5239 vixl32::Register second_reg = RegisterFrom(second);
5240
5241 if (op->IsShl()) {
5242 __ And(o_l, second_reg, kMaxLongShiftDistance);
5243 // Shift the high part
5244 __ Lsl(o_h, high, o_l);
5245 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005246 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005247 __ Lsr(temp, low, temp);
5248 __ Orr(o_h, o_h, temp);
5249 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005250 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005251 {
Artem Serov0fb37192016-12-06 18:13:40 +00005252 ExactAssemblyScope guard(GetVIXLAssembler(),
5253 2 * vixl32::kMaxInstructionSizeInBytes,
5254 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005255 __ it(pl);
5256 __ lsl(pl, o_h, low, temp);
5257 }
5258 // Shift the low part
5259 __ Lsl(o_l, low, o_l);
5260 } else if (op->IsShr()) {
5261 __ And(o_h, second_reg, kMaxLongShiftDistance);
5262 // Shift the low part
5263 __ Lsr(o_l, low, o_h);
5264 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005265 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005266 __ Lsl(temp, high, temp);
5267 __ Orr(o_l, o_l, temp);
5268 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005269 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005270 {
Artem Serov0fb37192016-12-06 18:13:40 +00005271 ExactAssemblyScope guard(GetVIXLAssembler(),
5272 2 * vixl32::kMaxInstructionSizeInBytes,
5273 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005274 __ it(pl);
5275 __ asr(pl, o_l, high, temp);
5276 }
5277 // Shift the high part
5278 __ Asr(o_h, high, o_h);
5279 } else {
5280 __ And(o_h, second_reg, kMaxLongShiftDistance);
5281 // same as Shr except we use `Lsr`s and not `Asr`s
5282 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005283 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005284 __ Lsl(temp, high, temp);
5285 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005286 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005287 {
Artem Serov0fb37192016-12-06 18:13:40 +00005288 ExactAssemblyScope guard(GetVIXLAssembler(),
5289 2 * vixl32::kMaxInstructionSizeInBytes,
5290 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005291 __ it(pl);
5292 __ lsr(pl, o_l, high, temp);
5293 }
5294 __ Lsr(o_h, high, o_h);
5295 }
5296 } else {
5297 // Register allocator doesn't create partial overlap.
5298 DCHECK(!o_l.Is(high));
5299 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005300 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005301 uint32_t shift_value = cst & kMaxLongShiftDistance;
5302 if (shift_value > 32) {
5303 if (op->IsShl()) {
5304 __ Lsl(o_h, low, shift_value - 32);
5305 __ Mov(o_l, 0);
5306 } else if (op->IsShr()) {
5307 __ Asr(o_l, high, shift_value - 32);
5308 __ Asr(o_h, high, 31);
5309 } else {
5310 __ Lsr(o_l, high, shift_value - 32);
5311 __ Mov(o_h, 0);
5312 }
5313 } else if (shift_value == 32) {
5314 if (op->IsShl()) {
5315 __ Mov(o_h, low);
5316 __ Mov(o_l, 0);
5317 } else if (op->IsShr()) {
5318 __ Mov(o_l, high);
5319 __ Asr(o_h, high, 31);
5320 } else {
5321 __ Mov(o_l, high);
5322 __ Mov(o_h, 0);
5323 }
5324 } else if (shift_value == 1) {
5325 if (op->IsShl()) {
5326 __ Lsls(o_l, low, 1);
5327 __ Adc(o_h, high, high);
5328 } else if (op->IsShr()) {
5329 __ Asrs(o_h, high, 1);
5330 __ Rrx(o_l, low);
5331 } else {
5332 __ Lsrs(o_h, high, 1);
5333 __ Rrx(o_l, low);
5334 }
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005335 } else if (shift_value == 0) {
5336 __ Mov(o_l, low);
5337 __ Mov(o_h, high);
Artem Serov02d37832016-10-25 15:25:33 +01005338 } else {
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005339 DCHECK(0 < shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01005340 if (op->IsShl()) {
5341 __ Lsl(o_h, high, shift_value);
5342 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5343 __ Lsl(o_l, low, shift_value);
5344 } else if (op->IsShr()) {
5345 __ Lsr(o_l, low, shift_value);
5346 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5347 __ Asr(o_h, high, shift_value);
5348 } else {
5349 __ Lsr(o_l, low, shift_value);
5350 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5351 __ Lsr(o_h, high, shift_value);
5352 }
5353 }
5354 }
5355 break;
5356 }
5357 default:
5358 LOG(FATAL) << "Unexpected operation type " << type;
5359 UNREACHABLE();
5360 }
5361}
5362
5363void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5364 HandleShift(shl);
5365}
5366
5367void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5368 HandleShift(shl);
5369}
5370
5371void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5372 HandleShift(shr);
5373}
5374
5375void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5376 HandleShift(shr);
5377}
5378
5379void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5380 HandleShift(ushr);
5381}
5382
5383void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5384 HandleShift(ushr);
5385}
5386
5387void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005388 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5389 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005390 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5391 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005392 locations->SetOut(LocationFrom(r0));
5393}
5394
5395void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005396 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5397 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andra Danciua0130e82020-07-23 12:34:56 +00005398 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005399}
5400
5401void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005402 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5403 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005404 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005405 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005406 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5407 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005408}
5409
5410void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005411 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5412 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005413 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005414 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005415 DCHECK(!codegen_->IsLeafMethod());
Andra Danciua0130e82020-07-23 12:34:56 +00005416 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 13);
Artem Serov02d37832016-10-25 15:25:33 +01005417}
5418
5419void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5420 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005421 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005422 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5423 if (location.IsStackSlot()) {
5424 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5425 } else if (location.IsDoubleStackSlot()) {
5426 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5427 }
5428 locations->SetOut(location);
5429}
5430
5431void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5432 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5433 // Nothing to do, the parameter is already at its location.
5434}
5435
5436void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5437 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005438 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005439 locations->SetOut(LocationFrom(kMethodRegister));
5440}
5441
5442void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5443 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5444 // Nothing to do, the method is already at its location.
5445}
5446
5447void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5448 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005449 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005450 locations->SetInAt(0, Location::RequiresRegister());
5451 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5452}
5453
5454void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5455 LocationSummary* locations = not_->GetLocations();
5456 Location out = locations->Out();
5457 Location in = locations->InAt(0);
5458 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005459 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005460 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5461 break;
5462
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005463 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005464 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5465 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5466 break;
5467
5468 default:
5469 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5470 }
5471}
5472
Scott Wakelingc34dba72016-10-03 10:14:44 +01005473void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5474 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005475 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005476 locations->SetInAt(0, Location::RequiresRegister());
5477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5478}
5479
5480void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5481 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5482}
5483
Artem Serov02d37832016-10-25 15:25:33 +01005484void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5485 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005486 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005487 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005488 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005489 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005490 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005491 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005492 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005493 case DataType::Type::kInt32:
5494 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005495 locations->SetInAt(0, Location::RequiresRegister());
5496 locations->SetInAt(1, Location::RequiresRegister());
5497 // Output overlaps because it is written before doing the low comparison.
5498 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5499 break;
5500 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kFloat32:
5502 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005503 locations->SetInAt(0, Location::RequiresFpuRegister());
5504 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5505 locations->SetOut(Location::RequiresRegister());
5506 break;
5507 }
5508 default:
5509 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5510 }
5511}
5512
5513void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5514 LocationSummary* locations = compare->GetLocations();
5515 vixl32::Register out = OutputRegister(compare);
5516 Location left = locations->InAt(0);
5517 Location right = locations->InAt(1);
5518
5519 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005520 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005521 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005522 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005523 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005524 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005525 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005526 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005528 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005529 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005530 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5531 __ Mov(out, 0);
5532 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5533 less_cond = lt;
5534 break;
5535 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005536 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005537 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Andreas Gampe3db70682018-12-26 15:12:03 -08005538 __ B(lt, &less, /* is_far_target= */ false);
5539 __ B(gt, &greater, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005540 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5541 __ Mov(out, 0);
5542 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5543 less_cond = lo;
5544 break;
5545 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 case DataType::Type::kFloat32:
5547 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005548 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005549 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005550 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5551 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5552 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5553 break;
5554 }
5555 default:
5556 LOG(FATAL) << "Unexpected compare type " << type;
5557 UNREACHABLE();
5558 }
5559
Andreas Gampe3db70682018-12-26 15:12:03 -08005560 __ B(eq, final_label, /* is_far_target= */ false);
5561 __ B(less_cond, &less, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005562
5563 __ Bind(&greater);
5564 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005565 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005566
5567 __ Bind(&less);
5568 __ Mov(out, -1);
5569
Anton Kirilov6f644202017-02-27 18:29:45 +00005570 if (done.IsReferenced()) {
5571 __ Bind(&done);
5572 }
Artem Serov02d37832016-10-25 15:25:33 +01005573}
5574
5575void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5576 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005577 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005578 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5579 locations->SetInAt(i, Location::Any());
5580 }
5581 locations->SetOut(Location::Any());
5582}
5583
5584void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5585 LOG(FATAL) << "Unreachable";
5586}
5587
5588void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5589 // TODO (ported from quick): revisit ARM barrier kinds.
5590 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5591 switch (kind) {
5592 case MemBarrierKind::kAnyStore:
5593 case MemBarrierKind::kLoadAny:
5594 case MemBarrierKind::kAnyAny: {
5595 flavor = DmbOptions::ISH;
5596 break;
5597 }
5598 case MemBarrierKind::kStoreStore: {
5599 flavor = DmbOptions::ISHST;
5600 break;
5601 }
5602 default:
5603 LOG(FATAL) << "Unexpected memory barrier " << kind;
5604 }
5605 __ Dmb(flavor);
5606}
5607
5608void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5609 uint32_t offset,
5610 vixl32::Register out_lo,
5611 vixl32::Register out_hi) {
5612 UseScratchRegisterScope temps(GetVIXLAssembler());
5613 if (offset != 0) {
5614 vixl32::Register temp = temps.Acquire();
5615 __ Add(temp, addr, offset);
5616 addr = temp;
5617 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005618 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005619}
5620
5621void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5622 uint32_t offset,
5623 vixl32::Register value_lo,
5624 vixl32::Register value_hi,
5625 vixl32::Register temp1,
5626 vixl32::Register temp2,
5627 HInstruction* instruction) {
5628 UseScratchRegisterScope temps(GetVIXLAssembler());
5629 vixl32::Label fail;
5630 if (offset != 0) {
5631 vixl32::Register temp = temps.Acquire();
5632 __ Add(temp, addr, offset);
5633 addr = temp;
5634 }
5635 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005636 {
5637 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005638 ExactAssemblyScope aas(GetVIXLAssembler(),
5639 vixl32::kMaxInstructionSizeInBytes,
5640 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005641 // We need a load followed by store. (The address used in a STREX instruction must
5642 // be the same as the address in the most recently executed LDREX instruction.)
5643 __ ldrexd(temp1, temp2, MemOperand(addr));
5644 codegen_->MaybeRecordImplicitNullCheck(instruction);
5645 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005646 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005647 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005648}
Artem Serov02109dd2016-09-23 17:17:54 +01005649
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005650void LocationsBuilderARMVIXL::HandleFieldSet(
5651 HInstruction* instruction, const FieldInfo& field_info) {
5652 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5653
5654 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005655 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005656 locations->SetInAt(0, Location::RequiresRegister());
5657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005658 DataType::Type field_type = field_info.GetFieldType();
5659 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005660 locations->SetInAt(1, Location::RequiresFpuRegister());
5661 } else {
5662 locations->SetInAt(1, Location::RequiresRegister());
5663 }
5664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005665 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005666 bool generate_volatile = field_info.IsVolatile()
5667 && is_wide
5668 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5669 bool needs_write_barrier =
5670 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5671 // Temporary registers for the write barrier.
5672 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5673 if (needs_write_barrier) {
5674 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5675 locations->AddTemp(Location::RequiresRegister());
5676 } else if (generate_volatile) {
5677 // ARM encoding have some additional constraints for ldrexd/strexd:
5678 // - registers need to be consecutive
5679 // - the first register should be even but not R14.
5680 // We don't test for ARM yet, and the assertion makes sure that we
5681 // revisit this if we ever enable ARM encoding.
5682 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5683
5684 locations->AddTemp(Location::RequiresRegister());
5685 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005686 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005687 // For doubles we need two more registers to copy the value.
5688 locations->AddTemp(LocationFrom(r2));
5689 locations->AddTemp(LocationFrom(r3));
5690 }
5691 }
5692}
5693
5694void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5695 const FieldInfo& field_info,
5696 bool value_can_be_null) {
5697 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5698
5699 LocationSummary* locations = instruction->GetLocations();
5700 vixl32::Register base = InputRegisterAt(instruction, 0);
5701 Location value = locations->InAt(1);
5702
5703 bool is_volatile = field_info.IsVolatile();
5704 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005706 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5707 bool needs_write_barrier =
5708 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5709
5710 if (is_volatile) {
5711 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5712 }
5713
5714 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005716 case DataType::Type::kUint8:
5717 case DataType::Type::kInt8:
5718 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005719 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005720 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005721 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5722 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005723 StoreOperandType operand_type = GetStoreOperandType(field_type);
5724 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005725 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005726 break;
5727 }
5728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005729 case DataType::Type::kReference: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005730 vixl32::Register value_reg = RegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005731 if (kPoisonHeapReferences && needs_write_barrier) {
5732 // Note that in the case where `value` is a null reference,
5733 // we do not enter this block, as a null reference does not
5734 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005735 DCHECK_EQ(field_type, DataType::Type::kReference);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005736 value_reg = RegisterFrom(locations->GetTemp(0));
5737 __ Mov(value_reg, RegisterFrom(value));
5738 GetAssembler()->PoisonHeapReference(value_reg);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005739 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005740 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5741 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5742 GetAssembler()->StoreToOffset(kStoreWord, value_reg, base, offset);
5743 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005744 break;
5745 }
5746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005748 if (is_volatile && !atomic_ldrd_strd) {
5749 GenerateWideAtomicStore(base,
5750 offset,
5751 LowRegisterFrom(value),
5752 HighRegisterFrom(value),
5753 RegisterFrom(locations->GetTemp(0)),
5754 RegisterFrom(locations->GetTemp(1)),
5755 instruction);
5756 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005757 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5758 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005759 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5760 codegen_->MaybeRecordImplicitNullCheck(instruction);
5761 }
5762 break;
5763 }
5764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005765 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005766 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5767 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005768 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005769 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005770 break;
5771 }
5772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005774 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005775 if (is_volatile && !atomic_ldrd_strd) {
5776 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5777 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5778
5779 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5780
5781 GenerateWideAtomicStore(base,
5782 offset,
5783 value_reg_lo,
5784 value_reg_hi,
5785 RegisterFrom(locations->GetTemp(2)),
5786 RegisterFrom(locations->GetTemp(3)),
5787 instruction);
5788 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005789 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5790 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005791 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5792 codegen_->MaybeRecordImplicitNullCheck(instruction);
5793 }
5794 break;
5795 }
5796
Aart Bik66c158e2018-01-31 12:55:04 -08005797 case DataType::Type::kUint32:
5798 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005799 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005800 LOG(FATAL) << "Unreachable type " << field_type;
5801 UNREACHABLE();
5802 }
5803
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005804 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5805 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5806 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5807 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5808 }
5809
5810 if (is_volatile) {
5811 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5812 }
5813}
5814
Artem Serov02d37832016-10-25 15:25:33 +01005815void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5816 const FieldInfo& field_info) {
5817 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5818
5819 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005821 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005822 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5823 object_field_get_with_read_barrier
5824 ? LocationSummary::kCallOnSlowPath
5825 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005826 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5827 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5828 }
5829 locations->SetInAt(0, Location::RequiresRegister());
5830
5831 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005833 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5834 // The output overlaps in case of volatile long: we don't want the
5835 // code generated by GenerateWideAtomicLoad to overwrite the
5836 // object's location. Likewise, in the case of an object field get
5837 // with read barriers enabled, we do not want the load to overwrite
5838 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005839 bool overlap =
5840 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005841 object_field_get_with_read_barrier;
5842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005843 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005844 locations->SetOut(Location::RequiresFpuRegister());
5845 } else {
5846 locations->SetOut(Location::RequiresRegister(),
5847 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5848 }
5849 if (volatile_for_double) {
5850 // ARM encoding have some additional constraints for ldrexd/strexd:
5851 // - registers need to be consecutive
5852 // - the first register should be even but not R14.
5853 // We don't test for ARM yet, and the assertion makes sure that we
5854 // revisit this if we ever enable ARM encoding.
5855 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5856 locations->AddTemp(Location::RequiresRegister());
5857 locations->AddTemp(Location::RequiresRegister());
5858 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005859 // We need a temporary register for the read barrier load in
5860 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5861 // only if the offset is too big.
5862 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005863 locations->AddTemp(Location::RequiresRegister());
5864 }
Artem Serov02d37832016-10-25 15:25:33 +01005865 }
5866}
5867
5868Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005869 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005870 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5871 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5872 return Location::ConstantLocation(input->AsConstant());
5873 } else {
5874 return Location::RequiresFpuRegister();
5875 }
5876}
5877
Artem Serov02109dd2016-09-23 17:17:54 +01005878Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5879 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005881 if (constant->IsConstant() &&
5882 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5883 return Location::ConstantLocation(constant->AsConstant());
5884 }
5885 return Location::RequiresRegister();
5886}
5887
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005888static bool CanEncode32BitConstantAsImmediate(
5889 CodeGeneratorARMVIXL* codegen,
5890 uint32_t value,
5891 Opcode opcode,
5892 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5893 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5894 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005895 return true;
5896 }
5897 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005898 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005899 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005900 case AND: neg_opcode = BIC; neg_value = ~value; break;
5901 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5902 case ADD: neg_opcode = SUB; neg_value = -value; break;
5903 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5904 case SUB: neg_opcode = ADD; neg_value = -value; break;
5905 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5906 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005907 default:
5908 return false;
5909 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005910
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005911 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005912 return true;
5913 }
5914
5915 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005916}
5917
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005918bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5919 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5920 if (DataType::Is64BitType(input_cst->GetType())) {
5921 Opcode high_opcode = opcode;
5922 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5923 switch (opcode) {
5924 case SUB:
5925 // Flip the operation to an ADD.
5926 value = -value;
5927 opcode = ADD;
5928 FALLTHROUGH_INTENDED;
5929 case ADD:
5930 if (Low32Bits(value) == 0u) {
5931 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5932 }
5933 high_opcode = ADC;
5934 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5935 break;
5936 default:
5937 break;
5938 }
5939 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5940 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5941 } else {
5942 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5943 }
5944}
5945
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005946void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5947 const FieldInfo& field_info) {
5948 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5949
5950 LocationSummary* locations = instruction->GetLocations();
5951 vixl32::Register base = InputRegisterAt(instruction, 0);
5952 Location out = locations->Out();
5953 bool is_volatile = field_info.IsVolatile();
5954 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005955 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5956 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005957 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5958
Vladimir Marko61b92282017-10-11 13:23:17 +01005959 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005960 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005961 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005963 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005964 case DataType::Type::kInt16:
5965 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005966 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5967 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01005968 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005969 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005970 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005971 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005972 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005973
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005975 // /* HeapReference<Object> */ out = *(base + offset)
5976 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01005977 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005978 // Note that a potential implicit null check is handled in this
5979 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5980 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005981 instruction, out, base, offset, maybe_temp, /* needs_null_check= */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005982 if (is_volatile) {
5983 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5984 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005985 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005986 {
5987 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5988 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5989 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5990 codegen_->MaybeRecordImplicitNullCheck(instruction);
5991 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005992 if (is_volatile) {
5993 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5994 }
5995 // If read barriers are enabled, emit read barriers other than
5996 // Baker's using a slow path (and also unpoison the loaded
5997 // reference, if heap poisoning is enabled).
5998 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5999 }
6000 break;
6001 }
6002
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006003 case DataType::Type::kInt64: {
6004 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6005 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006006 if (is_volatile && !atomic_ldrd_strd) {
6007 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
6008 } else {
6009 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
6010 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006011 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006012 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006013 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006014
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006015 case DataType::Type::kFloat32: {
6016 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6017 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006018 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006019 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006020 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006021 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006023 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006024 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6025 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006026 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006027 if (is_volatile && !atomic_ldrd_strd) {
6028 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
6029 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
6030 GenerateWideAtomicLoad(base, offset, lo, hi);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006031 codegen_->MaybeRecordImplicitNullCheck(instruction);
6032 __ Vmov(out_dreg, lo, hi);
6033 } else {
6034 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006035 codegen_->MaybeRecordImplicitNullCheck(instruction);
6036 }
6037 break;
6038 }
6039
Aart Bik66c158e2018-01-31 12:55:04 -08006040 case DataType::Type::kUint32:
6041 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01006043 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006044 UNREACHABLE();
6045 }
6046
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006047 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006048 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006049 // Memory barriers, in the case of references, are also handled
6050 // in the previous switch statement.
6051 } else {
6052 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6053 }
6054 }
6055}
6056
6057void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6058 HandleFieldSet(instruction, instruction->GetFieldInfo());
6059}
6060
6061void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6062 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6063}
6064
6065void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6066 HandleFieldGet(instruction, instruction->GetFieldInfo());
6067}
6068
6069void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6070 HandleFieldGet(instruction, instruction->GetFieldInfo());
6071}
6072
6073void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6074 HandleFieldGet(instruction, instruction->GetFieldInfo());
6075}
6076
6077void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6078 HandleFieldGet(instruction, instruction->GetFieldInfo());
6079}
6080
Scott Wakelingc34dba72016-10-03 10:14:44 +01006081void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6082 HandleFieldSet(instruction, instruction->GetFieldInfo());
6083}
6084
6085void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6086 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
6087}
6088
Vladimir Marko552a1342017-10-31 10:56:47 +00006089void LocationsBuilderARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6090 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(r0));
6091}
6092
6093void InstructionCodeGeneratorARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6094 __ Mov(r0, instruction->GetFormat()->GetValue());
6095 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6096}
6097
Artem Serovcfbe9132016-10-14 15:58:56 +01006098void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
6099 HUnresolvedInstanceFieldGet* instruction) {
6100 FieldAccessCallingConventionARMVIXL calling_convention;
6101 codegen_->CreateUnresolvedFieldLocationSummary(
6102 instruction, instruction->GetFieldType(), calling_convention);
6103}
6104
6105void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
6106 HUnresolvedInstanceFieldGet* instruction) {
6107 FieldAccessCallingConventionARMVIXL calling_convention;
6108 codegen_->GenerateUnresolvedFieldAccess(instruction,
6109 instruction->GetFieldType(),
6110 instruction->GetFieldIndex(),
6111 instruction->GetDexPc(),
6112 calling_convention);
6113}
6114
6115void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
6116 HUnresolvedInstanceFieldSet* instruction) {
6117 FieldAccessCallingConventionARMVIXL calling_convention;
6118 codegen_->CreateUnresolvedFieldLocationSummary(
6119 instruction, instruction->GetFieldType(), calling_convention);
6120}
6121
6122void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
6123 HUnresolvedInstanceFieldSet* instruction) {
6124 FieldAccessCallingConventionARMVIXL calling_convention;
6125 codegen_->GenerateUnresolvedFieldAccess(instruction,
6126 instruction->GetFieldType(),
6127 instruction->GetFieldIndex(),
6128 instruction->GetDexPc(),
6129 calling_convention);
6130}
6131
6132void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
6133 HUnresolvedStaticFieldGet* instruction) {
6134 FieldAccessCallingConventionARMVIXL calling_convention;
6135 codegen_->CreateUnresolvedFieldLocationSummary(
6136 instruction, instruction->GetFieldType(), calling_convention);
6137}
6138
6139void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
6140 HUnresolvedStaticFieldGet* instruction) {
6141 FieldAccessCallingConventionARMVIXL calling_convention;
6142 codegen_->GenerateUnresolvedFieldAccess(instruction,
6143 instruction->GetFieldType(),
6144 instruction->GetFieldIndex(),
6145 instruction->GetDexPc(),
6146 calling_convention);
6147}
6148
6149void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
6150 HUnresolvedStaticFieldSet* instruction) {
6151 FieldAccessCallingConventionARMVIXL calling_convention;
6152 codegen_->CreateUnresolvedFieldLocationSummary(
6153 instruction, instruction->GetFieldType(), calling_convention);
6154}
6155
6156void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
6157 HUnresolvedStaticFieldSet* instruction) {
6158 FieldAccessCallingConventionARMVIXL calling_convention;
6159 codegen_->GenerateUnresolvedFieldAccess(instruction,
6160 instruction->GetFieldType(),
6161 instruction->GetFieldIndex(),
6162 instruction->GetDexPc(),
6163 calling_convention);
6164}
6165
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006166void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00006167 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006168 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006169}
6170
6171void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
6172 if (CanMoveNullCheckToUser(instruction)) {
6173 return;
6174 }
6175
6176 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00006177 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006178 ExactAssemblyScope aas(GetVIXLAssembler(),
6179 vixl32::kMaxInstructionSizeInBytes,
6180 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006181 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
6182 RecordPcInfo(instruction, instruction->GetDexPc());
6183}
6184
6185void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6186 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006187 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006188 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006189 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006190}
6191
6192void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6193 codegen_->GenerateNullCheck(instruction);
6194}
6195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006196void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006197 Location out_loc,
6198 vixl32::Register base,
6199 vixl32::Register reg_index,
6200 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006201 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006202 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6203
6204 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006205 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006206 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006207 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6208 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006209 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006210 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6211 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006212 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006213 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6214 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006215 case DataType::Type::kInt16:
6216 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6217 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006218 case DataType::Type::kReference:
6219 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006220 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6221 break;
6222 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 case DataType::Type::kInt64:
6224 case DataType::Type::kFloat32:
6225 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006226 default:
6227 LOG(FATAL) << "Unreachable type " << type;
6228 UNREACHABLE();
6229 }
6230}
6231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006232void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006233 Location loc,
6234 vixl32::Register base,
6235 vixl32::Register reg_index,
6236 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006238 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6239
6240 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006242 case DataType::Type::kUint8:
6243 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006244 __ Strb(cond, RegisterFrom(loc), mem_address);
6245 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006247 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006248 __ Strh(cond, RegisterFrom(loc), mem_address);
6249 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006250 case DataType::Type::kReference:
6251 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006252 __ Str(cond, RegisterFrom(loc), mem_address);
6253 break;
6254 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006255 case DataType::Type::kInt64:
6256 case DataType::Type::kFloat32:
6257 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006258 default:
6259 LOG(FATAL) << "Unreachable type " << type;
6260 UNREACHABLE();
6261 }
6262}
6263
6264void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6265 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006266 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006267 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006268 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6269 object_array_get_with_read_barrier
6270 ? LocationSummary::kCallOnSlowPath
6271 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006272 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006273 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006274 }
6275 locations->SetInAt(0, Location::RequiresRegister());
6276 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006277 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006278 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6279 } else {
6280 // The output overlaps in the case of an object array get with
6281 // read barriers enabled: we do not want the move to overwrite the
6282 // array's location, as we need it to emit the read barrier.
6283 locations->SetOut(
6284 Location::RequiresRegister(),
6285 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6286 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006287 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006288 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006289 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01006290 // We need a temporary register for the read barrier load in
6291 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
6292 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006293 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6294 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006296 if (offset >= kReferenceLoadMinFarOffset) {
6297 locations->AddTemp(Location::RequiresRegister());
6298 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006299 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006300 // We need a non-scratch temporary for the array data pointer in
6301 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006302 locations->AddTemp(Location::RequiresRegister());
6303 }
6304 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6305 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006306 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006307 }
6308}
6309
6310void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006311 LocationSummary* locations = instruction->GetLocations();
6312 Location obj_loc = locations->InAt(0);
6313 vixl32::Register obj = InputRegisterAt(instruction, 0);
6314 Location index = locations->InAt(1);
6315 Location out_loc = locations->Out();
6316 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006318 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6319 instruction->IsStringCharAt();
6320 HInstruction* array_instr = instruction->GetArray();
6321 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006322
6323 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006324 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006325 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006326 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006328 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006329 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006330 vixl32::Register length;
6331 if (maybe_compressed_char_at) {
6332 length = RegisterFrom(locations->GetTemp(0));
6333 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006334 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6335 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006336 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6337 codegen_->MaybeRecordImplicitNullCheck(instruction);
6338 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006339 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006340 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006341 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006342 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006343 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006344 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6345 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6346 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006347 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006348 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6349 RegisterFrom(out_loc),
6350 obj,
6351 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006352 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006353 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006354 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006355 RegisterFrom(out_loc),
6356 obj,
6357 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006358 if (done.IsReferenced()) {
6359 __ Bind(&done);
6360 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006361 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006363
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006364 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6365 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006366 LoadOperandType load_type = GetLoadOperandType(type);
6367 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006368 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006369 }
6370 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006371 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006372 vixl32::Register temp = temps.Acquire();
6373
6374 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006375 // We do not need to compute the intermediate address from the array: the
6376 // input instruction has done it already. See the comment in
6377 // `TryExtractArrayAccessAddress()`.
6378 if (kIsDebugBuild) {
6379 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006380 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006381 }
6382 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006383 } else {
6384 __ Add(temp, obj, data_offset);
6385 }
6386 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006387 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006388 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006389 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6390 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6391 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006392 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006393 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006394 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006395 __ Bind(&uncompressed_load);
6396 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006397 if (done.IsReferenced()) {
6398 __ Bind(&done);
6399 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006400 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006401 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6402 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006403 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006404 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006405 }
6406 }
6407 break;
6408 }
6409
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006410 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006411 // The read barrier instrumentation of object ArrayGet
6412 // instructions does not support the HIntermediateAddress
6413 // instruction.
6414 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6415
Scott Wakelingc34dba72016-10-03 10:14:44 +01006416 static_assert(
6417 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6418 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6419 // /* HeapReference<Object> */ out =
6420 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6421 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006422 // Note that a potential implicit null check is handled in this
6423 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006424 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6425 if (index.IsConstant()) {
6426 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006427 Location maybe_temp =
6428 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006429 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006430 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6431 out_loc,
6432 obj,
6433 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006434 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006435 /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006436 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006437 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006438 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006439 out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006440 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006441 } else {
6442 vixl32::Register out = OutputRegister(instruction);
6443 if (index.IsConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006444 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6445 {
6446 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6447 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6448 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
6449 codegen_->MaybeRecordImplicitNullCheck(instruction);
6450 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006451 // If read barriers are enabled, emit read barriers other than
6452 // Baker's using a slow path (and also unpoison the loaded
6453 // reference, if heap poisoning is enabled).
6454 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6455 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006456 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006457 vixl32::Register temp = temps.Acquire();
6458
6459 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006460 // We do not need to compute the intermediate address from the array: the
6461 // input instruction has done it already. See the comment in
6462 // `TryExtractArrayAccessAddress()`.
6463 if (kIsDebugBuild) {
6464 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006465 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006466 }
6467 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006468 } else {
6469 __ Add(temp, obj, data_offset);
6470 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006471 {
6472 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6473 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6474 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6475 temps.Close();
6476 codegen_->MaybeRecordImplicitNullCheck(instruction);
6477 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006478 // If read barriers are enabled, emit read barriers other than
6479 // Baker's using a slow path (and also unpoison the loaded
6480 // reference, if heap poisoning is enabled).
6481 codegen_->MaybeGenerateReadBarrierSlow(
6482 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6483 }
6484 }
6485 break;
6486 }
6487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006488 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006489 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6490 // As two macro instructions can be emitted the max size is doubled.
6491 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006492 if (index.IsConstant()) {
6493 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006494 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006495 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6496 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006497 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006498 vixl32::Register temp = temps.Acquire();
6499 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6500 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6501 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006502 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006503 break;
6504 }
6505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006506 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006507 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6508 // As two macro instructions can be emitted the max size is doubled.
6509 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 vixl32::SRegister out = SRegisterFrom(out_loc);
6511 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006512 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006513 GetAssembler()->LoadSFromOffset(out, obj, offset);
6514 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006515 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006516 vixl32::Register temp = temps.Acquire();
6517 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6518 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6519 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006521 break;
6522 }
6523
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006524 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006525 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6526 // As two macro instructions can be emitted the max size is doubled.
6527 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006528 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006529 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006530 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6531 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006532 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006533 vixl32::Register temp = temps.Acquire();
6534 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6535 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6536 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006537 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006538 break;
6539 }
6540
Aart Bik66c158e2018-01-31 12:55:04 -08006541 case DataType::Type::kUint32:
6542 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006543 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006544 LOG(FATAL) << "Unreachable type " << type;
6545 UNREACHABLE();
6546 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006547}
6548
6549void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006550 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551
6552 bool needs_write_barrier =
6553 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006554 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006555
Vladimir Markoca6fff82017-10-03 14:49:14 +01006556 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006557 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006558 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006559
6560 locations->SetInAt(0, Location::RequiresRegister());
6561 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006562 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006563 locations->SetInAt(2, Location::RequiresFpuRegister());
6564 } else {
6565 locations->SetInAt(2, Location::RequiresRegister());
6566 }
6567 if (needs_write_barrier) {
6568 // Temporary registers for the write barrier.
6569 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6570 locations->AddTemp(Location::RequiresRegister());
6571 }
6572}
6573
6574void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006575 LocationSummary* locations = instruction->GetLocations();
6576 vixl32::Register array = InputRegisterAt(instruction, 0);
6577 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006578 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006579 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006580 bool needs_write_barrier =
6581 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6582 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006583 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006584 Location value_loc = locations->InAt(2);
6585 HInstruction* array_instr = instruction->GetArray();
6586 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006587
6588 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006589 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006590 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006591 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006592 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006593 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006594 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006595 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006596 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006597 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006598 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006599 StoreOperandType store_type = GetStoreOperandType(value_type);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006600 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6601 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006602 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006603 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006604 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006605 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006606 vixl32::Register temp = temps.Acquire();
6607
6608 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006609 // We do not need to compute the intermediate address from the array: the
6610 // input instruction has done it already. See the comment in
6611 // `TryExtractArrayAccessAddress()`.
6612 if (kIsDebugBuild) {
6613 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006614 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006615 }
6616 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006617 } else {
6618 __ Add(temp, array, data_offset);
6619 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006620 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6621 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006622 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006623 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006624 }
6625 break;
6626 }
6627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006628 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006629 vixl32::Register value = RegisterFrom(value_loc);
6630 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6631 // See the comment in instruction_simplifier_shared.cc.
6632 DCHECK(!has_intermediate_address);
6633
6634 if (instruction->InputAt(2)->IsNullConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006635 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6636 // As two macro instructions can be emitted the max size is doubled.
6637 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006638 // Just setting null.
6639 if (index.IsConstant()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006640 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006641 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6642 } else {
6643 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006644 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006645 vixl32::Register temp = temps.Acquire();
6646 __ Add(temp, array, data_offset);
6647 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6648 }
6649 codegen_->MaybeRecordImplicitNullCheck(instruction);
6650 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006651 DCHECK(!needs_type_check);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006652 break;
6653 }
6654
6655 DCHECK(needs_write_barrier);
6656 Location temp1_loc = locations->GetTemp(0);
6657 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6658 Location temp2_loc = locations->GetTemp(1);
6659 vixl32::Register temp2 = RegisterFrom(temp2_loc);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006660
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006661 bool can_value_be_null = instruction->GetValueCanBeNull();
6662 vixl32::Label do_store;
6663 if (can_value_be_null) {
6664 __ CompareAndBranchIfZero(value, &do_store, /* is_far_target= */ false);
6665 }
6666
6667 SlowPathCodeARMVIXL* slow_path = nullptr;
6668 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006669 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006670 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006671
6672 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6673 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6674 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006675
6676 // Note that when read barriers are enabled, the type checks
6677 // are performed without read barriers. This is fine, even in
6678 // the case where a class object is in the from-space after
6679 // the flip, as a comparison involving such a type would not
6680 // produce a false positive; it may of course produce a false
6681 // negative, in which case we would take the ArraySet slow
6682 // path.
6683
Alexandre Rames374ddf32016-11-04 10:40:49 +00006684 {
6685 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006686 ExactAssemblyScope aas(GetVIXLAssembler(),
6687 vixl32::kMaxInstructionSizeInBytes,
6688 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006689 // /* HeapReference<Class> */ temp1 = array->klass_
6690 __ ldr(temp1, MemOperand(array, class_offset));
6691 codegen_->MaybeRecordImplicitNullCheck(instruction);
6692 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006693 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6694
6695 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6696 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6697 // /* HeapReference<Class> */ temp2 = value->klass_
6698 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6699 // If heap poisoning is enabled, no need to unpoison `temp1`
6700 // nor `temp2`, as we are comparing two poisoned references.
6701 __ Cmp(temp1, temp2);
6702
6703 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006704 vixl32::Label do_put;
6705 __ B(eq, &do_put, /* is_far_target= */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006706 // If heap poisoning is enabled, the `temp1` reference has
6707 // not been unpoisoned yet; unpoison it now.
6708 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6709
6710 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6711 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6712 // If heap poisoning is enabled, no need to unpoison
6713 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006714 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006715 __ Bind(&do_put);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006716 } else {
6717 __ B(ne, slow_path->GetEntryLabel());
6718 }
6719 }
6720
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006721 codegen_->MarkGCCard(temp1, temp2, array, value, /* can_be_null= */ false);
6722
6723 if (can_value_be_null) {
6724 DCHECK(do_store.IsReferenced());
6725 __ Bind(&do_store);
6726 }
6727
Scott Wakelingc34dba72016-10-03 10:14:44 +01006728 vixl32::Register source = value;
6729 if (kPoisonHeapReferences) {
6730 // Note that in the case where `value` is a null reference,
6731 // we do not enter this block, as a null reference does not
6732 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006733 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006734 __ Mov(temp1, value);
6735 GetAssembler()->PoisonHeapReference(temp1);
6736 source = temp1;
6737 }
6738
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006739 {
6740 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6741 // As two macro instructions can be emitted the max size is doubled.
6742 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
6743 if (index.IsConstant()) {
6744 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6745 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6746 } else {
6747 DCHECK(index.IsRegister()) << index;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006748
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006749 UseScratchRegisterScope temps(GetVIXLAssembler());
6750 vixl32::Register temp = temps.Acquire();
6751 __ Add(temp, array, data_offset);
6752 codegen_->StoreToShiftedRegOffset(value_type,
6753 LocationFrom(source),
6754 temp,
6755 RegisterFrom(index));
6756 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006757
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006758 if (can_value_be_null || !needs_type_check) {
6759 codegen_->MaybeRecordImplicitNullCheck(instruction);
6760 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006761 }
6762
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006763 if (slow_path != nullptr) {
6764 __ Bind(slow_path->GetExitLabel());
6765 }
6766
Scott Wakelingc34dba72016-10-03 10:14:44 +01006767 break;
6768 }
6769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006770 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006771 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6772 // As two macro instructions can be emitted the max size is doubled.
6773 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006774 Location value = locations->InAt(2);
6775 if (index.IsConstant()) {
6776 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006777 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006778 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6779 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006780 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006781 vixl32::Register temp = temps.Acquire();
6782 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6783 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6784 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006786 break;
6787 }
6788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006789 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006790 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6791 // As two macro instructions can be emitted the max size is doubled.
6792 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006793 Location value = locations->InAt(2);
6794 DCHECK(value.IsFpuRegister());
6795 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006796 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006797 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6798 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006799 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006800 vixl32::Register temp = temps.Acquire();
6801 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6802 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6803 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006804 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006805 break;
6806 }
6807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006808 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006809 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6810 // As two macro instructions can be emitted the max size is doubled.
6811 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006812 Location value = locations->InAt(2);
6813 DCHECK(value.IsFpuRegisterPair());
6814 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006815 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006816 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6817 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006818 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006819 vixl32::Register temp = temps.Acquire();
6820 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6821 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6822 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006823 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006824 break;
6825 }
6826
Aart Bik66c158e2018-01-31 12:55:04 -08006827 case DataType::Type::kUint32:
6828 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006829 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006830 LOG(FATAL) << "Unreachable type " << value_type;
6831 UNREACHABLE();
6832 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006833}
6834
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006835void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6836 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006837 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006838 locations->SetInAt(0, Location::RequiresRegister());
6839 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6840}
6841
6842void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6843 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6844 vixl32::Register obj = InputRegisterAt(instruction, 0);
6845 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006846 {
Artem Serov0fb37192016-12-06 18:13:40 +00006847 ExactAssemblyScope aas(GetVIXLAssembler(),
6848 vixl32::kMaxInstructionSizeInBytes,
6849 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006850 __ ldr(out, MemOperand(obj, offset));
6851 codegen_->MaybeRecordImplicitNullCheck(instruction);
6852 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006853 // Mask out compression flag from String's array length.
6854 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006855 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006856 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006857}
6858
Artem Serov2bbc9532016-10-21 11:51:50 +01006859void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006860 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006861 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006862
6863 locations->SetInAt(0, Location::RequiresRegister());
6864 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6866}
6867
6868void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6869 vixl32::Register out = OutputRegister(instruction);
6870 vixl32::Register first = InputRegisterAt(instruction, 0);
6871 Location second = instruction->GetLocations()->InAt(1);
6872
Artem Serov2bbc9532016-10-21 11:51:50 +01006873 if (second.IsRegister()) {
6874 __ Add(out, first, RegisterFrom(second));
6875 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006876 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006877 }
6878}
6879
Artem Serove1811ed2017-04-27 16:50:47 +01006880void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6881 HIntermediateAddressIndex* instruction) {
6882 LOG(FATAL) << "Unreachable " << instruction->GetId();
6883}
6884
6885void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6886 HIntermediateAddressIndex* instruction) {
6887 LOG(FATAL) << "Unreachable " << instruction->GetId();
6888}
6889
Scott Wakelingc34dba72016-10-03 10:14:44 +01006890void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6891 RegisterSet caller_saves = RegisterSet::Empty();
6892 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6893 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6894 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6895 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006896
6897 HInstruction* index = instruction->InputAt(0);
6898 HInstruction* length = instruction->InputAt(1);
6899 // If both index and length are constants we can statically check the bounds. But if at least one
6900 // of them is not encodable ArmEncodableConstantOrRegister will create
6901 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6902 // locations.
6903 bool both_const = index->IsConstant() && length->IsConstant();
6904 locations->SetInAt(0, both_const
6905 ? Location::ConstantLocation(index->AsConstant())
6906 : ArmEncodableConstantOrRegister(index, CMP));
6907 locations->SetInAt(1, both_const
6908 ? Location::ConstantLocation(length->AsConstant())
6909 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006910}
6911
6912void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006913 LocationSummary* locations = instruction->GetLocations();
6914 Location index_loc = locations->InAt(0);
6915 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006916
Artem Serov2dd053d2017-03-08 14:54:06 +00006917 if (length_loc.IsConstant()) {
6918 int32_t length = Int32ConstantFrom(length_loc);
6919 if (index_loc.IsConstant()) {
6920 // BCE will remove the bounds check if we are guaranteed to pass.
6921 int32_t index = Int32ConstantFrom(index_loc);
6922 if (index < 0 || index >= length) {
6923 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006924 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006925 codegen_->AddSlowPath(slow_path);
6926 __ B(slow_path->GetEntryLabel());
6927 } else {
6928 // Some optimization after BCE may have generated this, and we should not
6929 // generate a bounds check if it is a valid range.
6930 }
6931 return;
6932 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006933
Artem Serov2dd053d2017-03-08 14:54:06 +00006934 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006935 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006936 __ Cmp(RegisterFrom(index_loc), length);
6937 codegen_->AddSlowPath(slow_path);
6938 __ B(hs, slow_path->GetEntryLabel());
6939 } else {
6940 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006941 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006942 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6943 codegen_->AddSlowPath(slow_path);
6944 __ B(ls, slow_path->GetEntryLabel());
6945 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006946}
6947
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006948void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6949 vixl32::Register card,
6950 vixl32::Register object,
6951 vixl32::Register value,
6952 bool can_be_null) {
6953 vixl32::Label is_null;
6954 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006955 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006956 }
Roland Levillainc73f0522018-08-14 15:16:50 +01006957 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006958 GetAssembler()->LoadFromOffset(
6959 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01006960 // Calculate the offset (in the card table) of the card corresponding to
6961 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00006962 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01006963 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
6964 // `object`'s card.
6965 //
6966 // Register `card` contains the address of the card table. Note that the card
6967 // table's base is biased during its creation so that it always starts at an
6968 // address whose least-significant byte is equal to `kCardDirty` (see
6969 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
6970 // below writes the `kCardDirty` (byte) value into the `object`'s card
6971 // (located at `card + object >> kCardShift`).
6972 //
6973 // This dual use of the value in register `card` (1. to calculate the location
6974 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
6975 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006976 __ Strb(card, MemOperand(card, temp));
6977 if (can_be_null) {
6978 __ Bind(&is_null);
6979 }
6980}
6981
Scott Wakelingfe885462016-09-22 10:24:38 +01006982void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6983 LOG(FATAL) << "Unreachable";
6984}
6985
6986void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006987 if (instruction->GetNext()->IsSuspendCheck() &&
6988 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6989 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6990 // The back edge will generate the suspend check.
6991 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6992 }
6993
Scott Wakelingfe885462016-09-22 10:24:38 +01006994 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6995}
6996
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006997void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006998 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6999 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00007000 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007001}
7002
7003void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
7004 HBasicBlock* block = instruction->GetBlock();
7005 if (block->GetLoopInformation() != nullptr) {
7006 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7007 // The back edge will generate the suspend check.
7008 return;
7009 }
7010 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7011 // The goto will generate the suspend check.
7012 return;
7013 }
7014 GenerateSuspendCheck(instruction, nullptr);
Andra Danciua0130e82020-07-23 12:34:56 +00007015 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007016}
7017
7018void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
7019 HBasicBlock* successor) {
7020 SuspendCheckSlowPathARMVIXL* slow_path =
7021 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
7022 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007023 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007024 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007025 instruction->SetSlowPath(slow_path);
7026 codegen_->AddSlowPath(slow_path);
7027 if (successor != nullptr) {
7028 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007029 }
7030 } else {
7031 DCHECK_EQ(slow_path->GetSuccessor(), successor);
7032 }
7033
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007034 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007035 vixl32::Register temp = temps.Acquire();
7036 GetAssembler()->LoadFromOffset(
7037 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
7038 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007039 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007040 __ Bind(slow_path->GetReturnLabel());
7041 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007042 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007043 __ B(slow_path->GetEntryLabel());
7044 }
7045}
7046
Scott Wakelingfe885462016-09-22 10:24:38 +01007047ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
7048 return codegen_->GetAssembler();
7049}
7050
7051void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007052 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01007053 MoveOperands* move = moves_[index];
7054 Location source = move->GetSource();
7055 Location destination = move->GetDestination();
7056
7057 if (source.IsRegister()) {
7058 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007059 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007060 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007061 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007062 } else {
7063 DCHECK(destination.IsStackSlot());
7064 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007065 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007066 sp,
7067 destination.GetStackIndex());
7068 }
7069 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007070 if (destination.IsRegister()) {
7071 GetAssembler()->LoadFromOffset(kLoadWord,
7072 RegisterFrom(destination),
7073 sp,
7074 source.GetStackIndex());
7075 } else if (destination.IsFpuRegister()) {
7076 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
7077 } else {
7078 DCHECK(destination.IsStackSlot());
7079 vixl32::Register temp = temps.Acquire();
7080 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
7081 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7082 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007083 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007084 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007085 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007086 } else if (destination.IsFpuRegister()) {
7087 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
7088 } else {
7089 DCHECK(destination.IsStackSlot());
7090 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
7091 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007092 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007093 if (destination.IsDoubleStackSlot()) {
7094 vixl32::DRegister temp = temps.AcquireD();
7095 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
7096 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
7097 } else if (destination.IsRegisterPair()) {
7098 DCHECK(ExpectedPairLayout(destination));
7099 GetAssembler()->LoadFromOffset(
7100 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
7101 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007102 DCHECK(destination.IsFpuRegisterPair()) << destination;
7103 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007104 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007105 } else if (source.IsRegisterPair()) {
7106 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007107 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
7108 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007109 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007110 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01007111 } else {
7112 DCHECK(destination.IsDoubleStackSlot()) << destination;
7113 DCHECK(ExpectedPairLayout(source));
7114 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007115 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01007116 sp,
7117 destination.GetStackIndex());
7118 }
7119 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007120 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007121 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01007122 } else if (destination.IsFpuRegisterPair()) {
7123 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
7124 } else {
7125 DCHECK(destination.IsDoubleStackSlot()) << destination;
7126 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
7127 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007128 } else {
7129 DCHECK(source.IsConstant()) << source;
7130 HConstant* constant = source.GetConstant();
7131 if (constant->IsIntConstant() || constant->IsNullConstant()) {
7132 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
7133 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007134 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007135 } else {
7136 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007137 vixl32::Register temp = temps.Acquire();
7138 __ Mov(temp, value);
7139 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7140 }
7141 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00007142 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01007143 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007144 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
7145 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007146 } else {
7147 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01007148 vixl32::Register temp = temps.Acquire();
7149 __ Mov(temp, Low32Bits(value));
7150 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7151 __ Mov(temp, High32Bits(value));
7152 GetAssembler()->StoreToOffset(kStoreWord,
7153 temp,
7154 sp,
7155 destination.GetHighStackIndex(kArmWordSize));
7156 }
7157 } else if (constant->IsDoubleConstant()) {
7158 double value = constant->AsDoubleConstant()->GetValue();
7159 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01007160 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007161 } else {
7162 DCHECK(destination.IsDoubleStackSlot()) << destination;
7163 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007164 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007165 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007166 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007167 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007168 GetAssembler()->StoreToOffset(kStoreWord,
7169 temp,
7170 sp,
7171 destination.GetHighStackIndex(kArmWordSize));
7172 }
7173 } else {
7174 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
7175 float value = constant->AsFloatConstant()->GetValue();
7176 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007177 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01007178 } else {
7179 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01007180 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007181 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01007182 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7183 }
7184 }
7185 }
7186}
7187
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007188void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7189 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7190 vixl32::Register temp = temps.Acquire();
7191 __ Mov(temp, reg);
7192 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7193 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007194}
7195
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007196void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7197 // TODO(VIXL32): Double check the performance of this implementation.
7198 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007199 vixl32::Register temp1 = temps.Acquire();
7200 ScratchRegisterScope ensure_scratch(
7201 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7202 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007203
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007204 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7205 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7206 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7207 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7208 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007209}
7210
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007211void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7212 MoveOperands* move = moves_[index];
7213 Location source = move->GetSource();
7214 Location destination = move->GetDestination();
7215 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7216
7217 if (source.IsRegister() && destination.IsRegister()) {
7218 vixl32::Register temp = temps.Acquire();
7219 DCHECK(!RegisterFrom(source).Is(temp));
7220 DCHECK(!RegisterFrom(destination).Is(temp));
7221 __ Mov(temp, RegisterFrom(destination));
7222 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7223 __ Mov(RegisterFrom(source), temp);
7224 } else if (source.IsRegister() && destination.IsStackSlot()) {
7225 Exchange(RegisterFrom(source), destination.GetStackIndex());
7226 } else if (source.IsStackSlot() && destination.IsRegister()) {
7227 Exchange(RegisterFrom(destination), source.GetStackIndex());
7228 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007229 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007230 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007231 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007232 __ Vmov(temp, SRegisterFrom(source));
7233 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7234 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007235 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7236 vixl32::DRegister temp = temps.AcquireD();
7237 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7238 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7239 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7240 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7241 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7242 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7243 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7244 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7245 vixl32::DRegister temp = temps.AcquireD();
7246 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7247 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7248 GetAssembler()->StoreDToOffset(temp, sp, mem);
7249 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007250 vixl32::DRegister first = DRegisterFrom(source);
7251 vixl32::DRegister second = DRegisterFrom(destination);
7252 vixl32::DRegister temp = temps.AcquireD();
7253 __ Vmov(temp, first);
7254 __ Vmov(first, second);
7255 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007256 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007257 vixl32::DRegister reg = source.IsFpuRegisterPair()
7258 ? DRegisterFrom(source)
7259 : DRegisterFrom(destination);
7260 int mem = source.IsFpuRegisterPair()
7261 ? destination.GetStackIndex()
7262 : source.GetStackIndex();
7263 vixl32::DRegister temp = temps.AcquireD();
7264 __ Vmov(temp, reg);
7265 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7266 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007267 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007268 vixl32::SRegister reg = source.IsFpuRegister()
7269 ? SRegisterFrom(source)
7270 : SRegisterFrom(destination);
7271 int mem = source.IsFpuRegister()
7272 ? destination.GetStackIndex()
7273 : source.GetStackIndex();
7274 vixl32::Register temp = temps.Acquire();
7275 __ Vmov(temp, reg);
7276 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7277 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007278 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7279 vixl32::DRegister temp1 = temps.AcquireD();
7280 vixl32::DRegister temp2 = temps.AcquireD();
7281 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7282 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7283 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7284 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7285 } else {
7286 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7287 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007288}
7289
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007290void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7291 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007292}
7293
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007294void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7295 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007296}
7297
Artem Serov02d37832016-10-25 15:25:33 +01007298HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007299 HLoadClass::LoadKind desired_class_load_kind) {
7300 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007301 case HLoadClass::LoadKind::kInvalid:
7302 LOG(FATAL) << "UNREACHABLE";
7303 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007304 case HLoadClass::LoadKind::kReferrersClass:
7305 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007306 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007307 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007308 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007309 case HLoadClass::LoadKind::kBssEntryPublic:
7310 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007311 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007312 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007313 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007314 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007315 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007316 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007317 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007318 break;
7319 }
7320 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007321}
7322
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007323void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007324 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007325 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007326 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007327 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007328 cls,
7329 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007330 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007331 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007332 return;
7333 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007334 DCHECK_EQ(cls->NeedsAccessCheck(),
7335 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7336 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelingfe885462016-09-22 10:24:38 +01007337
Artem Serovd4cc5b22016-11-04 11:19:09 +00007338 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7339 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007340 ? LocationSummary::kCallOnSlowPath
7341 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007342 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007343 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007344 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007345 }
7346
Vladimir Marko41559982017-01-06 14:04:23 +00007347 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007348 locations->SetInAt(0, Location::RequiresRegister());
7349 }
7350 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007351 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7352 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7353 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007354 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007355 } else {
7356 // For non-Baker read barrier we have a temp-clobbering call.
7357 }
7358 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007359}
7360
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007361// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7362// move.
7363void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007364 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007365 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007366 codegen_->GenerateLoadClassRuntimeCall(cls);
Andra Danciua0130e82020-07-23 12:34:56 +00007367 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007368 return;
7369 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007370 DCHECK_EQ(cls->NeedsAccessCheck(),
7371 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7372 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007373
Vladimir Marko41559982017-01-06 14:04:23 +00007374 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007375 Location out_loc = locations->Out();
7376 vixl32::Register out = OutputRegister(cls);
7377
Artem Serovd4cc5b22016-11-04 11:19:09 +00007378 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7379 ? kWithoutReadBarrier
7380 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007381 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007382 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007383 case HLoadClass::LoadKind::kReferrersClass: {
7384 DCHECK(!cls->CanCallRuntime());
7385 DCHECK(!cls->MustGenerateClinitCheck());
7386 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7387 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007388 codegen_->GenerateGcRootFieldLoad(cls,
7389 out_loc,
7390 current_method,
7391 ArtMethod::DeclaringClassOffset().Int32Value(),
7392 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007393 break;
7394 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007395 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007396 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7397 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007398 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7399 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007400 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007401 codegen_->EmitMovwMovtPlaceholder(labels, out);
7402 break;
7403 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007404 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007405 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7406 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007407 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007408 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007409 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007410 break;
7411 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007412 case HLoadClass::LoadKind::kBssEntry:
7413 case HLoadClass::LoadKind::kBssEntryPublic:
7414 case HLoadClass::LoadKind::kBssEntryPackage: {
7415 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels = codegen_->NewTypeBssEntryPatch(cls);
Vladimir Markof3c52b42017-11-17 17:32:12 +00007416 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007417 // All aligned loads are implicitly atomic consume operations on ARM.
Andreas Gampe3db70682018-12-26 15:12:03 -08007418 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007419 generate_null_check = true;
7420 break;
7421 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007422 case HLoadClass::LoadKind::kJitBootImageAddress: {
7423 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7424 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7425 DCHECK_NE(address, 0u);
7426 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7427 break;
7428 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007429 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007430 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7431 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007432 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007433 // /* GcRoot<mirror::Class> */ out = *out
Andreas Gampe3db70682018-12-26 15:12:03 -08007434 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007435 break;
7436 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007437 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007438 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007439 LOG(FATAL) << "UNREACHABLE";
7440 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007441 }
7442
7443 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7444 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007445 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007446 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007447 codegen_->AddSlowPath(slow_path);
7448 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007449 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007450 }
7451 if (cls->MustGenerateClinitCheck()) {
7452 GenerateClassInitializationCheck(slow_path, out);
7453 } else {
7454 __ Bind(slow_path->GetExitLabel());
7455 }
Andra Danciua0130e82020-07-23 12:34:56 +00007456 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 16);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007457 }
7458}
7459
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007460void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7461 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7462 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7463 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7464}
7465
7466void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7467 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7468}
7469
Orion Hodson18259d72018-04-12 11:18:23 +01007470void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7471 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7472 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7473 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7474}
7475
7476void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7477 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7478}
7479
Artem Serov02d37832016-10-25 15:25:33 +01007480void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7481 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007482 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007483 locations->SetInAt(0, Location::RequiresRegister());
7484 if (check->HasUses()) {
7485 locations->SetOut(Location::SameAsFirstInput());
7486 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007487 // Rely on the type initialization to save everything we need.
7488 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007489}
7490
7491void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7492 // We assume the class is not null.
7493 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007494 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007495 codegen_->AddSlowPath(slow_path);
7496 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7497}
7498
7499void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7500 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7501 UseScratchRegisterScope temps(GetVIXLAssembler());
7502 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007503 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Markobf121912019-06-04 13:49:05 +01007504 constexpr uint32_t shifted_visibly_initialized_value =
7505 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << status_lsb_position;
Vladimir Markodc682aa2018-01-04 18:42:57 +00007506
Vladimir Markobf121912019-06-04 13:49:05 +01007507 const size_t status_offset = mirror::Class::StatusOffset().SizeValue();
7508 GetAssembler()->LoadFromOffset(kLoadWord, temp, class_reg, status_offset);
7509 __ Cmp(temp, shifted_visibly_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007510 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007511 __ Bind(slow_path->GetExitLabel());
7512}
7513
Vladimir Marko175e7862018-03-27 09:03:13 +00007514void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7515 HTypeCheckInstruction* check,
7516 vixl32::Register temp,
7517 vixl32::FlagsUpdate flags_update) {
7518 uint32_t path_to_root = check->GetBitstringPathToRoot();
7519 uint32_t mask = check->GetBitstringMask();
7520 DCHECK(IsPowerOfTwo(mask + 1));
7521 size_t mask_bits = WhichPowerOf2(mask + 1);
7522
7523 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7524 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7525 if (mask_bits == 16u) {
7526 // Load only the bitstring part of the status word.
7527 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7528 // Check if the bitstring bits are equal to `path_to_root`.
7529 if (flags_update == SetFlags) {
7530 __ Cmp(temp, path_to_root);
7531 } else {
7532 __ Sub(temp, temp, path_to_root);
7533 }
7534 } else {
7535 // /* uint32_t */ temp = temp->status_
7536 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7537 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7538 // Compare the bitstring bits using SUB.
7539 __ Sub(temp, temp, path_to_root);
7540 // Shift out bits that do not contribute to the comparison.
7541 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7542 } else if (IsUint<16>(path_to_root)) {
7543 if (temp.IsLow()) {
7544 // Note: Optimized for size but contains one more dependent instruction than necessary.
7545 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7546 // macro assembler would use the high reg IP for the constant by default.
7547 // Compare the bitstring bits using SUB.
7548 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7549 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7550 // Shift out bits that do not contribute to the comparison.
7551 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7552 } else {
7553 // Extract the bitstring bits.
7554 __ Ubfx(temp, temp, 0, mask_bits);
7555 // Check if the bitstring bits are equal to `path_to_root`.
7556 if (flags_update == SetFlags) {
7557 __ Cmp(temp, path_to_root);
7558 } else {
7559 __ Sub(temp, temp, path_to_root);
7560 }
7561 }
7562 } else {
7563 // Shift out bits that do not contribute to the comparison.
7564 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7565 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7566 if (flags_update == SetFlags) {
7567 __ Cmp(temp, path_to_root << (32u - mask_bits));
7568 } else {
7569 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7570 }
7571 }
7572 }
7573}
7574
Artem Serov02d37832016-10-25 15:25:33 +01007575HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007576 HLoadString::LoadKind desired_string_load_kind) {
7577 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007578 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007579 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007580 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007581 DCHECK(!GetCompilerOptions().IsJitCompiler());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007582 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007583 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007584 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007585 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007586 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007587 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007588 break;
7589 }
7590 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007591}
7592
7593void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007594 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007595 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007596 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007597 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007598 locations->SetOut(LocationFrom(r0));
7599 } else {
7600 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007601 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7602 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007603 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007604 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007605 } else {
7606 // For non-Baker read barrier we have a temp-clobbering call.
7607 }
7608 }
Artem Serov02d37832016-10-25 15:25:33 +01007609 }
7610}
7611
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007612// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7613// move.
7614void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007615 LocationSummary* locations = load->GetLocations();
7616 Location out_loc = locations->Out();
7617 vixl32::Register out = OutputRegister(load);
7618 HLoadString::LoadKind load_kind = load->GetLoadKind();
7619
7620 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007621 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007622 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7623 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007624 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007625 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007626 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007627 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007628 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007629 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007630 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7631 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007632 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007633 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007634 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007635 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007636 }
7637 case HLoadString::LoadKind::kBssEntry: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007638 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007639 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007640 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007641 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007642 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007643 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007644 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007645 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007646 codegen_->AddSlowPath(slow_path);
7647 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7648 __ Bind(slow_path->GetExitLabel());
Andra Danciua0130e82020-07-23 12:34:56 +00007649 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 17);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007650 return;
7651 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007652 case HLoadString::LoadKind::kJitBootImageAddress: {
7653 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7654 DCHECK_NE(address, 0u);
7655 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7656 return;
7657 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007658 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007659 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007660 load->GetStringIndex(),
7661 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007662 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007663 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007664 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007665 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007666 }
7667 default:
7668 break;
7669 }
Artem Serov02d37832016-10-25 15:25:33 +01007670
7671 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007672 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007673 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007674 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007675 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7676 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andra Danciua0130e82020-07-23 12:34:56 +00007677 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 18);
Artem Serov02d37832016-10-25 15:25:33 +01007678}
7679
7680static int32_t GetExceptionTlsOffset() {
7681 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7682}
7683
7684void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7685 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007686 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007687 locations->SetOut(Location::RequiresRegister());
7688}
7689
7690void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7691 vixl32::Register out = OutputRegister(load);
7692 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7693}
7694
7695
7696void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007697 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007698}
7699
7700void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7701 UseScratchRegisterScope temps(GetVIXLAssembler());
7702 vixl32::Register temp = temps.Acquire();
7703 __ Mov(temp, 0);
7704 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7705}
7706
7707void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007708 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7709 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007710 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7711 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7712}
7713
7714void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7715 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7716 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7717}
7718
Artem Serov657022c2016-11-23 14:19:38 +00007719// Temp is used for read barrier.
7720static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7721 if (kEmitCompilerReadBarrier &&
7722 (kUseBakerReadBarrier ||
7723 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7724 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7725 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7726 return 1;
7727 }
7728 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007729}
7730
Artem Serov657022c2016-11-23 14:19:38 +00007731// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7732// interface pointer, one for loading the current interface.
7733// The other checks have one temp for loading the object's class.
7734static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7735 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7736 return 3;
7737 }
7738 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7739}
Artem Serovcfbe9132016-10-14 15:58:56 +01007740
7741void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7742 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7743 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7744 bool baker_read_barrier_slow_path = false;
7745 switch (type_check_kind) {
7746 case TypeCheckKind::kExactCheck:
7747 case TypeCheckKind::kAbstractClassCheck:
7748 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007749 case TypeCheckKind::kArrayObjectCheck: {
7750 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7751 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7752 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007753 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007754 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007755 case TypeCheckKind::kArrayCheck:
7756 case TypeCheckKind::kUnresolvedCheck:
7757 case TypeCheckKind::kInterfaceCheck:
7758 call_kind = LocationSummary::kCallOnSlowPath;
7759 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007760 case TypeCheckKind::kBitstringCheck:
7761 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007762 }
7763
Vladimir Markoca6fff82017-10-03 14:49:14 +01007764 LocationSummary* locations =
7765 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007766 if (baker_read_barrier_slow_path) {
7767 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7768 }
7769 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007770 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7771 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7772 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7773 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7774 } else {
7775 locations->SetInAt(1, Location::RequiresRegister());
7776 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007777 // The "out" register is used as a temporary, so it overlaps with the inputs.
7778 // Note that TypeCheckSlowPathARM uses this register too.
7779 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007780 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007781}
7782
7783void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7784 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7785 LocationSummary* locations = instruction->GetLocations();
7786 Location obj_loc = locations->InAt(0);
7787 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007788 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7789 ? vixl32::Register()
7790 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007791 Location out_loc = locations->Out();
7792 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007793 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7794 DCHECK_LE(num_temps, 1u);
7795 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007796 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7797 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7798 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7799 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007800 vixl32::Label done;
7801 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007802 SlowPathCodeARMVIXL* slow_path = nullptr;
7803
7804 // Return 0 if `obj` is null.
7805 // avoid null check if we know obj is not null.
7806 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007807 DCHECK(!out.Is(obj));
7808 __ Mov(out, 0);
Andreas Gampe3db70682018-12-26 15:12:03 -08007809 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007810 }
7811
Artem Serovcfbe9132016-10-14 15:58:56 +01007812 switch (type_check_kind) {
7813 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007814 ReadBarrierOption read_barrier_option =
7815 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007816 // /* HeapReference<Class> */ out = obj->klass_
7817 GenerateReferenceLoadTwoRegisters(instruction,
7818 out_loc,
7819 obj_loc,
7820 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007821 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007822 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007823 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007824 __ Cmp(out, cls);
7825 // We speculatively set the result to false without changing the condition
7826 // flags, which allows us to avoid some branching later.
7827 __ Mov(LeaveFlags, out, 0);
7828
7829 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7830 // we check that the output is in a low register, so that a 16-bit MOV
7831 // encoding can be used.
7832 if (out.IsLow()) {
7833 // We use the scope because of the IT block that follows.
7834 ExactAssemblyScope guard(GetVIXLAssembler(),
7835 2 * vixl32::k16BitT32InstructionSizeInBytes,
7836 CodeBufferCheckScope::kExactSize);
7837
7838 __ it(eq);
7839 __ mov(eq, out, 1);
7840 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007841 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007842 __ Mov(out, 1);
7843 }
7844
Artem Serovcfbe9132016-10-14 15:58:56 +01007845 break;
7846 }
7847
7848 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007849 ReadBarrierOption read_barrier_option =
7850 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007851 // /* HeapReference<Class> */ out = obj->klass_
7852 GenerateReferenceLoadTwoRegisters(instruction,
7853 out_loc,
7854 obj_loc,
7855 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007856 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007857 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007858 // If the class is abstract, we eagerly fetch the super class of the
7859 // object to avoid doing a comparison we know will fail.
7860 vixl32::Label loop;
7861 __ Bind(&loop);
7862 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007863 GenerateReferenceLoadOneRegister(instruction,
7864 out_loc,
7865 super_offset,
7866 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007867 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007868 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007869 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007870 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007871 __ B(ne, &loop, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007872 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007873 break;
7874 }
7875
7876 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007877 ReadBarrierOption read_barrier_option =
7878 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007879 // /* HeapReference<Class> */ out = obj->klass_
7880 GenerateReferenceLoadTwoRegisters(instruction,
7881 out_loc,
7882 obj_loc,
7883 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007884 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007885 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007886 // Walk over the class hierarchy to find a match.
7887 vixl32::Label loop, success;
7888 __ Bind(&loop);
7889 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007890 __ B(eq, &success, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007891 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007892 GenerateReferenceLoadOneRegister(instruction,
7893 out_loc,
7894 super_offset,
7895 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007896 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007897 // This is essentially a null check, but it sets the condition flags to the
7898 // proper value for the code that follows the loop, i.e. not `eq`.
7899 __ Cmp(out, 1);
Andreas Gampe3db70682018-12-26 15:12:03 -08007900 __ B(hs, &loop, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007901
7902 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7903 // we check that the output is in a low register, so that a 16-bit MOV
7904 // encoding can be used.
7905 if (out.IsLow()) {
7906 // If `out` is null, we use it for the result, and the condition flags
7907 // have already been set to `ne`, so the IT block that comes afterwards
7908 // (and which handles the successful case) turns into a NOP (instead of
7909 // overwriting `out`).
7910 __ Bind(&success);
7911
7912 // We use the scope because of the IT block that follows.
7913 ExactAssemblyScope guard(GetVIXLAssembler(),
7914 2 * vixl32::k16BitT32InstructionSizeInBytes,
7915 CodeBufferCheckScope::kExactSize);
7916
7917 // There is only one branch to the `success` label (which is bound to this
7918 // IT block), and it has the same condition, `eq`, so in that case the MOV
7919 // is executed.
7920 __ it(eq);
7921 __ mov(eq, out, 1);
7922 } else {
7923 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007924 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007925 __ Bind(&success);
7926 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007927 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007928
Artem Serovcfbe9132016-10-14 15:58:56 +01007929 break;
7930 }
7931
7932 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007933 ReadBarrierOption read_barrier_option =
7934 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007935 // /* HeapReference<Class> */ out = obj->klass_
7936 GenerateReferenceLoadTwoRegisters(instruction,
7937 out_loc,
7938 obj_loc,
7939 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007940 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007941 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007942 // Do an exact check.
7943 vixl32::Label exact_check;
7944 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007945 __ B(eq, &exact_check, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007946 // Otherwise, we need to check that the object's class is a non-primitive array.
7947 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007948 GenerateReferenceLoadOneRegister(instruction,
7949 out_loc,
7950 component_offset,
7951 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007952 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007953 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007954 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007955 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7956 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007957 __ Cmp(out, 0);
7958 // We speculatively set the result to false without changing the condition
7959 // flags, which allows us to avoid some branching later.
7960 __ Mov(LeaveFlags, out, 0);
7961
7962 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7963 // we check that the output is in a low register, so that a 16-bit MOV
7964 // encoding can be used.
7965 if (out.IsLow()) {
7966 __ Bind(&exact_check);
7967
7968 // We use the scope because of the IT block that follows.
7969 ExactAssemblyScope guard(GetVIXLAssembler(),
7970 2 * vixl32::k16BitT32InstructionSizeInBytes,
7971 CodeBufferCheckScope::kExactSize);
7972
7973 __ it(eq);
7974 __ mov(eq, out, 1);
7975 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007976 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007977 __ Bind(&exact_check);
7978 __ Mov(out, 1);
7979 }
7980
Artem Serovcfbe9132016-10-14 15:58:56 +01007981 break;
7982 }
7983
7984 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007985 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007986 // /* HeapReference<Class> */ out = obj->klass_
7987 GenerateReferenceLoadTwoRegisters(instruction,
7988 out_loc,
7989 obj_loc,
7990 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007991 maybe_temp_loc,
7992 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007993 __ Cmp(out, cls);
7994 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007995 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08007996 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007997 codegen_->AddSlowPath(slow_path);
7998 __ B(ne, slow_path->GetEntryLabel());
7999 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01008000 break;
8001 }
8002
8003 case TypeCheckKind::kUnresolvedCheck:
8004 case TypeCheckKind::kInterfaceCheck: {
8005 // Note that we indeed only call on slow path, but we always go
8006 // into the slow path for the unresolved and interface check
8007 // cases.
8008 //
8009 // We cannot directly call the InstanceofNonTrivial runtime
8010 // entry point without resorting to a type checking slow path
8011 // here (i.e. by calling InvokeRuntime directly), as it would
8012 // require to assign fixed registers for the inputs of this
8013 // HInstanceOf instruction (following the runtime calling
8014 // convention), which might be cluttered by the potential first
8015 // read barrier emission at the beginning of this method.
8016 //
8017 // TODO: Introduce a new runtime entry point taking the object
8018 // to test (instead of its class) as argument, and let it deal
8019 // with the read barrier issues. This will let us refactor this
8020 // case of the `switch` code as it was previously (with a direct
8021 // call to the runtime not using a type checking slow path).
8022 // This should also be beneficial for the other cases above.
8023 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008024 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08008025 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008026 codegen_->AddSlowPath(slow_path);
8027 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008028 break;
8029 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008030
8031 case TypeCheckKind::kBitstringCheck: {
8032 // /* HeapReference<Class> */ temp = obj->klass_
8033 GenerateReferenceLoadTwoRegisters(instruction,
8034 out_loc,
8035 obj_loc,
8036 class_offset,
8037 maybe_temp_loc,
8038 kWithoutReadBarrier);
8039
8040 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
8041 // If `out` is a low reg and we would have another low reg temp, we could
8042 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
8043 //
8044 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
8045 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
8046 // would be the same and we would have fewer direct data dependencies.
8047 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
8048 break;
8049 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008050 }
8051
Artem Serovcfbe9132016-10-14 15:58:56 +01008052 if (done.IsReferenced()) {
8053 __ Bind(&done);
8054 }
8055
8056 if (slow_path != nullptr) {
8057 __ Bind(slow_path->GetExitLabel());
8058 }
8059}
8060
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008061void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008062 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00008063 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008064 LocationSummary* locations =
8065 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008066 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00008067 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
8068 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
8069 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
8070 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
8071 } else {
8072 locations->SetInAt(1, Location::RequiresRegister());
8073 }
Artem Serov657022c2016-11-23 14:19:38 +00008074 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008075}
8076
8077void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
8078 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
8079 LocationSummary* locations = instruction->GetLocations();
8080 Location obj_loc = locations->InAt(0);
8081 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00008082 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
8083 ? vixl32::Register()
8084 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008085 Location temp_loc = locations->GetTemp(0);
8086 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00008087 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
8088 DCHECK_LE(num_temps, 3u);
8089 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
8090 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
8091 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
8092 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
8093 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
8094 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
8095 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
8096 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
8097 const uint32_t object_array_data_offset =
8098 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008099
Vladimir Marko87584542017-12-12 17:47:52 +00008100 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008101 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008102 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
8103 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008104 codegen_->AddSlowPath(type_check_slow_path);
8105
8106 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00008107 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008108 // Avoid null check if we know obj is not null.
8109 if (instruction->MustDoNullCheck()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08008110 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008111 }
8112
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008113 switch (type_check_kind) {
8114 case TypeCheckKind::kExactCheck:
8115 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008116 // /* HeapReference<Class> */ temp = obj->klass_
8117 GenerateReferenceLoadTwoRegisters(instruction,
8118 temp_loc,
8119 obj_loc,
8120 class_offset,
8121 maybe_temp2_loc,
8122 kWithoutReadBarrier);
8123
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008124 __ Cmp(temp, cls);
8125 // Jump to slow path for throwing the exception or doing a
8126 // more involved array check.
8127 __ B(ne, type_check_slow_path->GetEntryLabel());
8128 break;
8129 }
8130
8131 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008132 // /* HeapReference<Class> */ temp = obj->klass_
8133 GenerateReferenceLoadTwoRegisters(instruction,
8134 temp_loc,
8135 obj_loc,
8136 class_offset,
8137 maybe_temp2_loc,
8138 kWithoutReadBarrier);
8139
Artem Serovcfbe9132016-10-14 15:58:56 +01008140 // If the class is abstract, we eagerly fetch the super class of the
8141 // object to avoid doing a comparison we know will fail.
8142 vixl32::Label loop;
8143 __ Bind(&loop);
8144 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008145 GenerateReferenceLoadOneRegister(instruction,
8146 temp_loc,
8147 super_offset,
8148 maybe_temp2_loc,
8149 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008150
8151 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8152 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008153 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008154
8155 // Otherwise, compare the classes.
8156 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008157 __ B(ne, &loop, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008158 break;
8159 }
8160
8161 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008162 // /* HeapReference<Class> */ temp = obj->klass_
8163 GenerateReferenceLoadTwoRegisters(instruction,
8164 temp_loc,
8165 obj_loc,
8166 class_offset,
8167 maybe_temp2_loc,
8168 kWithoutReadBarrier);
8169
Artem Serovcfbe9132016-10-14 15:58:56 +01008170 // Walk over the class hierarchy to find a match.
8171 vixl32::Label loop;
8172 __ Bind(&loop);
8173 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008174 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008175
8176 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00008177 GenerateReferenceLoadOneRegister(instruction,
8178 temp_loc,
8179 super_offset,
8180 maybe_temp2_loc,
8181 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008182
8183 // If the class reference currently in `temp` is null, jump to the slow path to throw the
8184 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008185 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008186 // Otherwise, jump to the beginning of the loop.
8187 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008188 break;
8189 }
8190
Artem Serovcfbe9132016-10-14 15:58:56 +01008191 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008192 // /* HeapReference<Class> */ temp = obj->klass_
8193 GenerateReferenceLoadTwoRegisters(instruction,
8194 temp_loc,
8195 obj_loc,
8196 class_offset,
8197 maybe_temp2_loc,
8198 kWithoutReadBarrier);
8199
Artem Serovcfbe9132016-10-14 15:58:56 +01008200 // Do an exact check.
8201 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008202 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008203
8204 // Otherwise, we need to check that the object's class is a non-primitive array.
8205 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008206 GenerateReferenceLoadOneRegister(instruction,
8207 temp_loc,
8208 component_offset,
8209 maybe_temp2_loc,
8210 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008211 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008212 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008213 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8214 // to further check that this component type is not a primitive type.
8215 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008216 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008217 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008218 break;
8219 }
8220
8221 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008222 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008223 // We cannot directly call the CheckCast runtime entry point
8224 // without resorting to a type checking slow path here (i.e. by
8225 // calling InvokeRuntime directly), as it would require to
8226 // assign fixed registers for the inputs of this HInstanceOf
8227 // instruction (following the runtime calling convention), which
8228 // might be cluttered by the potential first read barrier
8229 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008230
Artem Serovcfbe9132016-10-14 15:58:56 +01008231 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008232 break;
Artem Serov657022c2016-11-23 14:19:38 +00008233
8234 case TypeCheckKind::kInterfaceCheck: {
8235 // Avoid read barriers to improve performance of the fast path. We can not get false
8236 // positives by doing this.
8237 // /* HeapReference<Class> */ temp = obj->klass_
8238 GenerateReferenceLoadTwoRegisters(instruction,
8239 temp_loc,
8240 obj_loc,
8241 class_offset,
8242 maybe_temp2_loc,
8243 kWithoutReadBarrier);
8244
8245 // /* HeapReference<Class> */ temp = temp->iftable_
8246 GenerateReferenceLoadTwoRegisters(instruction,
8247 temp_loc,
8248 temp_loc,
8249 iftable_offset,
8250 maybe_temp2_loc,
8251 kWithoutReadBarrier);
8252 // Iftable is never null.
8253 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8254 // Loop through the iftable and check if any class matches.
8255 vixl32::Label start_loop;
8256 __ Bind(&start_loop);
8257 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8258 type_check_slow_path->GetEntryLabel());
8259 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8260 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8261 // Go to next interface.
8262 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8263 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8264 // Compare the classes and continue the loop if they do not match.
8265 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Andreas Gampe3db70682018-12-26 15:12:03 -08008266 __ B(ne, &start_loop, /* is_far_target= */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008267 break;
8268 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008269
8270 case TypeCheckKind::kBitstringCheck: {
8271 // /* HeapReference<Class> */ temp = obj->klass_
8272 GenerateReferenceLoadTwoRegisters(instruction,
8273 temp_loc,
8274 obj_loc,
8275 class_offset,
8276 maybe_temp2_loc,
8277 kWithoutReadBarrier);
8278
8279 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8280 __ B(ne, type_check_slow_path->GetEntryLabel());
8281 break;
8282 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008283 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008284 if (done.IsReferenced()) {
8285 __ Bind(&done);
8286 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008287
8288 __ Bind(type_check_slow_path->GetExitLabel());
8289}
8290
Artem Serov551b28f2016-10-18 19:11:30 +01008291void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008292 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8293 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008294 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8295 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8296}
8297
8298void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8299 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8300 instruction,
8301 instruction->GetDexPc());
8302 if (instruction->IsEnter()) {
8303 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8304 } else {
8305 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8306 }
Andra Danciua0130e82020-07-23 12:34:56 +00008307 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 19);
Artem Serov551b28f2016-10-18 19:11:30 +01008308}
8309
Artem Serov02109dd2016-09-23 17:17:54 +01008310void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8311 HandleBitwiseOperation(instruction, AND);
8312}
8313
8314void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8315 HandleBitwiseOperation(instruction, ORR);
8316}
8317
8318void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8319 HandleBitwiseOperation(instruction, EOR);
8320}
8321
8322void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8323 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008324 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008325 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8326 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008327 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8328 locations->SetInAt(0, Location::RequiresRegister());
8329 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8331}
8332
8333void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8334 HandleBitwiseOperation(instruction);
8335}
8336
8337void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8338 HandleBitwiseOperation(instruction);
8339}
8340
8341void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8342 HandleBitwiseOperation(instruction);
8343}
8344
Artem Serov2bbc9532016-10-21 11:51:50 +01008345void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8346 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008347 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008348 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8349 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008350
8351 locations->SetInAt(0, Location::RequiresRegister());
8352 locations->SetInAt(1, Location::RequiresRegister());
8353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8354}
8355
8356void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8357 LocationSummary* locations = instruction->GetLocations();
8358 Location first = locations->InAt(0);
8359 Location second = locations->InAt(1);
8360 Location out = locations->Out();
8361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008362 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008363 vixl32::Register first_reg = RegisterFrom(first);
8364 vixl32::Register second_reg = RegisterFrom(second);
8365 vixl32::Register out_reg = RegisterFrom(out);
8366
8367 switch (instruction->GetOpKind()) {
8368 case HInstruction::kAnd:
8369 __ Bic(out_reg, first_reg, second_reg);
8370 break;
8371 case HInstruction::kOr:
8372 __ Orn(out_reg, first_reg, second_reg);
8373 break;
8374 // There is no EON on arm.
8375 case HInstruction::kXor:
8376 default:
8377 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8378 UNREACHABLE();
8379 }
8380 return;
8381
8382 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008383 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008384 vixl32::Register first_low = LowRegisterFrom(first);
8385 vixl32::Register first_high = HighRegisterFrom(first);
8386 vixl32::Register second_low = LowRegisterFrom(second);
8387 vixl32::Register second_high = HighRegisterFrom(second);
8388 vixl32::Register out_low = LowRegisterFrom(out);
8389 vixl32::Register out_high = HighRegisterFrom(out);
8390
8391 switch (instruction->GetOpKind()) {
8392 case HInstruction::kAnd:
8393 __ Bic(out_low, first_low, second_low);
8394 __ Bic(out_high, first_high, second_high);
8395 break;
8396 case HInstruction::kOr:
8397 __ Orn(out_low, first_low, second_low);
8398 __ Orn(out_high, first_high, second_high);
8399 break;
8400 // There is no EON on arm.
8401 case HInstruction::kXor:
8402 default:
8403 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8404 UNREACHABLE();
8405 }
8406 }
8407}
8408
Anton Kirilov74234da2017-01-13 14:42:47 +00008409void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8410 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008411 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8412 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008413 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008414 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008415 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008416 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8417
8418 locations->SetInAt(0, Location::RequiresRegister());
8419 locations->SetInAt(1, Location::RequiresRegister());
8420 locations->SetOut(Location::RequiresRegister(),
8421 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8422}
8423
8424void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8425 HDataProcWithShifterOp* instruction) {
8426 const LocationSummary* const locations = instruction->GetLocations();
8427 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8428 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008430 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008431 const vixl32::Register first = InputRegisterAt(instruction, 0);
8432 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008433 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008434 ? LowRegisterFrom(locations->InAt(1))
8435 : InputRegisterAt(instruction, 1);
8436
Anton Kirilov420ee302017-02-21 18:10:26 +00008437 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8438 DCHECK_EQ(kind, HInstruction::kAdd);
8439
8440 switch (op_kind) {
8441 case HDataProcWithShifterOp::kUXTB:
8442 __ Uxtab(output, first, second);
8443 break;
8444 case HDataProcWithShifterOp::kUXTH:
8445 __ Uxtah(output, first, second);
8446 break;
8447 case HDataProcWithShifterOp::kSXTB:
8448 __ Sxtab(output, first, second);
8449 break;
8450 case HDataProcWithShifterOp::kSXTH:
8451 __ Sxtah(output, first, second);
8452 break;
8453 default:
8454 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8455 UNREACHABLE();
8456 }
8457 } else {
8458 GenerateDataProcInstruction(kind,
8459 output,
8460 first,
8461 Operand(second,
8462 ShiftFromOpKind(op_kind),
8463 instruction->GetShiftAmount()),
8464 codegen_);
8465 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008466 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008467 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008468
8469 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8470 const vixl32::Register second = InputRegisterAt(instruction, 1);
8471
8472 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8473 GenerateDataProc(kind,
8474 locations->Out(),
8475 locations->InAt(0),
8476 second,
8477 Operand(second, ShiftType::ASR, 31),
8478 codegen_);
8479 } else {
8480 GenerateLongDataProc(instruction, codegen_);
8481 }
8482 }
8483}
8484
Artem Serov02109dd2016-09-23 17:17:54 +01008485// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8486void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8487 vixl32::Register first,
8488 uint32_t value) {
8489 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8490 if (value == 0xffffffffu) {
8491 if (!out.Is(first)) {
8492 __ Mov(out, first);
8493 }
8494 return;
8495 }
8496 if (value == 0u) {
8497 __ Mov(out, 0);
8498 return;
8499 }
8500 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008501 __ And(out, first, value);
8502 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8503 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008504 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008505 DCHECK(IsPowerOfTwo(value + 1));
8506 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008507 }
8508}
8509
8510// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8511void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8512 vixl32::Register first,
8513 uint32_t value) {
8514 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8515 if (value == 0u) {
8516 if (!out.Is(first)) {
8517 __ Mov(out, first);
8518 }
8519 return;
8520 }
8521 if (value == 0xffffffffu) {
8522 __ Mvn(out, 0);
8523 return;
8524 }
8525 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8526 __ Orr(out, first, value);
8527 } else {
8528 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8529 __ Orn(out, first, ~value);
8530 }
8531}
8532
8533// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8534void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8535 vixl32::Register first,
8536 uint32_t value) {
8537 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8538 if (value == 0u) {
8539 if (!out.Is(first)) {
8540 __ Mov(out, first);
8541 }
8542 return;
8543 }
8544 __ Eor(out, first, value);
8545}
8546
Anton Kirilovdda43962016-11-21 19:55:20 +00008547void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8548 Location first,
8549 uint64_t value) {
8550 vixl32::Register out_low = LowRegisterFrom(out);
8551 vixl32::Register out_high = HighRegisterFrom(out);
8552 vixl32::Register first_low = LowRegisterFrom(first);
8553 vixl32::Register first_high = HighRegisterFrom(first);
8554 uint32_t value_low = Low32Bits(value);
8555 uint32_t value_high = High32Bits(value);
8556 if (value_low == 0u) {
8557 if (!out_low.Is(first_low)) {
8558 __ Mov(out_low, first_low);
8559 }
8560 __ Add(out_high, first_high, value_high);
8561 return;
8562 }
8563 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008564 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008565 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008566 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008567 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8568 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008569 }
8570}
8571
Artem Serov02109dd2016-09-23 17:17:54 +01008572void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8573 LocationSummary* locations = instruction->GetLocations();
8574 Location first = locations->InAt(0);
8575 Location second = locations->InAt(1);
8576 Location out = locations->Out();
8577
8578 if (second.IsConstant()) {
8579 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8580 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008581 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008582 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8583 vixl32::Register out_reg = OutputRegister(instruction);
8584 if (instruction->IsAnd()) {
8585 GenerateAndConst(out_reg, first_reg, value_low);
8586 } else if (instruction->IsOr()) {
8587 GenerateOrrConst(out_reg, first_reg, value_low);
8588 } else {
8589 DCHECK(instruction->IsXor());
8590 GenerateEorConst(out_reg, first_reg, value_low);
8591 }
8592 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008593 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008594 uint32_t value_high = High32Bits(value);
8595 vixl32::Register first_low = LowRegisterFrom(first);
8596 vixl32::Register first_high = HighRegisterFrom(first);
8597 vixl32::Register out_low = LowRegisterFrom(out);
8598 vixl32::Register out_high = HighRegisterFrom(out);
8599 if (instruction->IsAnd()) {
8600 GenerateAndConst(out_low, first_low, value_low);
8601 GenerateAndConst(out_high, first_high, value_high);
8602 } else if (instruction->IsOr()) {
8603 GenerateOrrConst(out_low, first_low, value_low);
8604 GenerateOrrConst(out_high, first_high, value_high);
8605 } else {
8606 DCHECK(instruction->IsXor());
8607 GenerateEorConst(out_low, first_low, value_low);
8608 GenerateEorConst(out_high, first_high, value_high);
8609 }
8610 }
8611 return;
8612 }
8613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008614 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008615 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8616 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8617 vixl32::Register out_reg = OutputRegister(instruction);
8618 if (instruction->IsAnd()) {
8619 __ And(out_reg, first_reg, second_reg);
8620 } else if (instruction->IsOr()) {
8621 __ Orr(out_reg, first_reg, second_reg);
8622 } else {
8623 DCHECK(instruction->IsXor());
8624 __ Eor(out_reg, first_reg, second_reg);
8625 }
8626 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008627 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008628 vixl32::Register first_low = LowRegisterFrom(first);
8629 vixl32::Register first_high = HighRegisterFrom(first);
8630 vixl32::Register second_low = LowRegisterFrom(second);
8631 vixl32::Register second_high = HighRegisterFrom(second);
8632 vixl32::Register out_low = LowRegisterFrom(out);
8633 vixl32::Register out_high = HighRegisterFrom(out);
8634 if (instruction->IsAnd()) {
8635 __ And(out_low, first_low, second_low);
8636 __ And(out_high, first_high, second_high);
8637 } else if (instruction->IsOr()) {
8638 __ Orr(out_low, first_low, second_low);
8639 __ Orr(out_high, first_high, second_high);
8640 } else {
8641 DCHECK(instruction->IsXor());
8642 __ Eor(out_low, first_low, second_low);
8643 __ Eor(out_high, first_high, second_high);
8644 }
8645 }
8646}
8647
Artem Serovcfbe9132016-10-14 15:58:56 +01008648void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008649 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008650 Location out,
8651 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008652 Location maybe_temp,
8653 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008654 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008655 if (read_barrier_option == kWithReadBarrier) {
8656 CHECK(kEmitCompilerReadBarrier);
8657 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8658 if (kUseBakerReadBarrier) {
8659 // Load with fast path based Baker's read barrier.
8660 // /* HeapReference<Object> */ out = *(out + offset)
8661 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008662 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008663 } else {
8664 // Load with slow path based read barrier.
8665 // Save the value of `out` into `maybe_temp` before overwriting it
8666 // in the following move operation, as we will need it for the
8667 // read barrier below.
8668 __ Mov(RegisterFrom(maybe_temp), out_reg);
8669 // /* HeapReference<Object> */ out = *(out + offset)
8670 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8671 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8672 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008673 } else {
8674 // Plain load with no read barrier.
8675 // /* HeapReference<Object> */ out = *(out + offset)
8676 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8677 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8678 }
8679}
8680
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008681void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008682 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008683 Location out,
8684 Location obj,
8685 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008686 Location maybe_temp,
8687 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008688 vixl32::Register out_reg = RegisterFrom(out);
8689 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008690 if (read_barrier_option == kWithReadBarrier) {
8691 CHECK(kEmitCompilerReadBarrier);
8692 if (kUseBakerReadBarrier) {
8693 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8694 // Load with fast path based Baker's read barrier.
8695 // /* HeapReference<Object> */ out = *(obj + offset)
8696 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008697 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008698 } else {
8699 // Load with slow path based read barrier.
8700 // /* HeapReference<Object> */ out = *(obj + offset)
8701 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8702 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8703 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008704 } else {
8705 // Plain load with no read barrier.
8706 // /* HeapReference<Object> */ out = *(obj + offset)
8707 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8708 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8709 }
8710}
8711
Vladimir Markoca1e0382018-04-11 09:58:41 +00008712void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008713 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008714 Location root,
8715 vixl32::Register obj,
8716 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008717 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008718 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008719 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008720 DCHECK(kEmitCompilerReadBarrier);
8721 if (kUseBakerReadBarrier) {
8722 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008723 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008724
Vladimir Marko008e09f32018-08-06 15:42:43 +01008725 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8726 // the Marking Register) to decide whether we need to enter
8727 // the slow path to mark the GC root.
8728 //
8729 // We use shared thunks for the slow path; shared within the method
8730 // for JIT, across methods for AOT. That thunk checks the reference
8731 // and jumps to the entrypoint if needed.
8732 //
8733 // lr = &return_address;
8734 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8735 // if (mr) { // Thread::Current()->GetIsGcMarking()
8736 // goto gc_root_thunk<root_reg>(lr)
8737 // }
8738 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008739
Vladimir Marko008e09f32018-08-06 15:42:43 +01008740 UseScratchRegisterScope temps(GetVIXLAssembler());
8741 temps.Exclude(ip);
8742 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8743 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008744
Vladimir Markod887ed82018-08-14 13:52:12 +00008745 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8746 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8747 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8748 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8749 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008750 vixl32::Label return_address;
8751 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8752 __ cmp(mr, Operand(0));
8753 // Currently the offset is always within range. If that changes,
8754 // we shall have to split the load the same way as for fields.
8755 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8756 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8757 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8758 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008759 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008760 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8761 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8762 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008763 } else {
8764 // GC root loaded through a slow path for read barriers other
8765 // than Baker's.
8766 // /* GcRoot<mirror::Object>* */ root = obj + offset
8767 __ Add(root_reg, obj, offset);
8768 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008769 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008770 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008771 } else {
8772 // Plain GC root load with no read barrier.
8773 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8774 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8775 // Note that GC roots are not affected by heap poisoning, thus we
8776 // do not have to unpoison `root_reg` here.
8777 }
Andra Danciua0130e82020-07-23 12:34:56 +00008778 MaybeGenerateMarkingRegisterCheck(/* code= */ 20);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008779}
8780
Vladimir Markod887ed82018-08-14 13:52:12 +00008781void CodeGeneratorARMVIXL::GenerateUnsafeCasOldValueAddWithBakerReadBarrier(
8782 vixl::aarch32::Register old_value,
8783 vixl::aarch32::Register adjusted_old_value,
8784 vixl::aarch32::Register expected) {
8785 DCHECK(kEmitCompilerReadBarrier);
8786 DCHECK(kUseBakerReadBarrier);
8787
8788 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with an ADD instead of LDR.
8789 uint32_t custom_data = EncodeBakerReadBarrierUnsafeCasData(old_value.GetCode());
8790
8791 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8792 size_t wide_instructions = /* ADR+CMP+ADD+BNE */ 4u - narrow_instructions;
8793 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8794 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8795 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8796 vixl32::Label return_address;
8797 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8798 __ cmp(mr, Operand(0));
8799 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8800 __ add(EncodingSize(Wide), old_value, adjusted_old_value, Operand(expected)); // Preserves flags.
8801 EmitBakerReadBarrierBne(custom_data);
8802 __ bind(&return_address);
8803 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8804 BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ADD_OFFSET);
8805}
8806
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008807void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8808 Location ref,
8809 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008810 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008811 bool needs_null_check) {
8812 DCHECK(kEmitCompilerReadBarrier);
8813 DCHECK(kUseBakerReadBarrier);
8814
Vladimir Marko008e09f32018-08-06 15:42:43 +01008815 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8816 // Marking Register) to decide whether we need to enter the slow
8817 // path to mark the reference. Then, in the slow path, check the
8818 // gray bit in the lock word of the reference's holder (`obj`) to
8819 // decide whether to mark `ref` or not.
8820 //
8821 // We use shared thunks for the slow path; shared within the method
8822 // for JIT, across methods for AOT. That thunk checks the holder
8823 // and jumps to the entrypoint if needed. If the holder is not gray,
8824 // it creates a fake dependency and returns to the LDR instruction.
8825 //
8826 // lr = &gray_return_address;
8827 // if (mr) { // Thread::Current()->GetIsGcMarking()
8828 // goto field_thunk<holder_reg, base_reg>(lr)
8829 // }
8830 // not_gray_return_address:
8831 // // Original reference load. If the offset is too large to fit
8832 // // into LDR, we use an adjusted base register here.
8833 // HeapReference<mirror::Object> reference = *(obj+offset);
8834 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008835
Vladimir Marko248141f2018-08-10 10:40:07 +01008836 DCHECK(src.GetAddrMode() == vixl32::Offset);
8837 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008838 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008839 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8840
Vladimir Marko008e09f32018-08-06 15:42:43 +01008841 UseScratchRegisterScope temps(GetVIXLAssembler());
8842 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008843 uint32_t custom_data =
8844 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008845
Vladimir Marko008e09f32018-08-06 15:42:43 +01008846 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008847 size_t narrow_instructions =
8848 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8849 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
8850 size_t wide_instructions =
8851 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8852 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8853 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8854 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008855 vixl32::Label return_address;
8856 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8857 __ cmp(mr, Operand(0));
8858 EmitBakerReadBarrierBne(custom_data);
8859 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01008860 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008861 if (needs_null_check) {
8862 MaybeRecordImplicitNullCheck(instruction);
8863 }
8864 // Note: We need a specific width for the unpoisoning NEG.
8865 if (kPoisonHeapReferences) {
8866 if (narrow) {
8867 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8868 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8869 } else {
8870 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8871 }
8872 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008873 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008874 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8875 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8876 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
8877 }
Andra Danciua0130e82020-07-23 12:34:56 +00008878 MaybeGenerateMarkingRegisterCheck(/* code= */ 21, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008879}
8880
Vladimir Marko248141f2018-08-10 10:40:07 +01008881void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8882 Location ref,
8883 vixl32::Register obj,
8884 uint32_t offset,
8885 Location temp,
8886 bool needs_null_check) {
8887 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8888 vixl32::Register base = obj;
8889 if (offset >= kReferenceLoadMinFarOffset) {
8890 base = RegisterFrom(temp);
8891 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8892 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8893 offset &= (kReferenceLoadMinFarOffset - 1u);
8894 }
8895 GenerateFieldLoadWithBakerReadBarrier(
8896 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
8897}
8898
Vladimir Marko008e09f32018-08-06 15:42:43 +01008899void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008900 vixl32::Register obj,
8901 uint32_t data_offset,
8902 Location index,
8903 Location temp,
8904 bool needs_null_check) {
8905 DCHECK(kEmitCompilerReadBarrier);
8906 DCHECK(kUseBakerReadBarrier);
8907
8908 static_assert(
8909 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8910 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008911 ScaleFactor scale_factor = TIMES_4;
8912
Vladimir Marko008e09f32018-08-06 15:42:43 +01008913 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8914 // Marking Register) to decide whether we need to enter the slow
8915 // path to mark the reference. Then, in the slow path, check the
8916 // gray bit in the lock word of the reference's holder (`obj`) to
8917 // decide whether to mark `ref` or not.
8918 //
8919 // We use shared thunks for the slow path; shared within the method
8920 // for JIT, across methods for AOT. That thunk checks the holder
8921 // and jumps to the entrypoint if needed. If the holder is not gray,
8922 // it creates a fake dependency and returns to the LDR instruction.
8923 //
8924 // lr = &gray_return_address;
8925 // if (mr) { // Thread::Current()->GetIsGcMarking()
8926 // goto array_thunk<base_reg>(lr)
8927 // }
8928 // not_gray_return_address:
8929 // // Original reference load. If the offset is too large to fit
8930 // // into LDR, we use an adjusted base register here.
8931 // HeapReference<mirror::Object> reference = data[index];
8932 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008933
Vladimir Marko008e09f32018-08-06 15:42:43 +01008934 DCHECK(index.IsValid());
8935 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8936 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8937 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008938
Vladimir Marko008e09f32018-08-06 15:42:43 +01008939 UseScratchRegisterScope temps(GetVIXLAssembler());
8940 temps.Exclude(ip);
8941 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008942
Vladimir Marko008e09f32018-08-06 15:42:43 +01008943 __ Add(data_reg, obj, Operand(data_offset));
8944 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008945 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8946 size_t wide_instructions =
8947 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8948 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8949 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8950 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008951 vixl32::Label return_address;
8952 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8953 __ cmp(mr, Operand(0));
8954 EmitBakerReadBarrierBne(custom_data);
8955 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8956 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8957 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8958 // Note: We need a Wide NEG for the unpoisoning.
8959 if (kPoisonHeapReferences) {
8960 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008961 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008962 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008963 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8964 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008965 }
Andra Danciua0130e82020-07-23 12:34:56 +00008966 MaybeGenerateMarkingRegisterCheck(/* code= */ 22, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008967}
8968
Roland Levillain5daa4952017-07-03 17:23:56 +01008969void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8970 // The following condition is a compile-time one, so it does not have a run-time cost.
8971 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8972 // The following condition is a run-time one; it is executed after the
8973 // previous compile-time test, to avoid penalizing non-debug builds.
8974 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8975 UseScratchRegisterScope temps(GetVIXLAssembler());
8976 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8977 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8978 kMarkingRegisterCheckBreakCodeBaseCode + code);
8979 }
8980 }
8981}
8982
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008983void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8984 Location out,
8985 Location ref,
8986 Location obj,
8987 uint32_t offset,
8988 Location index) {
8989 DCHECK(kEmitCompilerReadBarrier);
8990
8991 // Insert a slow path based read barrier *after* the reference load.
8992 //
8993 // If heap poisoning is enabled, the unpoisoning of the loaded
8994 // reference will be carried out by the runtime within the slow
8995 // path.
8996 //
8997 // Note that `ref` currently does not get unpoisoned (when heap
8998 // poisoning is enabled), which is alright as the `ref` argument is
8999 // not used by the artReadBarrierSlow entry point.
9000 //
9001 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01009002 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009003 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
9004 AddSlowPath(slow_path);
9005
9006 __ B(slow_path->GetEntryLabel());
9007 __ Bind(slow_path->GetExitLabel());
9008}
9009
9010void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01009011 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009012 Location ref,
9013 Location obj,
9014 uint32_t offset,
9015 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01009016 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009017 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01009018 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01009019 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009020 // If heap poisoning is enabled, unpoisoning will be taken care of
9021 // by the runtime within the slow path.
9022 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01009023 } else if (kPoisonHeapReferences) {
9024 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
9025 }
9026}
9027
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009028void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
9029 Location out,
9030 Location root) {
9031 DCHECK(kEmitCompilerReadBarrier);
9032
9033 // Insert a slow path based read barrier *after* the GC root load.
9034 //
9035 // Note that GC roots are not affected by heap poisoning, so we do
9036 // not need to do anything special for this here.
9037 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01009038 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009039 AddSlowPath(slow_path);
9040
9041 __ B(slow_path->GetEntryLabel());
9042 __ Bind(slow_path->GetExitLabel());
9043}
9044
Artem Serov02d37832016-10-25 15:25:33 +01009045// Check if the desired_dispatch_info is supported. If it is, return it,
9046// otherwise return a fall-back info that should be used instead.
9047HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009048 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Vladimir Marko86c87522020-05-11 16:55:55 +01009049 ArtMethod* method) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009050 if (method->IsIntrinsic() &&
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009051 desired_dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative) {
Vladimir Markod3e9c622020-08-05 12:20:28 +01009052 // As a work-around for soft-float native ABI interfering with type checks, we are
9053 // inserting fake calls to Float.floatToRawIntBits() or Double.doubleToRawLongBits()
9054 // when a float or double argument is passed in core registers but we cannot do that
9055 // for actual intrinsic implementations that expect them in FP registers. Therefore
9056 // we do not use `kCallCriticalNative` for intrinsics with FP arguments; if they are
9057 // properly intrinsified, the dispatch type does not matter anyway.
Vladimir Marko86c87522020-05-11 16:55:55 +01009058 ScopedObjectAccess soa(Thread::Current());
9059 uint32_t shorty_len;
9060 const char* shorty = method->GetShorty(&shorty_len);
Vladimir Marko86c87522020-05-11 16:55:55 +01009061 for (uint32_t i = 1; i != shorty_len; ++i) {
Vladimir Marko86c87522020-05-11 16:55:55 +01009062 if (shorty[i] == 'D' || shorty[i] == 'F') {
9063 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009064 dispatch_info.code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Marko86c87522020-05-11 16:55:55 +01009065 return dispatch_info;
9066 }
Vladimir Marko86c87522020-05-11 16:55:55 +01009067 }
9068 }
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00009069 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01009070}
9071
Vladimir Markod254f5c2017-06-02 15:18:36 +00009072void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009073 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009074 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009075 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009076 case MethodLoadKind::kStringInit: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009077 uint32_t offset =
9078 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
9079 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00009080 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
9081 break;
9082 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009083 case MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01009084 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00009085 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009086 case MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01009087 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009088 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetResolvedMethodReference());
Vladimir Marko65979462017-05-19 17:25:12 +01009089 vixl32::Register temp_reg = RegisterFrom(temp);
9090 EmitMovwMovtPlaceholder(labels, temp_reg);
9091 break;
9092 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009093 case MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009094 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00009095 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
9096 vixl32::Register temp_reg = RegisterFrom(temp);
9097 EmitMovwMovtPlaceholder(labels, temp_reg);
9098 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
9099 break;
9100 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009101 case MethodLoadKind::kBssEntry: {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009102 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(invoke->GetMethodReference());
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009103 vixl32::Register temp_reg = RegisterFrom(temp);
9104 EmitMovwMovtPlaceholder(labels, temp_reg);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01009105 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009106 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009107 break;
9108 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009109 case MethodLoadKind::kJitDirectAddress:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009110 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
9111 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009112 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009113 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
9114 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009115 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009116 }
9117
Vladimir Marko86c87522020-05-11 16:55:55 +01009118 auto call_code_pointer_member = [&](MemberOffset offset) {
9119 // LR = callee_method->member;
9120 GetAssembler()->LoadFromOffset(kLoadWord, lr, RegisterFrom(callee_method), offset.Int32Value());
9121 {
9122 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9123 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9124 ExactAssemblyScope aas(GetVIXLAssembler(),
9125 vixl32::k16BitT32InstructionSizeInBytes,
9126 CodeBufferCheckScope::kExactSize);
9127 // LR()
9128 __ blx(lr);
9129 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9130 }
9131 };
Artem Serovd4cc5b22016-11-04 11:19:09 +00009132 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009133 case CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009134 {
9135 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9136 ExactAssemblyScope aas(GetVIXLAssembler(),
9137 vixl32::k32BitT32InstructionSizeInBytes,
9138 CodeBufferCheckScope::kMaximumSize);
9139 __ bl(GetFrameEntryLabel());
9140 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9141 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00009142 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009143 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01009144 size_t out_frame_size =
9145 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorARMVIXL,
9146 kAapcsStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01009147 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01009148 call_code_pointer_member(ArtMethod::EntryPointFromJniOffset(kArmPointerSize));
9149 // Move the result when needed due to native and managed ABI mismatch.
9150 switch (invoke->GetType()) {
9151 case DataType::Type::kFloat32:
9152 __ Vmov(s0, r0);
9153 break;
9154 case DataType::Type::kFloat64:
9155 __ Vmov(d0, r0, r1);
9156 break;
9157 case DataType::Type::kBool:
9158 case DataType::Type::kInt8:
9159 case DataType::Type::kUint16:
9160 case DataType::Type::kInt16:
9161 case DataType::Type::kInt32:
9162 case DataType::Type::kInt64:
9163 case DataType::Type::kVoid:
9164 break;
9165 default:
9166 DCHECK(false) << invoke->GetType();
9167 break;
9168 }
9169 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01009170 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01009171 }
9172 break;
9173 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01009174 case CodePtrLocation::kCallArtMethod:
Vladimir Marko86c87522020-05-11 16:55:55 +01009175 call_code_pointer_member(ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009176 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009177 }
9178
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009179 DCHECK(!IsLeafMethod());
9180}
9181
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009182void CodeGeneratorARMVIXL::GenerateVirtualCall(
9183 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009184 vixl32::Register temp = RegisterFrom(temp_location);
9185 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9186 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
9187
9188 // Use the calling convention instead of the location of the receiver, as
9189 // intrinsics may have put the receiver in a different register. In the intrinsics
9190 // slow path, the arguments have been moved to the right place, so here we are
9191 // guaranteed that the receiver is the first register of the calling convention.
9192 InvokeDexCallingConventionARMVIXL calling_convention;
9193 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
9194 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00009195 {
9196 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00009197 ExactAssemblyScope aas(GetVIXLAssembler(),
9198 vixl32::kMaxInstructionSizeInBytes,
9199 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00009200 // /* HeapReference<Class> */ temp = receiver->klass_
9201 __ ldr(temp, MemOperand(receiver, class_offset));
9202 MaybeRecordImplicitNullCheck(invoke);
9203 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009204 // Instead of simply (possibly) unpoisoning `temp` here, we should
9205 // emit a read barrier for the previous class reference load.
9206 // However this is not required in practice, as this is an
9207 // intermediate/temporary reference and because the current
9208 // concurrent copying collector keeps the from-space memory
9209 // intact/accessible until the end of the marking phase (the
9210 // concurrent copying collector may not in the future).
9211 GetAssembler()->MaybeUnpoisonHeapReference(temp);
9212
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00009213 // If we're compiling baseline, update the inline cache.
9214 MaybeGenerateInlineCacheCheck(invoke, temp);
9215
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009216 // temp = temp->GetMethodAt(method_offset);
9217 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9218 kArmPointerSize).Int32Value();
9219 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9220 // LR = temp->GetEntryPoint();
9221 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009222 {
9223 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9224 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9225 ExactAssemblyScope aas(GetVIXLAssembler(),
9226 vixl32::k16BitT32InstructionSizeInBytes,
9227 CodeBufferCheckScope::kExactSize);
9228 // LR();
9229 __ blx(lr);
9230 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9231 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009232}
9233
Vladimir Marko6fd16062018-06-26 11:02:04 +01009234CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
9235 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009236 return NewPcRelativePatch(/* dex_file= */ nullptr, intrinsic_data, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009237}
9238
Vladimir Markob066d432018-01-03 13:14:37 +00009239CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9240 uint32_t boot_image_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08009241 return NewPcRelativePatch(/* dex_file= */ nullptr,
Vladimir Markob066d432018-01-03 13:14:37 +00009242 boot_image_offset,
Vladimir Marko2d06e022019-07-08 15:45:19 +01009243 &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00009244}
9245
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009246CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009247 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009248 return NewPcRelativePatch(
9249 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009250}
9251
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009252CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9253 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009254 return NewPcRelativePatch(
9255 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009256}
9257
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009258CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009259 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009260 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009261}
9262
Vladimir Marko1998cd02017-01-13 13:02:58 +00009263CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
Vladimir Marko8f63f102020-09-28 12:10:28 +01009264 HLoadClass* load_class) {
9265 const DexFile& dex_file = load_class->GetDexFile();
9266 dex::TypeIndex type_index = load_class->GetTypeIndex();
9267 ArenaDeque<PcRelativePatchInfo>* patches = nullptr;
9268 switch (load_class->GetLoadKind()) {
9269 case HLoadClass::LoadKind::kBssEntry:
9270 patches = &type_bss_entry_patches_;
9271 break;
9272 case HLoadClass::LoadKind::kBssEntryPublic:
9273 patches = &public_type_bss_entry_patches_;
9274 break;
9275 case HLoadClass::LoadKind::kBssEntryPackage:
9276 patches = &package_type_bss_entry_patches_;
9277 break;
9278 default:
9279 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
9280 UNREACHABLE();
9281 }
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009282 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009283}
9284
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009285CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009286 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009287 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009288}
9289
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009290CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9291 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009292 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009293}
9294
Artem Serovd4cc5b22016-11-04 11:19:09 +00009295CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009296 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009297 patches->emplace_back(dex_file, offset_or_index);
9298 return &patches->back();
9299}
9300
Vladimir Markof6675082019-05-17 12:05:28 +01009301void CodeGeneratorARMVIXL::EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset) {
9302 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009303 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01009304 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
9305 vixl::aarch32::Label* bl_label = &call_entrypoint_patches_.back().label;
9306 __ bind(bl_label);
9307 vixl32::Label placeholder_label;
9308 __ bl(&placeholder_label); // Placeholder, patched at link-time.
9309 __ bind(&placeholder_label);
9310}
9311
Vladimir Marko966b46f2018-08-03 10:20:19 +00009312void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00009313 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009314 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00009315 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
9316 vixl::aarch32::Label* slow_path_entry = &it->second.label;
9317 __ b(ne, EncodingSize(Wide), slow_path_entry);
9318 } else {
9319 baker_read_barrier_patches_.emplace_back(custom_data);
9320 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
9321 __ bind(patch_label);
9322 vixl32::Label placeholder_label;
9323 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
9324 __ bind(&placeholder_label);
9325 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009326}
9327
Artem Serovc5fcb442016-12-02 19:19:58 +00009328VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009329 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009330}
9331
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009332VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9333 const DexFile& dex_file,
9334 dex::StringIndex string_index,
9335 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009336 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009337 return jit_string_patches_.GetOrCreate(
9338 StringReference(&dex_file, string_index),
9339 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009340 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009341 });
9342}
9343
9344VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9345 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009346 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009347 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009348 return jit_class_patches_.GetOrCreate(
9349 TypeReference(&dex_file, type_index),
9350 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009351 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009352 });
9353}
9354
Vladimir Marko6fd16062018-06-26 11:02:04 +01009355void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
9356 uint32_t boot_image_reference) {
9357 if (GetCompilerOptions().IsBootImage()) {
9358 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9359 NewBootImageIntrinsicPatch(boot_image_reference);
9360 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01009361 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009362 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9363 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009364 EmitMovwMovtPlaceholder(labels, reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08009365 __ Ldr(reg, MemOperand(reg, /* offset= */ 0));
Vladimir Markoeebb8212018-06-05 14:57:24 +01009366 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01009367 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01009368 gc::Heap* heap = Runtime::Current()->GetHeap();
9369 DCHECK(!heap->GetBootImageSpaces().empty());
9370 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01009371 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009372 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
9373 }
9374}
9375
Vladimir Marko6fd16062018-06-26 11:02:04 +01009376void CodeGeneratorARMVIXL::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
9377 uint32_t boot_image_offset) {
9378 DCHECK(invoke->IsStatic());
9379 InvokeRuntimeCallingConventionARMVIXL calling_convention;
9380 vixl32::Register argument = calling_convention.GetRegisterAt(0);
9381 if (GetCompilerOptions().IsBootImage()) {
9382 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
9383 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01009384 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01009385 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
9386 PcRelativePatchInfo* labels = NewBootImageTypePatch(*target_method.dex_file, type_idx);
9387 EmitMovwMovtPlaceholder(labels, argument);
9388 } else {
9389 LoadBootImageAddress(argument, boot_image_offset);
9390 }
9391 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
9392 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
9393}
9394
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009395template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009396inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9397 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009398 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009399 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009400 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009401 size_t offset_or_index = info.offset_or_index;
9402 DCHECK(info.add_pc_label.IsBound());
9403 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9404 // Add MOVW patch.
9405 DCHECK(info.movw_label.IsBound());
9406 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009407 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009408 // Add MOVT patch.
9409 DCHECK(info.movt_label.IsBound());
9410 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009411 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009412 }
9413}
9414
Vladimir Marko6fd16062018-06-26 11:02:04 +01009415template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
9416linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
9417 const DexFile* target_dex_file,
9418 uint32_t pc_insn_offset,
9419 uint32_t boot_image_offset) {
9420 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
9421 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00009422}
9423
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009424void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009425 DCHECK(linker_patches->empty());
9426 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009427 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009428 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009429 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009430 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01009431 /* MOVW+MOVT for each entry */ 2u * public_type_bss_entry_patches_.size() +
9432 /* MOVW+MOVT for each entry */ 2u * package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009433 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009434 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01009435 /* MOVW+MOVT for each entry */ 2u * boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01009436 call_entrypoint_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009437 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009438 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01009439 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009440 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009441 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009442 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009443 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009444 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009445 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009446 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009447 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009448 DCHECK(boot_image_type_patches_.empty());
9449 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01009450 }
9451 if (GetCompilerOptions().IsBootImage()) {
9452 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
9453 boot_image_other_patches_, linker_patches);
9454 } else {
9455 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
9456 boot_image_other_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009457 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009458 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9459 method_bss_entry_patches_, linker_patches);
9460 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9461 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01009462 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
9463 public_type_bss_entry_patches_, linker_patches);
9464 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
9465 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009466 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9467 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01009468 for (const PatchInfo<vixl32::Label>& info : call_entrypoint_patches_) {
9469 DCHECK(info.target_dex_file == nullptr);
9470 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
9471 info.label.GetLocation(), info.offset_or_index));
9472 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009473 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009474 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9475 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009476 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009477 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009478}
9479
Vladimir Markoca1e0382018-04-11 09:58:41 +00009480bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01009481 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
9482 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00009483 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9484}
9485
9486void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9487 /*out*/ ArenaVector<uint8_t>* code,
9488 /*out*/ std::string* debug_name) {
9489 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9490 switch (patch.GetType()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009491 case linker::LinkerPatch::Type::kCallRelative: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009492 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9493 // to the generic JNI and interpreter trampolines.
Vladimir Markof6675082019-05-17 12:05:28 +01009494 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
9495 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, vixl32::r0, offset.Int32Value());
Vladimir Markoca1e0382018-04-11 09:58:41 +00009496 assembler.GetVIXLAssembler()->Bkpt(0);
9497 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9498 *debug_name = "MethodCallThunk";
9499 }
9500 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009501 }
9502 case linker::LinkerPatch::Type::kCallEntrypoint: {
9503 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, tr, patch.EntrypointOffset());
9504 assembler.GetVIXLAssembler()->Bkpt(0);
9505 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9506 *debug_name = "EntrypointCallThunk_" + std::to_string(patch.EntrypointOffset());
9507 }
9508 break;
9509 }
9510 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009511 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9512 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9513 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009514 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009515 default:
9516 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9517 UNREACHABLE();
9518 }
9519
9520 // Ensure we emit the literal pool if any.
9521 assembler.FinalizeCode();
9522 code->resize(assembler.CodeSize());
9523 MemoryRegion code_region(code->data(), code->size());
9524 assembler.FinalizeInstructions(code_region);
9525}
9526
Artem Serovc5fcb442016-12-02 19:19:58 +00009527VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9528 uint32_t value,
9529 Uint32ToLiteralMap* map) {
9530 return map->GetOrCreate(
9531 value,
9532 [this, value]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009533 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ value);
Artem Serovc5fcb442016-12-02 19:19:58 +00009534 });
9535}
9536
Artem Serov2bbc9532016-10-21 11:51:50 +01009537void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9538 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009539 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009540 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9541 Location::RequiresRegister());
9542 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9543 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9545}
9546
9547void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9548 vixl32::Register res = OutputRegister(instr);
9549 vixl32::Register accumulator =
9550 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9551 vixl32::Register mul_left =
9552 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9553 vixl32::Register mul_right =
9554 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9555
9556 if (instr->GetOpKind() == HInstruction::kAdd) {
9557 __ Mla(res, mul_left, mul_right, accumulator);
9558 } else {
9559 __ Mls(res, mul_left, mul_right, accumulator);
9560 }
9561}
9562
Artem Serov551b28f2016-10-18 19:11:30 +01009563void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9564 // Nothing to do, this should be removed during prepare for register allocator.
9565 LOG(FATAL) << "Unreachable";
9566}
9567
9568void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9569 // Nothing to do, this should be removed during prepare for register allocator.
9570 LOG(FATAL) << "Unreachable";
9571}
9572
9573// Simple implementation of packed switch - generate cascaded compare/jumps.
9574void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009576 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009577 locations->SetInAt(0, Location::RequiresRegister());
9578 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9579 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9580 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9581 if (switch_instr->GetStartValue() != 0) {
9582 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9583 }
9584 }
9585}
9586
9587// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9588void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9589 int32_t lower_bound = switch_instr->GetStartValue();
9590 uint32_t num_entries = switch_instr->GetNumEntries();
9591 LocationSummary* locations = switch_instr->GetLocations();
9592 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9593 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9594
9595 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9596 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9597 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009598 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009599 vixl32::Register temp_reg = temps.Acquire();
9600 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9601 // the immediate, because IP is used as the destination register. For the other
9602 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9603 // and they can be encoded in the instruction without making use of IP register.
9604 __ Adds(temp_reg, value_reg, -lower_bound);
9605
9606 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9607 // Jump to successors[0] if value == lower_bound.
9608 __ B(eq, codegen_->GetLabelOf(successors[0]));
9609 int32_t last_index = 0;
9610 for (; num_entries - last_index > 2; last_index += 2) {
9611 __ Adds(temp_reg, temp_reg, -2);
9612 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9613 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9614 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9615 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9616 }
9617 if (num_entries - last_index == 2) {
9618 // The last missing case_value.
9619 __ Cmp(temp_reg, 1);
9620 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9621 }
9622
9623 // And the default for any other value.
9624 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9625 __ B(codegen_->GetLabelOf(default_block));
9626 }
9627 } else {
9628 // Create a table lookup.
9629 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9630
9631 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9632
9633 // Remove the bias.
9634 vixl32::Register key_reg;
9635 if (lower_bound != 0) {
9636 key_reg = RegisterFrom(locations->GetTemp(1));
9637 __ Sub(key_reg, value_reg, lower_bound);
9638 } else {
9639 key_reg = value_reg;
9640 }
9641
9642 // Check whether the value is in the table, jump to default block if not.
9643 __ Cmp(key_reg, num_entries - 1);
9644 __ B(hi, codegen_->GetLabelOf(default_block));
9645
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009646 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009647 vixl32::Register jump_offset = temps.Acquire();
9648
9649 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009650 {
9651 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9652 ExactAssemblyScope aas(GetVIXLAssembler(),
9653 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9654 CodeBufferCheckScope::kMaximumSize);
9655 __ adr(table_base, jump_table->GetTableStartLabel());
9656 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009657
Scott Wakeling86e9d262017-01-18 15:59:24 +00009658 // Jump to target block by branching to table_base(pc related) + offset.
9659 vixl32::Register target_address = table_base;
9660 __ add(target_address, table_base, jump_offset);
9661 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009662
Scott Wakeling86e9d262017-01-18 15:59:24 +00009663 jump_table->EmitTable(codegen_);
9664 }
Artem Serov551b28f2016-10-18 19:11:30 +01009665 }
9666}
9667
Artem Serov02d37832016-10-25 15:25:33 +01009668// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009669void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009670 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009671 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009672 return;
9673 }
9674
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009675 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009676
Artem Serovd4cc5b22016-11-04 11:19:09 +00009677 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009678 if (return_loc.Equals(trg)) {
9679 return;
9680 }
9681
9682 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9683 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009684 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009685 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009686 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009687 TODO_VIXL32(FATAL);
9688 } else {
9689 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009690 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009691 parallel_move.AddMove(return_loc, trg, type, nullptr);
9692 GetMoveResolver()->EmitNativeCode(&parallel_move);
9693 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009694}
Scott Wakelingfe885462016-09-22 10:24:38 +01009695
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009696void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9697 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009698 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009699 locations->SetInAt(0, Location::RequiresRegister());
9700 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009701}
9702
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009703void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9704 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9705 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9706 instruction->GetIndex(), kArmPointerSize).SizeValue();
9707 GetAssembler()->LoadFromOffset(kLoadWord,
9708 OutputRegister(instruction),
9709 InputRegisterAt(instruction, 0),
9710 method_offset);
9711 } else {
9712 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9713 instruction->GetIndex(), kArmPointerSize));
9714 GetAssembler()->LoadFromOffset(kLoadWord,
9715 OutputRegister(instruction),
9716 InputRegisterAt(instruction, 0),
9717 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9718 GetAssembler()->LoadFromOffset(kLoadWord,
9719 OutputRegister(instruction),
9720 OutputRegister(instruction),
9721 method_offset);
9722 }
Artem Serov551b28f2016-10-18 19:11:30 +01009723}
9724
Artem Serovc5fcb442016-12-02 19:19:58 +00009725static void PatchJitRootUse(uint8_t* code,
9726 const uint8_t* roots_data,
9727 VIXLUInt32Literal* literal,
9728 uint64_t index_in_table) {
9729 DCHECK(literal->IsBound());
9730 uint32_t literal_offset = literal->GetLocation();
9731 uintptr_t address =
9732 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9733 uint8_t* data = code + literal_offset;
9734 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9735}
9736
9737void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9738 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009739 const StringReference& string_reference = entry.first;
9740 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009741 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009742 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009743 }
9744 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009745 const TypeReference& type_reference = entry.first;
9746 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009747 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009748 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009749 }
9750}
9751
Artem Serovd4cc5b22016-11-04 11:19:09 +00009752void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9753 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9754 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009755 ExactAssemblyScope aas(GetVIXLAssembler(),
9756 3 * vixl32::kMaxInstructionSizeInBytes,
9757 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009758 // TODO(VIXL): Think about using mov instead of movw.
9759 __ bind(&labels->movw_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009760 __ movw(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009761 __ bind(&labels->movt_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009762 __ movt(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009763 __ bind(&labels->add_pc_label);
9764 __ add(out, out, pc);
9765}
9766
Scott Wakelingfe885462016-09-22 10:24:38 +01009767#undef __
9768#undef QUICK_ENTRY_POINT
9769#undef TODO_VIXL32
9770
Vladimir Markoca1e0382018-04-11 09:58:41 +00009771#define __ assembler.GetVIXLAssembler()->
9772
9773static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9774 vixl32::Register base_reg,
9775 vixl32::MemOperand& lock_word,
9776 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009777 int32_t raw_ldr_offset,
9778 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009779 // Load the lock word containing the rb_state.
9780 __ Ldr(ip, lock_word);
9781 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009782 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009783 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9784 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
Andreas Gampe3db70682018-12-26 15:12:03 -08009785 __ B(ne, slow_path, /* is_far_target= */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009786 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9787 if (throw_npe != nullptr) {
9788 __ Bind(throw_npe);
9789 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009790 __ Add(lr, lr, raw_ldr_offset);
9791 // Introduce a dependency on the lock_word including rb_state,
9792 // to prevent load-load reordering, and without using
9793 // a memory barrier (which would be more expensive).
9794 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9795 __ Bx(lr); // And return back to the function.
9796 // Note: The fake dependency is unnecessary for the slow path.
9797}
9798
9799// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009800static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009801 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009802 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9803 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009804 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9805 DCHECK_EQ(ip.GetCode(), 12u);
9806 const int32_t entry_point_offset =
9807 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9808 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +01009809 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009810}
9811
9812void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9813 uint32_t encoded_data,
9814 /*out*/ std::string* debug_name) {
9815 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9816 switch (kind) {
9817 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009818 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9819 CheckValidReg(base_reg.GetCode());
9820 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9821 CheckValidReg(holder_reg.GetCode());
9822 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9823 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9824 temps.Exclude(ip);
Roland Levillain988c3912019-09-25 19:33:35 +01009825 // In the case of a field load, if `base_reg` differs from
9826 // `holder_reg`, the offset was too large and we must have emitted (during the construction
9827 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
9828 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
9829 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
9830 // not necessarily do that check before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01009831 vixl32::Label throw_npe_label;
9832 vixl32::Label* throw_npe = nullptr;
9833 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9834 throw_npe = &throw_npe_label;
Andreas Gampe3db70682018-12-26 15:12:03 -08009835 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009836 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009837 // Check if the holder is gray and, if not, add fake dependency to the base register
9838 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9839 // to load the reference and call the entrypoint that performs further checks on the
9840 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009841 vixl32::Label slow_path;
9842 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9843 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9844 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9845 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009846 EmitGrayCheckAndFastPath(
9847 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009848 __ Bind(&slow_path);
9849 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9850 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +01009851 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009852 if (width == BakerReadBarrierWidth::kWide) {
9853 MemOperand ldr_half_address(lr, ldr_offset + 2);
9854 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9855 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9856 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9857 } else {
9858 MemOperand ldr_address(lr, ldr_offset);
9859 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9860 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9861 ep_reg, // for narrow LDR.
9862 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9863 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9864 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9865 }
9866 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9867 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009868 break;
9869 }
9870 case BakerReadBarrierKind::kArray: {
9871 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9872 CheckValidReg(base_reg.GetCode());
9873 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9874 BakerReadBarrierSecondRegField::Decode(encoded_data));
9875 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9876 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9877 temps.Exclude(ip);
9878 vixl32::Label slow_path;
9879 int32_t data_offset =
9880 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
9881 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
9882 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
9883 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
9884 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
9885 __ Bind(&slow_path);
9886 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9887 raw_ldr_offset;
9888 MemOperand ldr_address(lr, ldr_offset + 2);
9889 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
9890 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +01009891 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009892 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
9893 // a switch case target based on the index register.
9894 __ Mov(ip, base_reg); // Move the base register to ip0.
9895 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
9896 break;
9897 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009898 case BakerReadBarrierKind::kGcRoot:
9899 case BakerReadBarrierKind::kUnsafeCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009900 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
9901 // and it does not have a forwarding address), call the correct introspection entrypoint;
9902 // otherwise return the reference (or the extracted forwarding address).
9903 // There is no gray bit check for GC roots.
9904 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9905 CheckValidReg(root_reg.GetCode());
9906 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9907 BakerReadBarrierSecondRegField::Decode(encoded_data));
9908 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9909 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9910 temps.Exclude(ip);
9911 vixl32::Label return_label, not_marked, forwarding_address;
Andreas Gampe3db70682018-12-26 15:12:03 -08009912 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009913 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
9914 __ Ldr(ip, lock_word);
9915 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
9916 __ B(eq, &not_marked);
9917 __ Bind(&return_label);
9918 __ Bx(lr);
9919 __ Bind(&not_marked);
9920 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
9921 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
9922 " the highest bits and the 'forwarding address' state to have all bits set");
9923 __ Cmp(ip, Operand(0xc0000000));
9924 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +01009925 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009926 // Adjust the art_quick_read_barrier_mark_introspection address in kBakerCcEntrypointRegister
Vladimir Markod887ed82018-08-14 13:52:12 +00009927 // to one of art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},unsafe_cas}.
9928 DCHECK(kind != BakerReadBarrierKind::kUnsafeCas || width == BakerReadBarrierWidth::kWide);
9929 int32_t entrypoint_offset =
9930 (kind == BakerReadBarrierKind::kGcRoot)
9931 ? (width == BakerReadBarrierWidth::kWide)
9932 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
9933 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
9934 : BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009935 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
9936 __ Mov(ip, root_reg);
9937 __ Bx(ep_reg);
9938 __ Bind(&forwarding_address);
9939 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
9940 __ Bx(lr);
9941 break;
9942 }
9943 default:
9944 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
9945 UNREACHABLE();
9946 }
9947
Vladimir Marko966b46f2018-08-03 10:20:19 +00009948 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01009949 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01009950 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00009951 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009952 std::ostringstream oss;
9953 oss << "BakerReadBarrierThunk";
9954 switch (kind) {
9955 case BakerReadBarrierKind::kField:
9956 oss << "Field";
9957 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9958 oss << "Wide";
9959 }
9960 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
9961 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
9962 break;
9963 case BakerReadBarrierKind::kArray:
9964 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9965 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9966 BakerReadBarrierSecondRegField::Decode(encoded_data));
9967 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9968 break;
9969 case BakerReadBarrierKind::kGcRoot:
9970 oss << "GcRoot";
9971 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9972 oss << "Wide";
9973 }
9974 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9975 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9976 BakerReadBarrierSecondRegField::Decode(encoded_data));
9977 break;
Vladimir Markod887ed82018-08-14 13:52:12 +00009978 case BakerReadBarrierKind::kUnsafeCas:
9979 oss << "UnsafeCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9980 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9981 BakerReadBarrierSecondRegField::Decode(encoded_data));
9982 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9983 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009984 }
9985 *debug_name = oss.str();
9986 }
9987}
9988
9989#undef __
9990
Scott Wakelingfe885462016-09-22 10:24:38 +01009991} // namespace arm
9992} // namespace art