blob: 1d8fd6c66b036f210493437b81e2450114105c71 [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"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000021#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010025#include "code_generator_utils.h"
26#include "common_arm.h"
27#include "compiled_method.h"
28#include "entrypoints/quick/quick_entrypoints.h"
29#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010030#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070031#include "heap_poisoning.h"
Vladimir Marko6fd16062018-06-26 11:02:04 +010032#include "intrinsics.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010033#include "intrinsics_arm_vixl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010035#include "mirror/array-inl.h"
36#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000037#include "scoped_thread_state_change-inl.h"
Scott Wakelingfe885462016-09-22 10:24:38 +010038#include "thread.h"
39#include "utils/arm/assembler_arm_vixl.h"
40#include "utils/arm/managed_register_arm.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Vladimir Marko0a516052019-10-14 13:00:44 +000044namespace art {
Scott Wakelingfe885462016-09-22 10:24:38 +010045namespace arm {
46
47namespace vixl32 = vixl::aarch32;
48using namespace vixl32; // NOLINT(build/namespaces)
49
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +010050using helpers::DRegisterFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010051using helpers::HighRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080052using helpers::InputDRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010053using helpers::InputOperandAt;
Scott Wakelingc34dba72016-10-03 10:14:44 +010054using helpers::InputRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010055using helpers::InputRegisterAt;
Scott Wakelingfe885462016-09-22 10:24:38 +010056using helpers::InputSRegisterAt;
Anton Kirilov644032c2016-12-06 17:51:43 +000057using helpers::InputVRegister;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010058using helpers::InputVRegisterAt;
Scott Wakelingb77051e2016-11-21 19:46:00 +000059using helpers::Int32ConstantFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000060using helpers::Int64ConstantFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010061using helpers::LocationFrom;
62using helpers::LowRegisterFrom;
63using helpers::LowSRegisterFrom;
Donghui Bai426b49c2016-11-08 14:55:38 +080064using helpers::OperandFrom;
Scott Wakelinga7812ae2016-10-17 10:03:36 +010065using helpers::OutputRegister;
66using helpers::OutputSRegister;
67using helpers::OutputVRegister;
68using helpers::RegisterFrom;
69using helpers::SRegisterFrom;
Anton Kirilov644032c2016-12-06 17:51:43 +000070using helpers::Uint64ConstantFrom;
Scott Wakelingfe885462016-09-22 10:24:38 +010071
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +010072using vixl::EmissionCheckScope;
Artem Serov0fb37192016-12-06 18:13:40 +000073using vixl::ExactAssemblyScope;
74using vixl::CodeBufferCheckScope;
75
Scott Wakelingfe885462016-09-22 10:24:38 +010076using RegisterList = vixl32::RegisterList;
77
78static bool ExpectedPairLayout(Location location) {
79 // We expected this for both core and fpu register pairs.
80 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
81}
Artem Serovd4cc5b22016-11-04 11:19:09 +000082// Use a local definition to prevent copying mistakes.
83static constexpr size_t kArmWordSize = static_cast<size_t>(kArmPointerSize);
84static constexpr size_t kArmBitsPerWord = kArmWordSize * kBitsPerByte;
Artem Serov551b28f2016-10-18 19:11:30 +010085static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Scott Wakelingfe885462016-09-22 10:24:38 +010086
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010087// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
88// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010089// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010090// the offset explicitly.
91constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
92
Roland Levillain5daa4952017-07-03 17:23:56 +010093// Using a base helps identify when we hit Marking Register check breakpoints.
94constexpr int kMarkingRegisterCheckBreakCodeBaseCode = 0x10;
95
Scott Wakelingfe885462016-09-22 10:24:38 +010096#ifdef __
97#error "ARM Codegen VIXL macro-assembler macro already defined."
98#endif
99
Scott Wakelingfe885462016-09-22 10:24:38 +0100100// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
101#define __ down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler()-> // NOLINT
102#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
103
104// Marker that code is yet to be, and must, be implemented.
105#define TODO_VIXL32(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
106
Vladimir Marko88abba22017-05-03 17:09:25 +0100107static inline bool CanEmitNarrowLdr(vixl32::Register rt, vixl32::Register rn, uint32_t offset) {
108 return rt.IsLow() && rn.IsLow() && offset < 32u;
109}
110
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100111class EmitAdrCode {
112 public:
113 EmitAdrCode(ArmVIXLMacroAssembler* assembler, vixl32::Register rd, vixl32::Label* label)
114 : assembler_(assembler), rd_(rd), label_(label) {
Vladimir Markod887ed82018-08-14 13:52:12 +0000115 DCHECK(!assembler->AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +0100116 adr_location_ = assembler->GetCursorOffset();
117 assembler->adr(EncodingSize(Wide), rd, label);
118 }
119
120 ~EmitAdrCode() {
121 DCHECK(label_->IsBound());
122 // The ADR emitted by the assembler does not set the Thumb mode bit we need.
123 // TODO: Maybe extend VIXL to allow ADR for return address?
124 uint8_t* raw_adr = assembler_->GetBuffer()->GetOffsetAddress<uint8_t*>(adr_location_);
125 // Expecting ADR encoding T3 with `(offset & 1) == 0`.
126 DCHECK_EQ(raw_adr[1] & 0xfbu, 0xf2u); // Check bits 24-31, except 26.
127 DCHECK_EQ(raw_adr[0] & 0xffu, 0x0fu); // Check bits 16-23.
128 DCHECK_EQ(raw_adr[3] & 0x8fu, rd_.GetCode()); // Check bits 8-11 and 15.
129 DCHECK_EQ(raw_adr[2] & 0x01u, 0x00u); // Check bit 0, i.e. the `offset & 1`.
130 // Add the Thumb mode bit.
131 raw_adr[2] |= 0x01u;
132 }
133
134 private:
135 ArmVIXLMacroAssembler* const assembler_;
136 vixl32::Register rd_;
137 vixl32::Label* const label_;
138 int32_t adr_location_;
139};
140
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100141static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
142 InvokeRuntimeCallingConventionARMVIXL calling_convention;
143 RegisterSet caller_saves = RegisterSet::Empty();
144 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
145 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
146 // that the the kPrimNot result register is the same as the first argument register.
147 return caller_saves;
148}
149
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100150// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
151// for each live D registers they treat two corresponding S registers as live ones.
152//
153// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
154// from a list of contiguous S registers a list of contiguous D registers (processing first/last
155// S registers corner cases) and save/restore this new list treating them as D registers.
156// - decreasing code size
157// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
158// restored and then used in regular non SlowPath code as D register.
159//
160// For the following example (v means the S register is live):
161// D names: | D0 | D1 | D2 | D4 | ...
162// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
163// Live? | | v | v | v | v | v | v | | ...
164//
165// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
166// as D registers.
167//
168// TODO(VIXL): All this code should be unnecessary once the VIXL AArch32 backend provides helpers
169// for lists of floating-point registers.
170static size_t SaveContiguousSRegisterList(size_t first,
171 size_t last,
172 CodeGenerator* codegen,
173 size_t stack_offset) {
174 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
175 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
176 DCHECK_LE(first, last);
177 if ((first == last) && (first == 0)) {
178 __ Vstr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
179 return stack_offset + kSRegSizeInBytes;
180 }
181 if (first % 2 == 1) {
182 __ Vstr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
183 stack_offset += kSRegSizeInBytes;
184 }
185
186 bool save_last = false;
187 if (last % 2 == 0) {
188 save_last = true;
189 --last;
190 }
191
192 if (first < last) {
193 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
194 DCHECK_EQ((last - first + 1) % 2, 0u);
195 size_t number_of_d_regs = (last - first + 1) / 2;
196
197 if (number_of_d_regs == 1) {
198 __ Vstr(d_reg, MemOperand(sp, stack_offset));
199 } else if (number_of_d_regs > 1) {
200 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
201 vixl32::Register base = sp;
202 if (stack_offset != 0) {
203 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000204 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100205 }
206 __ Vstm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
207 }
208 stack_offset += number_of_d_regs * kDRegSizeInBytes;
209 }
210
211 if (save_last) {
212 __ Vstr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
213 stack_offset += kSRegSizeInBytes;
214 }
215
216 return stack_offset;
217}
218
219static size_t RestoreContiguousSRegisterList(size_t first,
220 size_t last,
221 CodeGenerator* codegen,
222 size_t stack_offset) {
223 static_assert(kSRegSizeInBytes == kArmWordSize, "Broken assumption on reg/word sizes.");
224 static_assert(kDRegSizeInBytes == 2 * kArmWordSize, "Broken assumption on reg/word sizes.");
225 DCHECK_LE(first, last);
226 if ((first == last) && (first == 0)) {
227 __ Vldr(vixl32::SRegister(first), MemOperand(sp, stack_offset));
228 return stack_offset + kSRegSizeInBytes;
229 }
230 if (first % 2 == 1) {
231 __ Vldr(vixl32::SRegister(first++), MemOperand(sp, stack_offset));
232 stack_offset += kSRegSizeInBytes;
233 }
234
235 bool restore_last = false;
236 if (last % 2 == 0) {
237 restore_last = true;
238 --last;
239 }
240
241 if (first < last) {
242 vixl32::DRegister d_reg = vixl32::DRegister(first / 2);
243 DCHECK_EQ((last - first + 1) % 2, 0u);
244 size_t number_of_d_regs = (last - first + 1) / 2;
245 if (number_of_d_regs == 1) {
246 __ Vldr(d_reg, MemOperand(sp, stack_offset));
247 } else if (number_of_d_regs > 1) {
248 UseScratchRegisterScope temps(down_cast<CodeGeneratorARMVIXL*>(codegen)->GetVIXLAssembler());
249 vixl32::Register base = sp;
250 if (stack_offset != 0) {
251 base = temps.Acquire();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000252 __ Add(base, sp, Operand::From(stack_offset));
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100253 }
254 __ Vldm(F64, base, NO_WRITE_BACK, DRegisterList(d_reg, number_of_d_regs));
255 }
256 stack_offset += number_of_d_regs * kDRegSizeInBytes;
257 }
258
259 if (restore_last) {
260 __ Vldr(vixl32::SRegister(last + 1), MemOperand(sp, stack_offset));
261 stack_offset += kSRegSizeInBytes;
262 }
263
264 return stack_offset;
265}
266
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100267static LoadOperandType GetLoadOperandType(DataType::Type type) {
268 switch (type) {
269 case DataType::Type::kReference:
270 return kLoadWord;
271 case DataType::Type::kBool:
272 case DataType::Type::kUint8:
273 return kLoadUnsignedByte;
274 case DataType::Type::kInt8:
275 return kLoadSignedByte;
276 case DataType::Type::kUint16:
277 return kLoadUnsignedHalfword;
278 case DataType::Type::kInt16:
279 return kLoadSignedHalfword;
280 case DataType::Type::kInt32:
281 return kLoadWord;
282 case DataType::Type::kInt64:
283 return kLoadWordPair;
284 case DataType::Type::kFloat32:
285 return kLoadSWord;
286 case DataType::Type::kFloat64:
287 return kLoadDWord;
288 default:
289 LOG(FATAL) << "Unreachable type " << type;
290 UNREACHABLE();
291 }
292}
293
294static StoreOperandType GetStoreOperandType(DataType::Type type) {
295 switch (type) {
296 case DataType::Type::kReference:
297 return kStoreWord;
298 case DataType::Type::kBool:
299 case DataType::Type::kUint8:
300 case DataType::Type::kInt8:
301 return kStoreByte;
302 case DataType::Type::kUint16:
303 case DataType::Type::kInt16:
304 return kStoreHalfword;
305 case DataType::Type::kInt32:
306 return kStoreWord;
307 case DataType::Type::kInt64:
308 return kStoreWordPair;
309 case DataType::Type::kFloat32:
310 return kStoreSWord;
311 case DataType::Type::kFloat64:
312 return kStoreDWord;
313 default:
314 LOG(FATAL) << "Unreachable type " << type;
315 UNREACHABLE();
316 }
317}
318
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100319void SlowPathCodeARMVIXL::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
320 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
321 size_t orig_offset = stack_offset;
322
Andreas Gampe3db70682018-12-26 15:12:03 -0800323 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100324 for (uint32_t i : LowToHighBits(core_spills)) {
325 // If the register holds an object, update the stack mask.
326 if (locations->RegisterContainsObject(i)) {
327 locations->SetStackBit(stack_offset / kVRegSize);
328 }
329 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
330 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
331 saved_core_stack_offsets_[i] = stack_offset;
332 stack_offset += kArmWordSize;
333 }
334
335 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
336 arm_codegen->GetAssembler()->StoreRegisterList(core_spills, orig_offset);
337
Andreas Gampe3db70682018-12-26 15:12:03 -0800338 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100339 orig_offset = stack_offset;
340 for (uint32_t i : LowToHighBits(fp_spills)) {
341 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
342 saved_fpu_stack_offsets_[i] = stack_offset;
343 stack_offset += kArmWordSize;
344 }
345
346 stack_offset = orig_offset;
347 while (fp_spills != 0u) {
348 uint32_t begin = CTZ(fp_spills);
349 uint32_t tmp = fp_spills + (1u << begin);
350 fp_spills &= tmp; // Clear the contiguous range of 1s.
351 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
352 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
353 }
354 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
355}
356
357void SlowPathCodeARMVIXL::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
358 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
359 size_t orig_offset = stack_offset;
360
Andreas Gampe3db70682018-12-26 15:12:03 -0800361 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100362 for (uint32_t i : LowToHighBits(core_spills)) {
363 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
364 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
365 stack_offset += kArmWordSize;
366 }
367
368 // TODO(VIXL): Check the coherency of stack_offset after this with a test.
369 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
370 arm_codegen->GetAssembler()->LoadRegisterList(core_spills, orig_offset);
371
Andreas Gampe3db70682018-12-26 15:12:03 -0800372 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100373 while (fp_spills != 0u) {
374 uint32_t begin = CTZ(fp_spills);
375 uint32_t tmp = fp_spills + (1u << begin);
376 fp_spills &= tmp; // Clear the contiguous range of 1s.
377 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
378 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
379 }
380 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
381}
382
383class NullCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
384 public:
385 explicit NullCheckSlowPathARMVIXL(HNullCheck* instruction) : SlowPathCodeARMVIXL(instruction) {}
386
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100387 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100388 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
389 __ Bind(GetEntryLabel());
390 if (instruction_->CanThrowIntoCatchBlock()) {
391 // Live registers will be restored in the catch block if caught.
392 SaveLiveRegisters(codegen, instruction_->GetLocations());
393 }
394 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
395 instruction_,
396 instruction_->GetDexPc(),
397 this);
398 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
399 }
400
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100401 bool IsFatal() const override { return true; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100403 const char* GetDescription() const override { return "NullCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100404
405 private:
406 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARMVIXL);
407};
408
Scott Wakelingfe885462016-09-22 10:24:38 +0100409class DivZeroCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
410 public:
411 explicit DivZeroCheckSlowPathARMVIXL(HDivZeroCheck* instruction)
412 : SlowPathCodeARMVIXL(instruction) {}
413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100414 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100415 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
Scott Wakelingfe885462016-09-22 10:24:38 +0100416 __ Bind(GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100417 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Scott Wakelingfe885462016-09-22 10:24:38 +0100418 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
419 }
420
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100421 bool IsFatal() const override { return true; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100423 const char* GetDescription() const override { return "DivZeroCheckSlowPathARMVIXL"; }
Scott Wakelingfe885462016-09-22 10:24:38 +0100424
425 private:
426 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARMVIXL);
427};
428
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100429class SuspendCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
430 public:
431 SuspendCheckSlowPathARMVIXL(HSuspendCheck* instruction, HBasicBlock* successor)
432 : SlowPathCodeARMVIXL(instruction), successor_(successor) {}
433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100434 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100435 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
436 __ Bind(GetEntryLabel());
437 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
438 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
439 if (successor_ == nullptr) {
440 __ B(GetReturnLabel());
441 } else {
442 __ B(arm_codegen->GetLabelOf(successor_));
443 }
444 }
445
446 vixl32::Label* GetReturnLabel() {
447 DCHECK(successor_ == nullptr);
448 return &return_label_;
449 }
450
451 HBasicBlock* GetSuccessor() const {
452 return successor_;
453 }
454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100455 const char* GetDescription() const override { return "SuspendCheckSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100456
457 private:
458 // If not null, the block to branch to after the suspend check.
459 HBasicBlock* const successor_;
460
461 // If `successor_` is null, the label to branch to after the suspend check.
462 vixl32::Label return_label_;
463
464 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARMVIXL);
465};
466
Scott Wakelingc34dba72016-10-03 10:14:44 +0100467class BoundsCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
468 public:
469 explicit BoundsCheckSlowPathARMVIXL(HBoundsCheck* instruction)
470 : SlowPathCodeARMVIXL(instruction) {}
471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100472 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100473 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
474 LocationSummary* locations = instruction_->GetLocations();
475
476 __ Bind(GetEntryLabel());
477 if (instruction_->CanThrowIntoCatchBlock()) {
478 // Live registers will be restored in the catch block if caught.
479 SaveLiveRegisters(codegen, instruction_->GetLocations());
480 }
481 // We're moving two locations to locations that could overlap, so we need a parallel
482 // move resolver.
483 InvokeRuntimeCallingConventionARMVIXL calling_convention;
484 codegen->EmitParallelMoves(
485 locations->InAt(0),
486 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100487 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100488 locations->InAt(1),
489 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100490 DataType::Type::kInt32);
Scott Wakelingc34dba72016-10-03 10:14:44 +0100491 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
492 ? kQuickThrowStringBounds
493 : kQuickThrowArrayBounds;
494 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
495 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
496 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
497 }
498
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100499 bool IsFatal() const override { return true; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100501 const char* GetDescription() const override { return "BoundsCheckSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100502
503 private:
504 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARMVIXL);
505};
506
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100507class LoadClassSlowPathARMVIXL : public SlowPathCodeARMVIXL {
508 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100509 LoadClassSlowPathARMVIXL(HLoadClass* cls, HInstruction* at)
510 : SlowPathCodeARMVIXL(at), cls_(cls) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100511 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100512 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100513 }
514
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100515 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000516 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000517 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100518 const uint32_t dex_pc = instruction_->GetDexPc();
519 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
520 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100521
522 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
523 __ Bind(GetEntryLabel());
524 SaveLiveRegisters(codegen, locations);
525
526 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100527 if (must_resolve_type) {
528 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm_codegen->GetGraph()->GetDexFile()));
529 dex::TypeIndex type_index = cls_->GetTypeIndex();
530 __ Mov(calling_convention.GetRegisterAt(0), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100531 arm_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
532 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100533 // If we also must_do_clinit, the resolved type is now in the correct register.
534 } else {
535 DCHECK(must_do_clinit);
536 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
537 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), source);
538 }
539 if (must_do_clinit) {
540 arm_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
541 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100542 }
543
544 // Move the class to the desired location.
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100545 if (out.IsValid()) {
546 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
547 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
548 }
549 RestoreLiveRegisters(codegen, locations);
550 __ B(GetExitLabel());
551 }
552
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100553 const char* GetDescription() const override { return "LoadClassSlowPathARMVIXL"; }
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100554
555 private:
556 // The class this slow path will load.
557 HLoadClass* const cls_;
558
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100559 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARMVIXL);
560};
561
Artem Serovd4cc5b22016-11-04 11:19:09 +0000562class LoadStringSlowPathARMVIXL : public SlowPathCodeARMVIXL {
563 public:
564 explicit LoadStringSlowPathARMVIXL(HLoadString* instruction)
565 : SlowPathCodeARMVIXL(instruction) {}
566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100567 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000568 DCHECK(instruction_->IsLoadString());
569 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000570 LocationSummary* locations = instruction_->GetLocations();
571 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000572 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Artem Serovd4cc5b22016-11-04 11:19:09 +0000573
574 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
575 __ Bind(GetEntryLabel());
576 SaveLiveRegisters(codegen, locations);
577
578 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000579 __ Mov(calling_convention.GetRegisterAt(0), string_index.index_);
Artem Serovd4cc5b22016-11-04 11:19:09 +0000580 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
581 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
582
Artem Serovd4cc5b22016-11-04 11:19:09 +0000583 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
584 RestoreLiveRegisters(codegen, locations);
585
586 __ B(GetExitLabel());
587 }
588
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100589 const char* GetDescription() const override { return "LoadStringSlowPathARMVIXL"; }
Artem Serovd4cc5b22016-11-04 11:19:09 +0000590
591 private:
592 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARMVIXL);
593};
594
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100595class TypeCheckSlowPathARMVIXL : public SlowPathCodeARMVIXL {
596 public:
597 TypeCheckSlowPathARMVIXL(HInstruction* instruction, bool is_fatal)
598 : SlowPathCodeARMVIXL(instruction), is_fatal_(is_fatal) {}
599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100600 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100601 LocationSummary* locations = instruction_->GetLocations();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100602 DCHECK(instruction_->IsCheckCast()
603 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
604
605 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
606 __ Bind(GetEntryLabel());
607
Vladimir Marko87584542017-12-12 17:47:52 +0000608 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100609 SaveLiveRegisters(codegen, locations);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100610 }
611
612 // We're moving two locations to locations that could overlap, so we need a parallel
613 // move resolver.
614 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100615
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800616 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800617 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100618 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800619 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800620 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100621 DataType::Type::kReference);
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100622 if (instruction_->IsInstanceOf()) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100623 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
624 instruction_,
625 instruction_->GetDexPc(),
626 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800627 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Artem Serovcfbe9132016-10-14 15:58:56 +0100628 arm_codegen->Move32(locations->Out(), LocationFrom(r0));
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100629 } else {
630 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800631 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
632 instruction_,
633 instruction_->GetDexPc(),
634 this);
635 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100636 }
637
638 if (!is_fatal_) {
Artem Serovcfbe9132016-10-14 15:58:56 +0100639 RestoreLiveRegisters(codegen, locations);
640 __ B(GetExitLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100641 }
642 }
643
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100644 const char* GetDescription() const override { return "TypeCheckSlowPathARMVIXL"; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100645
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100646 bool IsFatal() const override { return is_fatal_; }
Anton Kirilove28d9ae2016-10-25 18:17:23 +0100647
648 private:
649 const bool is_fatal_;
650
651 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARMVIXL);
652};
653
Scott Wakelingc34dba72016-10-03 10:14:44 +0100654class DeoptimizationSlowPathARMVIXL : public SlowPathCodeARMVIXL {
655 public:
656 explicit DeoptimizationSlowPathARMVIXL(HDeoptimize* instruction)
657 : SlowPathCodeARMVIXL(instruction) {}
658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100659 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100660 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
661 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100662 LocationSummary* locations = instruction_->GetLocations();
663 SaveLiveRegisters(codegen, locations);
664 InvokeRuntimeCallingConventionARMVIXL calling_convention;
665 __ Mov(calling_convention.GetRegisterAt(0),
666 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
667
Scott Wakelingc34dba72016-10-03 10:14:44 +0100668 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100669 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Scott Wakelingc34dba72016-10-03 10:14:44 +0100670 }
671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100672 const char* GetDescription() const override { return "DeoptimizationSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100673
674 private:
675 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARMVIXL);
676};
677
678class ArraySetSlowPathARMVIXL : public SlowPathCodeARMVIXL {
679 public:
680 explicit ArraySetSlowPathARMVIXL(HInstruction* instruction) : SlowPathCodeARMVIXL(instruction) {}
681
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100682 void EmitNativeCode(CodeGenerator* codegen) override {
Scott Wakelingc34dba72016-10-03 10:14:44 +0100683 LocationSummary* locations = instruction_->GetLocations();
684 __ Bind(GetEntryLabel());
685 SaveLiveRegisters(codegen, locations);
686
687 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100688 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Scott Wakelingc34dba72016-10-03 10:14:44 +0100689 parallel_move.AddMove(
690 locations->InAt(0),
691 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100692 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100693 nullptr);
694 parallel_move.AddMove(
695 locations->InAt(1),
696 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100697 DataType::Type::kInt32,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100698 nullptr);
699 parallel_move.AddMove(
700 locations->InAt(2),
701 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100702 DataType::Type::kReference,
Scott Wakelingc34dba72016-10-03 10:14:44 +0100703 nullptr);
704 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
705
706 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
707 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
708 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
709 RestoreLiveRegisters(codegen, locations);
710 __ B(GetExitLabel());
711 }
712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100713 const char* GetDescription() const override { return "ArraySetSlowPathARMVIXL"; }
Scott Wakelingc34dba72016-10-03 10:14:44 +0100714
715 private:
716 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARMVIXL);
717};
718
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000719// Slow path generating a read barrier for a heap reference.
720class ReadBarrierForHeapReferenceSlowPathARMVIXL : public SlowPathCodeARMVIXL {
721 public:
722 ReadBarrierForHeapReferenceSlowPathARMVIXL(HInstruction* instruction,
723 Location out,
724 Location ref,
725 Location obj,
726 uint32_t offset,
727 Location index)
728 : SlowPathCodeARMVIXL(instruction),
729 out_(out),
730 ref_(ref),
731 obj_(obj),
732 offset_(offset),
733 index_(index) {
734 DCHECK(kEmitCompilerReadBarrier);
735 // If `obj` is equal to `out` or `ref`, it means the initial object
736 // has been overwritten by (or after) the heap object reference load
737 // to be instrumented, e.g.:
738 //
739 // __ LoadFromOffset(kLoadWord, out, out, offset);
740 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
741 //
742 // In that case, we have lost the information about the original
743 // object, and the emitted read barrier cannot work properly.
744 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
745 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
746 }
747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100748 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000749 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
750 LocationSummary* locations = instruction_->GetLocations();
751 vixl32::Register reg_out = RegisterFrom(out_);
752 DCHECK(locations->CanCall());
753 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
754 DCHECK(instruction_->IsInstanceFieldGet() ||
755 instruction_->IsStaticFieldGet() ||
756 instruction_->IsArrayGet() ||
757 instruction_->IsInstanceOf() ||
758 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700759 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000760 << "Unexpected instruction in read barrier for heap reference slow path: "
761 << instruction_->DebugName();
762 // The read barrier instrumentation of object ArrayGet
763 // instructions does not support the HIntermediateAddress
764 // instruction.
765 DCHECK(!(instruction_->IsArrayGet() &&
766 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
767
768 __ Bind(GetEntryLabel());
769 SaveLiveRegisters(codegen, locations);
770
771 // We may have to change the index's value, but as `index_` is a
772 // constant member (like other "inputs" of this slow path),
773 // introduce a copy of it, `index`.
774 Location index = index_;
775 if (index_.IsValid()) {
776 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
777 if (instruction_->IsArrayGet()) {
778 // Compute the actual memory offset and store it in `index`.
779 vixl32::Register index_reg = RegisterFrom(index_);
780 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg.GetCode()));
781 if (codegen->IsCoreCalleeSaveRegister(index_reg.GetCode())) {
782 // We are about to change the value of `index_reg` (see the
Roland Levillain9983e302017-07-14 14:34:22 +0100783 // calls to art::arm::ArmVIXLMacroAssembler::Lsl and
784 // art::arm::ArmVIXLMacroAssembler::Add below), but it has
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000785 // not been saved by the previous call to
786 // art::SlowPathCode::SaveLiveRegisters, as it is a
787 // callee-save register --
788 // art::SlowPathCode::SaveLiveRegisters does not consider
789 // callee-save registers, as it has been designed with the
790 // assumption that callee-save registers are supposed to be
791 // handled by the called function. So, as a callee-save
792 // register, `index_reg` _would_ eventually be saved onto
793 // the stack, but it would be too late: we would have
794 // changed its value earlier. Therefore, we manually save
795 // it here into another freely available register,
796 // `free_reg`, chosen of course among the caller-save
797 // registers (as a callee-save `free_reg` register would
798 // exhibit the same problem).
799 //
800 // Note we could have requested a temporary register from
801 // the register allocator instead; but we prefer not to, as
802 // this is a slow path, and we know we can find a
803 // caller-save register that is available.
804 vixl32::Register free_reg = FindAvailableCallerSaveRegister(codegen);
805 __ Mov(free_reg, index_reg);
806 index_reg = free_reg;
807 index = LocationFrom(index_reg);
808 } else {
809 // The initial register stored in `index_` has already been
810 // saved in the call to art::SlowPathCode::SaveLiveRegisters
811 // (as it is not a callee-save register), so we can freely
812 // use it.
813 }
814 // Shifting the index value contained in `index_reg` by the scale
815 // factor (2) cannot overflow in practice, as the runtime is
816 // unable to allocate object arrays with a size larger than
817 // 2^26 - 1 (that is, 2^28 - 4 bytes).
818 __ Lsl(index_reg, index_reg, TIMES_4);
819 static_assert(
820 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
821 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
822 __ Add(index_reg, index_reg, offset_);
823 } else {
824 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
825 // intrinsics, `index_` is not shifted by a scale factor of 2
826 // (as in the case of ArrayGet), as it is actually an offset
827 // to an object field within an object.
828 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
829 DCHECK(instruction_->GetLocations()->Intrinsified());
830 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
831 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
832 << instruction_->AsInvoke()->GetIntrinsic();
833 DCHECK_EQ(offset_, 0U);
834 DCHECK(index_.IsRegisterPair());
835 // UnsafeGet's offset location is a register pair, the low
836 // part contains the correct offset.
837 index = index_.ToLow();
838 }
839 }
840
841 // We're moving two or three locations to locations that could
842 // overlap, so we need a parallel move resolver.
843 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100844 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000845 parallel_move.AddMove(ref_,
846 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100847 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000848 nullptr);
849 parallel_move.AddMove(obj_,
850 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100851 DataType::Type::kReference,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000852 nullptr);
853 if (index.IsValid()) {
854 parallel_move.AddMove(index,
855 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100856 DataType::Type::kInt32,
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000857 nullptr);
858 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
859 } else {
860 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
861 __ Mov(calling_convention.GetRegisterAt(2), offset_);
862 }
863 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
864 CheckEntrypointTypes<
865 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
866 arm_codegen->Move32(out_, LocationFrom(r0));
867
868 RestoreLiveRegisters(codegen, locations);
869 __ B(GetExitLabel());
870 }
871
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100872 const char* GetDescription() const override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000873 return "ReadBarrierForHeapReferenceSlowPathARMVIXL";
874 }
875
876 private:
877 vixl32::Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
878 uint32_t ref = RegisterFrom(ref_).GetCode();
879 uint32_t obj = RegisterFrom(obj_).GetCode();
880 for (uint32_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
881 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
882 return vixl32::Register(i);
883 }
884 }
885 // We shall never fail to find a free caller-save register, as
886 // there are more than two core caller-save registers on ARM
887 // (meaning it is possible to find one which is different from
888 // `ref` and `obj`).
889 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
890 LOG(FATAL) << "Could not find a free caller-save register";
891 UNREACHABLE();
892 }
893
894 const Location out_;
895 const Location ref_;
896 const Location obj_;
897 const uint32_t offset_;
898 // An additional location containing an index to an array.
899 // Only used for HArrayGet and the UnsafeGetObject &
900 // UnsafeGetObjectVolatile intrinsics.
901 const Location index_;
902
903 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARMVIXL);
904};
905
906// Slow path generating a read barrier for a GC root.
907class ReadBarrierForRootSlowPathARMVIXL : public SlowPathCodeARMVIXL {
908 public:
909 ReadBarrierForRootSlowPathARMVIXL(HInstruction* instruction, Location out, Location root)
910 : SlowPathCodeARMVIXL(instruction), out_(out), root_(root) {
911 DCHECK(kEmitCompilerReadBarrier);
912 }
913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100914 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000915 LocationSummary* locations = instruction_->GetLocations();
916 vixl32::Register reg_out = RegisterFrom(out_);
917 DCHECK(locations->CanCall());
918 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.GetCode()));
919 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
920 << "Unexpected instruction in read barrier for GC root slow path: "
921 << instruction_->DebugName();
922
923 __ Bind(GetEntryLabel());
924 SaveLiveRegisters(codegen, locations);
925
926 InvokeRuntimeCallingConventionARMVIXL calling_convention;
927 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
928 arm_codegen->Move32(LocationFrom(calling_convention.GetRegisterAt(0)), root_);
929 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
930 instruction_,
931 instruction_->GetDexPc(),
932 this);
933 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
934 arm_codegen->Move32(out_, LocationFrom(r0));
935
936 RestoreLiveRegisters(codegen, locations);
937 __ B(GetExitLabel());
938 }
939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100940 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARMVIXL"; }
Anton Kirilovedb2ac32016-11-30 15:14:10 +0000941
942 private:
943 const Location out_;
944 const Location root_;
945
946 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARMVIXL);
947};
Scott Wakelingc34dba72016-10-03 10:14:44 +0100948
Scott Wakelingfe885462016-09-22 10:24:38 +0100949inline vixl32::Condition ARMCondition(IfCondition cond) {
950 switch (cond) {
951 case kCondEQ: return eq;
952 case kCondNE: return ne;
953 case kCondLT: return lt;
954 case kCondLE: return le;
955 case kCondGT: return gt;
956 case kCondGE: return ge;
957 case kCondB: return lo;
958 case kCondBE: return ls;
959 case kCondA: return hi;
960 case kCondAE: return hs;
961 }
962 LOG(FATAL) << "Unreachable";
963 UNREACHABLE();
964}
965
966// Maps signed condition to unsigned condition.
967inline vixl32::Condition ARMUnsignedCondition(IfCondition cond) {
968 switch (cond) {
969 case kCondEQ: return eq;
970 case kCondNE: return ne;
971 // Signed to unsigned.
972 case kCondLT: return lo;
973 case kCondLE: return ls;
974 case kCondGT: return hi;
975 case kCondGE: return hs;
976 // Unsigned remain unchanged.
977 case kCondB: return lo;
978 case kCondBE: return ls;
979 case kCondA: return hi;
980 case kCondAE: return hs;
981 }
982 LOG(FATAL) << "Unreachable";
983 UNREACHABLE();
984}
985
986inline vixl32::Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
987 // The ARM condition codes can express all the necessary branches, see the
988 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
989 // There is no dex instruction or HIR that would need the missing conditions
990 // "equal or unordered" or "not equal".
991 switch (cond) {
992 case kCondEQ: return eq;
993 case kCondNE: return ne /* unordered */;
994 case kCondLT: return gt_bias ? cc : lt /* unordered */;
995 case kCondLE: return gt_bias ? ls : le /* unordered */;
996 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
997 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
998 default:
999 LOG(FATAL) << "UNREACHABLE";
1000 UNREACHABLE();
1001 }
1002}
1003
Anton Kirilov74234da2017-01-13 14:42:47 +00001004inline ShiftType ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1005 switch (op_kind) {
1006 case HDataProcWithShifterOp::kASR: return ShiftType::ASR;
1007 case HDataProcWithShifterOp::kLSL: return ShiftType::LSL;
1008 case HDataProcWithShifterOp::kLSR: return ShiftType::LSR;
1009 default:
1010 LOG(FATAL) << "Unexpected op kind " << op_kind;
1011 UNREACHABLE();
1012 }
1013}
1014
Scott Wakelingfe885462016-09-22 10:24:38 +01001015void CodeGeneratorARMVIXL::DumpCoreRegister(std::ostream& stream, int reg) const {
1016 stream << vixl32::Register(reg);
1017}
1018
1019void CodeGeneratorARMVIXL::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1020 stream << vixl32::SRegister(reg);
1021}
1022
Vladimir Markoa0431112018-06-25 09:32:54 +01001023const ArmInstructionSetFeatures& CodeGeneratorARMVIXL::GetInstructionSetFeatures() const {
1024 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArmInstructionSetFeatures();
1025}
1026
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001027static uint32_t ComputeSRegisterListMask(const SRegisterList& regs) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001028 uint32_t mask = 0;
1029 for (uint32_t i = regs.GetFirstSRegister().GetCode();
1030 i <= regs.GetLastSRegister().GetCode();
1031 ++i) {
1032 mask |= (1 << i);
1033 }
1034 return mask;
1035}
1036
Artem Serovd4cc5b22016-11-04 11:19:09 +00001037// Saves the register in the stack. Returns the size taken on stack.
1038size_t CodeGeneratorARMVIXL::SaveCoreRegister(size_t stack_index ATTRIBUTE_UNUSED,
1039 uint32_t reg_id ATTRIBUTE_UNUSED) {
1040 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001041 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001042}
1043
1044// Restores the register from the stack. Returns the size taken on stack.
1045size_t CodeGeneratorARMVIXL::RestoreCoreRegister(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
1051size_t CodeGeneratorARMVIXL::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1052 uint32_t reg_id ATTRIBUTE_UNUSED) {
1053 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001054 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00001055}
1056
1057size_t CodeGeneratorARMVIXL::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1058 uint32_t reg_id ATTRIBUTE_UNUSED) {
1059 TODO_VIXL32(FATAL);
Elliott Hughesc1896c92018-11-29 11:33:18 -08001060 UNREACHABLE();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01001061}
1062
Anton Kirilov74234da2017-01-13 14:42:47 +00001063static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1064 vixl32::Register out,
1065 vixl32::Register first,
1066 const Operand& second,
1067 CodeGeneratorARMVIXL* codegen) {
1068 if (second.IsImmediate() && second.GetImmediate() == 0) {
1069 const Operand in = kind == HInstruction::kAnd
1070 ? Operand(0)
1071 : Operand(first);
1072
1073 __ Mov(out, in);
1074 } else {
1075 switch (kind) {
1076 case HInstruction::kAdd:
1077 __ Add(out, first, second);
1078 break;
1079 case HInstruction::kAnd:
1080 __ And(out, first, second);
1081 break;
1082 case HInstruction::kOr:
1083 __ Orr(out, first, second);
1084 break;
1085 case HInstruction::kSub:
1086 __ Sub(out, first, second);
1087 break;
1088 case HInstruction::kXor:
1089 __ Eor(out, first, second);
1090 break;
1091 default:
1092 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1093 UNREACHABLE();
1094 }
1095 }
1096}
1097
1098static void GenerateDataProc(HInstruction::InstructionKind kind,
1099 const Location& out,
1100 const Location& first,
1101 const Operand& second_lo,
1102 const Operand& second_hi,
1103 CodeGeneratorARMVIXL* codegen) {
1104 const vixl32::Register first_hi = HighRegisterFrom(first);
1105 const vixl32::Register first_lo = LowRegisterFrom(first);
1106 const vixl32::Register out_hi = HighRegisterFrom(out);
1107 const vixl32::Register out_lo = LowRegisterFrom(out);
1108
1109 if (kind == HInstruction::kAdd) {
1110 __ Adds(out_lo, first_lo, second_lo);
1111 __ Adc(out_hi, first_hi, second_hi);
1112 } else if (kind == HInstruction::kSub) {
1113 __ Subs(out_lo, first_lo, second_lo);
1114 __ Sbc(out_hi, first_hi, second_hi);
1115 } else {
1116 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1117 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1118 }
1119}
1120
1121static Operand GetShifterOperand(vixl32::Register rm, ShiftType shift, uint32_t shift_imm) {
1122 return shift_imm == 0 ? Operand(rm) : Operand(rm, shift, shift_imm);
1123}
1124
1125static void GenerateLongDataProc(HDataProcWithShifterOp* instruction,
1126 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001127 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00001128 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1129
1130 const LocationSummary* const locations = instruction->GetLocations();
1131 const uint32_t shift_value = instruction->GetShiftAmount();
1132 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1133 const Location first = locations->InAt(0);
1134 const Location second = locations->InAt(1);
1135 const Location out = locations->Out();
1136 const vixl32::Register first_hi = HighRegisterFrom(first);
1137 const vixl32::Register first_lo = LowRegisterFrom(first);
1138 const vixl32::Register out_hi = HighRegisterFrom(out);
1139 const vixl32::Register out_lo = LowRegisterFrom(out);
1140 const vixl32::Register second_hi = HighRegisterFrom(second);
1141 const vixl32::Register second_lo = LowRegisterFrom(second);
1142 const ShiftType shift = ShiftFromOpKind(instruction->GetOpKind());
1143
1144 if (shift_value >= 32) {
1145 if (shift == ShiftType::LSL) {
1146 GenerateDataProcInstruction(kind,
1147 out_hi,
1148 first_hi,
1149 Operand(second_lo, ShiftType::LSL, shift_value - 32),
1150 codegen);
1151 GenerateDataProcInstruction(kind, out_lo, first_lo, 0, codegen);
1152 } else if (shift == ShiftType::ASR) {
1153 GenerateDataProc(kind,
1154 out,
1155 first,
1156 GetShifterOperand(second_hi, ShiftType::ASR, shift_value - 32),
1157 Operand(second_hi, ShiftType::ASR, 31),
1158 codegen);
1159 } else {
1160 DCHECK_EQ(shift, ShiftType::LSR);
1161 GenerateDataProc(kind,
1162 out,
1163 first,
1164 GetShifterOperand(second_hi, ShiftType::LSR, shift_value - 32),
1165 0,
1166 codegen);
1167 }
1168 } else {
1169 DCHECK_GT(shift_value, 1U);
1170 DCHECK_LT(shift_value, 32U);
1171
1172 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1173
1174 if (shift == ShiftType::LSL) {
1175 // We are not doing this for HInstruction::kAdd because the output will require
1176 // Location::kOutputOverlap; not applicable to other cases.
1177 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1178 GenerateDataProcInstruction(kind,
1179 out_hi,
1180 first_hi,
1181 Operand(second_hi, ShiftType::LSL, shift_value),
1182 codegen);
1183 GenerateDataProcInstruction(kind,
1184 out_hi,
1185 out_hi,
1186 Operand(second_lo, ShiftType::LSR, 32 - shift_value),
1187 codegen);
1188 GenerateDataProcInstruction(kind,
1189 out_lo,
1190 first_lo,
1191 Operand(second_lo, ShiftType::LSL, shift_value),
1192 codegen);
1193 } else {
1194 const vixl32::Register temp = temps.Acquire();
1195
1196 __ Lsl(temp, second_hi, shift_value);
1197 __ Orr(temp, temp, Operand(second_lo, ShiftType::LSR, 32 - shift_value));
1198 GenerateDataProc(kind,
1199 out,
1200 first,
1201 Operand(second_lo, ShiftType::LSL, shift_value),
1202 temp,
1203 codegen);
1204 }
1205 } else {
1206 DCHECK(shift == ShiftType::ASR || shift == ShiftType::LSR);
1207
1208 // We are not doing this for HInstruction::kAdd because the output will require
1209 // Location::kOutputOverlap; not applicable to other cases.
1210 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1211 GenerateDataProcInstruction(kind,
1212 out_lo,
1213 first_lo,
1214 Operand(second_lo, ShiftType::LSR, shift_value),
1215 codegen);
1216 GenerateDataProcInstruction(kind,
1217 out_lo,
1218 out_lo,
1219 Operand(second_hi, ShiftType::LSL, 32 - shift_value),
1220 codegen);
1221 GenerateDataProcInstruction(kind,
1222 out_hi,
1223 first_hi,
1224 Operand(second_hi, shift, shift_value),
1225 codegen);
1226 } else {
1227 const vixl32::Register temp = temps.Acquire();
1228
1229 __ Lsr(temp, second_lo, shift_value);
1230 __ Orr(temp, temp, Operand(second_hi, ShiftType::LSL, 32 - shift_value));
1231 GenerateDataProc(kind,
1232 out,
1233 first,
1234 temp,
1235 Operand(second_hi, shift, shift_value),
1236 codegen);
1237 }
1238 }
1239 }
1240}
1241
Donghui Bai426b49c2016-11-08 14:55:38 +08001242static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARMVIXL* codegen) {
1243 const Location rhs_loc = instruction->GetLocations()->InAt(1);
1244 if (rhs_loc.IsConstant()) {
1245 // 0.0 is the only immediate that can be encoded directly in
1246 // a VCMP instruction.
1247 //
1248 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1249 // specify that in a floating-point comparison, positive zero
1250 // and negative zero are considered equal, so we can use the
1251 // literal 0.0 for both cases here.
1252 //
1253 // Note however that some methods (Float.equal, Float.compare,
1254 // Float.compareTo, Double.equal, Double.compare,
1255 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1256 // StrictMath.min) consider 0.0 to be (strictly) greater than
1257 // -0.0. So if we ever translate calls to these methods into a
1258 // HCompare instruction, we must handle the -0.0 case with
1259 // care here.
1260 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001262 const DataType::Type type = instruction->InputAt(0)->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001264 if (type == DataType::Type::kFloat32) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001265 __ Vcmp(F32, InputSRegisterAt(instruction, 0), 0.0);
1266 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001267 DCHECK_EQ(type, DataType::Type::kFloat64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001268 __ Vcmp(F64, InputDRegisterAt(instruction, 0), 0.0);
1269 }
1270 } else {
1271 __ Vcmp(InputVRegisterAt(instruction, 0), InputVRegisterAt(instruction, 1));
1272 }
1273}
1274
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001275static int64_t AdjustConstantForCondition(int64_t value,
1276 IfCondition* condition,
1277 IfCondition* opposite) {
1278 if (value == 1) {
1279 if (*condition == kCondB) {
1280 value = 0;
1281 *condition = kCondEQ;
1282 *opposite = kCondNE;
1283 } else if (*condition == kCondAE) {
1284 value = 0;
1285 *condition = kCondNE;
1286 *opposite = kCondEQ;
1287 }
1288 } else if (value == -1) {
1289 if (*condition == kCondGT) {
1290 value = 0;
1291 *condition = kCondGE;
1292 *opposite = kCondLT;
1293 } else if (*condition == kCondLE) {
1294 value = 0;
1295 *condition = kCondLT;
1296 *opposite = kCondGE;
1297 }
1298 }
1299
1300 return value;
1301}
1302
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001303static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTestConstant(
1304 HCondition* condition,
1305 bool invert,
1306 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001307 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001308
1309 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001310 IfCondition cond = condition->GetCondition();
1311 IfCondition opposite = condition->GetOppositeCondition();
1312
1313 if (invert) {
1314 std::swap(cond, opposite);
1315 }
1316
1317 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001318 const Location left = locations->InAt(0);
1319 const Location right = locations->InAt(1);
1320
1321 DCHECK(right.IsConstant());
1322
1323 const vixl32::Register left_high = HighRegisterFrom(left);
1324 const vixl32::Register left_low = LowRegisterFrom(left);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001325 int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right), &cond, &opposite);
1326 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1327
1328 // Comparisons against 0 are common enough to deserve special attention.
1329 if (value == 0) {
1330 switch (cond) {
1331 case kCondNE:
1332 // x > 0 iff x != 0 when the comparison is unsigned.
1333 case kCondA:
1334 ret = std::make_pair(ne, eq);
1335 FALLTHROUGH_INTENDED;
1336 case kCondEQ:
1337 // x <= 0 iff x == 0 when the comparison is unsigned.
1338 case kCondBE:
1339 __ Orrs(temps.Acquire(), left_low, left_high);
1340 return ret;
1341 case kCondLT:
1342 case kCondGE:
1343 __ Cmp(left_high, 0);
1344 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1345 // Trivially true or false.
1346 case kCondB:
1347 ret = std::make_pair(ne, eq);
1348 FALLTHROUGH_INTENDED;
1349 case kCondAE:
1350 __ Cmp(left_low, left_low);
1351 return ret;
1352 default:
1353 break;
1354 }
1355 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001356
1357 switch (cond) {
1358 case kCondEQ:
1359 case kCondNE:
1360 case kCondB:
1361 case kCondBE:
1362 case kCondA:
1363 case kCondAE: {
Anton Kirilov23b752b2017-07-20 14:40:44 +01001364 const uint32_t value_low = Low32Bits(value);
1365 Operand operand_low(value_low);
1366
Donghui Bai426b49c2016-11-08 14:55:38 +08001367 __ Cmp(left_high, High32Bits(value));
1368
Anton Kirilov23b752b2017-07-20 14:40:44 +01001369 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1370 // we must ensure that the operands corresponding to the least significant
1371 // halves of the inputs fit into a 16-bit CMP encoding.
1372 if (!left_low.IsLow() || !IsUint<8>(value_low)) {
1373 operand_low = Operand(temps.Acquire());
1374 __ Mov(LeaveFlags, operand_low.GetBaseRegister(), value_low);
1375 }
1376
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001377 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001378 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1379 2 * vixl32::k16BitT32InstructionSizeInBytes,
1380 CodeBufferCheckScope::kExactSize);
1381
1382 __ it(eq);
Anton Kirilov23b752b2017-07-20 14:40:44 +01001383 __ cmp(eq, left_low, operand_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001384 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001385 break;
1386 }
1387 case kCondLE:
1388 case kCondGT:
1389 // Trivially true or false.
1390 if (value == std::numeric_limits<int64_t>::max()) {
1391 __ Cmp(left_low, left_low);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001392 ret = cond == kCondLE ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
Donghui Bai426b49c2016-11-08 14:55:38 +08001393 break;
1394 }
1395
1396 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001397 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001398 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001399 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001400 } else {
1401 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001402 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001403 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001404 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001405 }
1406
1407 value++;
1408 FALLTHROUGH_INTENDED;
1409 case kCondGE:
1410 case kCondLT: {
Donghui Bai426b49c2016-11-08 14:55:38 +08001411 __ Cmp(left_low, Low32Bits(value));
1412 __ Sbcs(temps.Acquire(), left_high, High32Bits(value));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001413 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001414 break;
1415 }
1416 default:
1417 LOG(FATAL) << "Unreachable";
1418 UNREACHABLE();
1419 }
1420
1421 return ret;
1422}
1423
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001424static std::pair<vixl32::Condition, vixl32::Condition> GenerateLongTest(
1425 HCondition* condition,
1426 bool invert,
1427 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001428 DCHECK_EQ(condition->GetLeft()->GetType(), DataType::Type::kInt64);
Donghui Bai426b49c2016-11-08 14:55:38 +08001429
1430 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001431 IfCondition cond = condition->GetCondition();
1432 IfCondition opposite = condition->GetOppositeCondition();
1433
1434 if (invert) {
1435 std::swap(cond, opposite);
1436 }
1437
1438 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001439 Location left = locations->InAt(0);
1440 Location right = locations->InAt(1);
1441
1442 DCHECK(right.IsRegisterPair());
1443
1444 switch (cond) {
1445 case kCondEQ:
1446 case kCondNE:
1447 case kCondB:
1448 case kCondBE:
1449 case kCondA:
1450 case kCondAE: {
1451 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right));
1452
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001453 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08001454 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1455 2 * vixl32::k16BitT32InstructionSizeInBytes,
1456 CodeBufferCheckScope::kExactSize);
1457
1458 __ it(eq);
1459 __ cmp(eq, LowRegisterFrom(left), LowRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001460 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001461 break;
1462 }
1463 case kCondLE:
1464 case kCondGT:
1465 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001466 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001467 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001468 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001469 } else {
1470 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001471 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001472 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001473 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001474 }
1475
1476 std::swap(left, right);
1477 FALLTHROUGH_INTENDED;
1478 case kCondGE:
1479 case kCondLT: {
1480 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1481
1482 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right));
1483 __ Sbcs(temps.Acquire(), HighRegisterFrom(left), HighRegisterFrom(right));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001484 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001485 break;
1486 }
1487 default:
1488 LOG(FATAL) << "Unreachable";
1489 UNREACHABLE();
1490 }
1491
1492 return ret;
1493}
1494
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001495static std::pair<vixl32::Condition, vixl32::Condition> GenerateTest(HCondition* condition,
1496 bool invert,
1497 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001498 const DataType::Type type = condition->GetLeft()->GetType();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001499 IfCondition cond = condition->GetCondition();
1500 IfCondition opposite = condition->GetOppositeCondition();
1501 std::pair<vixl32::Condition, vixl32::Condition> ret(eq, ne);
Donghui Bai426b49c2016-11-08 14:55:38 +08001502
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001503 if (invert) {
1504 std::swap(cond, opposite);
1505 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001507 if (type == DataType::Type::kInt64) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001508 ret = condition->GetLocations()->InAt(1).IsConstant()
1509 ? GenerateLongTestConstant(condition, invert, codegen)
1510 : GenerateLongTest(condition, invert, codegen);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001511 } else if (DataType::IsFloatingPointType(type)) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001512 GenerateVcmp(condition, codegen);
1513 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
1514 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1515 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001516 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001517 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001518 __ Cmp(InputRegisterAt(condition, 0), InputOperandAt(condition, 1));
1519 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001520 }
1521
1522 return ret;
1523}
1524
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001525static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001526 const vixl32::Register out = OutputRegister(cond);
1527 const auto condition = GenerateTest(cond, false, codegen);
1528
1529 __ Mov(LeaveFlags, out, 0);
1530
1531 if (out.IsLow()) {
1532 // We use the scope because of the IT block that follows.
1533 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1534 2 * vixl32::k16BitT32InstructionSizeInBytes,
1535 CodeBufferCheckScope::kExactSize);
1536
1537 __ it(condition.first);
1538 __ mov(condition.first, out, 1);
1539 } else {
1540 vixl32::Label done_label;
1541 vixl32::Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1542
Andreas Gampe3db70682018-12-26 15:12:03 -08001543 __ B(condition.second, final_label, /* is_far_target= */ false);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001544 __ Mov(out, 1);
1545
1546 if (done_label.IsReferenced()) {
1547 __ Bind(&done_label);
1548 }
1549 }
1550}
1551
1552static void GenerateEqualLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001553 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001554
1555 const LocationSummary* const locations = cond->GetLocations();
1556 IfCondition condition = cond->GetCondition();
1557 const vixl32::Register out = OutputRegister(cond);
1558 const Location left = locations->InAt(0);
1559 const Location right = locations->InAt(1);
1560 vixl32::Register left_high = HighRegisterFrom(left);
1561 vixl32::Register left_low = LowRegisterFrom(left);
1562 vixl32::Register temp;
1563 UseScratchRegisterScope temps(codegen->GetVIXLAssembler());
1564
1565 if (right.IsConstant()) {
1566 IfCondition opposite = cond->GetOppositeCondition();
1567 const int64_t value = AdjustConstantForCondition(Int64ConstantFrom(right),
1568 &condition,
1569 &opposite);
1570 Operand right_high = High32Bits(value);
1571 Operand right_low = Low32Bits(value);
1572
1573 // The output uses Location::kNoOutputOverlap.
1574 if (out.Is(left_high)) {
1575 std::swap(left_low, left_high);
1576 std::swap(right_low, right_high);
1577 }
1578
1579 __ Sub(out, left_low, right_low);
1580 temp = temps.Acquire();
1581 __ Sub(temp, left_high, right_high);
1582 } else {
1583 DCHECK(right.IsRegisterPair());
1584 temp = temps.Acquire();
1585 __ Sub(temp, left_high, HighRegisterFrom(right));
1586 __ Sub(out, left_low, LowRegisterFrom(right));
1587 }
1588
1589 // Need to check after calling AdjustConstantForCondition().
1590 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
1591
1592 if (condition == kCondNE && out.IsLow()) {
1593 __ Orrs(out, out, temp);
1594
1595 // We use the scope because of the IT block that follows.
1596 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1597 2 * vixl32::k16BitT32InstructionSizeInBytes,
1598 CodeBufferCheckScope::kExactSize);
1599
1600 __ it(ne);
1601 __ mov(ne, out, 1);
1602 } else {
1603 __ Orr(out, out, temp);
1604 codegen->GenerateConditionWithZero(condition, out, out, temp);
1605 }
1606}
1607
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001608static void GenerateConditionLong(HCondition* cond, CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 DCHECK_EQ(cond->GetLeft()->GetType(), DataType::Type::kInt64);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001610
1611 const LocationSummary* const locations = cond->GetLocations();
1612 IfCondition condition = cond->GetCondition();
1613 const vixl32::Register out = OutputRegister(cond);
1614 const Location left = locations->InAt(0);
1615 const Location right = locations->InAt(1);
1616
1617 if (right.IsConstant()) {
1618 IfCondition opposite = cond->GetOppositeCondition();
1619
1620 // Comparisons against 0 are common enough to deserve special attention.
1621 if (AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite) == 0) {
1622 switch (condition) {
1623 case kCondNE:
1624 case kCondA:
1625 if (out.IsLow()) {
1626 // We only care if both input registers are 0 or not.
1627 __ Orrs(out, LowRegisterFrom(left), HighRegisterFrom(left));
1628
1629 // We use the scope because of the IT block that follows.
1630 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1631 2 * vixl32::k16BitT32InstructionSizeInBytes,
1632 CodeBufferCheckScope::kExactSize);
1633
1634 __ it(ne);
1635 __ mov(ne, out, 1);
1636 return;
1637 }
1638
1639 FALLTHROUGH_INTENDED;
1640 case kCondEQ:
1641 case kCondBE:
1642 // We only care if both input registers are 0 or not.
1643 __ Orr(out, LowRegisterFrom(left), HighRegisterFrom(left));
1644 codegen->GenerateConditionWithZero(condition, out, out);
1645 return;
1646 case kCondLT:
1647 case kCondGE:
1648 // We only care about the sign bit.
1649 FALLTHROUGH_INTENDED;
1650 case kCondAE:
1651 case kCondB:
1652 codegen->GenerateConditionWithZero(condition, out, HighRegisterFrom(left));
1653 return;
1654 case kCondLE:
1655 case kCondGT:
1656 default:
1657 break;
1658 }
1659 }
1660 }
1661
Anton Kirilov23b752b2017-07-20 14:40:44 +01001662 // If `out` is a low register, then the GenerateConditionGeneric()
1663 // function generates a shorter code sequence that is still branchless.
1664 if ((condition == kCondEQ || condition == kCondNE) && !out.IsLow()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001665 GenerateEqualLong(cond, codegen);
1666 return;
1667 }
1668
Anton Kirilov23b752b2017-07-20 14:40:44 +01001669 GenerateConditionGeneric(cond, codegen);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001670}
1671
Roland Levillain6d729a72017-06-30 18:34:01 +01001672static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond,
1673 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 const DataType::Type type = cond->GetLeft()->GetType();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 if (type == DataType::Type::kInt64) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001679 GenerateConditionLong(cond, codegen);
1680 return;
1681 }
1682
1683 IfCondition condition = cond->GetCondition();
1684 vixl32::Register in = InputRegisterAt(cond, 0);
1685 const vixl32::Register out = OutputRegister(cond);
1686 const Location right = cond->GetLocations()->InAt(1);
1687 int64_t value;
1688
1689 if (right.IsConstant()) {
1690 IfCondition opposite = cond->GetOppositeCondition();
1691
1692 value = AdjustConstantForCondition(Int64ConstantFrom(right), &condition, &opposite);
1693
1694 // Comparisons against 0 are common enough to deserve special attention.
1695 if (value == 0) {
1696 switch (condition) {
1697 case kCondNE:
1698 case kCondA:
1699 if (out.IsLow() && out.Is(in)) {
1700 __ Cmp(out, 0);
1701
1702 // We use the scope because of the IT block that follows.
1703 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1704 2 * vixl32::k16BitT32InstructionSizeInBytes,
1705 CodeBufferCheckScope::kExactSize);
1706
1707 __ it(ne);
1708 __ mov(ne, out, 1);
1709 return;
1710 }
1711
1712 FALLTHROUGH_INTENDED;
1713 case kCondEQ:
1714 case kCondBE:
1715 case kCondLT:
1716 case kCondGE:
1717 case kCondAE:
1718 case kCondB:
1719 codegen->GenerateConditionWithZero(condition, out, in);
1720 return;
1721 case kCondLE:
1722 case kCondGT:
1723 default:
1724 break;
1725 }
1726 }
1727 }
1728
1729 if (condition == kCondEQ || condition == kCondNE) {
1730 Operand operand(0);
1731
1732 if (right.IsConstant()) {
1733 operand = Operand::From(value);
1734 } else if (out.Is(RegisterFrom(right))) {
1735 // Avoid 32-bit instructions if possible.
1736 operand = InputOperandAt(cond, 0);
1737 in = RegisterFrom(right);
1738 } else {
1739 operand = InputOperandAt(cond, 1);
1740 }
1741
1742 if (condition == kCondNE && out.IsLow()) {
1743 __ Subs(out, in, operand);
1744
1745 // We use the scope because of the IT block that follows.
1746 ExactAssemblyScope guard(codegen->GetVIXLAssembler(),
1747 2 * vixl32::k16BitT32InstructionSizeInBytes,
1748 CodeBufferCheckScope::kExactSize);
1749
1750 __ it(ne);
1751 __ mov(ne, out, 1);
1752 } else {
1753 __ Sub(out, in, operand);
1754 codegen->GenerateConditionWithZero(condition, out, out);
1755 }
1756
1757 return;
1758 }
1759
1760 GenerateConditionGeneric(cond, codegen);
1761}
1762
Donghui Bai426b49c2016-11-08 14:55:38 +08001763static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001764 const DataType::Type type = constant->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08001765 bool ret = false;
1766
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001769 if (type == DataType::Type::kInt64) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001770 const uint64_t value = Uint64ConstantFrom(constant);
1771
1772 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1773 } else {
1774 ret = IsUint<8>(Int32ConstantFrom(constant));
1775 }
1776
1777 return ret;
1778}
1779
1780static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001781 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001782
1783 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1784 return Location::ConstantLocation(constant->AsConstant());
1785 }
1786
1787 return Location::RequiresRegister();
1788}
1789
1790static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1791 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1792 // we check that we are not dealing with floating-point output (there is no
1793 // 16-bit VMOV encoding).
1794 if (!out.IsRegister() && !out.IsRegisterPair()) {
1795 return false;
1796 }
1797
1798 // For constants, we also check that the output is in one or two low registers,
1799 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1800 // MOV encoding can be used.
1801 if (src.IsConstant()) {
1802 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1803 return false;
1804 }
1805
1806 if (out.IsRegister()) {
1807 if (!RegisterFrom(out).IsLow()) {
1808 return false;
1809 }
1810 } else {
1811 DCHECK(out.IsRegisterPair());
1812
1813 if (!HighRegisterFrom(out).IsLow()) {
1814 return false;
1815 }
1816 }
1817 }
1818
1819 return true;
1820}
1821
Scott Wakelingfe885462016-09-22 10:24:38 +01001822#undef __
1823
Donghui Bai426b49c2016-11-08 14:55:38 +08001824vixl32::Label* CodeGeneratorARMVIXL::GetFinalLabel(HInstruction* instruction,
1825 vixl32::Label* final_label) {
1826 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001827 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001828
1829 const HBasicBlock* const block = instruction->GetBlock();
1830 const HLoopInformation* const info = block->GetLoopInformation();
1831 HInstruction* const next = instruction->GetNext();
1832
1833 // Avoid a branch to a branch.
1834 if (next->IsGoto() && (info == nullptr ||
1835 !info->IsBackEdge(*block) ||
1836 !info->HasSuspendCheck())) {
1837 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1838 }
1839
1840 return final_label;
1841}
1842
Scott Wakelingfe885462016-09-22 10:24:38 +01001843CodeGeneratorARMVIXL::CodeGeneratorARMVIXL(HGraph* graph,
Scott Wakelingfe885462016-09-22 10:24:38 +01001844 const CompilerOptions& compiler_options,
1845 OptimizingCompilerStats* stats)
1846 : CodeGenerator(graph,
1847 kNumberOfCoreRegisters,
1848 kNumberOfSRegisters,
1849 kNumberOfRegisterPairs,
1850 kCoreCalleeSaves.GetList(),
Scott Wakelinga7812ae2016-10-17 10:03:36 +01001851 ComputeSRegisterListMask(kFpuCalleeSaves),
Scott Wakelingfe885462016-09-22 10:24:38 +01001852 compiler_options,
1853 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001854 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1855 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Scott Wakelingfe885462016-09-22 10:24:38 +01001856 location_builder_(graph, this),
1857 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001858 move_resolver_(graph->GetAllocator(), this),
1859 assembler_(graph->GetAllocator()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001860 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001862 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001863 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001864 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001865 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001866 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001867 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +01001869 uint32_literals_(std::less<uint32_t>(),
1870 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001871 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001872 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serovc5fcb442016-12-02 19:19:58 +00001873 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001874 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1875 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1876 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Scott Wakelingfe885462016-09-22 10:24:38 +01001877 // Always save the LR register to mimic Quick.
1878 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00001879 // Give D30 and D31 as scratch register to VIXL. The register allocator only works on
1880 // S0-S31, which alias to D0-D15.
1881 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d31);
1882 GetVIXLAssembler()->GetScratchVRegisterList()->Combine(d30);
Scott Wakelingfe885462016-09-22 10:24:38 +01001883}
1884
Artem Serov551b28f2016-10-18 19:11:30 +01001885void JumpTableARMVIXL::EmitTable(CodeGeneratorARMVIXL* codegen) {
1886 uint32_t num_entries = switch_instr_->GetNumEntries();
1887 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1888
1889 // We are about to use the assembler to place literals directly. Make sure we have enough
Scott Wakelingb77051e2016-11-21 19:46:00 +00001890 // underlying code buffer and we have generated a jump table of the right size, using
1891 // codegen->GetVIXLAssembler()->GetBuffer().Align();
Artem Serov0fb37192016-12-06 18:13:40 +00001892 ExactAssemblyScope aas(codegen->GetVIXLAssembler(),
1893 num_entries * sizeof(int32_t),
1894 CodeBufferCheckScope::kMaximumSize);
Artem Serov551b28f2016-10-18 19:11:30 +01001895 // TODO(VIXL): Check that using lower case bind is fine here.
1896 codegen->GetVIXLAssembler()->bind(&table_start_);
Artem Serov09a940d2016-11-11 16:15:11 +00001897 for (uint32_t i = 0; i < num_entries; i++) {
1898 codegen->GetVIXLAssembler()->place(bb_addresses_[i].get());
1899 }
1900}
1901
1902void JumpTableARMVIXL::FixTable(CodeGeneratorARMVIXL* codegen) {
1903 uint32_t num_entries = switch_instr_->GetNumEntries();
1904 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
1905
Artem Serov551b28f2016-10-18 19:11:30 +01001906 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
1907 for (uint32_t i = 0; i < num_entries; i++) {
1908 vixl32::Label* target_label = codegen->GetLabelOf(successors[i]);
1909 DCHECK(target_label->IsBound());
1910 int32_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
1911 // When doing BX to address we need to have lower bit set to 1 in T32.
1912 if (codegen->GetVIXLAssembler()->IsUsingT32()) {
1913 jump_offset++;
1914 }
1915 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
1916 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
Artem Serov09a940d2016-11-11 16:15:11 +00001917
Scott Wakelingb77051e2016-11-21 19:46:00 +00001918 bb_addresses_[i].get()->UpdateValue(jump_offset, codegen->GetVIXLAssembler()->GetBuffer());
Artem Serov551b28f2016-10-18 19:11:30 +01001919 }
1920}
1921
Artem Serov09a940d2016-11-11 16:15:11 +00001922void CodeGeneratorARMVIXL::FixJumpTables() {
Artem Serov551b28f2016-10-18 19:11:30 +01001923 for (auto&& jump_table : jump_tables_) {
Artem Serov09a940d2016-11-11 16:15:11 +00001924 jump_table->FixTable(this);
Artem Serov551b28f2016-10-18 19:11:30 +01001925 }
1926}
1927
Andreas Gampeca620d72016-11-08 08:09:33 -08001928#define __ reinterpret_cast<ArmVIXLAssembler*>(GetAssembler())->GetVIXLAssembler()-> // NOLINT
Scott Wakelingfe885462016-09-22 10:24:38 +01001929
1930void CodeGeneratorARMVIXL::Finalize(CodeAllocator* allocator) {
Artem Serov09a940d2016-11-11 16:15:11 +00001931 FixJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001932
1933 // Emit JIT baker read barrier slow paths.
Vladimir Marko695348f2020-05-19 14:42:02 +01001934 DCHECK(GetCompilerOptions().IsJitCompiler() || jit_baker_read_barrier_slow_paths_.empty());
Vladimir Marko966b46f2018-08-03 10:20:19 +00001935 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1936 uint32_t encoded_data = entry.first;
1937 vixl::aarch32::Label* slow_path_entry = &entry.second.label;
1938 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -08001939 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00001940 }
1941
Scott Wakelingfe885462016-09-22 10:24:38 +01001942 GetAssembler()->FinalizeCode();
1943 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001944
1945 // Verify Baker read barrier linker patches.
1946 if (kIsDebugBuild) {
1947 ArrayRef<const uint8_t> code = allocator->GetMemory();
1948 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1949 DCHECK(info.label.IsBound());
1950 uint32_t literal_offset = info.label.GetLocation();
1951 DCHECK_ALIGNED(literal_offset, 2u);
1952
1953 auto GetInsn16 = [&code](uint32_t offset) {
1954 DCHECK_ALIGNED(offset, 2u);
1955 return (static_cast<uint32_t>(code[offset + 0]) << 0) +
1956 (static_cast<uint32_t>(code[offset + 1]) << 8);
1957 };
1958 auto GetInsn32 = [=](uint32_t offset) {
1959 return (GetInsn16(offset) << 16) + (GetInsn16(offset + 2u) << 0);
1960 };
1961
1962 uint32_t encoded_data = info.custom_data;
1963 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1964 // Check that the next instruction matches the expected LDR.
1965 switch (kind) {
1966 case BakerReadBarrierKind::kField: {
1967 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1968 if (width == BakerReadBarrierWidth::kWide) {
1969 DCHECK_GE(code.size() - literal_offset, 8u);
1970 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1971 // LDR (immediate), encoding T3, with correct base_reg.
1972 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1973 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1974 CHECK_EQ(next_insn & 0xffff0000u, 0xf8d00000u | (base_reg << 16));
1975 } else {
1976 DCHECK_GE(code.size() - literal_offset, 6u);
1977 uint32_t next_insn = GetInsn16(literal_offset + 4u);
1978 // LDR (immediate), encoding T1, with correct base_reg.
1979 CheckValidReg(next_insn & 0x7u); // Check destination register.
1980 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1981 CHECK_EQ(next_insn & 0xf838u, 0x6800u | (base_reg << 3));
1982 }
1983 break;
1984 }
1985 case BakerReadBarrierKind::kArray: {
1986 DCHECK_GE(code.size() - literal_offset, 8u);
1987 uint32_t next_insn = GetInsn32(literal_offset + 4u);
1988 // LDR (register) with correct base_reg, S=1 and option=011 (LDR Wt, [Xn, Xm, LSL #2]).
1989 CheckValidReg((next_insn >> 12) & 0xfu); // Check destination register.
1990 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1991 CHECK_EQ(next_insn & 0xffff0ff0u, 0xf8500020u | (base_reg << 16));
1992 CheckValidReg(next_insn & 0xf); // Check index register
1993 break;
1994 }
1995 case BakerReadBarrierKind::kGcRoot: {
1996 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
1997 if (width == BakerReadBarrierWidth::kWide) {
1998 DCHECK_GE(literal_offset, 4u);
1999 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
Vladimir Markof28be432018-08-14 12:20:51 +00002000 // LDR (immediate), encoding T3, with correct root_reg.
Vladimir Markoca1e0382018-04-11 09:58:41 +00002001 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Markof28be432018-08-14 12:20:51 +00002002 CHECK_EQ(prev_insn & 0xfff0f000u, 0xf8d00000u | (root_reg << 12));
Vladimir Markoca1e0382018-04-11 09:58:41 +00002003 } else {
2004 DCHECK_GE(literal_offset, 2u);
2005 uint32_t prev_insn = GetInsn16(literal_offset - 2u);
2006 // LDR (immediate), encoding T1, with correct root_reg.
2007 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2008 CHECK_EQ(prev_insn & 0xf807u, 0x6800u | root_reg);
2009 }
2010 break;
2011 }
Vladimir Markod887ed82018-08-14 13:52:12 +00002012 case BakerReadBarrierKind::kUnsafeCas: {
2013 DCHECK_GE(literal_offset, 4u);
2014 uint32_t prev_insn = GetInsn32(literal_offset - 4u);
2015 // ADD (register), encoding T3, with correct root_reg.
2016 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
2017 CHECK_EQ(prev_insn & 0xfff0fff0u, 0xeb000000u | (root_reg << 8));
2018 break;
2019 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00002020 default:
2021 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
2022 UNREACHABLE();
2023 }
2024 }
2025 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002026}
2027
2028void CodeGeneratorARMVIXL::SetupBlockedRegisters() const {
Scott Wakelingfe885462016-09-22 10:24:38 +01002029 // Stack register, LR and PC are always reserved.
2030 blocked_core_registers_[SP] = true;
2031 blocked_core_registers_[LR] = true;
2032 blocked_core_registers_[PC] = true;
2033
Roland Levillain6d729a72017-06-30 18:34:01 +01002034 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2035 // Reserve marking register.
2036 blocked_core_registers_[MR] = true;
2037 }
2038
Scott Wakelingfe885462016-09-22 10:24:38 +01002039 // Reserve thread register.
2040 blocked_core_registers_[TR] = true;
2041
2042 // Reserve temp register.
2043 blocked_core_registers_[IP] = true;
2044
2045 if (GetGraph()->IsDebuggable()) {
2046 // Stubs do not save callee-save floating point registers. If the graph
2047 // is debuggable, we need to deal with these registers differently. For
2048 // now, just block them.
2049 for (uint32_t i = kFpuCalleeSaves.GetFirstSRegister().GetCode();
2050 i <= kFpuCalleeSaves.GetLastSRegister().GetCode();
2051 ++i) {
2052 blocked_fpu_registers_[i] = true;
2053 }
2054 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002055}
2056
Scott Wakelingfe885462016-09-22 10:24:38 +01002057InstructionCodeGeneratorARMVIXL::InstructionCodeGeneratorARMVIXL(HGraph* graph,
2058 CodeGeneratorARMVIXL* codegen)
2059 : InstructionCodeGenerator(graph, codegen),
2060 assembler_(codegen->GetAssembler()),
2061 codegen_(codegen) {}
2062
2063void CodeGeneratorARMVIXL::ComputeSpillMask() {
2064 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Vladimir Marko460f0542019-07-04 14:02:08 +01002065 DCHECK_NE(core_spill_mask_ & (1u << kLrCode), 0u)
2066 << "At least the return address register must be saved";
2067 // 16-bit PUSH/POP (T1) can save/restore just the LR/PC.
2068 DCHECK(GetVIXLAssembler()->IsUsingT32());
Scott Wakelingfe885462016-09-22 10:24:38 +01002069 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2070 // We use vpush and vpop for saving and restoring floating point registers, which take
2071 // a SRegister and the number of registers to save/restore after that SRegister. We
2072 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2073 // but in the range.
2074 if (fpu_spill_mask_ != 0) {
2075 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2076 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2077 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2078 fpu_spill_mask_ |= (1 << i);
2079 }
2080 }
2081}
2082
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002083void CodeGeneratorARMVIXL::MaybeIncrementHotness(bool is_frame_entry) {
2084 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2085 UseScratchRegisterScope temps(GetVIXLAssembler());
2086 vixl32::Register temp = temps.Acquire();
2087 static_assert(ArtMethod::MaxCounter() == 0xFFFF, "asm is probably wrong");
2088 if (!is_frame_entry) {
2089 __ Push(vixl32::Register(kMethodRegister));
2090 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
2091 }
2092 // Load with zero extend to clear the high bits for integer overflow check.
2093 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2094 __ Add(temp, temp, 1);
2095 // Subtract one if the counter would overflow.
2096 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
2097 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2098 if (!is_frame_entry) {
2099 __ Pop(vixl32::Register(kMethodRegister));
2100 }
2101 }
2102
2103 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
2104 ScopedObjectAccess soa(Thread::Current());
2105 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002106 if (info != nullptr) {
2107 uint32_t address = reinterpret_cast32<uint32_t>(info);
2108 vixl::aarch32::Label done;
2109 UseScratchRegisterScope temps(GetVIXLAssembler());
2110 temps.Exclude(ip);
2111 if (!is_frame_entry) {
2112 __ Push(r4); // Will be used as temporary. For frame entry, r4 is always available.
2113 }
2114 __ Mov(r4, address);
2115 __ Ldrh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2116 __ Add(ip, ip, 1);
2117 __ Strh(ip, MemOperand(r4, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
2118 if (!is_frame_entry) {
2119 __ Pop(r4);
2120 }
2121 __ Lsls(ip, ip, 16);
2122 __ B(ne, &done);
2123 uint32_t entry_point_offset =
2124 GetThreadOffset<kArmPointerSize>(kQuickCompileOptimized).Int32Value();
2125 if (HasEmptyFrame()) {
2126 CHECK(is_frame_entry);
2127 // For leaf methods, we need to spill lr and r0. Also spill r1 and r2 for
2128 // alignment.
2129 uint32_t core_spill_mask =
2130 (1 << lr.GetCode()) | (1 << r0.GetCode()) | (1 << r1.GetCode()) | (1 << r2.GetCode());
2131 __ Push(RegisterList(core_spill_mask));
2132 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2133 __ Blx(lr);
2134 __ Pop(RegisterList(core_spill_mask));
2135 } else {
2136 if (!RequiresCurrentMethod()) {
2137 CHECK(is_frame_entry);
2138 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2139 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002140 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2141 __ Blx(lr);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002142 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002143 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002144 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002145 }
2146}
2147
Scott Wakelingfe885462016-09-22 10:24:38 +01002148void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2149 bool skip_overflow_check =
2150 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2151 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2152 __ Bind(&frame_entry_label_);
2153
2154 if (HasEmptyFrame()) {
David Srbecky30021842019-02-13 14:19:36 +00002155 // Ensure that the CFI opcode list is not empty.
2156 GetAssembler()->cfi().Nop();
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002157 MaybeIncrementHotness(/* is_frame_entry= */ true);
Scott Wakelingfe885462016-09-22 10:24:38 +01002158 return;
2159 }
2160
Scott Wakelingfe885462016-09-22 10:24:38 +01002161 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002162 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002163 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002164 vixl32::Register temp;
2165 // TODO: Remove this check when R4 is made a callee-save register
2166 // in ART compiled code (b/72801708). Currently we need to make
2167 // sure r4 is not blocked, e.g. in special purpose
2168 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2169 // here.
2170 if (!blocked_core_registers_[R4]) {
2171 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2172 DCHECK(!reg.Is(r4));
2173 }
2174 DCHECK(!kCoreCalleeSaves.Includes(r4));
2175 temp = r4;
2176 } else {
2177 temp = temps.Acquire();
2178 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002179 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002180 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002181 ExactAssemblyScope aas(GetVIXLAssembler(),
2182 vixl32::kMaxInstructionSizeInBytes,
2183 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002184 __ ldr(temp, MemOperand(temp));
2185 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002186 }
2187
Vladimir Marko1a225a72019-07-05 13:37:42 +01002188 uint32_t frame_size = GetFrameSize();
2189 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2190 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2191 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2192 core_spills_offset <= 3u * kArmWordSize) {
2193 // Do a single PUSH for core registers including the method and up to two
2194 // filler registers. Then store the single FP spill if any.
2195 // (The worst case is when the method is not required and we actually
2196 // store 3 extra registers but they are stored in the same properly
2197 // aligned 16-byte chunk where we're already writing anyway.)
2198 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2199 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize);
2200 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(core_spill_mask_));
2201 __ Push(RegisterList(core_spill_mask_ | extra_regs));
2202 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
2203 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2204 core_spills_offset,
2205 core_spill_mask_,
2206 kArmWordSize);
2207 if (fpu_spill_mask_ != 0u) {
2208 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2209 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2210 GetAssembler()->StoreSToOffset(sreg, sp, fp_spills_offset);
2211 GetAssembler()->cfi().RelOffset(DWARFReg(sreg), /*offset=*/ fp_spills_offset);
2212 }
2213 } else {
2214 __ Push(RegisterList(core_spill_mask_));
2215 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2216 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2217 /*offset=*/ 0,
2218 core_spill_mask_,
2219 kArmWordSize);
2220 if (fpu_spill_mask_ != 0) {
2221 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002222
Vladimir Marko1a225a72019-07-05 13:37:42 +01002223 // Check that list is contiguous.
2224 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002225
Vladimir Marko1a225a72019-07-05 13:37:42 +01002226 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2227 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
2228 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0),
2229 /*offset=*/ 0,
2230 fpu_spill_mask_,
2231 kArmWordSize);
2232 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002233
Vladimir Marko1a225a72019-07-05 13:37:42 +01002234 // Adjust SP and save the current method if we need it. Note that we do
2235 // not save the method in HCurrentMethod, as the instruction might have
2236 // been removed in the SSA graph.
2237 if (RequiresCurrentMethod() && fp_spills_offset <= 3 * kArmWordSize) {
2238 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2239 __ Push(RegisterList(MaxInt<uint32_t>(fp_spills_offset / kArmWordSize)));
2240 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2241 } else {
2242 __ Sub(sp, sp, dchecked_integral_cast<int32_t>(fp_spills_offset));
2243 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2244 if (RequiresCurrentMethod()) {
2245 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2246 }
2247 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002248 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002249
2250 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2251 UseScratchRegisterScope temps(GetVIXLAssembler());
2252 vixl32::Register temp = temps.Acquire();
2253 // Initialize should_deoptimize flag to 0.
2254 __ Mov(temp, 0);
2255 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2256 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002257
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002258 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08002259 MaybeGenerateMarkingRegisterCheck(/* code= */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002260}
2261
2262void CodeGeneratorARMVIXL::GenerateFrameExit() {
2263 if (HasEmptyFrame()) {
2264 __ Bx(lr);
2265 return;
2266 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002267
Scott Wakelingfe885462016-09-22 10:24:38 +01002268 // Pop LR into PC to return.
2269 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2270 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
Vladimir Marko1a225a72019-07-05 13:37:42 +01002271
2272 uint32_t frame_size = GetFrameSize();
2273 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2274 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2275 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2276 // r4 is blocked by TestCodeGeneratorARMVIXL used by some tests.
2277 core_spills_offset <= (blocked_core_registers_[r4.GetCode()] ? 2u : 3u) * kArmWordSize) {
2278 // Load the FP spill if any and then do a single POP including the method
2279 // and up to two filler registers. If we have no FP spills, this also has
2280 // the advantage that we do not need to emit CFI directives.
2281 if (fpu_spill_mask_ != 0u) {
2282 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2283 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2284 GetAssembler()->cfi().RememberState();
2285 GetAssembler()->LoadSFromOffset(sreg, sp, fp_spills_offset);
2286 GetAssembler()->cfi().Restore(DWARFReg(sreg));
2287 }
2288 // Clobber registers r2-r4 as they are caller-save in ART managed ABI and
2289 // never hold the return value.
2290 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize) << r2.GetCode();
2291 DCHECK_EQ(extra_regs & kCoreCalleeSaves.GetList(), 0u);
2292 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(pop_mask));
2293 __ Pop(RegisterList(pop_mask | extra_regs));
2294 if (fpu_spill_mask_ != 0u) {
2295 GetAssembler()->cfi().RestoreState();
2296 }
2297 } else {
2298 GetAssembler()->cfi().RememberState();
2299 __ Add(sp, sp, fp_spills_offset);
2300 GetAssembler()->cfi().AdjustCFAOffset(-dchecked_integral_cast<int32_t>(fp_spills_offset));
2301 if (fpu_spill_mask_ != 0) {
2302 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2303
2304 // Check that list is contiguous.
2305 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2306
2307 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2308 GetAssembler()->cfi().AdjustCFAOffset(
2309 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
2310 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
2311 }
2312 __ Pop(RegisterList(pop_mask));
2313 GetAssembler()->cfi().RestoreState();
2314 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2315 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002316}
2317
2318void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2319 __ Bind(GetLabelOf(block));
2320}
2321
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002323 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002324 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002325 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002326 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 case DataType::Type::kInt8:
2328 case DataType::Type::kUint16:
2329 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002330 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002331 uint32_t index = gp_index_++;
2332 uint32_t stack_index = stack_index_++;
2333 if (index < calling_convention.GetNumberOfRegisters()) {
2334 return LocationFrom(calling_convention.GetRegisterAt(index));
2335 } else {
2336 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2337 }
2338 }
2339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002341 uint32_t index = gp_index_;
2342 uint32_t stack_index = stack_index_;
2343 gp_index_ += 2;
2344 stack_index_ += 2;
2345 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2346 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2347 // Skip R1, and use R2_R3 instead.
2348 gp_index_++;
2349 index++;
2350 }
2351 }
2352 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2353 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2354 calling_convention.GetRegisterAt(index + 1).GetCode());
2355
2356 return LocationFrom(calling_convention.GetRegisterAt(index),
2357 calling_convention.GetRegisterAt(index + 1));
2358 } else {
2359 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2360 }
2361 }
2362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002364 uint32_t stack_index = stack_index_++;
2365 if (float_index_ % 2 == 0) {
2366 float_index_ = std::max(double_index_, float_index_);
2367 }
2368 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2369 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2370 } else {
2371 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2372 }
2373 }
2374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002376 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2377 uint32_t stack_index = stack_index_;
2378 stack_index_ += 2;
2379 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2380 uint32_t index = double_index_;
2381 double_index_ += 2;
2382 Location result = LocationFrom(
2383 calling_convention.GetFpuRegisterAt(index),
2384 calling_convention.GetFpuRegisterAt(index + 1));
2385 DCHECK(ExpectedPairLayout(result));
2386 return result;
2387 } else {
2388 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2389 }
2390 }
2391
Aart Bik66c158e2018-01-31 12:55:04 -08002392 case DataType::Type::kUint32:
2393 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002394 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002395 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002396 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002397 }
2398 return Location::NoLocation();
2399}
2400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002401Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002402 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002403 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002405 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 case DataType::Type::kInt8:
2407 case DataType::Type::kUint16:
2408 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002409 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002410 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002411 return LocationFrom(r0);
2412 }
2413
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002414 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002415 return LocationFrom(s0);
2416 }
2417
Aart Bik66c158e2018-01-31 12:55:04 -08002418 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002419 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002420 return LocationFrom(r0, r1);
2421 }
2422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002424 return LocationFrom(s0, s1);
2425 }
2426
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002428 return Location::NoLocation();
2429 }
2430
2431 UNREACHABLE();
2432}
2433
2434Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2435 return LocationFrom(kMethodRegister);
2436}
2437
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002438void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2439 if (source.Equals(destination)) {
2440 return;
2441 }
2442 if (destination.IsRegister()) {
2443 if (source.IsRegister()) {
2444 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2445 } else if (source.IsFpuRegister()) {
2446 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2447 } else {
2448 GetAssembler()->LoadFromOffset(kLoadWord,
2449 RegisterFrom(destination),
2450 sp,
2451 source.GetStackIndex());
2452 }
2453 } else if (destination.IsFpuRegister()) {
2454 if (source.IsRegister()) {
2455 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2456 } else if (source.IsFpuRegister()) {
2457 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2458 } else {
2459 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2460 }
2461 } else {
2462 DCHECK(destination.IsStackSlot()) << destination;
2463 if (source.IsRegister()) {
2464 GetAssembler()->StoreToOffset(kStoreWord,
2465 RegisterFrom(source),
2466 sp,
2467 destination.GetStackIndex());
2468 } else if (source.IsFpuRegister()) {
2469 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2470 } else {
2471 DCHECK(source.IsStackSlot()) << source;
2472 UseScratchRegisterScope temps(GetVIXLAssembler());
2473 vixl32::Register temp = temps.Acquire();
2474 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2475 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2476 }
2477 }
2478}
2479
Artem Serovcfbe9132016-10-14 15:58:56 +01002480void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2481 DCHECK(location.IsRegister());
2482 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002483}
2484
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002485void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002486 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2487 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002488 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002489 move.AddMove(src, dst, dst_type, nullptr);
2490 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002491}
2492
Artem Serovcfbe9132016-10-14 15:58:56 +01002493void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2494 if (location.IsRegister()) {
2495 locations->AddTemp(location);
2496 } else if (location.IsRegisterPair()) {
2497 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2498 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2499 } else {
2500 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2501 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002502}
2503
2504void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2505 HInstruction* instruction,
2506 uint32_t dex_pc,
2507 SlowPathCode* slow_path) {
2508 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Vladimir Markof6675082019-05-17 12:05:28 +01002509
2510 ThreadOffset32 entrypoint_offset = GetThreadOffset<kArmPointerSize>(entrypoint);
2511 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
2512 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
2513 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
Vladimir Marko695348f2020-05-19 14:42:02 +01002514 if (slow_path == nullptr || GetCompilerOptions().IsJitCompiler()) {
Vladimir Markof6675082019-05-17 12:05:28 +01002515 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
2516 // Ensure the pc position is recorded immediately after the `blx` instruction.
2517 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
2518 ExactAssemblyScope aas(GetVIXLAssembler(),
2519 vixl32::k16BitT32InstructionSizeInBytes,
2520 CodeBufferCheckScope::kExactSize);
2521 __ blx(lr);
2522 if (EntrypointRequiresStackMap(entrypoint)) {
2523 RecordPcInfo(instruction, dex_pc, slow_path);
2524 }
2525 } else {
2526 // Ensure the pc position is recorded immediately after the `bl` instruction.
2527 ExactAssemblyScope aas(GetVIXLAssembler(),
2528 vixl32::k32BitT32InstructionSizeInBytes,
2529 CodeBufferCheckScope::kExactSize);
2530 EmitEntrypointThunkCall(entrypoint_offset);
2531 if (EntrypointRequiresStackMap(entrypoint)) {
2532 RecordPcInfo(instruction, dex_pc, slow_path);
2533 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002534 }
2535}
2536
2537void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2538 HInstruction* instruction,
2539 SlowPathCode* slow_path) {
2540 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002541 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002542 __ Blx(lr);
2543}
2544
Scott Wakelingfe885462016-09-22 10:24:38 +01002545void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002546 if (successor->IsExitBlock()) {
2547 DCHECK(got->GetPrevious()->AlwaysThrows());
2548 return; // no code needed
2549 }
2550
Scott Wakelingfe885462016-09-22 10:24:38 +01002551 HBasicBlock* block = got->GetBlock();
2552 HInstruction* previous = got->GetPrevious();
2553 HLoopInformation* info = block->GetLoopInformation();
2554
2555 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00002556 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Scott Wakelingfe885462016-09-22 10:24:38 +01002557 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2558 return;
2559 }
2560 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2561 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08002562 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002563 }
2564 if (!codegen_->GoesToNextBlock(block, successor)) {
2565 __ B(codegen_->GetLabelOf(successor));
2566 }
2567}
2568
2569void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2570 got->SetLocations(nullptr);
2571}
2572
2573void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2574 HandleGoto(got, got->GetSuccessor());
2575}
2576
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002577void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2578 try_boundary->SetLocations(nullptr);
2579}
2580
2581void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2582 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2583 if (!successor->IsExitBlock()) {
2584 HandleGoto(try_boundary, successor);
2585 }
2586}
2587
Scott Wakelingfe885462016-09-22 10:24:38 +01002588void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2589 exit->SetLocations(nullptr);
2590}
2591
2592void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2593}
2594
Scott Wakelingfe885462016-09-22 10:24:38 +01002595void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002596 vixl32::Label* true_target,
2597 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002598 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002599 if (true_target == false_target) {
2600 DCHECK(true_target != nullptr);
2601 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002602 return;
2603 }
2604
Anton Kirilov23b752b2017-07-20 14:40:44 +01002605 vixl32::Label* non_fallthrough_target;
2606 bool invert;
2607 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002608
Anton Kirilov23b752b2017-07-20 14:40:44 +01002609 if (true_target == nullptr) {
2610 // The true target is fallthrough.
2611 DCHECK(false_target != nullptr);
2612 non_fallthrough_target = false_target;
2613 invert = true;
2614 emit_both_branches = false;
2615 } else {
2616 non_fallthrough_target = true_target;
2617 invert = false;
2618 // Either the false target is fallthrough, or there is no fallthrough
2619 // and both branches must be emitted.
2620 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002621 }
2622
Anton Kirilov23b752b2017-07-20 14:40:44 +01002623 const auto cond = GenerateTest(condition, invert, codegen_);
2624
2625 __ B(cond.first, non_fallthrough_target, is_far_target);
2626
2627 if (emit_both_branches) {
2628 // No target falls through, we need to branch.
2629 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002630 }
2631}
2632
2633void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2634 size_t condition_input_index,
2635 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002636 vixl32::Label* false_target,
2637 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002638 HInstruction* cond = instruction->InputAt(condition_input_index);
2639
2640 if (true_target == nullptr && false_target == nullptr) {
2641 // Nothing to do. The code always falls through.
2642 return;
2643 } else if (cond->IsIntConstant()) {
2644 // Constant condition, statically compared against "true" (integer value 1).
2645 if (cond->AsIntConstant()->IsTrue()) {
2646 if (true_target != nullptr) {
2647 __ B(true_target);
2648 }
2649 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002650 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002651 if (false_target != nullptr) {
2652 __ B(false_target);
2653 }
2654 }
2655 return;
2656 }
2657
2658 // The following code generates these patterns:
2659 // (1) true_target == nullptr && false_target != nullptr
2660 // - opposite condition true => branch to false_target
2661 // (2) true_target != nullptr && false_target == nullptr
2662 // - condition true => branch to true_target
2663 // (3) true_target != nullptr && false_target != nullptr
2664 // - condition true => branch to true_target
2665 // - branch to false_target
2666 if (IsBooleanValueOrMaterializedCondition(cond)) {
2667 // Condition has been materialized, compare the output to 0.
2668 if (kIsDebugBuild) {
2669 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2670 DCHECK(cond_val.IsRegister());
2671 }
2672 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002673 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2674 false_target,
2675 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002676 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002677 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2678 true_target,
2679 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002680 }
2681 } else {
2682 // Condition has not been materialized. Use its inputs as the comparison and
2683 // its condition as the branch condition.
2684 HCondition* condition = cond->AsCondition();
2685
2686 // If this is a long or FP comparison that has been folded into
2687 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002688 DataType::Type type = condition->InputAt(0)->GetType();
2689 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002690 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002691 return;
2692 }
2693
Donghui Bai426b49c2016-11-08 14:55:38 +08002694 vixl32::Label* non_fallthrough_target;
2695 vixl32::Condition arm_cond = vixl32::Condition::None();
2696 const vixl32::Register left = InputRegisterAt(cond, 0);
2697 const Operand right = InputOperandAt(cond, 1);
2698
Scott Wakelingfe885462016-09-22 10:24:38 +01002699 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002700 arm_cond = ARMCondition(condition->GetOppositeCondition());
2701 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002702 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002703 arm_cond = ARMCondition(condition->GetCondition());
2704 non_fallthrough_target = true_target;
2705 }
2706
2707 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2708 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002709 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002710 } else {
2711 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002712 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002713 }
2714 } else {
2715 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002716 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002717 }
2718 }
2719
2720 // If neither branch falls through (case 3), the conditional branch to `true_target`
2721 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2722 if (true_target != nullptr && false_target != nullptr) {
2723 __ B(false_target);
2724 }
2725}
2726
2727void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002728 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002729 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2730 locations->SetInAt(0, Location::RequiresRegister());
2731 }
2732}
2733
2734void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2735 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2736 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002737 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2738 nullptr : codegen_->GetLabelOf(true_successor);
2739 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2740 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002741 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002742}
2743
Scott Wakelingc34dba72016-10-03 10:14:44 +01002744void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002745 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002746 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002747 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2748 RegisterSet caller_saves = RegisterSet::Empty();
2749 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2750 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002751 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2752 locations->SetInAt(0, Location::RequiresRegister());
2753 }
2754}
2755
2756void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2757 SlowPathCodeARMVIXL* slow_path =
2758 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2759 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002760 /* condition_input_index= */ 0,
Scott Wakelingc34dba72016-10-03 10:14:44 +01002761 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002762 /* false_target= */ nullptr);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002763}
2764
Artem Serovd4cc5b22016-11-04 11:19:09 +00002765void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002766 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002767 LocationSummary(flag, LocationSummary::kNoCall);
2768 locations->SetOut(Location::RequiresRegister());
2769}
2770
2771void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2772 GetAssembler()->LoadFromOffset(kLoadWord,
2773 OutputRegister(flag),
2774 sp,
2775 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2776}
2777
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002778void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002779 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002781
2782 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002783 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002784 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002785 } else {
2786 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002787 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002788 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002789
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002790 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002791 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2792 // The code generator handles overlap with the values, but not with the condition.
2793 locations->SetOut(Location::SameAsFirstInput());
2794 } else if (is_floating_point) {
2795 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2796 } else {
2797 if (!locations->InAt(1).IsConstant()) {
2798 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2799 }
2800
2801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002802 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002803}
2804
2805void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002806 HInstruction* const condition = select->GetCondition();
2807 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002809 const Location first = locations->InAt(0);
2810 const Location out = locations->Out();
2811 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002812
2813 // In the unlucky case the output of this instruction overlaps
2814 // with an input of an "emitted-at-use-site" condition, and
2815 // the output of this instruction is not one of its inputs, we'll
2816 // need to fallback to branches instead of conditional ARM instructions.
2817 bool output_overlaps_with_condition_inputs =
2818 !IsBooleanValueOrMaterializedCondition(condition) &&
2819 !out.Equals(first) &&
2820 !out.Equals(second) &&
2821 (condition->GetLocations()->InAt(0).Equals(out) ||
2822 condition->GetLocations()->InAt(1).Equals(out));
2823 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002824 Location src;
2825
2826 if (condition->IsIntConstant()) {
2827 if (condition->AsIntConstant()->IsFalse()) {
2828 src = first;
2829 } else {
2830 src = second;
2831 }
2832
2833 codegen_->MoveLocation(out, src, type);
2834 return;
2835 }
2836
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002837 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002838 bool invert = false;
2839
2840 if (out.Equals(second)) {
2841 src = first;
2842 invert = true;
2843 } else if (out.Equals(first)) {
2844 src = second;
2845 } else if (second.IsConstant()) {
2846 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2847 src = second;
2848 } else if (first.IsConstant()) {
2849 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2850 src = first;
2851 invert = true;
2852 } else {
2853 src = second;
2854 }
2855
2856 if (CanGenerateConditionalMove(out, src)) {
2857 if (!out.Equals(first) && !out.Equals(second)) {
2858 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2859 }
2860
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002861 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2862
2863 if (IsBooleanValueOrMaterializedCondition(condition)) {
2864 __ Cmp(InputRegisterAt(select, 2), 0);
2865 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2866 } else {
2867 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2868 }
2869
Donghui Bai426b49c2016-11-08 14:55:38 +08002870 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002871 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002872 ExactAssemblyScope guard(GetVIXLAssembler(),
2873 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2874 CodeBufferCheckScope::kExactSize);
2875
2876 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002877 __ it(cond.first);
2878 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002879 } else {
2880 DCHECK(out.IsRegisterPair());
2881
2882 Operand operand_high(0);
2883 Operand operand_low(0);
2884
2885 if (src.IsConstant()) {
2886 const int64_t value = Int64ConstantFrom(src);
2887
2888 operand_high = High32Bits(value);
2889 operand_low = Low32Bits(value);
2890 } else {
2891 DCHECK(src.IsRegisterPair());
2892 operand_high = HighRegisterFrom(src);
2893 operand_low = LowRegisterFrom(src);
2894 }
2895
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002896 __ it(cond.first);
2897 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2898 __ it(cond.first);
2899 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002900 }
2901
2902 return;
2903 }
2904 }
2905
2906 vixl32::Label* false_target = nullptr;
2907 vixl32::Label* true_target = nullptr;
2908 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002909 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08002910 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2911
2912 if (out.Equals(second)) {
2913 true_target = target;
2914 src = first;
2915 } else {
2916 false_target = target;
2917 src = second;
2918
2919 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002920 if (output_overlaps_with_condition_inputs) {
2921 false_target = &other_case;
2922 } else {
2923 codegen_->MoveLocation(out, first, type);
2924 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002925 }
2926 }
2927
Andreas Gampe3db70682018-12-26 15:12:03 -08002928 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target= */ false);
Donghui Bai426b49c2016-11-08 14:55:38 +08002929 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002930 if (output_overlaps_with_condition_inputs) {
2931 __ B(target);
2932 __ Bind(&other_case);
2933 codegen_->MoveLocation(out, first, type);
2934 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002935
2936 if (select_end.IsReferenced()) {
2937 __ Bind(&select_end);
2938 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002939}
2940
Artem Serov551b28f2016-10-18 19:11:30 +01002941void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002942 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01002943}
2944
2945void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2946 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2947}
2948
Scott Wakelingfe885462016-09-22 10:24:38 +01002949void CodeGeneratorARMVIXL::GenerateNop() {
2950 __ Nop();
2951}
2952
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002953// `temp` is an extra temporary register that is used for some conditions;
2954// callers may not specify it, in which case the method will use a scratch
2955// register instead.
2956void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
2957 vixl32::Register out,
2958 vixl32::Register in,
2959 vixl32::Register temp) {
2960 switch (condition) {
2961 case kCondEQ:
2962 // x <= 0 iff x == 0 when the comparison is unsigned.
2963 case kCondBE:
2964 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
2965 temp = out;
2966 }
2967
2968 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
2969 // different as well.
2970 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
2971 // temp = - in; only 0 sets the carry flag.
2972 __ Rsbs(temp, in, 0);
2973
2974 if (out.Is(in)) {
2975 std::swap(in, temp);
2976 }
2977
2978 // out = - in + in + carry = carry
2979 __ Adc(out, temp, in);
2980 } else {
2981 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
2982 __ Clz(out, in);
2983 // Any number less than 32 logically shifted right by 5 bits results in 0;
2984 // the same operation on 32 yields 1.
2985 __ Lsr(out, out, 5);
2986 }
2987
2988 break;
2989 case kCondNE:
2990 // x > 0 iff x != 0 when the comparison is unsigned.
2991 case kCondA: {
2992 UseScratchRegisterScope temps(GetVIXLAssembler());
2993
2994 if (out.Is(in)) {
2995 if (!temp.IsValid() || in.Is(temp)) {
2996 temp = temps.Acquire();
2997 }
2998 } else if (!temp.IsValid() || !temp.IsLow()) {
2999 temp = out;
3000 }
3001
3002 // temp = in - 1; only 0 does not set the carry flag.
3003 __ Subs(temp, in, 1);
3004 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3005 __ Sbc(out, in, temp);
3006 break;
3007 }
3008 case kCondGE:
3009 __ Mvn(out, in);
3010 in = out;
3011 FALLTHROUGH_INTENDED;
3012 case kCondLT:
3013 // We only care about the sign bit.
3014 __ Lsr(out, in, 31);
3015 break;
3016 case kCondAE:
3017 // Trivially true.
3018 __ Mov(out, 1);
3019 break;
3020 case kCondB:
3021 // Trivially false.
3022 __ Mov(out, 0);
3023 break;
3024 default:
3025 LOG(FATAL) << "Unexpected condition " << condition;
3026 UNREACHABLE();
3027 }
3028}
3029
Scott Wakelingfe885462016-09-22 10:24:38 +01003030void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
3031 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003032 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01003033 const DataType::Type type = cond->InputAt(0)->GetType();
3034 if (DataType::IsFloatingPointType(type)) {
3035 locations->SetInAt(0, Location::RequiresFpuRegister());
3036 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
3037 } else {
3038 locations->SetInAt(0, Location::RequiresRegister());
3039 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
3040 }
3041 if (!cond->IsEmittedAtUseSite()) {
3042 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003043 }
3044}
3045
3046void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3047 if (cond->IsEmittedAtUseSite()) {
3048 return;
3049 }
3050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003054 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003055 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003056 }
3057
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003058 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003059
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003060 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003061
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003062 // A condition with only one boolean input, or two boolean inputs without being equality or
3063 // inequality results from transformations done by the instruction simplifier, and is handled
3064 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003065 if (type == DataType::Type::kBool &&
3066 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003067 (condition == kCondEQ || condition == kCondNE)) {
3068 vixl32::Register left = InputRegisterAt(cond, 0);
3069 const vixl32::Register out = OutputRegister(cond);
3070 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003071
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003072 // The constant case is handled by the instruction simplifier.
3073 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003074
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003075 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003076
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003077 // Avoid 32-bit instructions if possible.
3078 if (out.Is(right)) {
3079 std::swap(left, right);
3080 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003081
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003082 __ Eor(out, left, right);
3083
3084 if (condition == kCondEQ) {
3085 __ Eor(out, out, 1);
3086 }
3087
3088 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003089 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003090
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003091 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003092}
3093
3094void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3095 HandleCondition(comp);
3096}
3097
3098void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3099 HandleCondition(comp);
3100}
3101
3102void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3103 HandleCondition(comp);
3104}
3105
3106void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3107 HandleCondition(comp);
3108}
3109
3110void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3111 HandleCondition(comp);
3112}
3113
3114void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3115 HandleCondition(comp);
3116}
3117
3118void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3119 HandleCondition(comp);
3120}
3121
3122void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3123 HandleCondition(comp);
3124}
3125
3126void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3127 HandleCondition(comp);
3128}
3129
3130void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3131 HandleCondition(comp);
3132}
3133
3134void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3135 HandleCondition(comp);
3136}
3137
3138void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3139 HandleCondition(comp);
3140}
3141
3142void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3143 HandleCondition(comp);
3144}
3145
3146void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3147 HandleCondition(comp);
3148}
3149
3150void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3151 HandleCondition(comp);
3152}
3153
3154void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3155 HandleCondition(comp);
3156}
3157
3158void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3159 HandleCondition(comp);
3160}
3161
3162void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3163 HandleCondition(comp);
3164}
3165
3166void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3167 HandleCondition(comp);
3168}
3169
3170void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3171 HandleCondition(comp);
3172}
3173
3174void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3175 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003176 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003177 locations->SetOut(Location::ConstantLocation(constant));
3178}
3179
3180void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3181 // Will be generated at use site.
3182}
3183
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003184void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003186 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003187 locations->SetOut(Location::ConstantLocation(constant));
3188}
3189
3190void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3191 // Will be generated at use site.
3192}
3193
Scott Wakelingfe885462016-09-22 10:24:38 +01003194void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3195 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003196 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003197 locations->SetOut(Location::ConstantLocation(constant));
3198}
3199
3200void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3201 // Will be generated at use site.
3202}
3203
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003204void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3205 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003206 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003207 locations->SetOut(Location::ConstantLocation(constant));
3208}
3209
Scott Wakelingc34dba72016-10-03 10:14:44 +01003210void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3211 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003212 // Will be generated at use site.
3213}
3214
3215void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3216 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003217 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003218 locations->SetOut(Location::ConstantLocation(constant));
3219}
3220
Scott Wakelingc34dba72016-10-03 10:14:44 +01003221void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3222 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003223 // Will be generated at use site.
3224}
3225
Igor Murashkind01745e2017-04-05 16:40:31 -07003226void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3227 constructor_fence->SetLocations(nullptr);
3228}
3229
3230void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3231 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3232 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3233}
3234
Scott Wakelingfe885462016-09-22 10:24:38 +01003235void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3236 memory_barrier->SetLocations(nullptr);
3237}
3238
3239void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3240 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3241}
3242
3243void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3244 ret->SetLocations(nullptr);
3245}
3246
3247void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3248 codegen_->GenerateFrameExit();
3249}
3250
3251void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3252 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003253 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003254 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3255}
3256
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00003257void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret) {
3258 if (GetGraph()->IsCompilingOsr()) {
3259 // To simplify callers of an OSR method, we put the return value in both
3260 // floating point and core registers.
3261 switch (ret->InputAt(0)->GetType()) {
3262 case DataType::Type::kFloat32:
3263 __ Vmov(r0, s0);
3264 break;
3265 case DataType::Type::kFloat64:
3266 __ Vmov(r0, r1, d0);
3267 break;
3268 default:
3269 break;
3270 }
3271 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003272 codegen_->GenerateFrameExit();
3273}
3274
Artem Serovcfbe9132016-10-14 15:58:56 +01003275void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3276 // The trampoline uses the same calling convention as dex calling conventions,
3277 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3278 // the method_idx.
3279 HandleInvoke(invoke);
3280}
3281
3282void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3283 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003284 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003285}
3286
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003287void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3288 // Explicit clinit checks triggered by static invokes must have been pruned by
3289 // art::PrepareForRegisterAllocation.
3290 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3291
Anton Kirilov5ec62182016-10-13 20:16:02 +01003292 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3293 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003294 return;
3295 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003296
3297 HandleInvoke(invoke);
3298}
3299
Anton Kirilov5ec62182016-10-13 20:16:02 +01003300static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3301 if (invoke->GetLocations()->Intrinsified()) {
3302 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3303 intrinsic.Dispatch(invoke);
3304 return true;
3305 }
3306 return false;
3307}
3308
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003309void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3310 // Explicit clinit checks triggered by static invokes must have been pruned by
3311 // art::PrepareForRegisterAllocation.
3312 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3313
Anton Kirilov5ec62182016-10-13 20:16:02 +01003314 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003315 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003316 return;
3317 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003318
3319 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003320 codegen_->GenerateStaticOrDirectCall(
3321 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003322
Andreas Gampe3db70682018-12-26 15:12:03 -08003323 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003324}
3325
3326void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003327 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003328 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3329}
3330
3331void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003332 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3333 if (intrinsic.TryDispatch(invoke)) {
3334 return;
3335 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003336
3337 HandleInvoke(invoke);
3338}
3339
3340void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003341 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003342 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003343 return;
3344 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003345
3346 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003347 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003348
Andreas Gampe3db70682018-12-26 15:12:03 -08003349 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003350}
3351
Artem Serovcfbe9132016-10-14 15:58:56 +01003352void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3353 HandleInvoke(invoke);
3354 // Add the hidden argument.
3355 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3356}
3357
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003358void CodeGeneratorARMVIXL::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
3359 vixl32::Register klass) {
3360 DCHECK_EQ(r0.GetCode(), klass.GetCode());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003361 // We know the destination of an intrinsic, so no need to record inline
3362 // caches.
3363 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00003364 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003365 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003366 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
3367 ScopedObjectAccess soa(Thread::Current());
3368 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00003369 if (info != nullptr) {
3370 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
3371 uint32_t address = reinterpret_cast32<uint32_t>(cache);
3372 vixl32::Label done;
3373 UseScratchRegisterScope temps(GetVIXLAssembler());
3374 temps.Exclude(ip);
3375 __ Mov(r4, address);
3376 __ Ldr(ip, MemOperand(r4, InlineCache::ClassesOffset().Int32Value()));
3377 // Fast path for a monomorphic cache.
3378 __ Cmp(klass, ip);
3379 __ B(eq, &done, /* is_far_target= */ false);
3380 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
3381 __ Bind(&done);
3382 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003383 }
3384}
3385
Artem Serovcfbe9132016-10-14 15:58:56 +01003386void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3387 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3388 LocationSummary* locations = invoke->GetLocations();
3389 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3390 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3391 Location receiver = locations->InAt(0);
3392 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3393
3394 DCHECK(!receiver.IsStackSlot());
3395
Alexandre Rames374ddf32016-11-04 10:40:49 +00003396 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3397 {
Artem Serov0fb37192016-12-06 18:13:40 +00003398 ExactAssemblyScope aas(GetVIXLAssembler(),
3399 vixl32::kMaxInstructionSizeInBytes,
3400 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003401 // /* HeapReference<Class> */ temp = receiver->klass_
3402 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3403 codegen_->MaybeRecordImplicitNullCheck(invoke);
3404 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003405 // Instead of simply (possibly) unpoisoning `temp` here, we should
3406 // emit a read barrier for the previous class reference load.
3407 // However this is not required in practice, as this is an
3408 // intermediate/temporary reference and because the current
3409 // concurrent copying collector keeps the from-space memory
3410 // intact/accessible until the end of the marking phase (the
3411 // concurrent copying collector may not in the future).
3412 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003413
3414 // If we're compiling baseline, update the inline cache.
3415 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
3416
Artem Serovcfbe9132016-10-14 15:58:56 +01003417 GetAssembler()->LoadFromOffset(kLoadWord,
3418 temp,
3419 temp,
3420 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003421
Artem Serovcfbe9132016-10-14 15:58:56 +01003422 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3423 invoke->GetImtIndex(), kArmPointerSize));
3424 // temp = temp->GetImtEntryAt(method_offset);
3425 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3426 uint32_t entry_point =
3427 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3428 // LR = temp->GetEntryPoint();
3429 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3430
3431 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3432 // instruction from clobbering it as they might use r12 as a scratch register.
3433 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003434
3435 {
3436 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3437 // so it checks if the application is using them (by passing them to the macro assembler
3438 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3439 // what is available, and is the opposite of the standard usage: Instead of requesting a
3440 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3441 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3442 // (to materialize the constant), since the destination register becomes available for such use
3443 // internally for the duration of the macro instruction.
3444 UseScratchRegisterScope temps(GetVIXLAssembler());
3445 temps.Exclude(hidden_reg);
3446 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3447 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003448 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003449 // Ensure the pc position is recorded immediately after the `blx` instruction.
3450 // 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 +00003451 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003452 vixl32::k16BitT32InstructionSizeInBytes,
3453 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003454 // LR();
3455 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003456 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003457 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003458 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003459
Andreas Gampe3db70682018-12-26 15:12:03 -08003460 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003461}
3462
Orion Hodsonac141392017-01-13 11:53:47 +00003463void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3464 HandleInvoke(invoke);
3465}
3466
3467void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3468 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003469 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003470}
3471
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003472void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3473 HandleInvoke(invoke);
3474}
3475
3476void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3477 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003478 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 10);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003479}
3480
Artem Serov02109dd2016-09-23 17:17:54 +01003481void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3482 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003483 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003484 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003485 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003486 locations->SetInAt(0, Location::RequiresRegister());
3487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3488 break;
3489 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003491 locations->SetInAt(0, Location::RequiresRegister());
3492 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3493 break;
3494 }
3495
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003496 case DataType::Type::kFloat32:
3497 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003498 locations->SetInAt(0, Location::RequiresFpuRegister());
3499 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3500 break;
3501
3502 default:
3503 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3504 }
3505}
3506
3507void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3508 LocationSummary* locations = neg->GetLocations();
3509 Location out = locations->Out();
3510 Location in = locations->InAt(0);
3511 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003513 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3514 break;
3515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003517 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3518 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3519 // We cannot emit an RSC (Reverse Subtract with Carry)
3520 // instruction here, as it does not exist in the Thumb-2
3521 // instruction set. We use the following approach
3522 // using SBC and SUB instead.
3523 //
3524 // out.hi = -C
3525 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3526 // out.hi = out.hi - in.hi
3527 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3528 break;
3529
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003530 case DataType::Type::kFloat32:
3531 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003532 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003533 break;
3534
3535 default:
3536 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3537 }
3538}
3539
Scott Wakelingfe885462016-09-22 10:24:38 +01003540void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003541 DataType::Type result_type = conversion->GetResultType();
3542 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003543 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3544 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003545
3546 // The float-to-long, double-to-long and long-to-float type conversions
3547 // rely on a call to the runtime.
3548 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003549 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3550 && result_type == DataType::Type::kInt64)
3551 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003552 ? LocationSummary::kCallOnMainOnly
3553 : LocationSummary::kNoCall;
3554 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003555 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003556
Scott Wakelingfe885462016-09-22 10:24:38 +01003557 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003558 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003559 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003560 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003561 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003562 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3563 locations->SetInAt(0, Location::RequiresRegister());
3564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003565 break;
3566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003568 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003570 locations->SetInAt(0, Location::Any());
3571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3572 break;
3573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003574 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003575 locations->SetInAt(0, Location::RequiresFpuRegister());
3576 locations->SetOut(Location::RequiresRegister());
3577 locations->AddTemp(Location::RequiresFpuRegister());
3578 break;
3579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003580 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003581 locations->SetInAt(0, Location::RequiresFpuRegister());
3582 locations->SetOut(Location::RequiresRegister());
3583 locations->AddTemp(Location::RequiresFpuRegister());
3584 break;
3585
3586 default:
3587 LOG(FATAL) << "Unexpected type conversion from " << input_type
3588 << " to " << result_type;
3589 }
3590 break;
3591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003592 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003593 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003595 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003596 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003597 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003598 case DataType::Type::kInt16:
3599 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003600 locations->SetInAt(0, Location::RequiresRegister());
3601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3602 break;
3603
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003604 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003605 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3606 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3607 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003608 break;
3609 }
3610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003611 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003612 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3613 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3614 calling_convention.GetFpuRegisterAt(1)));
3615 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003616 break;
3617 }
3618
3619 default:
3620 LOG(FATAL) << "Unexpected type conversion from " << input_type
3621 << " to " << result_type;
3622 }
3623 break;
3624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003626 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003627 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003628 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003629 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003630 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 case DataType::Type::kInt16:
3632 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003633 locations->SetInAt(0, Location::RequiresRegister());
3634 locations->SetOut(Location::RequiresFpuRegister());
3635 break;
3636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003637 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003638 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3639 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3640 calling_convention.GetRegisterAt(1)));
3641 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003642 break;
3643 }
3644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003645 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003646 locations->SetInAt(0, Location::RequiresFpuRegister());
3647 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3648 break;
3649
3650 default:
3651 LOG(FATAL) << "Unexpected type conversion from " << input_type
3652 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003653 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003654 break;
3655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003657 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003659 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003661 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 case DataType::Type::kInt16:
3663 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003664 locations->SetInAt(0, Location::RequiresRegister());
3665 locations->SetOut(Location::RequiresFpuRegister());
3666 break;
3667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003668 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003669 locations->SetInAt(0, Location::RequiresRegister());
3670 locations->SetOut(Location::RequiresFpuRegister());
3671 locations->AddTemp(Location::RequiresFpuRegister());
3672 locations->AddTemp(Location::RequiresFpuRegister());
3673 break;
3674
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003675 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003676 locations->SetInAt(0, Location::RequiresFpuRegister());
3677 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3678 break;
3679
3680 default:
3681 LOG(FATAL) << "Unexpected type conversion from " << input_type
3682 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003683 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003684 break;
3685
3686 default:
3687 LOG(FATAL) << "Unexpected type conversion from " << input_type
3688 << " to " << result_type;
3689 }
3690}
3691
3692void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3693 LocationSummary* locations = conversion->GetLocations();
3694 Location out = locations->Out();
3695 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 DataType::Type result_type = conversion->GetResultType();
3697 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003698 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3699 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003700 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003701 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003702 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003703 case DataType::Type::kInt8:
3704 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003705 case DataType::Type::kInt16:
3706 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003707 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3708 break;
3709 case DataType::Type::kInt64:
3710 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3711 break;
3712
3713 default:
3714 LOG(FATAL) << "Unexpected type conversion from " << input_type
3715 << " to " << result_type;
3716 }
3717 break;
3718
3719 case DataType::Type::kInt8:
3720 switch (input_type) {
3721 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003722 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003723 case DataType::Type::kInt16:
3724 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003725 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3726 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003727 case DataType::Type::kInt64:
3728 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3729 break;
3730
3731 default:
3732 LOG(FATAL) << "Unexpected type conversion from " << input_type
3733 << " to " << result_type;
3734 }
3735 break;
3736
3737 case DataType::Type::kUint16:
3738 switch (input_type) {
3739 case DataType::Type::kInt8:
3740 case DataType::Type::kInt16:
3741 case DataType::Type::kInt32:
3742 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3743 break;
3744 case DataType::Type::kInt64:
3745 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3746 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003747
3748 default:
3749 LOG(FATAL) << "Unexpected type conversion from " << input_type
3750 << " to " << result_type;
3751 }
3752 break;
3753
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003755 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003756 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003757 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3759 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003760 case DataType::Type::kInt64:
3761 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3762 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003763
3764 default:
3765 LOG(FATAL) << "Unexpected type conversion from " << input_type
3766 << " to " << result_type;
3767 }
3768 break;
3769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003771 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003772 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003773 DCHECK(out.IsRegister());
3774 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003775 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003776 } else if (in.IsDoubleStackSlot()) {
3777 GetAssembler()->LoadFromOffset(kLoadWord,
3778 OutputRegister(conversion),
3779 sp,
3780 in.GetStackIndex());
3781 } else {
3782 DCHECK(in.IsConstant());
3783 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003784 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3785 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003786 }
3787 break;
3788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003789 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003790 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003791 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003792 __ Vmov(OutputRegister(conversion), temp);
3793 break;
3794 }
3795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003797 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003798 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003799 __ Vmov(OutputRegister(conversion), temp_s);
3800 break;
3801 }
3802
3803 default:
3804 LOG(FATAL) << "Unexpected type conversion from " << input_type
3805 << " to " << result_type;
3806 }
3807 break;
3808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003810 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003812 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003813 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003814 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 case DataType::Type::kInt16:
3816 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003817 DCHECK(out.IsRegisterPair());
3818 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003819 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003820 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003821 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003822 break;
3823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003824 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003825 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3826 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3827 break;
3828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003830 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3831 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3832 break;
3833
3834 default:
3835 LOG(FATAL) << "Unexpected type conversion from " << input_type
3836 << " to " << result_type;
3837 }
3838 break;
3839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003840 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003841 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003842 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003843 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003844 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003845 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003846 case DataType::Type::kInt16:
3847 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003848 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003849 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003850 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003852 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003853 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3854 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3855 break;
3856
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003857 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003858 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003859 break;
3860
3861 default:
3862 LOG(FATAL) << "Unexpected type conversion from " << input_type
3863 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003864 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003865 break;
3866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003867 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003868 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003870 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003871 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003872 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003873 case DataType::Type::kInt16:
3874 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003875 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003876 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003877 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003879 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003880 vixl32::Register low = LowRegisterFrom(in);
3881 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003882 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003883 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003884 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003885 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003886 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003887
3888 // temp_d = int-to-double(high)
3889 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003890 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003891 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003892 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003893 // out_d = unsigned-to-double(low)
3894 __ Vmov(out_s, low);
3895 __ Vcvt(F64, U32, out_d, out_s);
3896 // out_d += temp_d * constant_d
3897 __ Vmla(F64, out_d, temp_d, constant_d);
3898 break;
3899 }
3900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003901 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003902 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003903 break;
3904
3905 default:
3906 LOG(FATAL) << "Unexpected type conversion from " << input_type
3907 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003908 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003909 break;
3910
3911 default:
3912 LOG(FATAL) << "Unexpected type conversion from " << input_type
3913 << " to " << result_type;
3914 }
3915}
3916
3917void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3918 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003919 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003920 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003921 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003922 locations->SetInAt(0, Location::RequiresRegister());
3923 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3925 break;
3926 }
3927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003929 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003930 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3932 break;
3933 }
3934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003935 case DataType::Type::kFloat32:
3936 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003937 locations->SetInAt(0, Location::RequiresFpuRegister());
3938 locations->SetInAt(1, Location::RequiresFpuRegister());
3939 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3940 break;
3941 }
3942
3943 default:
3944 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3945 }
3946}
3947
3948void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3949 LocationSummary* locations = add->GetLocations();
3950 Location out = locations->Out();
3951 Location first = locations->InAt(0);
3952 Location second = locations->InAt(1);
3953
3954 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003955 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003956 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3957 }
3958 break;
3959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003961 if (second.IsConstant()) {
3962 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3963 GenerateAddLongConst(out, first, value);
3964 } else {
3965 DCHECK(second.IsRegisterPair());
3966 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3967 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3968 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003969 break;
3970 }
3971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003972 case DataType::Type::kFloat32:
3973 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003974 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003975 break;
3976
3977 default:
3978 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3979 }
3980}
3981
3982void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3983 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003984 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003985 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003986 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003987 locations->SetInAt(0, Location::RequiresRegister());
3988 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3990 break;
3991 }
3992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003993 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003994 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003995 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3997 break;
3998 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003999 case DataType::Type::kFloat32:
4000 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004001 locations->SetInAt(0, Location::RequiresFpuRegister());
4002 locations->SetInAt(1, Location::RequiresFpuRegister());
4003 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4004 break;
4005 }
4006 default:
4007 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4008 }
4009}
4010
4011void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
4012 LocationSummary* locations = sub->GetLocations();
4013 Location out = locations->Out();
4014 Location first = locations->InAt(0);
4015 Location second = locations->InAt(1);
4016 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004018 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004019 break;
4020 }
4021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004022 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00004023 if (second.IsConstant()) {
4024 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4025 GenerateAddLongConst(out, first, -value);
4026 } else {
4027 DCHECK(second.IsRegisterPair());
4028 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
4029 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
4030 }
Scott Wakelingfe885462016-09-22 10:24:38 +01004031 break;
4032 }
4033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004034 case DataType::Type::kFloat32:
4035 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004036 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004037 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004038
4039 default:
4040 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
4041 }
4042}
4043
4044void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
4045 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004046 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01004047 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004048 case DataType::Type::kInt32:
4049 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004050 locations->SetInAt(0, Location::RequiresRegister());
4051 locations->SetInAt(1, Location::RequiresRegister());
4052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4053 break;
4054 }
4055
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004056 case DataType::Type::kFloat32:
4057 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004058 locations->SetInAt(0, Location::RequiresFpuRegister());
4059 locations->SetInAt(1, Location::RequiresFpuRegister());
4060 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4061 break;
4062 }
4063
4064 default:
4065 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4066 }
4067}
4068
4069void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4070 LocationSummary* locations = mul->GetLocations();
4071 Location out = locations->Out();
4072 Location first = locations->InAt(0);
4073 Location second = locations->InAt(1);
4074 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004076 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4077 break;
4078 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004079 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004080 vixl32::Register out_hi = HighRegisterFrom(out);
4081 vixl32::Register out_lo = LowRegisterFrom(out);
4082 vixl32::Register in1_hi = HighRegisterFrom(first);
4083 vixl32::Register in1_lo = LowRegisterFrom(first);
4084 vixl32::Register in2_hi = HighRegisterFrom(second);
4085 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004086
4087 // Extra checks to protect caused by the existence of R1_R2.
4088 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4089 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004090 DCHECK(!out_hi.Is(in1_lo));
4091 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004092
4093 // input: in1 - 64 bits, in2 - 64 bits
4094 // output: out
4095 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4096 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4097 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4098
4099 UseScratchRegisterScope temps(GetVIXLAssembler());
4100 vixl32::Register temp = temps.Acquire();
4101 // temp <- in1.lo * in2.hi
4102 __ Mul(temp, in1_lo, in2_hi);
4103 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4104 __ Mla(out_hi, in1_hi, in2_lo, temp);
4105 // out.lo <- (in1.lo * in2.lo)[31:0];
4106 __ Umull(out_lo, temp, in1_lo, in2_lo);
4107 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004108 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004109 break;
4110 }
4111
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004112 case DataType::Type::kFloat32:
4113 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004114 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004115 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004116
4117 default:
4118 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4119 }
4120}
4121
Scott Wakelingfe885462016-09-22 10:24:38 +01004122void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4123 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004125
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004126 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004127 DCHECK(second.IsConstant());
4128
4129 vixl32::Register out = OutputRegister(instruction);
4130 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004131 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004132 DCHECK(imm == 1 || imm == -1);
4133
4134 if (instruction->IsRem()) {
4135 __ Mov(out, 0);
4136 } else {
4137 if (imm == 1) {
4138 __ Mov(out, dividend);
4139 } else {
4140 __ Rsb(out, dividend, 0);
4141 }
4142 }
4143}
4144
4145void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4146 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004147 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004148
4149 LocationSummary* locations = instruction->GetLocations();
4150 Location second = locations->InAt(1);
4151 DCHECK(second.IsConstant());
4152
4153 vixl32::Register out = OutputRegister(instruction);
4154 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004155 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004156 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004157 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4158 int ctz_imm = CTZ(abs_imm);
4159
4160 if (ctz_imm == 1) {
4161 __ Lsr(temp, dividend, 32 - ctz_imm);
4162 } else {
4163 __ Asr(temp, dividend, 31);
4164 __ Lsr(temp, temp, 32 - ctz_imm);
4165 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004166 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004167
4168 if (instruction->IsDiv()) {
4169 __ Asr(out, out, ctz_imm);
4170 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004171 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004172 }
4173 } else {
4174 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004175 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004176 }
4177}
4178
4179void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4180 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004181 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004182
4183 LocationSummary* locations = instruction->GetLocations();
4184 Location second = locations->InAt(1);
4185 DCHECK(second.IsConstant());
4186
4187 vixl32::Register out = OutputRegister(instruction);
4188 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004189 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4190 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004191 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004192
4193 int64_t magic;
4194 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004195 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Scott Wakelingfe885462016-09-22 10:24:38 +01004196
Anton Kirilovdda43962016-11-21 19:55:20 +00004197 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4198 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004199 __ Smull(temp2, temp1, dividend, temp1);
4200
4201 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004202 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004203 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004204 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004205 }
4206
4207 if (shift != 0) {
4208 __ Asr(temp1, temp1, shift);
4209 }
4210
4211 if (instruction->IsDiv()) {
4212 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4213 } else {
4214 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4215 // TODO: Strength reduction for mls.
4216 __ Mov(temp2, imm);
4217 __ Mls(out, temp1, temp2, dividend);
4218 }
4219}
4220
4221void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4222 HBinaryOperation* instruction) {
4223 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004224 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004225
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004226 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004227 DCHECK(second.IsConstant());
4228
Anton Kirilov644032c2016-12-06 17:51:43 +00004229 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004230 if (imm == 0) {
4231 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4232 } else if (imm == 1 || imm == -1) {
4233 DivRemOneOrMinusOne(instruction);
4234 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4235 DivRemByPowerOfTwo(instruction);
4236 } else {
4237 DCHECK(imm <= -2 || imm >= 2);
4238 GenerateDivRemWithAnyConstant(instruction);
4239 }
4240}
4241
4242void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4243 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004244 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004245 // pLdiv runtime call.
4246 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004247 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004248 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004249 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004250 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4251 // pIdivmod runtime call.
4252 call_kind = LocationSummary::kCallOnMainOnly;
4253 }
4254
Vladimir Markoca6fff82017-10-03 14:49:14 +01004255 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004256
4257 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004258 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004259 if (div->InputAt(1)->IsConstant()) {
4260 locations->SetInAt(0, Location::RequiresRegister());
4261 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004263 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004264 if (value == 1 || value == 0 || value == -1) {
4265 // No temp register required.
4266 } else {
4267 locations->AddTemp(Location::RequiresRegister());
4268 if (!IsPowerOfTwo(AbsOrMin(value))) {
4269 locations->AddTemp(Location::RequiresRegister());
4270 }
4271 }
4272 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4273 locations->SetInAt(0, Location::RequiresRegister());
4274 locations->SetInAt(1, Location::RequiresRegister());
4275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4276 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004277 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4278 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4279 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004280 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004281 // we only need the former.
4282 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004283 }
4284 break;
4285 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004286 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004287 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4288 locations->SetInAt(0, LocationFrom(
4289 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4290 locations->SetInAt(1, LocationFrom(
4291 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4292 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004293 break;
4294 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 case DataType::Type::kFloat32:
4296 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004297 locations->SetInAt(0, Location::RequiresFpuRegister());
4298 locations->SetInAt(1, Location::RequiresFpuRegister());
4299 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4300 break;
4301 }
4302
4303 default:
4304 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4305 }
4306}
4307
4308void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004309 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004310 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004311
4312 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004313 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004314 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004315 GenerateDivRemConstantIntegral(div);
4316 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4317 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4318 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004319 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4320 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4321 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4322 DCHECK(r0.Is(OutputRegister(div)));
4323
4324 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4325 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004326 }
4327 break;
4328 }
4329
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004331 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4332 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4333 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4334 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4335 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4336 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4337 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4338
4339 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4340 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004341 break;
4342 }
4343
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004344 case DataType::Type::kFloat32:
4345 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004346 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004347 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004348
4349 default:
4350 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4351 }
4352}
4353
Artem Serov551b28f2016-10-18 19:11:30 +01004354void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004356
4357 // Most remainders are implemented in the runtime.
4358 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004360 // sdiv will be replaced by other instruction sequence.
4361 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004363 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4364 // Have hardware divide instruction for int, do it with three instructions.
4365 call_kind = LocationSummary::kNoCall;
4366 }
4367
Vladimir Markoca6fff82017-10-03 14:49:14 +01004368 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004369
4370 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004371 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004372 if (rem->InputAt(1)->IsConstant()) {
4373 locations->SetInAt(0, Location::RequiresRegister());
4374 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004376 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004377 if (value == 1 || value == 0 || value == -1) {
4378 // No temp register required.
4379 } else {
4380 locations->AddTemp(Location::RequiresRegister());
4381 if (!IsPowerOfTwo(AbsOrMin(value))) {
4382 locations->AddTemp(Location::RequiresRegister());
4383 }
4384 }
4385 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4386 locations->SetInAt(0, Location::RequiresRegister());
4387 locations->SetInAt(1, Location::RequiresRegister());
4388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4389 locations->AddTemp(Location::RequiresRegister());
4390 } else {
4391 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4392 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4393 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004394 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004395 // we only need the latter.
4396 locations->SetOut(LocationFrom(r1));
4397 }
4398 break;
4399 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004400 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004401 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4402 locations->SetInAt(0, LocationFrom(
4403 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4404 locations->SetInAt(1, LocationFrom(
4405 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4406 // The runtime helper puts the output in R2,R3.
4407 locations->SetOut(LocationFrom(r2, r3));
4408 break;
4409 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004410 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004411 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4412 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4413 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4414 locations->SetOut(LocationFrom(s0));
4415 break;
4416 }
4417
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004418 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004419 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4420 locations->SetInAt(0, LocationFrom(
4421 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4422 locations->SetInAt(1, LocationFrom(
4423 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4424 locations->SetOut(LocationFrom(s0, s1));
4425 break;
4426 }
4427
4428 default:
4429 LOG(FATAL) << "Unexpected rem type " << type;
4430 }
4431}
4432
4433void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4434 LocationSummary* locations = rem->GetLocations();
4435 Location second = locations->InAt(1);
4436
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004437 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004438 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004439 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004440 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4441 vixl32::Register out_reg = OutputRegister(rem);
4442 if (second.IsConstant()) {
4443 GenerateDivRemConstantIntegral(rem);
4444 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4445 vixl32::Register reg2 = RegisterFrom(second);
4446 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4447
4448 // temp = reg1 / reg2 (integer division)
4449 // dest = reg1 - temp * reg2
4450 __ Sdiv(temp, reg1, reg2);
4451 __ Mls(out_reg, temp, reg2, reg1);
4452 } else {
4453 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4454 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4455 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4456 DCHECK(out_reg.Is(r1));
4457
4458 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4459 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4460 }
4461 break;
4462 }
4463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004464 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004465 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4466 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4467 break;
4468 }
4469
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004470 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004471 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4472 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4473 break;
4474 }
4475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004476 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004477 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4478 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4479 break;
4480 }
4481
4482 default:
4483 LOG(FATAL) << "Unexpected rem type " << type;
4484 }
4485}
4486
Aart Bik1f8d51b2018-02-15 10:42:37 -08004487static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4488 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4489 switch (minmax->GetResultType()) {
4490 case DataType::Type::kInt32:
4491 locations->SetInAt(0, Location::RequiresRegister());
4492 locations->SetInAt(1, Location::RequiresRegister());
4493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4494 break;
4495 case DataType::Type::kInt64:
4496 locations->SetInAt(0, Location::RequiresRegister());
4497 locations->SetInAt(1, Location::RequiresRegister());
4498 locations->SetOut(Location::SameAsFirstInput());
4499 break;
4500 case DataType::Type::kFloat32:
4501 locations->SetInAt(0, Location::RequiresFpuRegister());
4502 locations->SetInAt(1, Location::RequiresFpuRegister());
4503 locations->SetOut(Location::SameAsFirstInput());
4504 locations->AddTemp(Location::RequiresRegister());
4505 break;
4506 case DataType::Type::kFloat64:
4507 locations->SetInAt(0, Location::RequiresFpuRegister());
4508 locations->SetInAt(1, Location::RequiresFpuRegister());
4509 locations->SetOut(Location::SameAsFirstInput());
4510 break;
4511 default:
4512 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4513 }
4514}
4515
Aart Bik351df3e2018-03-07 11:54:57 -08004516void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004517 Location op1_loc = locations->InAt(0);
4518 Location op2_loc = locations->InAt(1);
4519 Location out_loc = locations->Out();
4520
4521 vixl32::Register op1 = RegisterFrom(op1_loc);
4522 vixl32::Register op2 = RegisterFrom(op2_loc);
4523 vixl32::Register out = RegisterFrom(out_loc);
4524
4525 __ Cmp(op1, op2);
4526
4527 {
4528 ExactAssemblyScope aas(GetVIXLAssembler(),
4529 3 * kMaxInstructionSizeInBytes,
4530 CodeBufferCheckScope::kMaximumSize);
4531
4532 __ ite(is_min ? lt : gt);
4533 __ mov(is_min ? lt : gt, out, op1);
4534 __ mov(is_min ? ge : le, out, op2);
4535 }
4536}
4537
4538void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4539 Location op1_loc = locations->InAt(0);
4540 Location op2_loc = locations->InAt(1);
4541 Location out_loc = locations->Out();
4542
4543 // Optimization: don't generate any code if inputs are the same.
4544 if (op1_loc.Equals(op2_loc)) {
4545 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4546 return;
4547 }
4548
4549 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4550 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4551 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4552 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4553 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4554 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4555 UseScratchRegisterScope temps(GetVIXLAssembler());
4556 const vixl32::Register temp = temps.Acquire();
4557
4558 DCHECK(op1_lo.Is(out_lo));
4559 DCHECK(op1_hi.Is(out_hi));
4560
4561 // Compare op1 >= op2, or op1 < op2.
4562 __ Cmp(out_lo, op2_lo);
4563 __ Sbcs(temp, out_hi, op2_hi);
4564
4565 // Now GE/LT condition code is correct for the long comparison.
4566 {
4567 vixl32::ConditionType cond = is_min ? ge : lt;
4568 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4569 3 * kMaxInstructionSizeInBytes,
4570 CodeBufferCheckScope::kMaximumSize);
4571 __ itt(cond);
4572 __ mov(cond, out_lo, op2_lo);
4573 __ mov(cond, out_hi, op2_hi);
4574 }
4575}
4576
Aart Bik351df3e2018-03-07 11:54:57 -08004577void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4578 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004579 Location op1_loc = locations->InAt(0);
4580 Location op2_loc = locations->InAt(1);
4581 Location out_loc = locations->Out();
4582
4583 // Optimization: don't generate any code if inputs are the same.
4584 if (op1_loc.Equals(op2_loc)) {
4585 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4586 return;
4587 }
4588
4589 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4590 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4591 vixl32::SRegister out = SRegisterFrom(out_loc);
4592
4593 UseScratchRegisterScope temps(GetVIXLAssembler());
4594 const vixl32::Register temp1 = temps.Acquire();
4595 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4596 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004597 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004598
4599 DCHECK(op1.Is(out));
4600
4601 __ Vcmp(op1, op2);
4602 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004603 __ B(vs, &nan, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004604
4605 // op1 <> op2
4606 vixl32::ConditionType cond = is_min ? gt : lt;
4607 {
4608 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4609 2 * kMaxInstructionSizeInBytes,
4610 CodeBufferCheckScope::kMaximumSize);
4611 __ it(cond);
4612 __ vmov(cond, F32, out, op2);
4613 }
4614 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004615 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004616
4617 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4618 __ Vmov(temp1, op1);
4619 __ Vmov(temp2, op2);
4620 if (is_min) {
4621 __ Orr(temp1, temp1, temp2);
4622 } else {
4623 __ And(temp1, temp1, temp2);
4624 }
4625 __ Vmov(out, temp1);
4626 __ B(final_label);
4627
4628 // handle NaN input.
4629 __ Bind(&nan);
4630 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4631 __ Vmov(out, temp1);
4632
4633 if (done.IsReferenced()) {
4634 __ Bind(&done);
4635 }
4636}
4637
Aart Bik351df3e2018-03-07 11:54:57 -08004638void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4639 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004640 Location op1_loc = locations->InAt(0);
4641 Location op2_loc = locations->InAt(1);
4642 Location out_loc = locations->Out();
4643
4644 // Optimization: don't generate any code if inputs are the same.
4645 if (op1_loc.Equals(op2_loc)) {
4646 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4647 return;
4648 }
4649
4650 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4651 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4652 vixl32::DRegister out = DRegisterFrom(out_loc);
4653 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004654 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004655
4656 DCHECK(op1.Is(out));
4657
4658 __ Vcmp(op1, op2);
4659 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004660 __ B(vs, &handle_nan_eq, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004661
4662 // op1 <> op2
4663 vixl32::ConditionType cond = is_min ? gt : lt;
4664 {
4665 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4666 2 * kMaxInstructionSizeInBytes,
4667 CodeBufferCheckScope::kMaximumSize);
4668 __ it(cond);
4669 __ vmov(cond, F64, out, op2);
4670 }
4671 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004672 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004673
4674 // handle op1 == op2, max(+0.0,-0.0).
4675 if (!is_min) {
4676 __ Vand(F64, out, op1, op2);
4677 __ B(final_label);
4678 }
4679
4680 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4681 __ Bind(&handle_nan_eq);
4682 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4683
4684 if (done.IsReferenced()) {
4685 __ Bind(&done);
4686 }
4687}
4688
Aart Bik351df3e2018-03-07 11:54:57 -08004689void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4690 DataType::Type type = minmax->GetResultType();
4691 switch (type) {
4692 case DataType::Type::kInt32:
4693 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4694 break;
4695 case DataType::Type::kInt64:
4696 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4697 break;
4698 case DataType::Type::kFloat32:
4699 GenerateMinMaxFloat(minmax, is_min);
4700 break;
4701 case DataType::Type::kFloat64:
4702 GenerateMinMaxDouble(minmax, is_min);
4703 break;
4704 default:
4705 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4706 }
4707}
4708
Aart Bik1f8d51b2018-02-15 10:42:37 -08004709void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4710 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4711}
4712
4713void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004714 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004715}
4716
4717void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4718 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4719}
4720
4721void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004722 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004723}
4724
Aart Bik3dad3412018-02-28 12:01:46 -08004725void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4726 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4727 switch (abs->GetResultType()) {
4728 case DataType::Type::kInt32:
4729 case DataType::Type::kInt64:
4730 locations->SetInAt(0, Location::RequiresRegister());
4731 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4732 locations->AddTemp(Location::RequiresRegister());
4733 break;
4734 case DataType::Type::kFloat32:
4735 case DataType::Type::kFloat64:
4736 locations->SetInAt(0, Location::RequiresFpuRegister());
4737 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4738 break;
4739 default:
4740 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4741 }
4742}
4743
4744void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4745 LocationSummary* locations = abs->GetLocations();
4746 switch (abs->GetResultType()) {
4747 case DataType::Type::kInt32: {
4748 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4749 vixl32::Register out_reg = RegisterFrom(locations->Out());
4750 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4751 __ Asr(mask, in_reg, 31);
4752 __ Add(out_reg, in_reg, mask);
4753 __ Eor(out_reg, out_reg, mask);
4754 break;
4755 }
4756 case DataType::Type::kInt64: {
4757 Location in = locations->InAt(0);
4758 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4759 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4760 Location output = locations->Out();
4761 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4762 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4763 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4764 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4765 __ Asr(mask, in_reg_hi, 31);
4766 __ Adds(out_reg_lo, in_reg_lo, mask);
4767 __ Adc(out_reg_hi, in_reg_hi, mask);
4768 __ Eor(out_reg_lo, out_reg_lo, mask);
4769 __ Eor(out_reg_hi, out_reg_hi, mask);
4770 break;
4771 }
4772 case DataType::Type::kFloat32:
4773 case DataType::Type::kFloat64:
4774 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4775 break;
4776 default:
4777 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4778 }
4779}
Artem Serov551b28f2016-10-18 19:11:30 +01004780
Scott Wakelingfe885462016-09-22 10:24:38 +01004781void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004782 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004783 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004784}
4785
4786void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4787 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004788 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004789 codegen_->AddSlowPath(slow_path);
4790
4791 LocationSummary* locations = instruction->GetLocations();
4792 Location value = locations->InAt(0);
4793
4794 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004796 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004797 case DataType::Type::kInt8:
4798 case DataType::Type::kUint16:
4799 case DataType::Type::kInt16:
4800 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004801 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004802 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004803 } else {
4804 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004805 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004806 __ B(slow_path->GetEntryLabel());
4807 }
4808 }
4809 break;
4810 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004811 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004812 if (value.IsRegisterPair()) {
4813 UseScratchRegisterScope temps(GetVIXLAssembler());
4814 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004815 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004816 __ B(eq, slow_path->GetEntryLabel());
4817 } else {
4818 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004819 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004820 __ B(slow_path->GetEntryLabel());
4821 }
4822 }
4823 break;
4824 }
4825 default:
4826 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4827 }
4828}
4829
Artem Serov02109dd2016-09-23 17:17:54 +01004830void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4831 LocationSummary* locations = ror->GetLocations();
4832 vixl32::Register in = InputRegisterAt(ror, 0);
4833 Location rhs = locations->InAt(1);
4834 vixl32::Register out = OutputRegister(ror);
4835
4836 if (rhs.IsConstant()) {
4837 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4838 // so map all rotations to a +ve. equivalent in that range.
4839 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4840 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4841 if (rot) {
4842 // Rotate, mapping left rotations to right equivalents if necessary.
4843 // (e.g. left by 2 bits == right by 30.)
4844 __ Ror(out, in, rot);
4845 } else if (!out.Is(in)) {
4846 __ Mov(out, in);
4847 }
4848 } else {
4849 __ Ror(out, in, RegisterFrom(rhs));
4850 }
4851}
4852
4853// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4854// rotates by swapping input regs (effectively rotating by the first 32-bits of
4855// a larger rotation) or flipping direction (thus treating larger right/left
4856// rotations as sub-word sized rotations in the other direction) as appropriate.
4857void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4858 LocationSummary* locations = ror->GetLocations();
4859 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4860 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4861 Location rhs = locations->InAt(1);
4862 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4863 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4864
4865 if (rhs.IsConstant()) {
4866 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4867 // Map all rotations to +ve. equivalents on the interval [0,63].
4868 rot &= kMaxLongShiftDistance;
4869 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4870 // logic below to a simple pair of binary orr.
4871 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4872 if (rot >= kArmBitsPerWord) {
4873 rot -= kArmBitsPerWord;
4874 std::swap(in_reg_hi, in_reg_lo);
4875 }
4876 // Rotate, or mov to out for zero or word size rotations.
4877 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004878 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004879 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004880 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004881 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4882 } else {
4883 __ Mov(out_reg_lo, in_reg_lo);
4884 __ Mov(out_reg_hi, in_reg_hi);
4885 }
4886 } else {
4887 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4888 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4889 vixl32::Label end;
4890 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004891 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004892
4893 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4894 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004895 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Andreas Gampe3db70682018-12-26 15:12:03 -08004896 __ B(cc, &shift_by_32_plus_shift_right, /* is_far_target= */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004897
4898 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4899 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4900 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4901 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4902 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4903 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4904 __ Lsr(shift_left, in_reg_hi, shift_right);
4905 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004906 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004907
4908 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4909 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4910 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4911 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4912 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4913 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4914 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4915 __ Lsl(shift_right, in_reg_hi, shift_left);
4916 __ Add(out_reg_lo, out_reg_lo, shift_right);
4917
Anton Kirilov6f644202017-02-27 18:29:45 +00004918 if (end.IsReferenced()) {
4919 __ Bind(&end);
4920 }
Artem Serov02109dd2016-09-23 17:17:54 +01004921 }
4922}
4923
4924void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4925 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004926 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004927 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004928 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004929 locations->SetInAt(0, Location::RequiresRegister());
4930 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4932 break;
4933 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004934 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004935 locations->SetInAt(0, Location::RequiresRegister());
4936 if (ror->InputAt(1)->IsConstant()) {
4937 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4938 } else {
4939 locations->SetInAt(1, Location::RequiresRegister());
4940 locations->AddTemp(Location::RequiresRegister());
4941 locations->AddTemp(Location::RequiresRegister());
4942 }
4943 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4944 break;
4945 }
4946 default:
4947 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4948 }
4949}
4950
4951void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004952 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004953 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004954 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004955 HandleIntegerRotate(ror);
4956 break;
4957 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004958 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004959 HandleLongRotate(ror);
4960 break;
4961 }
4962 default:
4963 LOG(FATAL) << "Unexpected operation type " << type;
4964 UNREACHABLE();
4965 }
4966}
4967
Artem Serov02d37832016-10-25 15:25:33 +01004968void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4969 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4970
4971 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004972 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004973
4974 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004975 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004976 locations->SetInAt(0, Location::RequiresRegister());
4977 if (op->InputAt(1)->IsConstant()) {
4978 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4980 } else {
4981 locations->SetInAt(1, Location::RequiresRegister());
4982 // Make the output overlap, as it will be used to hold the masked
4983 // second input.
4984 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4985 }
4986 break;
4987 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004988 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004989 locations->SetInAt(0, Location::RequiresRegister());
4990 if (op->InputAt(1)->IsConstant()) {
4991 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4992 // For simplicity, use kOutputOverlap even though we only require that low registers
4993 // don't clash with high registers which the register allocator currently guarantees.
4994 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4995 } else {
4996 locations->SetInAt(1, Location::RequiresRegister());
4997 locations->AddTemp(Location::RequiresRegister());
4998 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4999 }
5000 break;
5001 }
5002 default:
5003 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5004 }
5005}
5006
5007void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
5008 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5009
5010 LocationSummary* locations = op->GetLocations();
5011 Location out = locations->Out();
5012 Location first = locations->InAt(0);
5013 Location second = locations->InAt(1);
5014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005015 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01005016 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005018 vixl32::Register out_reg = OutputRegister(op);
5019 vixl32::Register first_reg = InputRegisterAt(op, 0);
5020 if (second.IsRegister()) {
5021 vixl32::Register second_reg = RegisterFrom(second);
5022 // ARM doesn't mask the shift count so we need to do it ourselves.
5023 __ And(out_reg, second_reg, kMaxIntShiftDistance);
5024 if (op->IsShl()) {
5025 __ Lsl(out_reg, first_reg, out_reg);
5026 } else if (op->IsShr()) {
5027 __ Asr(out_reg, first_reg, out_reg);
5028 } else {
5029 __ Lsr(out_reg, first_reg, out_reg);
5030 }
5031 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00005032 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005033 uint32_t shift_value = cst & kMaxIntShiftDistance;
5034 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
5035 __ Mov(out_reg, first_reg);
5036 } else if (op->IsShl()) {
5037 __ Lsl(out_reg, first_reg, shift_value);
5038 } else if (op->IsShr()) {
5039 __ Asr(out_reg, first_reg, shift_value);
5040 } else {
5041 __ Lsr(out_reg, first_reg, shift_value);
5042 }
5043 }
5044 break;
5045 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005046 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005047 vixl32::Register o_h = HighRegisterFrom(out);
5048 vixl32::Register o_l = LowRegisterFrom(out);
5049
5050 vixl32::Register high = HighRegisterFrom(first);
5051 vixl32::Register low = LowRegisterFrom(first);
5052
5053 if (second.IsRegister()) {
5054 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5055
5056 vixl32::Register second_reg = RegisterFrom(second);
5057
5058 if (op->IsShl()) {
5059 __ And(o_l, second_reg, kMaxLongShiftDistance);
5060 // Shift the high part
5061 __ Lsl(o_h, high, o_l);
5062 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005063 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005064 __ Lsr(temp, low, temp);
5065 __ Orr(o_h, o_h, temp);
5066 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005067 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005068 {
Artem Serov0fb37192016-12-06 18:13:40 +00005069 ExactAssemblyScope guard(GetVIXLAssembler(),
5070 2 * vixl32::kMaxInstructionSizeInBytes,
5071 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005072 __ it(pl);
5073 __ lsl(pl, o_h, low, temp);
5074 }
5075 // Shift the low part
5076 __ Lsl(o_l, low, o_l);
5077 } else if (op->IsShr()) {
5078 __ And(o_h, second_reg, kMaxLongShiftDistance);
5079 // Shift the low part
5080 __ Lsr(o_l, low, o_h);
5081 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005082 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005083 __ Lsl(temp, high, temp);
5084 __ Orr(o_l, o_l, temp);
5085 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005086 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005087 {
Artem Serov0fb37192016-12-06 18:13:40 +00005088 ExactAssemblyScope guard(GetVIXLAssembler(),
5089 2 * vixl32::kMaxInstructionSizeInBytes,
5090 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005091 __ it(pl);
5092 __ asr(pl, o_l, high, temp);
5093 }
5094 // Shift the high part
5095 __ Asr(o_h, high, o_h);
5096 } else {
5097 __ And(o_h, second_reg, kMaxLongShiftDistance);
5098 // same as Shr except we use `Lsr`s and not `Asr`s
5099 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005100 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005101 __ Lsl(temp, high, temp);
5102 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005103 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005104 {
Artem Serov0fb37192016-12-06 18:13:40 +00005105 ExactAssemblyScope guard(GetVIXLAssembler(),
5106 2 * vixl32::kMaxInstructionSizeInBytes,
5107 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005108 __ it(pl);
5109 __ lsr(pl, o_l, high, temp);
5110 }
5111 __ Lsr(o_h, high, o_h);
5112 }
5113 } else {
5114 // Register allocator doesn't create partial overlap.
5115 DCHECK(!o_l.Is(high));
5116 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005117 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005118 uint32_t shift_value = cst & kMaxLongShiftDistance;
5119 if (shift_value > 32) {
5120 if (op->IsShl()) {
5121 __ Lsl(o_h, low, shift_value - 32);
5122 __ Mov(o_l, 0);
5123 } else if (op->IsShr()) {
5124 __ Asr(o_l, high, shift_value - 32);
5125 __ Asr(o_h, high, 31);
5126 } else {
5127 __ Lsr(o_l, high, shift_value - 32);
5128 __ Mov(o_h, 0);
5129 }
5130 } else if (shift_value == 32) {
5131 if (op->IsShl()) {
5132 __ Mov(o_h, low);
5133 __ Mov(o_l, 0);
5134 } else if (op->IsShr()) {
5135 __ Mov(o_l, high);
5136 __ Asr(o_h, high, 31);
5137 } else {
5138 __ Mov(o_l, high);
5139 __ Mov(o_h, 0);
5140 }
5141 } else if (shift_value == 1) {
5142 if (op->IsShl()) {
5143 __ Lsls(o_l, low, 1);
5144 __ Adc(o_h, high, high);
5145 } else if (op->IsShr()) {
5146 __ Asrs(o_h, high, 1);
5147 __ Rrx(o_l, low);
5148 } else {
5149 __ Lsrs(o_h, high, 1);
5150 __ Rrx(o_l, low);
5151 }
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005152 } else if (shift_value == 0) {
5153 __ Mov(o_l, low);
5154 __ Mov(o_h, high);
Artem Serov02d37832016-10-25 15:25:33 +01005155 } else {
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005156 DCHECK(0 < shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01005157 if (op->IsShl()) {
5158 __ Lsl(o_h, high, shift_value);
5159 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5160 __ Lsl(o_l, low, shift_value);
5161 } else if (op->IsShr()) {
5162 __ Lsr(o_l, low, shift_value);
5163 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5164 __ Asr(o_h, high, shift_value);
5165 } else {
5166 __ Lsr(o_l, low, shift_value);
5167 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5168 __ Lsr(o_h, high, shift_value);
5169 }
5170 }
5171 }
5172 break;
5173 }
5174 default:
5175 LOG(FATAL) << "Unexpected operation type " << type;
5176 UNREACHABLE();
5177 }
5178}
5179
5180void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5181 HandleShift(shl);
5182}
5183
5184void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5185 HandleShift(shl);
5186}
5187
5188void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5189 HandleShift(shr);
5190}
5191
5192void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5193 HandleShift(shr);
5194}
5195
5196void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5197 HandleShift(ushr);
5198}
5199
5200void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5201 HandleShift(ushr);
5202}
5203
5204void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005205 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5206 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005207 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5208 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005209 locations->SetOut(LocationFrom(r0));
5210}
5211
5212void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005213 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5214 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005215 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005216}
5217
5218void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005219 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5220 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005221 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005222 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005223 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5224 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005225}
5226
5227void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005228 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5229 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005230 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005231 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005232 DCHECK(!codegen_->IsLeafMethod());
Andreas Gampe3db70682018-12-26 15:12:03 -08005233 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005234}
5235
5236void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5237 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005238 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005239 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5240 if (location.IsStackSlot()) {
5241 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5242 } else if (location.IsDoubleStackSlot()) {
5243 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5244 }
5245 locations->SetOut(location);
5246}
5247
5248void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5249 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5250 // Nothing to do, the parameter is already at its location.
5251}
5252
5253void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5254 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005255 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005256 locations->SetOut(LocationFrom(kMethodRegister));
5257}
5258
5259void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5260 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5261 // Nothing to do, the method is already at its location.
5262}
5263
5264void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5265 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005266 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005267 locations->SetInAt(0, Location::RequiresRegister());
5268 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5269}
5270
5271void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5272 LocationSummary* locations = not_->GetLocations();
5273 Location out = locations->Out();
5274 Location in = locations->InAt(0);
5275 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005277 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5278 break;
5279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005280 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005281 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5282 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5283 break;
5284
5285 default:
5286 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5287 }
5288}
5289
Scott Wakelingc34dba72016-10-03 10:14:44 +01005290void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5291 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005292 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005293 locations->SetInAt(0, Location::RequiresRegister());
5294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5295}
5296
5297void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5298 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5299}
5300
Artem Serov02d37832016-10-25 15:25:33 +01005301void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5302 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005303 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005304 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005305 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005306 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005307 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005308 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005309 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005310 case DataType::Type::kInt32:
5311 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005312 locations->SetInAt(0, Location::RequiresRegister());
5313 locations->SetInAt(1, Location::RequiresRegister());
5314 // Output overlaps because it is written before doing the low comparison.
5315 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5316 break;
5317 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005318 case DataType::Type::kFloat32:
5319 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005320 locations->SetInAt(0, Location::RequiresFpuRegister());
5321 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5322 locations->SetOut(Location::RequiresRegister());
5323 break;
5324 }
5325 default:
5326 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5327 }
5328}
5329
5330void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5331 LocationSummary* locations = compare->GetLocations();
5332 vixl32::Register out = OutputRegister(compare);
5333 Location left = locations->InAt(0);
5334 Location right = locations->InAt(1);
5335
5336 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005337 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005338 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005339 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005340 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005341 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005342 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005343 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005344 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005345 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005346 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005347 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5348 __ Mov(out, 0);
5349 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5350 less_cond = lt;
5351 break;
5352 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005353 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005354 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Andreas Gampe3db70682018-12-26 15:12:03 -08005355 __ B(lt, &less, /* is_far_target= */ false);
5356 __ B(gt, &greater, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005357 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5358 __ Mov(out, 0);
5359 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5360 less_cond = lo;
5361 break;
5362 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005363 case DataType::Type::kFloat32:
5364 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005365 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005366 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005367 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5368 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5369 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5370 break;
5371 }
5372 default:
5373 LOG(FATAL) << "Unexpected compare type " << type;
5374 UNREACHABLE();
5375 }
5376
Andreas Gampe3db70682018-12-26 15:12:03 -08005377 __ B(eq, final_label, /* is_far_target= */ false);
5378 __ B(less_cond, &less, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005379
5380 __ Bind(&greater);
5381 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005382 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005383
5384 __ Bind(&less);
5385 __ Mov(out, -1);
5386
Anton Kirilov6f644202017-02-27 18:29:45 +00005387 if (done.IsReferenced()) {
5388 __ Bind(&done);
5389 }
Artem Serov02d37832016-10-25 15:25:33 +01005390}
5391
5392void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5393 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005394 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005395 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5396 locations->SetInAt(i, Location::Any());
5397 }
5398 locations->SetOut(Location::Any());
5399}
5400
5401void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5402 LOG(FATAL) << "Unreachable";
5403}
5404
5405void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5406 // TODO (ported from quick): revisit ARM barrier kinds.
5407 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5408 switch (kind) {
5409 case MemBarrierKind::kAnyStore:
5410 case MemBarrierKind::kLoadAny:
5411 case MemBarrierKind::kAnyAny: {
5412 flavor = DmbOptions::ISH;
5413 break;
5414 }
5415 case MemBarrierKind::kStoreStore: {
5416 flavor = DmbOptions::ISHST;
5417 break;
5418 }
5419 default:
5420 LOG(FATAL) << "Unexpected memory barrier " << kind;
5421 }
5422 __ Dmb(flavor);
5423}
5424
5425void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5426 uint32_t offset,
5427 vixl32::Register out_lo,
5428 vixl32::Register out_hi) {
5429 UseScratchRegisterScope temps(GetVIXLAssembler());
5430 if (offset != 0) {
5431 vixl32::Register temp = temps.Acquire();
5432 __ Add(temp, addr, offset);
5433 addr = temp;
5434 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005435 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005436}
5437
5438void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5439 uint32_t offset,
5440 vixl32::Register value_lo,
5441 vixl32::Register value_hi,
5442 vixl32::Register temp1,
5443 vixl32::Register temp2,
5444 HInstruction* instruction) {
5445 UseScratchRegisterScope temps(GetVIXLAssembler());
5446 vixl32::Label fail;
5447 if (offset != 0) {
5448 vixl32::Register temp = temps.Acquire();
5449 __ Add(temp, addr, offset);
5450 addr = temp;
5451 }
5452 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005453 {
5454 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005455 ExactAssemblyScope aas(GetVIXLAssembler(),
5456 vixl32::kMaxInstructionSizeInBytes,
5457 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005458 // We need a load followed by store. (The address used in a STREX instruction must
5459 // be the same as the address in the most recently executed LDREX instruction.)
5460 __ ldrexd(temp1, temp2, MemOperand(addr));
5461 codegen_->MaybeRecordImplicitNullCheck(instruction);
5462 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005463 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005464 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005465}
Artem Serov02109dd2016-09-23 17:17:54 +01005466
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005467void LocationsBuilderARMVIXL::HandleFieldSet(
5468 HInstruction* instruction, const FieldInfo& field_info) {
5469 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5470
5471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005472 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005473 locations->SetInAt(0, Location::RequiresRegister());
5474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 DataType::Type field_type = field_info.GetFieldType();
5476 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005477 locations->SetInAt(1, Location::RequiresFpuRegister());
5478 } else {
5479 locations->SetInAt(1, Location::RequiresRegister());
5480 }
5481
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005482 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005483 bool generate_volatile = field_info.IsVolatile()
5484 && is_wide
5485 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5486 bool needs_write_barrier =
5487 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5488 // Temporary registers for the write barrier.
5489 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5490 if (needs_write_barrier) {
5491 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5492 locations->AddTemp(Location::RequiresRegister());
5493 } else if (generate_volatile) {
5494 // ARM encoding have some additional constraints for ldrexd/strexd:
5495 // - registers need to be consecutive
5496 // - the first register should be even but not R14.
5497 // We don't test for ARM yet, and the assertion makes sure that we
5498 // revisit this if we ever enable ARM encoding.
5499 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5500
5501 locations->AddTemp(Location::RequiresRegister());
5502 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005504 // For doubles we need two more registers to copy the value.
5505 locations->AddTemp(LocationFrom(r2));
5506 locations->AddTemp(LocationFrom(r3));
5507 }
5508 }
5509}
5510
5511void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5512 const FieldInfo& field_info,
5513 bool value_can_be_null) {
5514 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5515
5516 LocationSummary* locations = instruction->GetLocations();
5517 vixl32::Register base = InputRegisterAt(instruction, 0);
5518 Location value = locations->InAt(1);
5519
5520 bool is_volatile = field_info.IsVolatile();
5521 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005522 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005523 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5524 bool needs_write_barrier =
5525 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5526
5527 if (is_volatile) {
5528 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5529 }
5530
5531 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005533 case DataType::Type::kUint8:
5534 case DataType::Type::kInt8:
5535 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005536 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005537 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005538 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5539 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005540 StoreOperandType operand_type = GetStoreOperandType(field_type);
5541 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005542 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005543 break;
5544 }
5545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 case DataType::Type::kReference: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005547 vixl32::Register value_reg = RegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005548 if (kPoisonHeapReferences && needs_write_barrier) {
5549 // Note that in the case where `value` is a null reference,
5550 // we do not enter this block, as a null reference does not
5551 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005552 DCHECK_EQ(field_type, DataType::Type::kReference);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005553 value_reg = RegisterFrom(locations->GetTemp(0));
5554 __ Mov(value_reg, RegisterFrom(value));
5555 GetAssembler()->PoisonHeapReference(value_reg);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005556 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005557 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5558 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5559 GetAssembler()->StoreToOffset(kStoreWord, value_reg, base, offset);
5560 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005561 break;
5562 }
5563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005565 if (is_volatile && !atomic_ldrd_strd) {
5566 GenerateWideAtomicStore(base,
5567 offset,
5568 LowRegisterFrom(value),
5569 HighRegisterFrom(value),
5570 RegisterFrom(locations->GetTemp(0)),
5571 RegisterFrom(locations->GetTemp(1)),
5572 instruction);
5573 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005574 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5575 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005576 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5577 codegen_->MaybeRecordImplicitNullCheck(instruction);
5578 }
5579 break;
5580 }
5581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005582 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005583 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5584 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005585 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005586 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005587 break;
5588 }
5589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005590 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005591 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005592 if (is_volatile && !atomic_ldrd_strd) {
5593 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5594 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5595
5596 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5597
5598 GenerateWideAtomicStore(base,
5599 offset,
5600 value_reg_lo,
5601 value_reg_hi,
5602 RegisterFrom(locations->GetTemp(2)),
5603 RegisterFrom(locations->GetTemp(3)),
5604 instruction);
5605 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005606 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5607 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005608 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5609 codegen_->MaybeRecordImplicitNullCheck(instruction);
5610 }
5611 break;
5612 }
5613
Aart Bik66c158e2018-01-31 12:55:04 -08005614 case DataType::Type::kUint32:
5615 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005616 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005617 LOG(FATAL) << "Unreachable type " << field_type;
5618 UNREACHABLE();
5619 }
5620
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005621 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5622 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5623 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5624 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5625 }
5626
5627 if (is_volatile) {
5628 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5629 }
5630}
5631
Artem Serov02d37832016-10-25 15:25:33 +01005632void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5633 const FieldInfo& field_info) {
5634 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5635
5636 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005638 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005639 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5640 object_field_get_with_read_barrier
5641 ? LocationSummary::kCallOnSlowPath
5642 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005643 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5644 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5645 }
5646 locations->SetInAt(0, Location::RequiresRegister());
5647
5648 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005649 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005650 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5651 // The output overlaps in case of volatile long: we don't want the
5652 // code generated by GenerateWideAtomicLoad to overwrite the
5653 // object's location. Likewise, in the case of an object field get
5654 // with read barriers enabled, we do not want the load to overwrite
5655 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 bool overlap =
5657 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005658 object_field_get_with_read_barrier;
5659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005660 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005661 locations->SetOut(Location::RequiresFpuRegister());
5662 } else {
5663 locations->SetOut(Location::RequiresRegister(),
5664 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5665 }
5666 if (volatile_for_double) {
5667 // ARM encoding have some additional constraints for ldrexd/strexd:
5668 // - registers need to be consecutive
5669 // - the first register should be even but not R14.
5670 // We don't test for ARM yet, and the assertion makes sure that we
5671 // revisit this if we ever enable ARM encoding.
5672 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5673 locations->AddTemp(Location::RequiresRegister());
5674 locations->AddTemp(Location::RequiresRegister());
5675 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005676 // We need a temporary register for the read barrier load in
5677 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5678 // only if the offset is too big.
5679 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005680 locations->AddTemp(Location::RequiresRegister());
5681 }
Artem Serov02d37832016-10-25 15:25:33 +01005682 }
5683}
5684
5685Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005686 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005687 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5688 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5689 return Location::ConstantLocation(input->AsConstant());
5690 } else {
5691 return Location::RequiresFpuRegister();
5692 }
5693}
5694
Artem Serov02109dd2016-09-23 17:17:54 +01005695Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5696 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005697 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005698 if (constant->IsConstant() &&
5699 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5700 return Location::ConstantLocation(constant->AsConstant());
5701 }
5702 return Location::RequiresRegister();
5703}
5704
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005705static bool CanEncode32BitConstantAsImmediate(
5706 CodeGeneratorARMVIXL* codegen,
5707 uint32_t value,
5708 Opcode opcode,
5709 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5710 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5711 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005712 return true;
5713 }
5714 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005715 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005716 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005717 case AND: neg_opcode = BIC; neg_value = ~value; break;
5718 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5719 case ADD: neg_opcode = SUB; neg_value = -value; break;
5720 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5721 case SUB: neg_opcode = ADD; neg_value = -value; break;
5722 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5723 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005724 default:
5725 return false;
5726 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005727
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005728 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005729 return true;
5730 }
5731
5732 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005733}
5734
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005735bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5736 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5737 if (DataType::Is64BitType(input_cst->GetType())) {
5738 Opcode high_opcode = opcode;
5739 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5740 switch (opcode) {
5741 case SUB:
5742 // Flip the operation to an ADD.
5743 value = -value;
5744 opcode = ADD;
5745 FALLTHROUGH_INTENDED;
5746 case ADD:
5747 if (Low32Bits(value) == 0u) {
5748 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5749 }
5750 high_opcode = ADC;
5751 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5752 break;
5753 default:
5754 break;
5755 }
5756 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5757 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5758 } else {
5759 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5760 }
5761}
5762
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005763void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5764 const FieldInfo& field_info) {
5765 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5766
5767 LocationSummary* locations = instruction->GetLocations();
5768 vixl32::Register base = InputRegisterAt(instruction, 0);
5769 Location out = locations->Out();
5770 bool is_volatile = field_info.IsVolatile();
5771 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005772 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5773 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005774 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5775
Vladimir Marko61b92282017-10-11 13:23:17 +01005776 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005778 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005779 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005780 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005781 case DataType::Type::kInt16:
5782 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005783 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5784 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01005785 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005786 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005787 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005788 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005789 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005792 // /* HeapReference<Object> */ out = *(base + offset)
5793 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01005794 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005795 // Note that a potential implicit null check is handled in this
5796 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5797 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005798 instruction, out, base, offset, maybe_temp, /* needs_null_check= */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005799 if (is_volatile) {
5800 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5801 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005802 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005803 {
5804 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5805 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5806 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5807 codegen_->MaybeRecordImplicitNullCheck(instruction);
5808 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005809 if (is_volatile) {
5810 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5811 }
5812 // If read barriers are enabled, emit read barriers other than
5813 // Baker's using a slow path (and also unpoison the loaded
5814 // reference, if heap poisoning is enabled).
5815 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5816 }
5817 break;
5818 }
5819
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005820 case DataType::Type::kInt64: {
5821 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5822 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005823 if (is_volatile && !atomic_ldrd_strd) {
5824 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5825 } else {
5826 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5827 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005828 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005829 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005830 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005831
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005832 case DataType::Type::kFloat32: {
5833 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5834 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005835 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005836 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005837 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005838 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005841 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5842 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005843 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005844 if (is_volatile && !atomic_ldrd_strd) {
5845 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5846 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5847 GenerateWideAtomicLoad(base, offset, lo, hi);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005848 codegen_->MaybeRecordImplicitNullCheck(instruction);
5849 __ Vmov(out_dreg, lo, hi);
5850 } else {
5851 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005852 codegen_->MaybeRecordImplicitNullCheck(instruction);
5853 }
5854 break;
5855 }
5856
Aart Bik66c158e2018-01-31 12:55:04 -08005857 case DataType::Type::kUint32:
5858 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005860 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005861 UNREACHABLE();
5862 }
5863
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005864 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005865 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005866 // Memory barriers, in the case of references, are also handled
5867 // in the previous switch statement.
5868 } else {
5869 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5870 }
5871 }
5872}
5873
5874void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5875 HandleFieldSet(instruction, instruction->GetFieldInfo());
5876}
5877
5878void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5879 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5880}
5881
5882void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5883 HandleFieldGet(instruction, instruction->GetFieldInfo());
5884}
5885
5886void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5887 HandleFieldGet(instruction, instruction->GetFieldInfo());
5888}
5889
5890void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5891 HandleFieldGet(instruction, instruction->GetFieldInfo());
5892}
5893
5894void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5895 HandleFieldGet(instruction, instruction->GetFieldInfo());
5896}
5897
Scott Wakelingc34dba72016-10-03 10:14:44 +01005898void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5899 HandleFieldSet(instruction, instruction->GetFieldInfo());
5900}
5901
5902void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5903 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5904}
5905
Vladimir Marko552a1342017-10-31 10:56:47 +00005906void LocationsBuilderARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5907 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(r0));
5908}
5909
5910void InstructionCodeGeneratorARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5911 __ Mov(r0, instruction->GetFormat()->GetValue());
5912 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5913}
5914
Artem Serovcfbe9132016-10-14 15:58:56 +01005915void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5916 HUnresolvedInstanceFieldGet* instruction) {
5917 FieldAccessCallingConventionARMVIXL calling_convention;
5918 codegen_->CreateUnresolvedFieldLocationSummary(
5919 instruction, instruction->GetFieldType(), calling_convention);
5920}
5921
5922void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5923 HUnresolvedInstanceFieldGet* instruction) {
5924 FieldAccessCallingConventionARMVIXL calling_convention;
5925 codegen_->GenerateUnresolvedFieldAccess(instruction,
5926 instruction->GetFieldType(),
5927 instruction->GetFieldIndex(),
5928 instruction->GetDexPc(),
5929 calling_convention);
5930}
5931
5932void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5933 HUnresolvedInstanceFieldSet* instruction) {
5934 FieldAccessCallingConventionARMVIXL calling_convention;
5935 codegen_->CreateUnresolvedFieldLocationSummary(
5936 instruction, instruction->GetFieldType(), calling_convention);
5937}
5938
5939void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5940 HUnresolvedInstanceFieldSet* instruction) {
5941 FieldAccessCallingConventionARMVIXL calling_convention;
5942 codegen_->GenerateUnresolvedFieldAccess(instruction,
5943 instruction->GetFieldType(),
5944 instruction->GetFieldIndex(),
5945 instruction->GetDexPc(),
5946 calling_convention);
5947}
5948
5949void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5950 HUnresolvedStaticFieldGet* instruction) {
5951 FieldAccessCallingConventionARMVIXL calling_convention;
5952 codegen_->CreateUnresolvedFieldLocationSummary(
5953 instruction, instruction->GetFieldType(), calling_convention);
5954}
5955
5956void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5957 HUnresolvedStaticFieldGet* instruction) {
5958 FieldAccessCallingConventionARMVIXL calling_convention;
5959 codegen_->GenerateUnresolvedFieldAccess(instruction,
5960 instruction->GetFieldType(),
5961 instruction->GetFieldIndex(),
5962 instruction->GetDexPc(),
5963 calling_convention);
5964}
5965
5966void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5967 HUnresolvedStaticFieldSet* instruction) {
5968 FieldAccessCallingConventionARMVIXL calling_convention;
5969 codegen_->CreateUnresolvedFieldLocationSummary(
5970 instruction, instruction->GetFieldType(), calling_convention);
5971}
5972
5973void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5974 HUnresolvedStaticFieldSet* instruction) {
5975 FieldAccessCallingConventionARMVIXL calling_convention;
5976 codegen_->GenerateUnresolvedFieldAccess(instruction,
5977 instruction->GetFieldType(),
5978 instruction->GetFieldIndex(),
5979 instruction->GetDexPc(),
5980 calling_convention);
5981}
5982
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005983void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005984 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005985 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005986}
5987
5988void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5989 if (CanMoveNullCheckToUser(instruction)) {
5990 return;
5991 }
5992
5993 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005994 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005995 ExactAssemblyScope aas(GetVIXLAssembler(),
5996 vixl32::kMaxInstructionSizeInBytes,
5997 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005998 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5999 RecordPcInfo(instruction, instruction->GetDexPc());
6000}
6001
6002void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
6003 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006004 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006005 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00006006 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006007}
6008
6009void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
6010 codegen_->GenerateNullCheck(instruction);
6011}
6012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006013void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006014 Location out_loc,
6015 vixl32::Register base,
6016 vixl32::Register reg_index,
6017 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006019 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6020
6021 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006022 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006023 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01006024 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
6025 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006026 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006027 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
6028 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006030 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
6031 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006032 case DataType::Type::kInt16:
6033 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
6034 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 case DataType::Type::kReference:
6036 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006037 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
6038 break;
6039 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006040 case DataType::Type::kInt64:
6041 case DataType::Type::kFloat32:
6042 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006043 default:
6044 LOG(FATAL) << "Unreachable type " << type;
6045 UNREACHABLE();
6046 }
6047}
6048
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006049void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01006050 Location loc,
6051 vixl32::Register base,
6052 vixl32::Register reg_index,
6053 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006054 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006055 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
6056
6057 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006058 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006059 case DataType::Type::kUint8:
6060 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006061 __ Strb(cond, RegisterFrom(loc), mem_address);
6062 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006063 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006064 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006065 __ Strh(cond, RegisterFrom(loc), mem_address);
6066 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006067 case DataType::Type::kReference:
6068 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006069 __ Str(cond, RegisterFrom(loc), mem_address);
6070 break;
6071 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006072 case DataType::Type::kInt64:
6073 case DataType::Type::kFloat32:
6074 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006075 default:
6076 LOG(FATAL) << "Unreachable type " << type;
6077 UNREACHABLE();
6078 }
6079}
6080
6081void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6082 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006083 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006084 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006085 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6086 object_array_get_with_read_barrier
6087 ? LocationSummary::kCallOnSlowPath
6088 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006089 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006090 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006091 }
6092 locations->SetInAt(0, Location::RequiresRegister());
6093 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006094 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006095 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6096 } else {
6097 // The output overlaps in the case of an object array get with
6098 // read barriers enabled: we do not want the move to overwrite the
6099 // array's location, as we need it to emit the read barrier.
6100 locations->SetOut(
6101 Location::RequiresRegister(),
6102 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6103 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006104 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006105 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006106 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01006107 // We need a temporary register for the read barrier load in
6108 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
6109 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006110 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6111 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006113 if (offset >= kReferenceLoadMinFarOffset) {
6114 locations->AddTemp(Location::RequiresRegister());
6115 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006116 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006117 // We need a non-scratch temporary for the array data pointer in
6118 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006119 locations->AddTemp(Location::RequiresRegister());
6120 }
6121 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6122 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006123 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006124 }
6125}
6126
6127void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006128 LocationSummary* locations = instruction->GetLocations();
6129 Location obj_loc = locations->InAt(0);
6130 vixl32::Register obj = InputRegisterAt(instruction, 0);
6131 Location index = locations->InAt(1);
6132 Location out_loc = locations->Out();
6133 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006134 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006135 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6136 instruction->IsStringCharAt();
6137 HInstruction* array_instr = instruction->GetArray();
6138 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006139
6140 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006141 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006142 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006143 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006145 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006146 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006147 vixl32::Register length;
6148 if (maybe_compressed_char_at) {
6149 length = RegisterFrom(locations->GetTemp(0));
6150 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006151 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6152 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006153 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6154 codegen_->MaybeRecordImplicitNullCheck(instruction);
6155 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006156 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006157 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006158 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006159 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006160 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006161 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6162 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6163 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006164 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006165 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6166 RegisterFrom(out_loc),
6167 obj,
6168 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006169 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006170 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006171 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006172 RegisterFrom(out_loc),
6173 obj,
6174 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006175 if (done.IsReferenced()) {
6176 __ Bind(&done);
6177 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006178 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006179 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006180
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006181 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6182 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006183 LoadOperandType load_type = GetLoadOperandType(type);
6184 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006185 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006186 }
6187 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006188 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006189 vixl32::Register temp = temps.Acquire();
6190
6191 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006192 // We do not need to compute the intermediate address from the array: the
6193 // input instruction has done it already. See the comment in
6194 // `TryExtractArrayAccessAddress()`.
6195 if (kIsDebugBuild) {
6196 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006197 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006198 }
6199 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006200 } else {
6201 __ Add(temp, obj, data_offset);
6202 }
6203 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006204 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006205 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006206 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6207 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6208 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006209 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006210 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006211 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006212 __ Bind(&uncompressed_load);
6213 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006214 if (done.IsReferenced()) {
6215 __ Bind(&done);
6216 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006217 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006218 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6219 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006220 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006221 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006222 }
6223 }
6224 break;
6225 }
6226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006228 // The read barrier instrumentation of object ArrayGet
6229 // instructions does not support the HIntermediateAddress
6230 // instruction.
6231 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6232
Scott Wakelingc34dba72016-10-03 10:14:44 +01006233 static_assert(
6234 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6235 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6236 // /* HeapReference<Object> */ out =
6237 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6238 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006239 // Note that a potential implicit null check is handled in this
6240 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006241 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6242 if (index.IsConstant()) {
6243 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006244 Location maybe_temp =
6245 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006247 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6248 out_loc,
6249 obj,
6250 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006251 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006252 /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006253 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006254 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006255 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006256 out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006257 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006258 } else {
6259 vixl32::Register out = OutputRegister(instruction);
6260 if (index.IsConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006261 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6262 {
6263 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6264 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6265 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
6266 codegen_->MaybeRecordImplicitNullCheck(instruction);
6267 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006268 // If read barriers are enabled, emit read barriers other than
6269 // Baker's using a slow path (and also unpoison the loaded
6270 // reference, if heap poisoning is enabled).
6271 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6272 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006273 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006274 vixl32::Register temp = temps.Acquire();
6275
6276 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006277 // We do not need to compute the intermediate address from the array: the
6278 // input instruction has done it already. See the comment in
6279 // `TryExtractArrayAccessAddress()`.
6280 if (kIsDebugBuild) {
6281 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006282 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006283 }
6284 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006285 } else {
6286 __ Add(temp, obj, data_offset);
6287 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006288 {
6289 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6290 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6291 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6292 temps.Close();
6293 codegen_->MaybeRecordImplicitNullCheck(instruction);
6294 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006295 // If read barriers are enabled, emit read barriers other than
6296 // Baker's using a slow path (and also unpoison the loaded
6297 // reference, if heap poisoning is enabled).
6298 codegen_->MaybeGenerateReadBarrierSlow(
6299 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6300 }
6301 }
6302 break;
6303 }
6304
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006305 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006306 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6307 // As two macro instructions can be emitted the max size is doubled.
6308 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006309 if (index.IsConstant()) {
6310 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006311 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006312 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6313 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006314 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315 vixl32::Register temp = temps.Acquire();
6316 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6317 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6318 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006319 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006320 break;
6321 }
6322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006323 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006324 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6325 // As two macro instructions can be emitted the max size is doubled.
6326 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006327 vixl32::SRegister out = SRegisterFrom(out_loc);
6328 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006329 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006330 GetAssembler()->LoadSFromOffset(out, obj, offset);
6331 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006332 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006333 vixl32::Register temp = temps.Acquire();
6334 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6335 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6336 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006337 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 break;
6339 }
6340
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006341 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006342 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6343 // As two macro instructions can be emitted the max size is doubled.
6344 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006345 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006346 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006347 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6348 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006349 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006350 vixl32::Register temp = temps.Acquire();
6351 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6352 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6353 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006354 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006355 break;
6356 }
6357
Aart Bik66c158e2018-01-31 12:55:04 -08006358 case DataType::Type::kUint32:
6359 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006360 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006361 LOG(FATAL) << "Unreachable type " << type;
6362 UNREACHABLE();
6363 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006364}
6365
6366void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006367 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006368
6369 bool needs_write_barrier =
6370 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006371 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006372
Vladimir Markoca6fff82017-10-03 14:49:14 +01006373 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006374 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006375 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006376
6377 locations->SetInAt(0, Location::RequiresRegister());
6378 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006380 locations->SetInAt(2, Location::RequiresFpuRegister());
6381 } else {
6382 locations->SetInAt(2, Location::RequiresRegister());
6383 }
6384 if (needs_write_barrier) {
6385 // Temporary registers for the write barrier.
6386 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6387 locations->AddTemp(Location::RequiresRegister());
6388 }
6389}
6390
6391void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006392 LocationSummary* locations = instruction->GetLocations();
6393 vixl32::Register array = InputRegisterAt(instruction, 0);
6394 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006395 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006396 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006397 bool needs_write_barrier =
6398 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6399 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006400 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006401 Location value_loc = locations->InAt(2);
6402 HInstruction* array_instr = instruction->GetArray();
6403 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006404
6405 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006406 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006407 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006408 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006410 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006411 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006412 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006413 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006414 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006415 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006416 StoreOperandType store_type = GetStoreOperandType(value_type);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006417 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6418 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006419 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006420 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006421 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006422 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006423 vixl32::Register temp = temps.Acquire();
6424
6425 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006426 // We do not need to compute the intermediate address from the array: the
6427 // input instruction has done it already. See the comment in
6428 // `TryExtractArrayAccessAddress()`.
6429 if (kIsDebugBuild) {
6430 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006431 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006432 }
6433 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006434 } else {
6435 __ Add(temp, array, data_offset);
6436 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006437 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6438 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006439 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006440 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006441 }
6442 break;
6443 }
6444
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006445 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006446 vixl32::Register value = RegisterFrom(value_loc);
6447 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6448 // See the comment in instruction_simplifier_shared.cc.
6449 DCHECK(!has_intermediate_address);
6450
6451 if (instruction->InputAt(2)->IsNullConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006452 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6453 // As two macro instructions can be emitted the max size is doubled.
6454 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006455 // Just setting null.
6456 if (index.IsConstant()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006457 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006458 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6459 } else {
6460 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006461 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006462 vixl32::Register temp = temps.Acquire();
6463 __ Add(temp, array, data_offset);
6464 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6465 }
6466 codegen_->MaybeRecordImplicitNullCheck(instruction);
6467 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006468 DCHECK(!needs_type_check);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006469 break;
6470 }
6471
6472 DCHECK(needs_write_barrier);
6473 Location temp1_loc = locations->GetTemp(0);
6474 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6475 Location temp2_loc = locations->GetTemp(1);
6476 vixl32::Register temp2 = RegisterFrom(temp2_loc);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006477
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006478 bool can_value_be_null = instruction->GetValueCanBeNull();
6479 vixl32::Label do_store;
6480 if (can_value_be_null) {
6481 __ CompareAndBranchIfZero(value, &do_store, /* is_far_target= */ false);
6482 }
6483
6484 SlowPathCodeARMVIXL* slow_path = nullptr;
6485 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006486 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006487 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006488
6489 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6490 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6491 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006492
6493 // Note that when read barriers are enabled, the type checks
6494 // are performed without read barriers. This is fine, even in
6495 // the case where a class object is in the from-space after
6496 // the flip, as a comparison involving such a type would not
6497 // produce a false positive; it may of course produce a false
6498 // negative, in which case we would take the ArraySet slow
6499 // path.
6500
Alexandre Rames374ddf32016-11-04 10:40:49 +00006501 {
6502 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006503 ExactAssemblyScope aas(GetVIXLAssembler(),
6504 vixl32::kMaxInstructionSizeInBytes,
6505 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006506 // /* HeapReference<Class> */ temp1 = array->klass_
6507 __ ldr(temp1, MemOperand(array, class_offset));
6508 codegen_->MaybeRecordImplicitNullCheck(instruction);
6509 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006510 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6511
6512 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6513 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6514 // /* HeapReference<Class> */ temp2 = value->klass_
6515 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6516 // If heap poisoning is enabled, no need to unpoison `temp1`
6517 // nor `temp2`, as we are comparing two poisoned references.
6518 __ Cmp(temp1, temp2);
6519
6520 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006521 vixl32::Label do_put;
6522 __ B(eq, &do_put, /* is_far_target= */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006523 // If heap poisoning is enabled, the `temp1` reference has
6524 // not been unpoisoned yet; unpoison it now.
6525 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6526
6527 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6528 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6529 // If heap poisoning is enabled, no need to unpoison
6530 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006531 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006532 __ Bind(&do_put);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006533 } else {
6534 __ B(ne, slow_path->GetEntryLabel());
6535 }
6536 }
6537
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006538 codegen_->MarkGCCard(temp1, temp2, array, value, /* can_be_null= */ false);
6539
6540 if (can_value_be_null) {
6541 DCHECK(do_store.IsReferenced());
6542 __ Bind(&do_store);
6543 }
6544
Scott Wakelingc34dba72016-10-03 10:14:44 +01006545 vixl32::Register source = value;
6546 if (kPoisonHeapReferences) {
6547 // Note that in the case where `value` is a null reference,
6548 // we do not enter this block, as a null reference does not
6549 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006550 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551 __ Mov(temp1, value);
6552 GetAssembler()->PoisonHeapReference(temp1);
6553 source = temp1;
6554 }
6555
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006556 {
6557 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6558 // As two macro instructions can be emitted the max size is doubled.
6559 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
6560 if (index.IsConstant()) {
6561 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6562 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6563 } else {
6564 DCHECK(index.IsRegister()) << index;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006565
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006566 UseScratchRegisterScope temps(GetVIXLAssembler());
6567 vixl32::Register temp = temps.Acquire();
6568 __ Add(temp, array, data_offset);
6569 codegen_->StoreToShiftedRegOffset(value_type,
6570 LocationFrom(source),
6571 temp,
6572 RegisterFrom(index));
6573 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006574
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006575 if (can_value_be_null || !needs_type_check) {
6576 codegen_->MaybeRecordImplicitNullCheck(instruction);
6577 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006578 }
6579
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006580 if (slow_path != nullptr) {
6581 __ Bind(slow_path->GetExitLabel());
6582 }
6583
Scott Wakelingc34dba72016-10-03 10:14:44 +01006584 break;
6585 }
6586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006587 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006588 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6589 // As two macro instructions can be emitted the max size is doubled.
6590 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006591 Location value = locations->InAt(2);
6592 if (index.IsConstant()) {
6593 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006594 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006595 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6596 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006597 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006598 vixl32::Register temp = temps.Acquire();
6599 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6600 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6601 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006602 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006603 break;
6604 }
6605
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006606 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006607 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6608 // As two macro instructions can be emitted the max size is doubled.
6609 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006610 Location value = locations->InAt(2);
6611 DCHECK(value.IsFpuRegister());
6612 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006613 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006614 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6615 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006616 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006617 vixl32::Register temp = temps.Acquire();
6618 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6619 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6620 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006621 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006622 break;
6623 }
6624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006625 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006626 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6627 // As two macro instructions can be emitted the max size is doubled.
6628 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006629 Location value = locations->InAt(2);
6630 DCHECK(value.IsFpuRegisterPair());
6631 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006632 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006633 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6634 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006635 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006636 vixl32::Register temp = temps.Acquire();
6637 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6638 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6639 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006640 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006641 break;
6642 }
6643
Aart Bik66c158e2018-01-31 12:55:04 -08006644 case DataType::Type::kUint32:
6645 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006646 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006647 LOG(FATAL) << "Unreachable type " << value_type;
6648 UNREACHABLE();
6649 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006650}
6651
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006652void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6653 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006654 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006655 locations->SetInAt(0, Location::RequiresRegister());
6656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6657}
6658
6659void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6660 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6661 vixl32::Register obj = InputRegisterAt(instruction, 0);
6662 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006663 {
Artem Serov0fb37192016-12-06 18:13:40 +00006664 ExactAssemblyScope aas(GetVIXLAssembler(),
6665 vixl32::kMaxInstructionSizeInBytes,
6666 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006667 __ ldr(out, MemOperand(obj, offset));
6668 codegen_->MaybeRecordImplicitNullCheck(instruction);
6669 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006670 // Mask out compression flag from String's array length.
6671 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006672 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006673 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006674}
6675
Artem Serov2bbc9532016-10-21 11:51:50 +01006676void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006677 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006678 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006679
6680 locations->SetInAt(0, Location::RequiresRegister());
6681 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6683}
6684
6685void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6686 vixl32::Register out = OutputRegister(instruction);
6687 vixl32::Register first = InputRegisterAt(instruction, 0);
6688 Location second = instruction->GetLocations()->InAt(1);
6689
Artem Serov2bbc9532016-10-21 11:51:50 +01006690 if (second.IsRegister()) {
6691 __ Add(out, first, RegisterFrom(second));
6692 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006693 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006694 }
6695}
6696
Artem Serove1811ed2017-04-27 16:50:47 +01006697void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6698 HIntermediateAddressIndex* instruction) {
6699 LOG(FATAL) << "Unreachable " << instruction->GetId();
6700}
6701
6702void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6703 HIntermediateAddressIndex* instruction) {
6704 LOG(FATAL) << "Unreachable " << instruction->GetId();
6705}
6706
Scott Wakelingc34dba72016-10-03 10:14:44 +01006707void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6708 RegisterSet caller_saves = RegisterSet::Empty();
6709 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6710 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6711 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6712 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006713
6714 HInstruction* index = instruction->InputAt(0);
6715 HInstruction* length = instruction->InputAt(1);
6716 // If both index and length are constants we can statically check the bounds. But if at least one
6717 // of them is not encodable ArmEncodableConstantOrRegister will create
6718 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6719 // locations.
6720 bool both_const = index->IsConstant() && length->IsConstant();
6721 locations->SetInAt(0, both_const
6722 ? Location::ConstantLocation(index->AsConstant())
6723 : ArmEncodableConstantOrRegister(index, CMP));
6724 locations->SetInAt(1, both_const
6725 ? Location::ConstantLocation(length->AsConstant())
6726 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006727}
6728
6729void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006730 LocationSummary* locations = instruction->GetLocations();
6731 Location index_loc = locations->InAt(0);
6732 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006733
Artem Serov2dd053d2017-03-08 14:54:06 +00006734 if (length_loc.IsConstant()) {
6735 int32_t length = Int32ConstantFrom(length_loc);
6736 if (index_loc.IsConstant()) {
6737 // BCE will remove the bounds check if we are guaranteed to pass.
6738 int32_t index = Int32ConstantFrom(index_loc);
6739 if (index < 0 || index >= length) {
6740 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006741 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006742 codegen_->AddSlowPath(slow_path);
6743 __ B(slow_path->GetEntryLabel());
6744 } else {
6745 // Some optimization after BCE may have generated this, and we should not
6746 // generate a bounds check if it is a valid range.
6747 }
6748 return;
6749 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006750
Artem Serov2dd053d2017-03-08 14:54:06 +00006751 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006752 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006753 __ Cmp(RegisterFrom(index_loc), length);
6754 codegen_->AddSlowPath(slow_path);
6755 __ B(hs, slow_path->GetEntryLabel());
6756 } else {
6757 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006758 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006759 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6760 codegen_->AddSlowPath(slow_path);
6761 __ B(ls, slow_path->GetEntryLabel());
6762 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006763}
6764
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006765void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6766 vixl32::Register card,
6767 vixl32::Register object,
6768 vixl32::Register value,
6769 bool can_be_null) {
6770 vixl32::Label is_null;
6771 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006772 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006773 }
Roland Levillainc73f0522018-08-14 15:16:50 +01006774 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006775 GetAssembler()->LoadFromOffset(
6776 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01006777 // Calculate the offset (in the card table) of the card corresponding to
6778 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00006779 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01006780 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
6781 // `object`'s card.
6782 //
6783 // Register `card` contains the address of the card table. Note that the card
6784 // table's base is biased during its creation so that it always starts at an
6785 // address whose least-significant byte is equal to `kCardDirty` (see
6786 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
6787 // below writes the `kCardDirty` (byte) value into the `object`'s card
6788 // (located at `card + object >> kCardShift`).
6789 //
6790 // This dual use of the value in register `card` (1. to calculate the location
6791 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
6792 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006793 __ Strb(card, MemOperand(card, temp));
6794 if (can_be_null) {
6795 __ Bind(&is_null);
6796 }
6797}
6798
Scott Wakelingfe885462016-09-22 10:24:38 +01006799void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6800 LOG(FATAL) << "Unreachable";
6801}
6802
6803void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006804 if (instruction->GetNext()->IsSuspendCheck() &&
6805 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6806 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6807 // The back edge will generate the suspend check.
6808 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6809 }
6810
Scott Wakelingfe885462016-09-22 10:24:38 +01006811 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6812}
6813
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006814void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006815 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6816 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006817 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006818}
6819
6820void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6821 HBasicBlock* block = instruction->GetBlock();
6822 if (block->GetLoopInformation() != nullptr) {
6823 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6824 // The back edge will generate the suspend check.
6825 return;
6826 }
6827 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6828 // The goto will generate the suspend check.
6829 return;
6830 }
6831 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006832 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006833}
6834
6835void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6836 HBasicBlock* successor) {
6837 SuspendCheckSlowPathARMVIXL* slow_path =
6838 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6839 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006840 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006841 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006842 instruction->SetSlowPath(slow_path);
6843 codegen_->AddSlowPath(slow_path);
6844 if (successor != nullptr) {
6845 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006846 }
6847 } else {
6848 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6849 }
6850
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006851 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006852 vixl32::Register temp = temps.Acquire();
6853 GetAssembler()->LoadFromOffset(
6854 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6855 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006856 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006857 __ Bind(slow_path->GetReturnLabel());
6858 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006859 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006860 __ B(slow_path->GetEntryLabel());
6861 }
6862}
6863
Scott Wakelingfe885462016-09-22 10:24:38 +01006864ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6865 return codegen_->GetAssembler();
6866}
6867
6868void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006869 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006870 MoveOperands* move = moves_[index];
6871 Location source = move->GetSource();
6872 Location destination = move->GetDestination();
6873
6874 if (source.IsRegister()) {
6875 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006876 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006877 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006878 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006879 } else {
6880 DCHECK(destination.IsStackSlot());
6881 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006882 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006883 sp,
6884 destination.GetStackIndex());
6885 }
6886 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006887 if (destination.IsRegister()) {
6888 GetAssembler()->LoadFromOffset(kLoadWord,
6889 RegisterFrom(destination),
6890 sp,
6891 source.GetStackIndex());
6892 } else if (destination.IsFpuRegister()) {
6893 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6894 } else {
6895 DCHECK(destination.IsStackSlot());
6896 vixl32::Register temp = temps.Acquire();
6897 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6898 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6899 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006900 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006901 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006902 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006903 } else if (destination.IsFpuRegister()) {
6904 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6905 } else {
6906 DCHECK(destination.IsStackSlot());
6907 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6908 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006909 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006910 if (destination.IsDoubleStackSlot()) {
6911 vixl32::DRegister temp = temps.AcquireD();
6912 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6913 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6914 } else if (destination.IsRegisterPair()) {
6915 DCHECK(ExpectedPairLayout(destination));
6916 GetAssembler()->LoadFromOffset(
6917 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6918 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006919 DCHECK(destination.IsFpuRegisterPair()) << destination;
6920 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006921 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006922 } else if (source.IsRegisterPair()) {
6923 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006924 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6925 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006926 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006927 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006928 } else {
6929 DCHECK(destination.IsDoubleStackSlot()) << destination;
6930 DCHECK(ExpectedPairLayout(source));
6931 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006932 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006933 sp,
6934 destination.GetStackIndex());
6935 }
6936 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006937 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006938 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006939 } else if (destination.IsFpuRegisterPair()) {
6940 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6941 } else {
6942 DCHECK(destination.IsDoubleStackSlot()) << destination;
6943 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6944 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006945 } else {
6946 DCHECK(source.IsConstant()) << source;
6947 HConstant* constant = source.GetConstant();
6948 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6949 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6950 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006951 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006952 } else {
6953 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006954 vixl32::Register temp = temps.Acquire();
6955 __ Mov(temp, value);
6956 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6957 }
6958 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006959 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006960 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006961 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6962 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006963 } else {
6964 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006965 vixl32::Register temp = temps.Acquire();
6966 __ Mov(temp, Low32Bits(value));
6967 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6968 __ Mov(temp, High32Bits(value));
6969 GetAssembler()->StoreToOffset(kStoreWord,
6970 temp,
6971 sp,
6972 destination.GetHighStackIndex(kArmWordSize));
6973 }
6974 } else if (constant->IsDoubleConstant()) {
6975 double value = constant->AsDoubleConstant()->GetValue();
6976 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006977 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006978 } else {
6979 DCHECK(destination.IsDoubleStackSlot()) << destination;
6980 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006981 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006982 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006983 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006984 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006985 GetAssembler()->StoreToOffset(kStoreWord,
6986 temp,
6987 sp,
6988 destination.GetHighStackIndex(kArmWordSize));
6989 }
6990 } else {
6991 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6992 float value = constant->AsFloatConstant()->GetValue();
6993 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006994 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006995 } else {
6996 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006997 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006998 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006999 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
7000 }
7001 }
7002 }
7003}
7004
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007005void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
7006 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7007 vixl32::Register temp = temps.Acquire();
7008 __ Mov(temp, reg);
7009 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
7010 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01007011}
7012
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007013void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
7014 // TODO(VIXL32): Double check the performance of this implementation.
7015 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007016 vixl32::Register temp1 = temps.Acquire();
7017 ScratchRegisterScope ensure_scratch(
7018 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
7019 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007020
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007021 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7022 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
7023 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
7024 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
7025 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01007026}
7027
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007028void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
7029 MoveOperands* move = moves_[index];
7030 Location source = move->GetSource();
7031 Location destination = move->GetDestination();
7032 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
7033
7034 if (source.IsRegister() && destination.IsRegister()) {
7035 vixl32::Register temp = temps.Acquire();
7036 DCHECK(!RegisterFrom(source).Is(temp));
7037 DCHECK(!RegisterFrom(destination).Is(temp));
7038 __ Mov(temp, RegisterFrom(destination));
7039 __ Mov(RegisterFrom(destination), RegisterFrom(source));
7040 __ Mov(RegisterFrom(source), temp);
7041 } else if (source.IsRegister() && destination.IsStackSlot()) {
7042 Exchange(RegisterFrom(source), destination.GetStackIndex());
7043 } else if (source.IsStackSlot() && destination.IsRegister()) {
7044 Exchange(RegisterFrom(destination), source.GetStackIndex());
7045 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007046 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007047 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007048 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00007049 __ Vmov(temp, SRegisterFrom(source));
7050 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
7051 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007052 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
7053 vixl32::DRegister temp = temps.AcquireD();
7054 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
7055 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
7056 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
7057 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
7058 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7059 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7060 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7061 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7062 vixl32::DRegister temp = temps.AcquireD();
7063 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7064 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7065 GetAssembler()->StoreDToOffset(temp, sp, mem);
7066 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007067 vixl32::DRegister first = DRegisterFrom(source);
7068 vixl32::DRegister second = DRegisterFrom(destination);
7069 vixl32::DRegister temp = temps.AcquireD();
7070 __ Vmov(temp, first);
7071 __ Vmov(first, second);
7072 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007073 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007074 vixl32::DRegister reg = source.IsFpuRegisterPair()
7075 ? DRegisterFrom(source)
7076 : DRegisterFrom(destination);
7077 int mem = source.IsFpuRegisterPair()
7078 ? destination.GetStackIndex()
7079 : source.GetStackIndex();
7080 vixl32::DRegister temp = temps.AcquireD();
7081 __ Vmov(temp, reg);
7082 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7083 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007084 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007085 vixl32::SRegister reg = source.IsFpuRegister()
7086 ? SRegisterFrom(source)
7087 : SRegisterFrom(destination);
7088 int mem = source.IsFpuRegister()
7089 ? destination.GetStackIndex()
7090 : source.GetStackIndex();
7091 vixl32::Register temp = temps.Acquire();
7092 __ Vmov(temp, reg);
7093 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7094 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007095 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7096 vixl32::DRegister temp1 = temps.AcquireD();
7097 vixl32::DRegister temp2 = temps.AcquireD();
7098 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7099 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7100 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7101 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7102 } else {
7103 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7104 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007105}
7106
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007107void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7108 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007109}
7110
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007111void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7112 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007113}
7114
Artem Serov02d37832016-10-25 15:25:33 +01007115HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007116 HLoadClass::LoadKind desired_class_load_kind) {
7117 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007118 case HLoadClass::LoadKind::kInvalid:
7119 LOG(FATAL) << "UNREACHABLE";
7120 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007121 case HLoadClass::LoadKind::kReferrersClass:
7122 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007123 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007124 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007125 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007126 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007127 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007128 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007129 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007130 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007131 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007132 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007133 break;
7134 }
7135 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007136}
7137
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007138void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007139 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007140 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007141 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007142 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007143 cls,
7144 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007145 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007146 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007147 return;
7148 }
Vladimir Marko41559982017-01-06 14:04:23 +00007149 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007150
Artem Serovd4cc5b22016-11-04 11:19:09 +00007151 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7152 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007153 ? LocationSummary::kCallOnSlowPath
7154 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007155 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007156 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007157 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007158 }
7159
Vladimir Marko41559982017-01-06 14:04:23 +00007160 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007161 locations->SetInAt(0, Location::RequiresRegister());
7162 }
7163 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007164 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7165 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7166 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007167 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007168 } else {
7169 // For non-Baker read barrier we have a temp-clobbering call.
7170 }
7171 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007172}
7173
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007174// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7175// move.
7176void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007177 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007178 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007179 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007180 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007181 return;
7182 }
Vladimir Marko41559982017-01-06 14:04:23 +00007183 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007184
Vladimir Marko41559982017-01-06 14:04:23 +00007185 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007186 Location out_loc = locations->Out();
7187 vixl32::Register out = OutputRegister(cls);
7188
Artem Serovd4cc5b22016-11-04 11:19:09 +00007189 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7190 ? kWithoutReadBarrier
7191 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007192 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007193 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007194 case HLoadClass::LoadKind::kReferrersClass: {
7195 DCHECK(!cls->CanCallRuntime());
7196 DCHECK(!cls->MustGenerateClinitCheck());
7197 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7198 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007199 codegen_->GenerateGcRootFieldLoad(cls,
7200 out_loc,
7201 current_method,
7202 ArtMethod::DeclaringClassOffset().Int32Value(),
7203 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007204 break;
7205 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007206 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007207 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7208 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007209 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7210 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007211 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007212 codegen_->EmitMovwMovtPlaceholder(labels, out);
7213 break;
7214 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007215 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007216 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7217 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007218 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007219 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007220 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007221 break;
7222 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007223 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007224 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007225 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007226 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007227 // All aligned loads are implicitly atomic consume operations on ARM.
Andreas Gampe3db70682018-12-26 15:12:03 -08007228 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007229 generate_null_check = true;
7230 break;
7231 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007232 case HLoadClass::LoadKind::kJitBootImageAddress: {
7233 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7234 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7235 DCHECK_NE(address, 0u);
7236 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7237 break;
7238 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007239 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007240 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7241 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007242 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007243 // /* GcRoot<mirror::Class> */ out = *out
Andreas Gampe3db70682018-12-26 15:12:03 -08007244 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007245 break;
7246 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007247 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007248 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007249 LOG(FATAL) << "UNREACHABLE";
7250 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007251 }
7252
7253 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7254 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007255 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007256 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007257 codegen_->AddSlowPath(slow_path);
7258 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007259 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007260 }
7261 if (cls->MustGenerateClinitCheck()) {
7262 GenerateClassInitializationCheck(slow_path, out);
7263 } else {
7264 __ Bind(slow_path->GetExitLabel());
7265 }
Andreas Gampe3db70682018-12-26 15:12:03 -08007266 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007267 }
7268}
7269
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007270void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7271 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7272 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7273 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7274}
7275
7276void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7277 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7278}
7279
Orion Hodson18259d72018-04-12 11:18:23 +01007280void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7281 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7282 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7283 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7284}
7285
7286void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7287 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7288}
7289
Artem Serov02d37832016-10-25 15:25:33 +01007290void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7291 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007292 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007293 locations->SetInAt(0, Location::RequiresRegister());
7294 if (check->HasUses()) {
7295 locations->SetOut(Location::SameAsFirstInput());
7296 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007297 // Rely on the type initialization to save everything we need.
7298 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007299}
7300
7301void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7302 // We assume the class is not null.
7303 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007304 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007305 codegen_->AddSlowPath(slow_path);
7306 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7307}
7308
7309void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7310 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7311 UseScratchRegisterScope temps(GetVIXLAssembler());
7312 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007313 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Markobf121912019-06-04 13:49:05 +01007314 constexpr uint32_t shifted_visibly_initialized_value =
7315 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << status_lsb_position;
Vladimir Markodc682aa2018-01-04 18:42:57 +00007316
Vladimir Markobf121912019-06-04 13:49:05 +01007317 const size_t status_offset = mirror::Class::StatusOffset().SizeValue();
7318 GetAssembler()->LoadFromOffset(kLoadWord, temp, class_reg, status_offset);
7319 __ Cmp(temp, shifted_visibly_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007320 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007321 __ Bind(slow_path->GetExitLabel());
7322}
7323
Vladimir Marko175e7862018-03-27 09:03:13 +00007324void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7325 HTypeCheckInstruction* check,
7326 vixl32::Register temp,
7327 vixl32::FlagsUpdate flags_update) {
7328 uint32_t path_to_root = check->GetBitstringPathToRoot();
7329 uint32_t mask = check->GetBitstringMask();
7330 DCHECK(IsPowerOfTwo(mask + 1));
7331 size_t mask_bits = WhichPowerOf2(mask + 1);
7332
7333 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7334 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7335 if (mask_bits == 16u) {
7336 // Load only the bitstring part of the status word.
7337 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7338 // Check if the bitstring bits are equal to `path_to_root`.
7339 if (flags_update == SetFlags) {
7340 __ Cmp(temp, path_to_root);
7341 } else {
7342 __ Sub(temp, temp, path_to_root);
7343 }
7344 } else {
7345 // /* uint32_t */ temp = temp->status_
7346 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7347 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7348 // Compare the bitstring bits using SUB.
7349 __ Sub(temp, temp, path_to_root);
7350 // Shift out bits that do not contribute to the comparison.
7351 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7352 } else if (IsUint<16>(path_to_root)) {
7353 if (temp.IsLow()) {
7354 // Note: Optimized for size but contains one more dependent instruction than necessary.
7355 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7356 // macro assembler would use the high reg IP for the constant by default.
7357 // Compare the bitstring bits using SUB.
7358 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7359 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7360 // Shift out bits that do not contribute to the comparison.
7361 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7362 } else {
7363 // Extract the bitstring bits.
7364 __ Ubfx(temp, temp, 0, mask_bits);
7365 // Check if the bitstring bits are equal to `path_to_root`.
7366 if (flags_update == SetFlags) {
7367 __ Cmp(temp, path_to_root);
7368 } else {
7369 __ Sub(temp, temp, path_to_root);
7370 }
7371 }
7372 } else {
7373 // Shift out bits that do not contribute to the comparison.
7374 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7375 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7376 if (flags_update == SetFlags) {
7377 __ Cmp(temp, path_to_root << (32u - mask_bits));
7378 } else {
7379 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7380 }
7381 }
7382 }
7383}
7384
Artem Serov02d37832016-10-25 15:25:33 +01007385HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007386 HLoadString::LoadKind desired_string_load_kind) {
7387 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007388 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007389 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007390 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007391 DCHECK(!GetCompilerOptions().IsJitCompiler());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007392 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007393 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007394 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007395 DCHECK(GetCompilerOptions().IsJitCompiler());
Artem Serovc5fcb442016-12-02 19:19:58 +00007396 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007397 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007398 break;
7399 }
7400 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007401}
7402
7403void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007404 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007405 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007406 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007407 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007408 locations->SetOut(LocationFrom(r0));
7409 } else {
7410 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007411 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7412 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007413 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007414 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007415 } else {
7416 // For non-Baker read barrier we have a temp-clobbering call.
7417 }
7418 }
Artem Serov02d37832016-10-25 15:25:33 +01007419 }
7420}
7421
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007422// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7423// move.
7424void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007425 LocationSummary* locations = load->GetLocations();
7426 Location out_loc = locations->Out();
7427 vixl32::Register out = OutputRegister(load);
7428 HLoadString::LoadKind load_kind = load->GetLoadKind();
7429
7430 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007431 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007432 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7433 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007434 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007435 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007436 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007437 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007438 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007439 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007440 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7441 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007442 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007443 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007444 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007445 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007446 }
7447 case HLoadString::LoadKind::kBssEntry: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007448 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007449 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007450 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007451 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007452 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007453 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007454 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007455 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007456 codegen_->AddSlowPath(slow_path);
7457 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7458 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08007459 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 16);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007460 return;
7461 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007462 case HLoadString::LoadKind::kJitBootImageAddress: {
7463 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7464 DCHECK_NE(address, 0u);
7465 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7466 return;
7467 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007468 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007469 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007470 load->GetStringIndex(),
7471 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007472 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007473 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007474 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007475 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007476 }
7477 default:
7478 break;
7479 }
Artem Serov02d37832016-10-25 15:25:33 +01007480
7481 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007482 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007483 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007484 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007485 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7486 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08007487 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 17);
Artem Serov02d37832016-10-25 15:25:33 +01007488}
7489
7490static int32_t GetExceptionTlsOffset() {
7491 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7492}
7493
7494void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7495 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007496 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007497 locations->SetOut(Location::RequiresRegister());
7498}
7499
7500void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7501 vixl32::Register out = OutputRegister(load);
7502 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7503}
7504
7505
7506void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007507 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007508}
7509
7510void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7511 UseScratchRegisterScope temps(GetVIXLAssembler());
7512 vixl32::Register temp = temps.Acquire();
7513 __ Mov(temp, 0);
7514 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7515}
7516
7517void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007518 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7519 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007520 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7521 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7522}
7523
7524void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7525 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7526 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7527}
7528
Artem Serov657022c2016-11-23 14:19:38 +00007529// Temp is used for read barrier.
7530static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7531 if (kEmitCompilerReadBarrier &&
7532 (kUseBakerReadBarrier ||
7533 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7534 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7535 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7536 return 1;
7537 }
7538 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007539}
7540
Artem Serov657022c2016-11-23 14:19:38 +00007541// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7542// interface pointer, one for loading the current interface.
7543// The other checks have one temp for loading the object's class.
7544static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7545 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7546 return 3;
7547 }
7548 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7549}
Artem Serovcfbe9132016-10-14 15:58:56 +01007550
7551void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7552 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7553 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7554 bool baker_read_barrier_slow_path = false;
7555 switch (type_check_kind) {
7556 case TypeCheckKind::kExactCheck:
7557 case TypeCheckKind::kAbstractClassCheck:
7558 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007559 case TypeCheckKind::kArrayObjectCheck: {
7560 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7561 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7562 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007563 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007564 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007565 case TypeCheckKind::kArrayCheck:
7566 case TypeCheckKind::kUnresolvedCheck:
7567 case TypeCheckKind::kInterfaceCheck:
7568 call_kind = LocationSummary::kCallOnSlowPath;
7569 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007570 case TypeCheckKind::kBitstringCheck:
7571 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007572 }
7573
Vladimir Markoca6fff82017-10-03 14:49:14 +01007574 LocationSummary* locations =
7575 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007576 if (baker_read_barrier_slow_path) {
7577 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7578 }
7579 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007580 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7581 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7582 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7583 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7584 } else {
7585 locations->SetInAt(1, Location::RequiresRegister());
7586 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007587 // The "out" register is used as a temporary, so it overlaps with the inputs.
7588 // Note that TypeCheckSlowPathARM uses this register too.
7589 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007590 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007591}
7592
7593void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7594 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7595 LocationSummary* locations = instruction->GetLocations();
7596 Location obj_loc = locations->InAt(0);
7597 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007598 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7599 ? vixl32::Register()
7600 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007601 Location out_loc = locations->Out();
7602 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007603 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7604 DCHECK_LE(num_temps, 1u);
7605 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007606 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7607 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7608 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7609 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007610 vixl32::Label done;
7611 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007612 SlowPathCodeARMVIXL* slow_path = nullptr;
7613
7614 // Return 0 if `obj` is null.
7615 // avoid null check if we know obj is not null.
7616 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007617 DCHECK(!out.Is(obj));
7618 __ Mov(out, 0);
Andreas Gampe3db70682018-12-26 15:12:03 -08007619 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007620 }
7621
Artem Serovcfbe9132016-10-14 15:58:56 +01007622 switch (type_check_kind) {
7623 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007624 ReadBarrierOption read_barrier_option =
7625 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007626 // /* HeapReference<Class> */ out = obj->klass_
7627 GenerateReferenceLoadTwoRegisters(instruction,
7628 out_loc,
7629 obj_loc,
7630 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007631 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007632 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007633 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007634 __ Cmp(out, cls);
7635 // We speculatively set the result to false without changing the condition
7636 // flags, which allows us to avoid some branching later.
7637 __ Mov(LeaveFlags, out, 0);
7638
7639 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7640 // we check that the output is in a low register, so that a 16-bit MOV
7641 // encoding can be used.
7642 if (out.IsLow()) {
7643 // We use the scope because of the IT block that follows.
7644 ExactAssemblyScope guard(GetVIXLAssembler(),
7645 2 * vixl32::k16BitT32InstructionSizeInBytes,
7646 CodeBufferCheckScope::kExactSize);
7647
7648 __ it(eq);
7649 __ mov(eq, out, 1);
7650 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007651 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007652 __ Mov(out, 1);
7653 }
7654
Artem Serovcfbe9132016-10-14 15:58:56 +01007655 break;
7656 }
7657
7658 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007659 ReadBarrierOption read_barrier_option =
7660 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007661 // /* HeapReference<Class> */ out = obj->klass_
7662 GenerateReferenceLoadTwoRegisters(instruction,
7663 out_loc,
7664 obj_loc,
7665 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007666 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007667 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007668 // If the class is abstract, we eagerly fetch the super class of the
7669 // object to avoid doing a comparison we know will fail.
7670 vixl32::Label loop;
7671 __ Bind(&loop);
7672 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007673 GenerateReferenceLoadOneRegister(instruction,
7674 out_loc,
7675 super_offset,
7676 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007677 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007678 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007679 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007680 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007681 __ B(ne, &loop, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007682 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007683 break;
7684 }
7685
7686 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007687 ReadBarrierOption read_barrier_option =
7688 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007689 // /* HeapReference<Class> */ out = obj->klass_
7690 GenerateReferenceLoadTwoRegisters(instruction,
7691 out_loc,
7692 obj_loc,
7693 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007694 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007695 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007696 // Walk over the class hierarchy to find a match.
7697 vixl32::Label loop, success;
7698 __ Bind(&loop);
7699 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007700 __ B(eq, &success, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007701 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007702 GenerateReferenceLoadOneRegister(instruction,
7703 out_loc,
7704 super_offset,
7705 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007706 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007707 // This is essentially a null check, but it sets the condition flags to the
7708 // proper value for the code that follows the loop, i.e. not `eq`.
7709 __ Cmp(out, 1);
Andreas Gampe3db70682018-12-26 15:12:03 -08007710 __ B(hs, &loop, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007711
7712 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7713 // we check that the output is in a low register, so that a 16-bit MOV
7714 // encoding can be used.
7715 if (out.IsLow()) {
7716 // If `out` is null, we use it for the result, and the condition flags
7717 // have already been set to `ne`, so the IT block that comes afterwards
7718 // (and which handles the successful case) turns into a NOP (instead of
7719 // overwriting `out`).
7720 __ Bind(&success);
7721
7722 // We use the scope because of the IT block that follows.
7723 ExactAssemblyScope guard(GetVIXLAssembler(),
7724 2 * vixl32::k16BitT32InstructionSizeInBytes,
7725 CodeBufferCheckScope::kExactSize);
7726
7727 // There is only one branch to the `success` label (which is bound to this
7728 // IT block), and it has the same condition, `eq`, so in that case the MOV
7729 // is executed.
7730 __ it(eq);
7731 __ mov(eq, out, 1);
7732 } else {
7733 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007734 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007735 __ Bind(&success);
7736 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007737 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007738
Artem Serovcfbe9132016-10-14 15:58:56 +01007739 break;
7740 }
7741
7742 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007743 ReadBarrierOption read_barrier_option =
7744 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007745 // /* HeapReference<Class> */ out = obj->klass_
7746 GenerateReferenceLoadTwoRegisters(instruction,
7747 out_loc,
7748 obj_loc,
7749 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007750 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007751 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007752 // Do an exact check.
7753 vixl32::Label exact_check;
7754 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007755 __ B(eq, &exact_check, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007756 // Otherwise, we need to check that the object's class is a non-primitive array.
7757 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007758 GenerateReferenceLoadOneRegister(instruction,
7759 out_loc,
7760 component_offset,
7761 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007762 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007763 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007764 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007765 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7766 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007767 __ Cmp(out, 0);
7768 // We speculatively set the result to false without changing the condition
7769 // flags, which allows us to avoid some branching later.
7770 __ Mov(LeaveFlags, out, 0);
7771
7772 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7773 // we check that the output is in a low register, so that a 16-bit MOV
7774 // encoding can be used.
7775 if (out.IsLow()) {
7776 __ Bind(&exact_check);
7777
7778 // We use the scope because of the IT block that follows.
7779 ExactAssemblyScope guard(GetVIXLAssembler(),
7780 2 * vixl32::k16BitT32InstructionSizeInBytes,
7781 CodeBufferCheckScope::kExactSize);
7782
7783 __ it(eq);
7784 __ mov(eq, out, 1);
7785 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007786 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007787 __ Bind(&exact_check);
7788 __ Mov(out, 1);
7789 }
7790
Artem Serovcfbe9132016-10-14 15:58:56 +01007791 break;
7792 }
7793
7794 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007795 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007796 // /* HeapReference<Class> */ out = obj->klass_
7797 GenerateReferenceLoadTwoRegisters(instruction,
7798 out_loc,
7799 obj_loc,
7800 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007801 maybe_temp_loc,
7802 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007803 __ Cmp(out, cls);
7804 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007805 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08007806 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007807 codegen_->AddSlowPath(slow_path);
7808 __ B(ne, slow_path->GetEntryLabel());
7809 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007810 break;
7811 }
7812
7813 case TypeCheckKind::kUnresolvedCheck:
7814 case TypeCheckKind::kInterfaceCheck: {
7815 // Note that we indeed only call on slow path, but we always go
7816 // into the slow path for the unresolved and interface check
7817 // cases.
7818 //
7819 // We cannot directly call the InstanceofNonTrivial runtime
7820 // entry point without resorting to a type checking slow path
7821 // here (i.e. by calling InvokeRuntime directly), as it would
7822 // require to assign fixed registers for the inputs of this
7823 // HInstanceOf instruction (following the runtime calling
7824 // convention), which might be cluttered by the potential first
7825 // read barrier emission at the beginning of this method.
7826 //
7827 // TODO: Introduce a new runtime entry point taking the object
7828 // to test (instead of its class) as argument, and let it deal
7829 // with the read barrier issues. This will let us refactor this
7830 // case of the `switch` code as it was previously (with a direct
7831 // call to the runtime not using a type checking slow path).
7832 // This should also be beneficial for the other cases above.
7833 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007834 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08007835 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007836 codegen_->AddSlowPath(slow_path);
7837 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007838 break;
7839 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007840
7841 case TypeCheckKind::kBitstringCheck: {
7842 // /* HeapReference<Class> */ temp = obj->klass_
7843 GenerateReferenceLoadTwoRegisters(instruction,
7844 out_loc,
7845 obj_loc,
7846 class_offset,
7847 maybe_temp_loc,
7848 kWithoutReadBarrier);
7849
7850 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
7851 // If `out` is a low reg and we would have another low reg temp, we could
7852 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
7853 //
7854 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
7855 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
7856 // would be the same and we would have fewer direct data dependencies.
7857 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
7858 break;
7859 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007860 }
7861
Artem Serovcfbe9132016-10-14 15:58:56 +01007862 if (done.IsReferenced()) {
7863 __ Bind(&done);
7864 }
7865
7866 if (slow_path != nullptr) {
7867 __ Bind(slow_path->GetExitLabel());
7868 }
7869}
7870
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007871void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007872 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007873 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007874 LocationSummary* locations =
7875 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007876 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007877 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7878 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7879 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7880 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7881 } else {
7882 locations->SetInAt(1, Location::RequiresRegister());
7883 }
Artem Serov657022c2016-11-23 14:19:38 +00007884 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007885}
7886
7887void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7888 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7889 LocationSummary* locations = instruction->GetLocations();
7890 Location obj_loc = locations->InAt(0);
7891 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007892 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7893 ? vixl32::Register()
7894 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007895 Location temp_loc = locations->GetTemp(0);
7896 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007897 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7898 DCHECK_LE(num_temps, 3u);
7899 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7900 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7901 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7902 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7903 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7904 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7905 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7906 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7907 const uint32_t object_array_data_offset =
7908 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007909
Vladimir Marko87584542017-12-12 17:47:52 +00007910 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007911 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007912 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7913 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007914 codegen_->AddSlowPath(type_check_slow_path);
7915
7916 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007917 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007918 // Avoid null check if we know obj is not null.
7919 if (instruction->MustDoNullCheck()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08007920 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007921 }
7922
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007923 switch (type_check_kind) {
7924 case TypeCheckKind::kExactCheck:
7925 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007926 // /* HeapReference<Class> */ temp = obj->klass_
7927 GenerateReferenceLoadTwoRegisters(instruction,
7928 temp_loc,
7929 obj_loc,
7930 class_offset,
7931 maybe_temp2_loc,
7932 kWithoutReadBarrier);
7933
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007934 __ Cmp(temp, cls);
7935 // Jump to slow path for throwing the exception or doing a
7936 // more involved array check.
7937 __ B(ne, type_check_slow_path->GetEntryLabel());
7938 break;
7939 }
7940
7941 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007942 // /* HeapReference<Class> */ temp = obj->klass_
7943 GenerateReferenceLoadTwoRegisters(instruction,
7944 temp_loc,
7945 obj_loc,
7946 class_offset,
7947 maybe_temp2_loc,
7948 kWithoutReadBarrier);
7949
Artem Serovcfbe9132016-10-14 15:58:56 +01007950 // If the class is abstract, we eagerly fetch the super class of the
7951 // object to avoid doing a comparison we know will fail.
7952 vixl32::Label loop;
7953 __ Bind(&loop);
7954 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007955 GenerateReferenceLoadOneRegister(instruction,
7956 temp_loc,
7957 super_offset,
7958 maybe_temp2_loc,
7959 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007960
7961 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7962 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007963 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007964
7965 // Otherwise, compare the classes.
7966 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007967 __ B(ne, &loop, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007968 break;
7969 }
7970
7971 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007972 // /* HeapReference<Class> */ temp = obj->klass_
7973 GenerateReferenceLoadTwoRegisters(instruction,
7974 temp_loc,
7975 obj_loc,
7976 class_offset,
7977 maybe_temp2_loc,
7978 kWithoutReadBarrier);
7979
Artem Serovcfbe9132016-10-14 15:58:56 +01007980 // Walk over the class hierarchy to find a match.
7981 vixl32::Label loop;
7982 __ Bind(&loop);
7983 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007984 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007985
7986 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007987 GenerateReferenceLoadOneRegister(instruction,
7988 temp_loc,
7989 super_offset,
7990 maybe_temp2_loc,
7991 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007992
7993 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7994 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007995 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007996 // Otherwise, jump to the beginning of the loop.
7997 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007998 break;
7999 }
8000
Artem Serovcfbe9132016-10-14 15:58:56 +01008001 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00008002 // /* HeapReference<Class> */ temp = obj->klass_
8003 GenerateReferenceLoadTwoRegisters(instruction,
8004 temp_loc,
8005 obj_loc,
8006 class_offset,
8007 maybe_temp2_loc,
8008 kWithoutReadBarrier);
8009
Artem Serovcfbe9132016-10-14 15:58:56 +01008010 // Do an exact check.
8011 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08008012 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01008013
8014 // Otherwise, we need to check that the object's class is a non-primitive array.
8015 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00008016 GenerateReferenceLoadOneRegister(instruction,
8017 temp_loc,
8018 component_offset,
8019 maybe_temp2_loc,
8020 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01008021 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00008022 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01008023 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
8024 // to further check that this component type is not a primitive type.
8025 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008026 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00008027 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008028 break;
8029 }
8030
8031 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00008032 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01008033 // We cannot directly call the CheckCast runtime entry point
8034 // without resorting to a type checking slow path here (i.e. by
8035 // calling InvokeRuntime directly), as it would require to
8036 // assign fixed registers for the inputs of this HInstanceOf
8037 // instruction (following the runtime calling convention), which
8038 // might be cluttered by the potential first read barrier
8039 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00008040
Artem Serovcfbe9132016-10-14 15:58:56 +01008041 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008042 break;
Artem Serov657022c2016-11-23 14:19:38 +00008043
8044 case TypeCheckKind::kInterfaceCheck: {
8045 // Avoid read barriers to improve performance of the fast path. We can not get false
8046 // positives by doing this.
8047 // /* HeapReference<Class> */ temp = obj->klass_
8048 GenerateReferenceLoadTwoRegisters(instruction,
8049 temp_loc,
8050 obj_loc,
8051 class_offset,
8052 maybe_temp2_loc,
8053 kWithoutReadBarrier);
8054
8055 // /* HeapReference<Class> */ temp = temp->iftable_
8056 GenerateReferenceLoadTwoRegisters(instruction,
8057 temp_loc,
8058 temp_loc,
8059 iftable_offset,
8060 maybe_temp2_loc,
8061 kWithoutReadBarrier);
8062 // Iftable is never null.
8063 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8064 // Loop through the iftable and check if any class matches.
8065 vixl32::Label start_loop;
8066 __ Bind(&start_loop);
8067 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8068 type_check_slow_path->GetEntryLabel());
8069 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8070 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8071 // Go to next interface.
8072 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8073 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8074 // Compare the classes and continue the loop if they do not match.
8075 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Andreas Gampe3db70682018-12-26 15:12:03 -08008076 __ B(ne, &start_loop, /* is_far_target= */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008077 break;
8078 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008079
8080 case TypeCheckKind::kBitstringCheck: {
8081 // /* HeapReference<Class> */ temp = obj->klass_
8082 GenerateReferenceLoadTwoRegisters(instruction,
8083 temp_loc,
8084 obj_loc,
8085 class_offset,
8086 maybe_temp2_loc,
8087 kWithoutReadBarrier);
8088
8089 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8090 __ B(ne, type_check_slow_path->GetEntryLabel());
8091 break;
8092 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008093 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008094 if (done.IsReferenced()) {
8095 __ Bind(&done);
8096 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008097
8098 __ Bind(type_check_slow_path->GetExitLabel());
8099}
8100
Artem Serov551b28f2016-10-18 19:11:30 +01008101void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008102 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8103 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008104 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8105 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8106}
8107
8108void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8109 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8110 instruction,
8111 instruction->GetDexPc());
8112 if (instruction->IsEnter()) {
8113 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8114 } else {
8115 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8116 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008117 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 18);
Artem Serov551b28f2016-10-18 19:11:30 +01008118}
8119
Artem Serov02109dd2016-09-23 17:17:54 +01008120void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8121 HandleBitwiseOperation(instruction, AND);
8122}
8123
8124void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8125 HandleBitwiseOperation(instruction, ORR);
8126}
8127
8128void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8129 HandleBitwiseOperation(instruction, EOR);
8130}
8131
8132void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8133 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008134 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008135 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8136 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008137 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8138 locations->SetInAt(0, Location::RequiresRegister());
8139 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8140 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8141}
8142
8143void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8144 HandleBitwiseOperation(instruction);
8145}
8146
8147void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8148 HandleBitwiseOperation(instruction);
8149}
8150
8151void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8152 HandleBitwiseOperation(instruction);
8153}
8154
Artem Serov2bbc9532016-10-21 11:51:50 +01008155void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8156 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008157 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008158 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8159 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008160
8161 locations->SetInAt(0, Location::RequiresRegister());
8162 locations->SetInAt(1, Location::RequiresRegister());
8163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8164}
8165
8166void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8167 LocationSummary* locations = instruction->GetLocations();
8168 Location first = locations->InAt(0);
8169 Location second = locations->InAt(1);
8170 Location out = locations->Out();
8171
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008172 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008173 vixl32::Register first_reg = RegisterFrom(first);
8174 vixl32::Register second_reg = RegisterFrom(second);
8175 vixl32::Register out_reg = RegisterFrom(out);
8176
8177 switch (instruction->GetOpKind()) {
8178 case HInstruction::kAnd:
8179 __ Bic(out_reg, first_reg, second_reg);
8180 break;
8181 case HInstruction::kOr:
8182 __ Orn(out_reg, first_reg, second_reg);
8183 break;
8184 // There is no EON on arm.
8185 case HInstruction::kXor:
8186 default:
8187 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8188 UNREACHABLE();
8189 }
8190 return;
8191
8192 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008193 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008194 vixl32::Register first_low = LowRegisterFrom(first);
8195 vixl32::Register first_high = HighRegisterFrom(first);
8196 vixl32::Register second_low = LowRegisterFrom(second);
8197 vixl32::Register second_high = HighRegisterFrom(second);
8198 vixl32::Register out_low = LowRegisterFrom(out);
8199 vixl32::Register out_high = HighRegisterFrom(out);
8200
8201 switch (instruction->GetOpKind()) {
8202 case HInstruction::kAnd:
8203 __ Bic(out_low, first_low, second_low);
8204 __ Bic(out_high, first_high, second_high);
8205 break;
8206 case HInstruction::kOr:
8207 __ Orn(out_low, first_low, second_low);
8208 __ Orn(out_high, first_high, second_high);
8209 break;
8210 // There is no EON on arm.
8211 case HInstruction::kXor:
8212 default:
8213 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8214 UNREACHABLE();
8215 }
8216 }
8217}
8218
Anton Kirilov74234da2017-01-13 14:42:47 +00008219void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8220 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008221 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8222 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008223 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008224 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008225 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008226 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8227
8228 locations->SetInAt(0, Location::RequiresRegister());
8229 locations->SetInAt(1, Location::RequiresRegister());
8230 locations->SetOut(Location::RequiresRegister(),
8231 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8232}
8233
8234void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8235 HDataProcWithShifterOp* instruction) {
8236 const LocationSummary* const locations = instruction->GetLocations();
8237 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8238 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008240 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008241 const vixl32::Register first = InputRegisterAt(instruction, 0);
8242 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008243 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008244 ? LowRegisterFrom(locations->InAt(1))
8245 : InputRegisterAt(instruction, 1);
8246
Anton Kirilov420ee302017-02-21 18:10:26 +00008247 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8248 DCHECK_EQ(kind, HInstruction::kAdd);
8249
8250 switch (op_kind) {
8251 case HDataProcWithShifterOp::kUXTB:
8252 __ Uxtab(output, first, second);
8253 break;
8254 case HDataProcWithShifterOp::kUXTH:
8255 __ Uxtah(output, first, second);
8256 break;
8257 case HDataProcWithShifterOp::kSXTB:
8258 __ Sxtab(output, first, second);
8259 break;
8260 case HDataProcWithShifterOp::kSXTH:
8261 __ Sxtah(output, first, second);
8262 break;
8263 default:
8264 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8265 UNREACHABLE();
8266 }
8267 } else {
8268 GenerateDataProcInstruction(kind,
8269 output,
8270 first,
8271 Operand(second,
8272 ShiftFromOpKind(op_kind),
8273 instruction->GetShiftAmount()),
8274 codegen_);
8275 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008276 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008277 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008278
8279 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8280 const vixl32::Register second = InputRegisterAt(instruction, 1);
8281
8282 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8283 GenerateDataProc(kind,
8284 locations->Out(),
8285 locations->InAt(0),
8286 second,
8287 Operand(second, ShiftType::ASR, 31),
8288 codegen_);
8289 } else {
8290 GenerateLongDataProc(instruction, codegen_);
8291 }
8292 }
8293}
8294
Artem Serov02109dd2016-09-23 17:17:54 +01008295// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8296void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8297 vixl32::Register first,
8298 uint32_t value) {
8299 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8300 if (value == 0xffffffffu) {
8301 if (!out.Is(first)) {
8302 __ Mov(out, first);
8303 }
8304 return;
8305 }
8306 if (value == 0u) {
8307 __ Mov(out, 0);
8308 return;
8309 }
8310 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008311 __ And(out, first, value);
8312 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8313 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008314 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008315 DCHECK(IsPowerOfTwo(value + 1));
8316 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008317 }
8318}
8319
8320// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8321void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8322 vixl32::Register first,
8323 uint32_t value) {
8324 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8325 if (value == 0u) {
8326 if (!out.Is(first)) {
8327 __ Mov(out, first);
8328 }
8329 return;
8330 }
8331 if (value == 0xffffffffu) {
8332 __ Mvn(out, 0);
8333 return;
8334 }
8335 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8336 __ Orr(out, first, value);
8337 } else {
8338 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8339 __ Orn(out, first, ~value);
8340 }
8341}
8342
8343// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8344void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8345 vixl32::Register first,
8346 uint32_t value) {
8347 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8348 if (value == 0u) {
8349 if (!out.Is(first)) {
8350 __ Mov(out, first);
8351 }
8352 return;
8353 }
8354 __ Eor(out, first, value);
8355}
8356
Anton Kirilovdda43962016-11-21 19:55:20 +00008357void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8358 Location first,
8359 uint64_t value) {
8360 vixl32::Register out_low = LowRegisterFrom(out);
8361 vixl32::Register out_high = HighRegisterFrom(out);
8362 vixl32::Register first_low = LowRegisterFrom(first);
8363 vixl32::Register first_high = HighRegisterFrom(first);
8364 uint32_t value_low = Low32Bits(value);
8365 uint32_t value_high = High32Bits(value);
8366 if (value_low == 0u) {
8367 if (!out_low.Is(first_low)) {
8368 __ Mov(out_low, first_low);
8369 }
8370 __ Add(out_high, first_high, value_high);
8371 return;
8372 }
8373 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008374 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008375 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008376 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008377 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8378 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008379 }
8380}
8381
Artem Serov02109dd2016-09-23 17:17:54 +01008382void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8383 LocationSummary* locations = instruction->GetLocations();
8384 Location first = locations->InAt(0);
8385 Location second = locations->InAt(1);
8386 Location out = locations->Out();
8387
8388 if (second.IsConstant()) {
8389 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8390 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008391 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008392 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8393 vixl32::Register out_reg = OutputRegister(instruction);
8394 if (instruction->IsAnd()) {
8395 GenerateAndConst(out_reg, first_reg, value_low);
8396 } else if (instruction->IsOr()) {
8397 GenerateOrrConst(out_reg, first_reg, value_low);
8398 } else {
8399 DCHECK(instruction->IsXor());
8400 GenerateEorConst(out_reg, first_reg, value_low);
8401 }
8402 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008403 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008404 uint32_t value_high = High32Bits(value);
8405 vixl32::Register first_low = LowRegisterFrom(first);
8406 vixl32::Register first_high = HighRegisterFrom(first);
8407 vixl32::Register out_low = LowRegisterFrom(out);
8408 vixl32::Register out_high = HighRegisterFrom(out);
8409 if (instruction->IsAnd()) {
8410 GenerateAndConst(out_low, first_low, value_low);
8411 GenerateAndConst(out_high, first_high, value_high);
8412 } else if (instruction->IsOr()) {
8413 GenerateOrrConst(out_low, first_low, value_low);
8414 GenerateOrrConst(out_high, first_high, value_high);
8415 } else {
8416 DCHECK(instruction->IsXor());
8417 GenerateEorConst(out_low, first_low, value_low);
8418 GenerateEorConst(out_high, first_high, value_high);
8419 }
8420 }
8421 return;
8422 }
8423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008424 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008425 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8426 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8427 vixl32::Register out_reg = OutputRegister(instruction);
8428 if (instruction->IsAnd()) {
8429 __ And(out_reg, first_reg, second_reg);
8430 } else if (instruction->IsOr()) {
8431 __ Orr(out_reg, first_reg, second_reg);
8432 } else {
8433 DCHECK(instruction->IsXor());
8434 __ Eor(out_reg, first_reg, second_reg);
8435 }
8436 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008437 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008438 vixl32::Register first_low = LowRegisterFrom(first);
8439 vixl32::Register first_high = HighRegisterFrom(first);
8440 vixl32::Register second_low = LowRegisterFrom(second);
8441 vixl32::Register second_high = HighRegisterFrom(second);
8442 vixl32::Register out_low = LowRegisterFrom(out);
8443 vixl32::Register out_high = HighRegisterFrom(out);
8444 if (instruction->IsAnd()) {
8445 __ And(out_low, first_low, second_low);
8446 __ And(out_high, first_high, second_high);
8447 } else if (instruction->IsOr()) {
8448 __ Orr(out_low, first_low, second_low);
8449 __ Orr(out_high, first_high, second_high);
8450 } else {
8451 DCHECK(instruction->IsXor());
8452 __ Eor(out_low, first_low, second_low);
8453 __ Eor(out_high, first_high, second_high);
8454 }
8455 }
8456}
8457
Artem Serovcfbe9132016-10-14 15:58:56 +01008458void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008459 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008460 Location out,
8461 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008462 Location maybe_temp,
8463 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008464 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008465 if (read_barrier_option == kWithReadBarrier) {
8466 CHECK(kEmitCompilerReadBarrier);
8467 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8468 if (kUseBakerReadBarrier) {
8469 // Load with fast path based Baker's read barrier.
8470 // /* HeapReference<Object> */ out = *(out + offset)
8471 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008472 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008473 } else {
8474 // Load with slow path based read barrier.
8475 // Save the value of `out` into `maybe_temp` before overwriting it
8476 // in the following move operation, as we will need it for the
8477 // read barrier below.
8478 __ Mov(RegisterFrom(maybe_temp), out_reg);
8479 // /* HeapReference<Object> */ out = *(out + offset)
8480 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8481 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8482 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008483 } else {
8484 // Plain load with no read barrier.
8485 // /* HeapReference<Object> */ out = *(out + offset)
8486 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8487 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8488 }
8489}
8490
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008491void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008492 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008493 Location out,
8494 Location obj,
8495 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008496 Location maybe_temp,
8497 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008498 vixl32::Register out_reg = RegisterFrom(out);
8499 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008500 if (read_barrier_option == kWithReadBarrier) {
8501 CHECK(kEmitCompilerReadBarrier);
8502 if (kUseBakerReadBarrier) {
8503 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8504 // Load with fast path based Baker's read barrier.
8505 // /* HeapReference<Object> */ out = *(obj + offset)
8506 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008507 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008508 } else {
8509 // Load with slow path based read barrier.
8510 // /* HeapReference<Object> */ out = *(obj + offset)
8511 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8512 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8513 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008514 } else {
8515 // Plain load with no read barrier.
8516 // /* HeapReference<Object> */ out = *(obj + offset)
8517 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8518 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8519 }
8520}
8521
Vladimir Markoca1e0382018-04-11 09:58:41 +00008522void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008523 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008524 Location root,
8525 vixl32::Register obj,
8526 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008527 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008528 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008529 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008530 DCHECK(kEmitCompilerReadBarrier);
8531 if (kUseBakerReadBarrier) {
8532 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008533 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008534
Vladimir Marko008e09f32018-08-06 15:42:43 +01008535 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8536 // the Marking Register) to decide whether we need to enter
8537 // the slow path to mark the GC root.
8538 //
8539 // We use shared thunks for the slow path; shared within the method
8540 // for JIT, across methods for AOT. That thunk checks the reference
8541 // and jumps to the entrypoint if needed.
8542 //
8543 // lr = &return_address;
8544 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8545 // if (mr) { // Thread::Current()->GetIsGcMarking()
8546 // goto gc_root_thunk<root_reg>(lr)
8547 // }
8548 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008549
Vladimir Marko008e09f32018-08-06 15:42:43 +01008550 UseScratchRegisterScope temps(GetVIXLAssembler());
8551 temps.Exclude(ip);
8552 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8553 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008554
Vladimir Markod887ed82018-08-14 13:52:12 +00008555 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8556 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8557 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8558 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8559 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008560 vixl32::Label return_address;
8561 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8562 __ cmp(mr, Operand(0));
8563 // Currently the offset is always within range. If that changes,
8564 // we shall have to split the load the same way as for fields.
8565 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8566 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8567 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8568 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008569 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008570 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8571 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8572 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008573 } else {
8574 // GC root loaded through a slow path for read barriers other
8575 // than Baker's.
8576 // /* GcRoot<mirror::Object>* */ root = obj + offset
8577 __ Add(root_reg, obj, offset);
8578 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008579 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008580 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008581 } else {
8582 // Plain GC root load with no read barrier.
8583 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8584 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8585 // Note that GC roots are not affected by heap poisoning, thus we
8586 // do not have to unpoison `root_reg` here.
8587 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008588 MaybeGenerateMarkingRegisterCheck(/* code= */ 19);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008589}
8590
Vladimir Markod887ed82018-08-14 13:52:12 +00008591void CodeGeneratorARMVIXL::GenerateUnsafeCasOldValueAddWithBakerReadBarrier(
8592 vixl::aarch32::Register old_value,
8593 vixl::aarch32::Register adjusted_old_value,
8594 vixl::aarch32::Register expected) {
8595 DCHECK(kEmitCompilerReadBarrier);
8596 DCHECK(kUseBakerReadBarrier);
8597
8598 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with an ADD instead of LDR.
8599 uint32_t custom_data = EncodeBakerReadBarrierUnsafeCasData(old_value.GetCode());
8600
8601 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8602 size_t wide_instructions = /* ADR+CMP+ADD+BNE */ 4u - narrow_instructions;
8603 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8604 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8605 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8606 vixl32::Label return_address;
8607 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8608 __ cmp(mr, Operand(0));
8609 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8610 __ add(EncodingSize(Wide), old_value, adjusted_old_value, Operand(expected)); // Preserves flags.
8611 EmitBakerReadBarrierBne(custom_data);
8612 __ bind(&return_address);
8613 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8614 BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ADD_OFFSET);
8615}
8616
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008617void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8618 Location ref,
8619 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008620 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008621 bool needs_null_check) {
8622 DCHECK(kEmitCompilerReadBarrier);
8623 DCHECK(kUseBakerReadBarrier);
8624
Vladimir Marko008e09f32018-08-06 15:42:43 +01008625 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8626 // Marking Register) to decide whether we need to enter the slow
8627 // path to mark the reference. Then, in the slow path, check the
8628 // gray bit in the lock word of the reference's holder (`obj`) to
8629 // decide whether to mark `ref` or not.
8630 //
8631 // We use shared thunks for the slow path; shared within the method
8632 // for JIT, across methods for AOT. That thunk checks the holder
8633 // and jumps to the entrypoint if needed. If the holder is not gray,
8634 // it creates a fake dependency and returns to the LDR instruction.
8635 //
8636 // lr = &gray_return_address;
8637 // if (mr) { // Thread::Current()->GetIsGcMarking()
8638 // goto field_thunk<holder_reg, base_reg>(lr)
8639 // }
8640 // not_gray_return_address:
8641 // // Original reference load. If the offset is too large to fit
8642 // // into LDR, we use an adjusted base register here.
8643 // HeapReference<mirror::Object> reference = *(obj+offset);
8644 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008645
Vladimir Marko248141f2018-08-10 10:40:07 +01008646 DCHECK(src.GetAddrMode() == vixl32::Offset);
8647 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008648 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008649 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8650
Vladimir Marko008e09f32018-08-06 15:42:43 +01008651 UseScratchRegisterScope temps(GetVIXLAssembler());
8652 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008653 uint32_t custom_data =
8654 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008655
Vladimir Marko008e09f32018-08-06 15:42:43 +01008656 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008657 size_t narrow_instructions =
8658 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8659 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
8660 size_t wide_instructions =
8661 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8662 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8663 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8664 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008665 vixl32::Label return_address;
8666 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8667 __ cmp(mr, Operand(0));
8668 EmitBakerReadBarrierBne(custom_data);
8669 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01008670 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008671 if (needs_null_check) {
8672 MaybeRecordImplicitNullCheck(instruction);
8673 }
8674 // Note: We need a specific width for the unpoisoning NEG.
8675 if (kPoisonHeapReferences) {
8676 if (narrow) {
8677 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8678 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8679 } else {
8680 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8681 }
8682 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008683 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008684 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8685 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8686 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
8687 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008688 MaybeGenerateMarkingRegisterCheck(/* code= */ 20, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008689}
8690
Vladimir Marko248141f2018-08-10 10:40:07 +01008691void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8692 Location ref,
8693 vixl32::Register obj,
8694 uint32_t offset,
8695 Location temp,
8696 bool needs_null_check) {
8697 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8698 vixl32::Register base = obj;
8699 if (offset >= kReferenceLoadMinFarOffset) {
8700 base = RegisterFrom(temp);
8701 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8702 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8703 offset &= (kReferenceLoadMinFarOffset - 1u);
8704 }
8705 GenerateFieldLoadWithBakerReadBarrier(
8706 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
8707}
8708
Vladimir Marko008e09f32018-08-06 15:42:43 +01008709void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008710 vixl32::Register obj,
8711 uint32_t data_offset,
8712 Location index,
8713 Location temp,
8714 bool needs_null_check) {
8715 DCHECK(kEmitCompilerReadBarrier);
8716 DCHECK(kUseBakerReadBarrier);
8717
8718 static_assert(
8719 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8720 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008721 ScaleFactor scale_factor = TIMES_4;
8722
Vladimir Marko008e09f32018-08-06 15:42:43 +01008723 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8724 // Marking Register) to decide whether we need to enter the slow
8725 // path to mark the reference. Then, in the slow path, check the
8726 // gray bit in the lock word of the reference's holder (`obj`) to
8727 // decide whether to mark `ref` or not.
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 holder
8731 // and jumps to the entrypoint if needed. If the holder is not gray,
8732 // it creates a fake dependency and returns to the LDR instruction.
8733 //
8734 // lr = &gray_return_address;
8735 // if (mr) { // Thread::Current()->GetIsGcMarking()
8736 // goto array_thunk<base_reg>(lr)
8737 // }
8738 // not_gray_return_address:
8739 // // Original reference load. If the offset is too large to fit
8740 // // into LDR, we use an adjusted base register here.
8741 // HeapReference<mirror::Object> reference = data[index];
8742 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008743
Vladimir Marko008e09f32018-08-06 15:42:43 +01008744 DCHECK(index.IsValid());
8745 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8746 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8747 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008748
Vladimir Marko008e09f32018-08-06 15:42:43 +01008749 UseScratchRegisterScope temps(GetVIXLAssembler());
8750 temps.Exclude(ip);
8751 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008752
Vladimir Marko008e09f32018-08-06 15:42:43 +01008753 __ Add(data_reg, obj, Operand(data_offset));
8754 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008755 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8756 size_t wide_instructions =
8757 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8758 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8759 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8760 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008761 vixl32::Label return_address;
8762 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8763 __ cmp(mr, Operand(0));
8764 EmitBakerReadBarrierBne(custom_data);
8765 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8766 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8767 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8768 // Note: We need a Wide NEG for the unpoisoning.
8769 if (kPoisonHeapReferences) {
8770 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008771 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008772 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008773 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8774 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008775 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008776 MaybeGenerateMarkingRegisterCheck(/* code= */ 21, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008777}
8778
Roland Levillain5daa4952017-07-03 17:23:56 +01008779void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8780 // The following condition is a compile-time one, so it does not have a run-time cost.
8781 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8782 // The following condition is a run-time one; it is executed after the
8783 // previous compile-time test, to avoid penalizing non-debug builds.
8784 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8785 UseScratchRegisterScope temps(GetVIXLAssembler());
8786 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8787 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8788 kMarkingRegisterCheckBreakCodeBaseCode + code);
8789 }
8790 }
8791}
8792
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008793void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8794 Location out,
8795 Location ref,
8796 Location obj,
8797 uint32_t offset,
8798 Location index) {
8799 DCHECK(kEmitCompilerReadBarrier);
8800
8801 // Insert a slow path based read barrier *after* the reference load.
8802 //
8803 // If heap poisoning is enabled, the unpoisoning of the loaded
8804 // reference will be carried out by the runtime within the slow
8805 // path.
8806 //
8807 // Note that `ref` currently does not get unpoisoned (when heap
8808 // poisoning is enabled), which is alright as the `ref` argument is
8809 // not used by the artReadBarrierSlow entry point.
8810 //
8811 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008812 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008813 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8814 AddSlowPath(slow_path);
8815
8816 __ B(slow_path->GetEntryLabel());
8817 __ Bind(slow_path->GetExitLabel());
8818}
8819
8820void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008821 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008822 Location ref,
8823 Location obj,
8824 uint32_t offset,
8825 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008826 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008827 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008828 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008829 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008830 // If heap poisoning is enabled, unpoisoning will be taken care of
8831 // by the runtime within the slow path.
8832 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008833 } else if (kPoisonHeapReferences) {
8834 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8835 }
8836}
8837
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008838void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8839 Location out,
8840 Location root) {
8841 DCHECK(kEmitCompilerReadBarrier);
8842
8843 // Insert a slow path based read barrier *after* the GC root load.
8844 //
8845 // Note that GC roots are not affected by heap poisoning, so we do
8846 // not need to do anything special for this here.
8847 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008848 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008849 AddSlowPath(slow_path);
8850
8851 __ B(slow_path->GetEntryLabel());
8852 __ Bind(slow_path->GetExitLabel());
8853}
8854
Artem Serov02d37832016-10-25 15:25:33 +01008855// Check if the desired_dispatch_info is supported. If it is, return it,
8856// otherwise return a fall-back info that should be used instead.
8857HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008858 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01008859 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008860 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008861}
8862
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008863vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8864 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8865 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8866 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8867 if (!invoke->GetLocations()->Intrinsified()) {
8868 return RegisterFrom(location);
8869 }
8870 // For intrinsics we allow any location, so it may be on the stack.
8871 if (!location.IsRegister()) {
8872 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8873 return temp;
8874 }
8875 // For register locations, check if the register was saved. If so, get it from the stack.
8876 // Note: There is a chance that the register was saved but not overwritten, so we could
8877 // save one load. However, since this is just an intrinsic slow path we prefer this
8878 // simple and more robust approach rather that trying to determine if that's the case.
8879 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008880 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008881 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8882 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8883 return temp;
8884 }
8885 return RegisterFrom(location);
8886}
8887
Vladimir Markod254f5c2017-06-02 15:18:36 +00008888void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008889 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008890 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008891 switch (invoke->GetMethodLoadKind()) {
8892 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8893 uint32_t offset =
8894 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8895 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008896 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8897 break;
8898 }
8899 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8900 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8901 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008902 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01008903 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008904 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01008905 vixl32::Register temp_reg = RegisterFrom(temp);
8906 EmitMovwMovtPlaceholder(labels, temp_reg);
8907 break;
8908 }
Vladimir Markob066d432018-01-03 13:14:37 +00008909 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008910 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00008911 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
8912 vixl32::Register temp_reg = RegisterFrom(temp);
8913 EmitMovwMovtPlaceholder(labels, temp_reg);
8914 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
8915 break;
8916 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008917 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8918 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8919 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8920 vixl32::Register temp_reg = RegisterFrom(temp);
8921 EmitMovwMovtPlaceholder(labels, temp_reg);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01008922 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008923 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008924 break;
8925 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008926 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
8927 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8928 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008929 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8930 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8931 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008932 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008933 }
8934
Artem Serovd4cc5b22016-11-04 11:19:09 +00008935 switch (invoke->GetCodePtrLocation()) {
8936 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008937 {
8938 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8939 ExactAssemblyScope aas(GetVIXLAssembler(),
8940 vixl32::k32BitT32InstructionSizeInBytes,
8941 CodeBufferCheckScope::kMaximumSize);
8942 __ bl(GetFrameEntryLabel());
8943 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8944 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008945 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008946 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8947 // LR = callee_method->entry_point_from_quick_compiled_code_
8948 GetAssembler()->LoadFromOffset(
8949 kLoadWord,
8950 lr,
8951 RegisterFrom(callee_method),
8952 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008953 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008954 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008955 // 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 +00008956 ExactAssemblyScope aas(GetVIXLAssembler(),
8957 vixl32::k16BitT32InstructionSizeInBytes,
8958 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008959 // LR()
8960 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008961 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008962 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008963 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008964 }
8965
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008966 DCHECK(!IsLeafMethod());
8967}
8968
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008969void CodeGeneratorARMVIXL::GenerateVirtualCall(
8970 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008971 vixl32::Register temp = RegisterFrom(temp_location);
8972 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8973 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8974
8975 // Use the calling convention instead of the location of the receiver, as
8976 // intrinsics may have put the receiver in a different register. In the intrinsics
8977 // slow path, the arguments have been moved to the right place, so here we are
8978 // guaranteed that the receiver is the first register of the calling convention.
8979 InvokeDexCallingConventionARMVIXL calling_convention;
8980 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8981 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008982 {
8983 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008984 ExactAssemblyScope aas(GetVIXLAssembler(),
8985 vixl32::kMaxInstructionSizeInBytes,
8986 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008987 // /* HeapReference<Class> */ temp = receiver->klass_
8988 __ ldr(temp, MemOperand(receiver, class_offset));
8989 MaybeRecordImplicitNullCheck(invoke);
8990 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008991 // Instead of simply (possibly) unpoisoning `temp` here, we should
8992 // emit a read barrier for the previous class reference load.
8993 // However this is not required in practice, as this is an
8994 // intermediate/temporary reference and because the current
8995 // concurrent copying collector keeps the from-space memory
8996 // intact/accessible until the end of the marking phase (the
8997 // concurrent copying collector may not in the future).
8998 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8999
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00009000 // If we're compiling baseline, update the inline cache.
9001 MaybeGenerateInlineCacheCheck(invoke, temp);
9002
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009003 // temp = temp->GetMethodAt(method_offset);
9004 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
9005 kArmPointerSize).Int32Value();
9006 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
9007 // LR = temp->GetEntryPoint();
9008 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009009 {
9010 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
9011 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
9012 ExactAssemblyScope aas(GetVIXLAssembler(),
9013 vixl32::k16BitT32InstructionSizeInBytes,
9014 CodeBufferCheckScope::kExactSize);
9015 // LR();
9016 __ blx(lr);
9017 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
9018 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009019}
9020
Vladimir Marko6fd16062018-06-26 11:02:04 +01009021CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
9022 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009023 return NewPcRelativePatch(/* dex_file= */ nullptr, intrinsic_data, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01009024}
9025
Vladimir Markob066d432018-01-03 13:14:37 +00009026CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
9027 uint32_t boot_image_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08009028 return NewPcRelativePatch(/* dex_file= */ nullptr,
Vladimir Markob066d432018-01-03 13:14:37 +00009029 boot_image_offset,
Vladimir Marko2d06e022019-07-08 15:45:19 +01009030 &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00009031}
9032
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009033CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009034 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009035 return NewPcRelativePatch(
9036 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009037}
9038
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009039CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
9040 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009041 return NewPcRelativePatch(
9042 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009043}
9044
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009045CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00009046 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009047 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009048}
9049
Vladimir Marko1998cd02017-01-13 13:02:58 +00009050CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
9051 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009052 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009053}
9054
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009055CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01009056 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009057 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01009058}
9059
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009060CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9061 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009062 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009063}
9064
Artem Serovd4cc5b22016-11-04 11:19:09 +00009065CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009066 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009067 patches->emplace_back(dex_file, offset_or_index);
9068 return &patches->back();
9069}
9070
Vladimir Markof6675082019-05-17 12:05:28 +01009071void CodeGeneratorARMVIXL::EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset) {
9072 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009073 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markof6675082019-05-17 12:05:28 +01009074 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
9075 vixl::aarch32::Label* bl_label = &call_entrypoint_patches_.back().label;
9076 __ bind(bl_label);
9077 vixl32::Label placeholder_label;
9078 __ bl(&placeholder_label); // Placeholder, patched at link-time.
9079 __ bind(&placeholder_label);
9080}
9081
Vladimir Marko966b46f2018-08-03 10:20:19 +00009082void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00009083 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko695348f2020-05-19 14:42:02 +01009084 if (GetCompilerOptions().IsJitCompiler()) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00009085 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
9086 vixl::aarch32::Label* slow_path_entry = &it->second.label;
9087 __ b(ne, EncodingSize(Wide), slow_path_entry);
9088 } else {
9089 baker_read_barrier_patches_.emplace_back(custom_data);
9090 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
9091 __ bind(patch_label);
9092 vixl32::Label placeholder_label;
9093 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
9094 __ bind(&placeholder_label);
9095 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009096}
9097
Artem Serovc5fcb442016-12-02 19:19:58 +00009098VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009099 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009100}
9101
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009102VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9103 const DexFile& dex_file,
9104 dex::StringIndex string_index,
9105 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009106 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009107 return jit_string_patches_.GetOrCreate(
9108 StringReference(&dex_file, string_index),
9109 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009110 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009111 });
9112}
9113
9114VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9115 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009116 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009117 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009118 return jit_class_patches_.GetOrCreate(
9119 TypeReference(&dex_file, type_index),
9120 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009121 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009122 });
9123}
9124
Vladimir Marko6fd16062018-06-26 11:02:04 +01009125void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
9126 uint32_t boot_image_reference) {
9127 if (GetCompilerOptions().IsBootImage()) {
9128 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9129 NewBootImageIntrinsicPatch(boot_image_reference);
9130 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01009131 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009132 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9133 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009134 EmitMovwMovtPlaceholder(labels, reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08009135 __ Ldr(reg, MemOperand(reg, /* offset= */ 0));
Vladimir Markoeebb8212018-06-05 14:57:24 +01009136 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01009137 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01009138 gc::Heap* heap = Runtime::Current()->GetHeap();
9139 DCHECK(!heap->GetBootImageSpaces().empty());
9140 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01009141 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009142 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
9143 }
9144}
9145
Vladimir Marko6fd16062018-06-26 11:02:04 +01009146void CodeGeneratorARMVIXL::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
9147 uint32_t boot_image_offset) {
9148 DCHECK(invoke->IsStatic());
9149 InvokeRuntimeCallingConventionARMVIXL calling_convention;
9150 vixl32::Register argument = calling_convention.GetRegisterAt(0);
9151 if (GetCompilerOptions().IsBootImage()) {
9152 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
9153 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
9154 MethodReference target_method = invoke->GetTargetMethod();
9155 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
9156 PcRelativePatchInfo* labels = NewBootImageTypePatch(*target_method.dex_file, type_idx);
9157 EmitMovwMovtPlaceholder(labels, argument);
9158 } else {
9159 LoadBootImageAddress(argument, boot_image_offset);
9160 }
9161 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
9162 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
9163}
9164
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009165template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009166inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9167 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009168 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009169 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009170 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009171 size_t offset_or_index = info.offset_or_index;
9172 DCHECK(info.add_pc_label.IsBound());
9173 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9174 // Add MOVW patch.
9175 DCHECK(info.movw_label.IsBound());
9176 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009177 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009178 // Add MOVT patch.
9179 DCHECK(info.movt_label.IsBound());
9180 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009181 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009182 }
9183}
9184
Vladimir Marko6fd16062018-06-26 11:02:04 +01009185template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
9186linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
9187 const DexFile* target_dex_file,
9188 uint32_t pc_insn_offset,
9189 uint32_t boot_image_offset) {
9190 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
9191 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00009192}
9193
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009194void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009195 DCHECK(linker_patches->empty());
9196 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009197 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009198 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009199 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009200 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009201 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009202 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01009203 /* MOVW+MOVT for each entry */ 2u * boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01009204 call_entrypoint_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009205 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009206 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01009207 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009208 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009209 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009210 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009211 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009212 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009213 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009214 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009215 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009216 DCHECK(boot_image_type_patches_.empty());
9217 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01009218 }
9219 if (GetCompilerOptions().IsBootImage()) {
9220 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
9221 boot_image_other_patches_, linker_patches);
9222 } else {
9223 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
9224 boot_image_other_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009225 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009226 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9227 method_bss_entry_patches_, linker_patches);
9228 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9229 type_bss_entry_patches_, linker_patches);
9230 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9231 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01009232 for (const PatchInfo<vixl32::Label>& info : call_entrypoint_patches_) {
9233 DCHECK(info.target_dex_file == nullptr);
9234 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
9235 info.label.GetLocation(), info.offset_or_index));
9236 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009237 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009238 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9239 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009240 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009241 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009242}
9243
Vladimir Markoca1e0382018-04-11 09:58:41 +00009244bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01009245 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
9246 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00009247 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9248}
9249
9250void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9251 /*out*/ ArenaVector<uint8_t>* code,
9252 /*out*/ std::string* debug_name) {
9253 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9254 switch (patch.GetType()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009255 case linker::LinkerPatch::Type::kCallRelative: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009256 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9257 // to the generic JNI and interpreter trampolines.
Vladimir Markof6675082019-05-17 12:05:28 +01009258 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
9259 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, vixl32::r0, offset.Int32Value());
Vladimir Markoca1e0382018-04-11 09:58:41 +00009260 assembler.GetVIXLAssembler()->Bkpt(0);
9261 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9262 *debug_name = "MethodCallThunk";
9263 }
9264 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009265 }
9266 case linker::LinkerPatch::Type::kCallEntrypoint: {
9267 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, tr, patch.EntrypointOffset());
9268 assembler.GetVIXLAssembler()->Bkpt(0);
9269 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9270 *debug_name = "EntrypointCallThunk_" + std::to_string(patch.EntrypointOffset());
9271 }
9272 break;
9273 }
9274 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009275 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9276 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9277 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009278 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009279 default:
9280 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9281 UNREACHABLE();
9282 }
9283
9284 // Ensure we emit the literal pool if any.
9285 assembler.FinalizeCode();
9286 code->resize(assembler.CodeSize());
9287 MemoryRegion code_region(code->data(), code->size());
9288 assembler.FinalizeInstructions(code_region);
9289}
9290
Artem Serovc5fcb442016-12-02 19:19:58 +00009291VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9292 uint32_t value,
9293 Uint32ToLiteralMap* map) {
9294 return map->GetOrCreate(
9295 value,
9296 [this, value]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009297 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ value);
Artem Serovc5fcb442016-12-02 19:19:58 +00009298 });
9299}
9300
Artem Serov2bbc9532016-10-21 11:51:50 +01009301void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9302 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009303 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009304 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9305 Location::RequiresRegister());
9306 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9307 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9309}
9310
9311void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9312 vixl32::Register res = OutputRegister(instr);
9313 vixl32::Register accumulator =
9314 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9315 vixl32::Register mul_left =
9316 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9317 vixl32::Register mul_right =
9318 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9319
9320 if (instr->GetOpKind() == HInstruction::kAdd) {
9321 __ Mla(res, mul_left, mul_right, accumulator);
9322 } else {
9323 __ Mls(res, mul_left, mul_right, accumulator);
9324 }
9325}
9326
Artem Serov551b28f2016-10-18 19:11:30 +01009327void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9328 // Nothing to do, this should be removed during prepare for register allocator.
9329 LOG(FATAL) << "Unreachable";
9330}
9331
9332void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9333 // Nothing to do, this should be removed during prepare for register allocator.
9334 LOG(FATAL) << "Unreachable";
9335}
9336
9337// Simple implementation of packed switch - generate cascaded compare/jumps.
9338void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9339 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009340 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009341 locations->SetInAt(0, Location::RequiresRegister());
9342 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9343 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9344 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9345 if (switch_instr->GetStartValue() != 0) {
9346 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9347 }
9348 }
9349}
9350
9351// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9352void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9353 int32_t lower_bound = switch_instr->GetStartValue();
9354 uint32_t num_entries = switch_instr->GetNumEntries();
9355 LocationSummary* locations = switch_instr->GetLocations();
9356 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9357 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9358
9359 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9360 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9361 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009362 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009363 vixl32::Register temp_reg = temps.Acquire();
9364 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9365 // the immediate, because IP is used as the destination register. For the other
9366 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9367 // and they can be encoded in the instruction without making use of IP register.
9368 __ Adds(temp_reg, value_reg, -lower_bound);
9369
9370 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9371 // Jump to successors[0] if value == lower_bound.
9372 __ B(eq, codegen_->GetLabelOf(successors[0]));
9373 int32_t last_index = 0;
9374 for (; num_entries - last_index > 2; last_index += 2) {
9375 __ Adds(temp_reg, temp_reg, -2);
9376 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9377 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9378 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9379 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9380 }
9381 if (num_entries - last_index == 2) {
9382 // The last missing case_value.
9383 __ Cmp(temp_reg, 1);
9384 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9385 }
9386
9387 // And the default for any other value.
9388 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9389 __ B(codegen_->GetLabelOf(default_block));
9390 }
9391 } else {
9392 // Create a table lookup.
9393 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9394
9395 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9396
9397 // Remove the bias.
9398 vixl32::Register key_reg;
9399 if (lower_bound != 0) {
9400 key_reg = RegisterFrom(locations->GetTemp(1));
9401 __ Sub(key_reg, value_reg, lower_bound);
9402 } else {
9403 key_reg = value_reg;
9404 }
9405
9406 // Check whether the value is in the table, jump to default block if not.
9407 __ Cmp(key_reg, num_entries - 1);
9408 __ B(hi, codegen_->GetLabelOf(default_block));
9409
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009410 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009411 vixl32::Register jump_offset = temps.Acquire();
9412
9413 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009414 {
9415 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9416 ExactAssemblyScope aas(GetVIXLAssembler(),
9417 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9418 CodeBufferCheckScope::kMaximumSize);
9419 __ adr(table_base, jump_table->GetTableStartLabel());
9420 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009421
Scott Wakeling86e9d262017-01-18 15:59:24 +00009422 // Jump to target block by branching to table_base(pc related) + offset.
9423 vixl32::Register target_address = table_base;
9424 __ add(target_address, table_base, jump_offset);
9425 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009426
Scott Wakeling86e9d262017-01-18 15:59:24 +00009427 jump_table->EmitTable(codegen_);
9428 }
Artem Serov551b28f2016-10-18 19:11:30 +01009429 }
9430}
9431
Artem Serov02d37832016-10-25 15:25:33 +01009432// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009433void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009434 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009435 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009436 return;
9437 }
9438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009439 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009440
Artem Serovd4cc5b22016-11-04 11:19:09 +00009441 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009442 if (return_loc.Equals(trg)) {
9443 return;
9444 }
9445
9446 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9447 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009448 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009449 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009450 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009451 TODO_VIXL32(FATAL);
9452 } else {
9453 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009454 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009455 parallel_move.AddMove(return_loc, trg, type, nullptr);
9456 GetMoveResolver()->EmitNativeCode(&parallel_move);
9457 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009458}
Scott Wakelingfe885462016-09-22 10:24:38 +01009459
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009460void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9461 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009462 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009463 locations->SetInAt(0, Location::RequiresRegister());
9464 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009465}
9466
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009467void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9468 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9469 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9470 instruction->GetIndex(), kArmPointerSize).SizeValue();
9471 GetAssembler()->LoadFromOffset(kLoadWord,
9472 OutputRegister(instruction),
9473 InputRegisterAt(instruction, 0),
9474 method_offset);
9475 } else {
9476 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9477 instruction->GetIndex(), kArmPointerSize));
9478 GetAssembler()->LoadFromOffset(kLoadWord,
9479 OutputRegister(instruction),
9480 InputRegisterAt(instruction, 0),
9481 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9482 GetAssembler()->LoadFromOffset(kLoadWord,
9483 OutputRegister(instruction),
9484 OutputRegister(instruction),
9485 method_offset);
9486 }
Artem Serov551b28f2016-10-18 19:11:30 +01009487}
9488
Artem Serovc5fcb442016-12-02 19:19:58 +00009489static void PatchJitRootUse(uint8_t* code,
9490 const uint8_t* roots_data,
9491 VIXLUInt32Literal* literal,
9492 uint64_t index_in_table) {
9493 DCHECK(literal->IsBound());
9494 uint32_t literal_offset = literal->GetLocation();
9495 uintptr_t address =
9496 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9497 uint8_t* data = code + literal_offset;
9498 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9499}
9500
9501void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9502 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009503 const StringReference& string_reference = entry.first;
9504 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009505 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009506 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009507 }
9508 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009509 const TypeReference& type_reference = entry.first;
9510 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009511 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009512 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009513 }
9514}
9515
Artem Serovd4cc5b22016-11-04 11:19:09 +00009516void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9517 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9518 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009519 ExactAssemblyScope aas(GetVIXLAssembler(),
9520 3 * vixl32::kMaxInstructionSizeInBytes,
9521 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009522 // TODO(VIXL): Think about using mov instead of movw.
9523 __ bind(&labels->movw_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009524 __ movw(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009525 __ bind(&labels->movt_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009526 __ movt(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009527 __ bind(&labels->add_pc_label);
9528 __ add(out, out, pc);
9529}
9530
Scott Wakelingfe885462016-09-22 10:24:38 +01009531#undef __
9532#undef QUICK_ENTRY_POINT
9533#undef TODO_VIXL32
9534
Vladimir Markoca1e0382018-04-11 09:58:41 +00009535#define __ assembler.GetVIXLAssembler()->
9536
9537static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9538 vixl32::Register base_reg,
9539 vixl32::MemOperand& lock_word,
9540 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009541 int32_t raw_ldr_offset,
9542 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009543 // Load the lock word containing the rb_state.
9544 __ Ldr(ip, lock_word);
9545 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009546 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009547 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9548 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
Andreas Gampe3db70682018-12-26 15:12:03 -08009549 __ B(ne, slow_path, /* is_far_target= */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009550 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9551 if (throw_npe != nullptr) {
9552 __ Bind(throw_npe);
9553 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009554 __ Add(lr, lr, raw_ldr_offset);
9555 // Introduce a dependency on the lock_word including rb_state,
9556 // to prevent load-load reordering, and without using
9557 // a memory barrier (which would be more expensive).
9558 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9559 __ Bx(lr); // And return back to the function.
9560 // Note: The fake dependency is unnecessary for the slow path.
9561}
9562
9563// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009564static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009565 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009566 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9567 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009568 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9569 DCHECK_EQ(ip.GetCode(), 12u);
9570 const int32_t entry_point_offset =
9571 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9572 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +01009573 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009574}
9575
9576void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9577 uint32_t encoded_data,
9578 /*out*/ std::string* debug_name) {
9579 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9580 switch (kind) {
9581 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009582 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9583 CheckValidReg(base_reg.GetCode());
9584 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9585 CheckValidReg(holder_reg.GetCode());
9586 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9587 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9588 temps.Exclude(ip);
Roland Levillain988c3912019-09-25 19:33:35 +01009589 // In the case of a field load, if `base_reg` differs from
9590 // `holder_reg`, the offset was too large and we must have emitted (during the construction
9591 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
9592 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
9593 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
9594 // not necessarily do that check before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01009595 vixl32::Label throw_npe_label;
9596 vixl32::Label* throw_npe = nullptr;
9597 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9598 throw_npe = &throw_npe_label;
Andreas Gampe3db70682018-12-26 15:12:03 -08009599 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009600 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009601 // Check if the holder is gray and, if not, add fake dependency to the base register
9602 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9603 // to load the reference and call the entrypoint that performs further checks on the
9604 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009605 vixl32::Label slow_path;
9606 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9607 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9608 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9609 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009610 EmitGrayCheckAndFastPath(
9611 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009612 __ Bind(&slow_path);
9613 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9614 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +01009615 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009616 if (width == BakerReadBarrierWidth::kWide) {
9617 MemOperand ldr_half_address(lr, ldr_offset + 2);
9618 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9619 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9620 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9621 } else {
9622 MemOperand ldr_address(lr, ldr_offset);
9623 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9624 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9625 ep_reg, // for narrow LDR.
9626 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9627 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9628 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9629 }
9630 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9631 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009632 break;
9633 }
9634 case BakerReadBarrierKind::kArray: {
9635 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9636 CheckValidReg(base_reg.GetCode());
9637 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9638 BakerReadBarrierSecondRegField::Decode(encoded_data));
9639 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9640 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9641 temps.Exclude(ip);
9642 vixl32::Label slow_path;
9643 int32_t data_offset =
9644 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
9645 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
9646 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
9647 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
9648 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
9649 __ Bind(&slow_path);
9650 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9651 raw_ldr_offset;
9652 MemOperand ldr_address(lr, ldr_offset + 2);
9653 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
9654 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +01009655 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009656 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
9657 // a switch case target based on the index register.
9658 __ Mov(ip, base_reg); // Move the base register to ip0.
9659 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
9660 break;
9661 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009662 case BakerReadBarrierKind::kGcRoot:
9663 case BakerReadBarrierKind::kUnsafeCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009664 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
9665 // and it does not have a forwarding address), call the correct introspection entrypoint;
9666 // otherwise return the reference (or the extracted forwarding address).
9667 // There is no gray bit check for GC roots.
9668 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9669 CheckValidReg(root_reg.GetCode());
9670 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9671 BakerReadBarrierSecondRegField::Decode(encoded_data));
9672 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9673 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9674 temps.Exclude(ip);
9675 vixl32::Label return_label, not_marked, forwarding_address;
Andreas Gampe3db70682018-12-26 15:12:03 -08009676 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009677 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
9678 __ Ldr(ip, lock_word);
9679 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
9680 __ B(eq, &not_marked);
9681 __ Bind(&return_label);
9682 __ Bx(lr);
9683 __ Bind(&not_marked);
9684 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
9685 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
9686 " the highest bits and the 'forwarding address' state to have all bits set");
9687 __ Cmp(ip, Operand(0xc0000000));
9688 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +01009689 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009690 // Adjust the art_quick_read_barrier_mark_introspection address in kBakerCcEntrypointRegister
Vladimir Markod887ed82018-08-14 13:52:12 +00009691 // to one of art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},unsafe_cas}.
9692 DCHECK(kind != BakerReadBarrierKind::kUnsafeCas || width == BakerReadBarrierWidth::kWide);
9693 int32_t entrypoint_offset =
9694 (kind == BakerReadBarrierKind::kGcRoot)
9695 ? (width == BakerReadBarrierWidth::kWide)
9696 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
9697 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
9698 : BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009699 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
9700 __ Mov(ip, root_reg);
9701 __ Bx(ep_reg);
9702 __ Bind(&forwarding_address);
9703 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
9704 __ Bx(lr);
9705 break;
9706 }
9707 default:
9708 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
9709 UNREACHABLE();
9710 }
9711
Vladimir Marko966b46f2018-08-03 10:20:19 +00009712 // For JIT, the slow path is considered part of the compiled method,
Vladimir Markof91fc122020-05-13 09:21:00 +01009713 // so JIT should pass null as `debug_name`.
Vladimir Marko695348f2020-05-19 14:42:02 +01009714 DCHECK(!GetCompilerOptions().IsJitCompiler() || debug_name == nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +00009715 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009716 std::ostringstream oss;
9717 oss << "BakerReadBarrierThunk";
9718 switch (kind) {
9719 case BakerReadBarrierKind::kField:
9720 oss << "Field";
9721 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9722 oss << "Wide";
9723 }
9724 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
9725 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
9726 break;
9727 case BakerReadBarrierKind::kArray:
9728 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9729 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9730 BakerReadBarrierSecondRegField::Decode(encoded_data));
9731 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9732 break;
9733 case BakerReadBarrierKind::kGcRoot:
9734 oss << "GcRoot";
9735 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9736 oss << "Wide";
9737 }
9738 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9739 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9740 BakerReadBarrierSecondRegField::Decode(encoded_data));
9741 break;
Vladimir Markod887ed82018-08-14 13:52:12 +00009742 case BakerReadBarrierKind::kUnsafeCas:
9743 oss << "UnsafeCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9744 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9745 BakerReadBarrierSecondRegField::Decode(encoded_data));
9746 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9747 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009748 }
9749 *debug_name = oss.str();
9750 }
9751}
9752
9753#undef __
9754
Scott Wakelingfe885462016-09-22 10:24:38 +01009755} // namespace arm
9756} // namespace art