blob: 9100c6c54762bdc5f6be51501a1ae41d6917bb4c [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.
1934 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
1935 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
2083void CodeGeneratorARMVIXL::GenerateFrameEntry() {
2084 bool skip_overflow_check =
2085 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
2086 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
2087 __ Bind(&frame_entry_label_);
2088
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002089 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
2090 UseScratchRegisterScope temps(GetVIXLAssembler());
2091 vixl32::Register temp = temps.Acquire();
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07002092 static_assert(ArtMethod::MaxCounter() == 0xFFFF, "asm is probably wrong");
2093 // Load with sign extend to set the high bits for integer overflow check.
Nicolas Geoffray7ab07772019-08-30 08:26:59 +00002094 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2095 __ Add(temp, temp, 1);
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07002096 // Subtract one if the counter would overflow.
2097 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002098 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2099 }
2100
Scott Wakelingfe885462016-09-22 10:24:38 +01002101 if (HasEmptyFrame()) {
David Srbecky30021842019-02-13 14:19:36 +00002102 // Ensure that the CFI opcode list is not empty.
2103 GetAssembler()->cfi().Nop();
Scott Wakelingfe885462016-09-22 10:24:38 +01002104 return;
2105 }
2106
Scott Wakelingfe885462016-09-22 10:24:38 +01002107 if (!skip_overflow_check) {
xueliang.zhong10049552018-01-31 17:10:36 +00002108 // Using r4 instead of IP saves 2 bytes.
Nicolas Geoffray1a4f3ca2018-01-25 14:07:15 +00002109 UseScratchRegisterScope temps(GetVIXLAssembler());
xueliang.zhong10049552018-01-31 17:10:36 +00002110 vixl32::Register temp;
2111 // TODO: Remove this check when R4 is made a callee-save register
2112 // in ART compiled code (b/72801708). Currently we need to make
2113 // sure r4 is not blocked, e.g. in special purpose
2114 // TestCodeGeneratorARMVIXL; also asserting that r4 is available
2115 // here.
2116 if (!blocked_core_registers_[R4]) {
2117 for (vixl32::Register reg : kParameterCoreRegistersVIXL) {
2118 DCHECK(!reg.Is(r4));
2119 }
2120 DCHECK(!kCoreCalleeSaves.Includes(r4));
2121 temp = r4;
2122 } else {
2123 temp = temps.Acquire();
2124 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002125 __ Sub(temp, sp, Operand::From(GetStackOverflowReservedBytes(InstructionSet::kArm)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002126 // The load must immediately precede RecordPcInfo.
Artem Serov0fb37192016-12-06 18:13:40 +00002127 ExactAssemblyScope aas(GetVIXLAssembler(),
2128 vixl32::kMaxInstructionSizeInBytes,
2129 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002130 __ ldr(temp, MemOperand(temp));
2131 RecordPcInfo(nullptr, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01002132 }
2133
Vladimir Marko1a225a72019-07-05 13:37:42 +01002134 uint32_t frame_size = GetFrameSize();
2135 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2136 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2137 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2138 core_spills_offset <= 3u * kArmWordSize) {
2139 // Do a single PUSH for core registers including the method and up to two
2140 // filler registers. Then store the single FP spill if any.
2141 // (The worst case is when the method is not required and we actually
2142 // store 3 extra registers but they are stored in the same properly
2143 // aligned 16-byte chunk where we're already writing anyway.)
2144 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2145 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize);
2146 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(core_spill_mask_));
2147 __ Push(RegisterList(core_spill_mask_ | extra_regs));
2148 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
2149 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2150 core_spills_offset,
2151 core_spill_mask_,
2152 kArmWordSize);
2153 if (fpu_spill_mask_ != 0u) {
2154 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2155 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2156 GetAssembler()->StoreSToOffset(sreg, sp, fp_spills_offset);
2157 GetAssembler()->cfi().RelOffset(DWARFReg(sreg), /*offset=*/ fp_spills_offset);
2158 }
2159 } else {
2160 __ Push(RegisterList(core_spill_mask_));
2161 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2162 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(kMethodRegister),
2163 /*offset=*/ 0,
2164 core_spill_mask_,
2165 kArmWordSize);
2166 if (fpu_spill_mask_ != 0) {
2167 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
Scott Wakelingfe885462016-09-22 10:24:38 +01002168
Vladimir Marko1a225a72019-07-05 13:37:42 +01002169 // Check that list is contiguous.
2170 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
Scott Wakelingfe885462016-09-22 10:24:38 +01002171
Vladimir Marko1a225a72019-07-05 13:37:42 +01002172 __ Vpush(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2173 GetAssembler()->cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
2174 GetAssembler()->cfi().RelOffsetForMany(DWARFReg(s0),
2175 /*offset=*/ 0,
2176 fpu_spill_mask_,
2177 kArmWordSize);
2178 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002179
Vladimir Marko1a225a72019-07-05 13:37:42 +01002180 // Adjust SP and save the current method if we need it. Note that we do
2181 // not save the method in HCurrentMethod, as the instruction might have
2182 // been removed in the SSA graph.
2183 if (RequiresCurrentMethod() && fp_spills_offset <= 3 * kArmWordSize) {
2184 DCHECK_EQ(kMethodRegister.GetCode(), 0u);
2185 __ Push(RegisterList(MaxInt<uint32_t>(fp_spills_offset / kArmWordSize)));
2186 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2187 } else {
2188 __ Sub(sp, sp, dchecked_integral_cast<int32_t>(fp_spills_offset));
2189 GetAssembler()->cfi().AdjustCFAOffset(fp_spills_offset);
2190 if (RequiresCurrentMethod()) {
2191 GetAssembler()->StoreToOffset(kStoreWord, kMethodRegister, sp, 0);
2192 }
2193 }
Scott Wakelingbffdc702016-12-07 17:46:03 +00002194 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01002195
2196 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2197 UseScratchRegisterScope temps(GetVIXLAssembler());
2198 vixl32::Register temp = temps.Acquire();
2199 // Initialize should_deoptimize flag to 0.
2200 __ Mov(temp, 0);
2201 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, GetStackOffsetOfShouldDeoptimizeFlag());
2202 }
Roland Levillain5daa4952017-07-03 17:23:56 +01002203
Andreas Gampe3db70682018-12-26 15:12:03 -08002204 MaybeGenerateMarkingRegisterCheck(/* code= */ 1);
Scott Wakelingfe885462016-09-22 10:24:38 +01002205}
2206
2207void CodeGeneratorARMVIXL::GenerateFrameExit() {
2208 if (HasEmptyFrame()) {
2209 __ Bx(lr);
2210 return;
2211 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002212
Scott Wakelingfe885462016-09-22 10:24:38 +01002213 // Pop LR into PC to return.
2214 DCHECK_NE(core_spill_mask_ & (1 << kLrCode), 0U);
2215 uint32_t pop_mask = (core_spill_mask_ & (~(1 << kLrCode))) | 1 << kPcCode;
Vladimir Marko1a225a72019-07-05 13:37:42 +01002216
2217 uint32_t frame_size = GetFrameSize();
2218 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
2219 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
2220 if ((fpu_spill_mask_ == 0u || IsPowerOfTwo(fpu_spill_mask_)) &&
2221 // r4 is blocked by TestCodeGeneratorARMVIXL used by some tests.
2222 core_spills_offset <= (blocked_core_registers_[r4.GetCode()] ? 2u : 3u) * kArmWordSize) {
2223 // Load the FP spill if any and then do a single POP including the method
2224 // and up to two filler registers. If we have no FP spills, this also has
2225 // the advantage that we do not need to emit CFI directives.
2226 if (fpu_spill_mask_ != 0u) {
2227 DCHECK(IsPowerOfTwo(fpu_spill_mask_));
2228 vixl::aarch32::SRegister sreg(LeastSignificantBit(fpu_spill_mask_));
2229 GetAssembler()->cfi().RememberState();
2230 GetAssembler()->LoadSFromOffset(sreg, sp, fp_spills_offset);
2231 GetAssembler()->cfi().Restore(DWARFReg(sreg));
2232 }
2233 // Clobber registers r2-r4 as they are caller-save in ART managed ABI and
2234 // never hold the return value.
2235 uint32_t extra_regs = MaxInt<uint32_t>(core_spills_offset / kArmWordSize) << r2.GetCode();
2236 DCHECK_EQ(extra_regs & kCoreCalleeSaves.GetList(), 0u);
2237 DCHECK_LT(MostSignificantBit(extra_regs), LeastSignificantBit(pop_mask));
2238 __ Pop(RegisterList(pop_mask | extra_regs));
2239 if (fpu_spill_mask_ != 0u) {
2240 GetAssembler()->cfi().RestoreState();
2241 }
2242 } else {
2243 GetAssembler()->cfi().RememberState();
2244 __ Add(sp, sp, fp_spills_offset);
2245 GetAssembler()->cfi().AdjustCFAOffset(-dchecked_integral_cast<int32_t>(fp_spills_offset));
2246 if (fpu_spill_mask_ != 0) {
2247 uint32_t first = LeastSignificantBit(fpu_spill_mask_);
2248
2249 // Check that list is contiguous.
2250 DCHECK_EQ(fpu_spill_mask_ >> CTZ(fpu_spill_mask_), ~0u >> (32 - POPCOUNT(fpu_spill_mask_)));
2251
2252 __ Vpop(SRegisterList(vixl32::SRegister(first), POPCOUNT(fpu_spill_mask_)));
2253 GetAssembler()->cfi().AdjustCFAOffset(
2254 -static_cast<int>(kArmWordSize) * POPCOUNT(fpu_spill_mask_));
2255 GetAssembler()->cfi().RestoreMany(DWARFReg(vixl32::SRegister(0)), fpu_spill_mask_);
2256 }
2257 __ Pop(RegisterList(pop_mask));
2258 GetAssembler()->cfi().RestoreState();
2259 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
2260 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002261}
2262
2263void CodeGeneratorARMVIXL::Bind(HBasicBlock* block) {
2264 __ Bind(GetLabelOf(block));
2265}
2266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267Location InvokeDexCallingConventionVisitorARMVIXL::GetNextLocation(DataType::Type type) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002268 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002269 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002271 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 case DataType::Type::kInt8:
2273 case DataType::Type::kUint16:
2274 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002275 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002276 uint32_t index = gp_index_++;
2277 uint32_t stack_index = stack_index_++;
2278 if (index < calling_convention.GetNumberOfRegisters()) {
2279 return LocationFrom(calling_convention.GetRegisterAt(index));
2280 } else {
2281 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2282 }
2283 }
2284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002285 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002286 uint32_t index = gp_index_;
2287 uint32_t stack_index = stack_index_;
2288 gp_index_ += 2;
2289 stack_index_ += 2;
2290 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2291 if (calling_convention.GetRegisterAt(index).Is(r1)) {
2292 // Skip R1, and use R2_R3 instead.
2293 gp_index_++;
2294 index++;
2295 }
2296 }
2297 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2298 DCHECK_EQ(calling_convention.GetRegisterAt(index).GetCode() + 1,
2299 calling_convention.GetRegisterAt(index + 1).GetCode());
2300
2301 return LocationFrom(calling_convention.GetRegisterAt(index),
2302 calling_convention.GetRegisterAt(index + 1));
2303 } else {
2304 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2305 }
2306 }
2307
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002309 uint32_t stack_index = stack_index_++;
2310 if (float_index_ % 2 == 0) {
2311 float_index_ = std::max(double_index_, float_index_);
2312 }
2313 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2314 return LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
2315 } else {
2316 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2317 }
2318 }
2319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002321 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2322 uint32_t stack_index = stack_index_;
2323 stack_index_ += 2;
2324 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2325 uint32_t index = double_index_;
2326 double_index_ += 2;
2327 Location result = LocationFrom(
2328 calling_convention.GetFpuRegisterAt(index),
2329 calling_convention.GetFpuRegisterAt(index + 1));
2330 DCHECK(ExpectedPairLayout(result));
2331 return result;
2332 } else {
2333 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2334 }
2335 }
2336
Aart Bik66c158e2018-01-31 12:55:04 -08002337 case DataType::Type::kUint32:
2338 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002339 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002340 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002341 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00002342 }
2343 return Location::NoLocation();
2344}
2345
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346Location InvokeDexCallingConventionVisitorARMVIXL::GetReturnLocation(DataType::Type type) const {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002347 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002348 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002350 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 case DataType::Type::kInt8:
2352 case DataType::Type::kUint16:
2353 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08002354 case DataType::Type::kUint32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002355 case DataType::Type::kInt32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002356 return LocationFrom(r0);
2357 }
2358
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002359 case DataType::Type::kFloat32: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002360 return LocationFrom(s0);
2361 }
2362
Aart Bik66c158e2018-01-31 12:55:04 -08002363 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002364 case DataType::Type::kInt64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002365 return LocationFrom(r0, r1);
2366 }
2367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 case DataType::Type::kFloat64: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00002369 return LocationFrom(s0, s1);
2370 }
2371
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kVoid:
Artem Serovd4cc5b22016-11-04 11:19:09 +00002373 return Location::NoLocation();
2374 }
2375
2376 UNREACHABLE();
2377}
2378
2379Location InvokeDexCallingConventionVisitorARMVIXL::GetMethodLocation() const {
2380 return LocationFrom(kMethodRegister);
2381}
2382
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002383void CodeGeneratorARMVIXL::Move32(Location destination, Location source) {
2384 if (source.Equals(destination)) {
2385 return;
2386 }
2387 if (destination.IsRegister()) {
2388 if (source.IsRegister()) {
2389 __ Mov(RegisterFrom(destination), RegisterFrom(source));
2390 } else if (source.IsFpuRegister()) {
2391 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
2392 } else {
2393 GetAssembler()->LoadFromOffset(kLoadWord,
2394 RegisterFrom(destination),
2395 sp,
2396 source.GetStackIndex());
2397 }
2398 } else if (destination.IsFpuRegister()) {
2399 if (source.IsRegister()) {
2400 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
2401 } else if (source.IsFpuRegister()) {
2402 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
2403 } else {
2404 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
2405 }
2406 } else {
2407 DCHECK(destination.IsStackSlot()) << destination;
2408 if (source.IsRegister()) {
2409 GetAssembler()->StoreToOffset(kStoreWord,
2410 RegisterFrom(source),
2411 sp,
2412 destination.GetStackIndex());
2413 } else if (source.IsFpuRegister()) {
2414 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
2415 } else {
2416 DCHECK(source.IsStackSlot()) << source;
2417 UseScratchRegisterScope temps(GetVIXLAssembler());
2418 vixl32::Register temp = temps.Acquire();
2419 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
2420 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
2421 }
2422 }
2423}
2424
Artem Serovcfbe9132016-10-14 15:58:56 +01002425void CodeGeneratorARMVIXL::MoveConstant(Location location, int32_t value) {
2426 DCHECK(location.IsRegister());
2427 __ Mov(RegisterFrom(location), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01002428}
2429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002430void CodeGeneratorARMVIXL::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002431 // TODO(VIXL): Maybe refactor to have the 'move' implementation here and use it in
2432 // `ParallelMoveResolverARMVIXL::EmitMove`, as is done in the `arm64` backend.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002433 HParallelMove move(GetGraph()->GetAllocator());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002434 move.AddMove(src, dst, dst_type, nullptr);
2435 GetMoveResolver()->EmitNativeCode(&move);
Scott Wakelingfe885462016-09-22 10:24:38 +01002436}
2437
Artem Serovcfbe9132016-10-14 15:58:56 +01002438void CodeGeneratorARMVIXL::AddLocationAsTemp(Location location, LocationSummary* locations) {
2439 if (location.IsRegister()) {
2440 locations->AddTemp(location);
2441 } else if (location.IsRegisterPair()) {
2442 locations->AddTemp(LocationFrom(LowRegisterFrom(location)));
2443 locations->AddTemp(LocationFrom(HighRegisterFrom(location)));
2444 } else {
2445 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2446 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002447}
2448
2449void CodeGeneratorARMVIXL::InvokeRuntime(QuickEntrypointEnum entrypoint,
2450 HInstruction* instruction,
2451 uint32_t dex_pc,
2452 SlowPathCode* slow_path) {
2453 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Vladimir Markof6675082019-05-17 12:05:28 +01002454
2455 ThreadOffset32 entrypoint_offset = GetThreadOffset<kArmPointerSize>(entrypoint);
2456 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
2457 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
2458 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
2459 if (slow_path == nullptr || Runtime::Current()->UseJitCompilation()) {
2460 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
2461 // Ensure the pc position is recorded immediately after the `blx` instruction.
2462 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
2463 ExactAssemblyScope aas(GetVIXLAssembler(),
2464 vixl32::k16BitT32InstructionSizeInBytes,
2465 CodeBufferCheckScope::kExactSize);
2466 __ blx(lr);
2467 if (EntrypointRequiresStackMap(entrypoint)) {
2468 RecordPcInfo(instruction, dex_pc, slow_path);
2469 }
2470 } else {
2471 // Ensure the pc position is recorded immediately after the `bl` instruction.
2472 ExactAssemblyScope aas(GetVIXLAssembler(),
2473 vixl32::k32BitT32InstructionSizeInBytes,
2474 CodeBufferCheckScope::kExactSize);
2475 EmitEntrypointThunkCall(entrypoint_offset);
2476 if (EntrypointRequiresStackMap(entrypoint)) {
2477 RecordPcInfo(instruction, dex_pc, slow_path);
2478 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002479 }
2480}
2481
2482void CodeGeneratorARMVIXL::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2483 HInstruction* instruction,
2484 SlowPathCode* slow_path) {
2485 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00002486 __ Ldr(lr, MemOperand(tr, entry_point_offset));
Scott Wakelingfe885462016-09-22 10:24:38 +01002487 __ Blx(lr);
2488}
2489
Scott Wakelingfe885462016-09-22 10:24:38 +01002490void InstructionCodeGeneratorARMVIXL::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08002491 if (successor->IsExitBlock()) {
2492 DCHECK(got->GetPrevious()->AlwaysThrows());
2493 return; // no code needed
2494 }
2495
Scott Wakelingfe885462016-09-22 10:24:38 +01002496 HBasicBlock* block = got->GetBlock();
2497 HInstruction* previous = got->GetPrevious();
2498 HLoopInformation* info = block->GetLoopInformation();
2499
2500 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002501 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
2502 UseScratchRegisterScope temps(GetVIXLAssembler());
2503 vixl32::Register temp = temps.Acquire();
2504 __ Push(vixl32::Register(kMethodRegister));
2505 GetAssembler()->LoadFromOffset(kLoadWord, kMethodRegister, sp, kArmWordSize);
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07002506 // Load with sign extend to set the high bits for integer overflow check.
Nicolas Geoffray7ab07772019-08-30 08:26:59 +00002507 __ Ldrh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2508 __ Add(temp, temp, 1);
Mathieu Chartier7f8678e2019-08-30 16:22:28 -07002509 // Subtract one if the counter would overflow.
2510 __ Sub(temp, temp, Operand(temp, ShiftType::LSR, 16));
Nicolas Geoffray8d728322018-01-18 22:44:32 +00002511 __ Strh(temp, MemOperand(kMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
2512 __ Pop(vixl32::Register(kMethodRegister));
2513 }
Scott Wakelingfe885462016-09-22 10:24:38 +01002514 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2515 return;
2516 }
2517 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2518 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08002519 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 2);
Scott Wakelingfe885462016-09-22 10:24:38 +01002520 }
2521 if (!codegen_->GoesToNextBlock(block, successor)) {
2522 __ B(codegen_->GetLabelOf(successor));
2523 }
2524}
2525
2526void LocationsBuilderARMVIXL::VisitGoto(HGoto* got) {
2527 got->SetLocations(nullptr);
2528}
2529
2530void InstructionCodeGeneratorARMVIXL::VisitGoto(HGoto* got) {
2531 HandleGoto(got, got->GetSuccessor());
2532}
2533
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002534void LocationsBuilderARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2535 try_boundary->SetLocations(nullptr);
2536}
2537
2538void InstructionCodeGeneratorARMVIXL::VisitTryBoundary(HTryBoundary* try_boundary) {
2539 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2540 if (!successor->IsExitBlock()) {
2541 HandleGoto(try_boundary, successor);
2542 }
2543}
2544
Scott Wakelingfe885462016-09-22 10:24:38 +01002545void LocationsBuilderARMVIXL::VisitExit(HExit* exit) {
2546 exit->SetLocations(nullptr);
2547}
2548
2549void InstructionCodeGeneratorARMVIXL::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2550}
2551
Scott Wakelingfe885462016-09-22 10:24:38 +01002552void InstructionCodeGeneratorARMVIXL::GenerateCompareTestAndBranch(HCondition* condition,
Anton Kirilov23b752b2017-07-20 14:40:44 +01002553 vixl32::Label* true_target,
2554 vixl32::Label* false_target,
Anton Kirilovfd522532017-05-10 12:46:57 +01002555 bool is_far_target) {
Anton Kirilov23b752b2017-07-20 14:40:44 +01002556 if (true_target == false_target) {
2557 DCHECK(true_target != nullptr);
2558 __ B(true_target);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002559 return;
2560 }
2561
Anton Kirilov23b752b2017-07-20 14:40:44 +01002562 vixl32::Label* non_fallthrough_target;
2563 bool invert;
2564 bool emit_both_branches;
Scott Wakelingfe885462016-09-22 10:24:38 +01002565
Anton Kirilov23b752b2017-07-20 14:40:44 +01002566 if (true_target == nullptr) {
2567 // The true target is fallthrough.
2568 DCHECK(false_target != nullptr);
2569 non_fallthrough_target = false_target;
2570 invert = true;
2571 emit_both_branches = false;
2572 } else {
2573 non_fallthrough_target = true_target;
2574 invert = false;
2575 // Either the false target is fallthrough, or there is no fallthrough
2576 // and both branches must be emitted.
2577 emit_both_branches = (false_target != nullptr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002578 }
2579
Anton Kirilov23b752b2017-07-20 14:40:44 +01002580 const auto cond = GenerateTest(condition, invert, codegen_);
2581
2582 __ B(cond.first, non_fallthrough_target, is_far_target);
2583
2584 if (emit_both_branches) {
2585 // No target falls through, we need to branch.
2586 __ B(false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002587 }
2588}
2589
2590void InstructionCodeGeneratorARMVIXL::GenerateTestAndBranch(HInstruction* instruction,
2591 size_t condition_input_index,
2592 vixl32::Label* true_target,
xueliang.zhongf51bc622016-11-04 09:23:32 +00002593 vixl32::Label* false_target,
2594 bool far_target) {
Scott Wakelingfe885462016-09-22 10:24:38 +01002595 HInstruction* cond = instruction->InputAt(condition_input_index);
2596
2597 if (true_target == nullptr && false_target == nullptr) {
2598 // Nothing to do. The code always falls through.
2599 return;
2600 } else if (cond->IsIntConstant()) {
2601 // Constant condition, statically compared against "true" (integer value 1).
2602 if (cond->AsIntConstant()->IsTrue()) {
2603 if (true_target != nullptr) {
2604 __ B(true_target);
2605 }
2606 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00002607 DCHECK(cond->AsIntConstant()->IsFalse()) << Int32ConstantFrom(cond);
Scott Wakelingfe885462016-09-22 10:24:38 +01002608 if (false_target != nullptr) {
2609 __ B(false_target);
2610 }
2611 }
2612 return;
2613 }
2614
2615 // The following code generates these patterns:
2616 // (1) true_target == nullptr && false_target != nullptr
2617 // - opposite condition true => branch to false_target
2618 // (2) true_target != nullptr && false_target == nullptr
2619 // - condition true => branch to true_target
2620 // (3) true_target != nullptr && false_target != nullptr
2621 // - condition true => branch to true_target
2622 // - branch to false_target
2623 if (IsBooleanValueOrMaterializedCondition(cond)) {
2624 // Condition has been materialized, compare the output to 0.
2625 if (kIsDebugBuild) {
2626 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2627 DCHECK(cond_val.IsRegister());
2628 }
2629 if (true_target == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002630 __ CompareAndBranchIfZero(InputRegisterAt(instruction, condition_input_index),
2631 false_target,
2632 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002633 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00002634 __ CompareAndBranchIfNonZero(InputRegisterAt(instruction, condition_input_index),
2635 true_target,
2636 far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002637 }
2638 } else {
2639 // Condition has not been materialized. Use its inputs as the comparison and
2640 // its condition as the branch condition.
2641 HCondition* condition = cond->AsCondition();
2642
2643 // If this is a long or FP comparison that has been folded into
2644 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 DataType::Type type = condition->InputAt(0)->GetType();
2646 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002647 GenerateCompareTestAndBranch(condition, true_target, false_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002648 return;
2649 }
2650
Donghui Bai426b49c2016-11-08 14:55:38 +08002651 vixl32::Label* non_fallthrough_target;
2652 vixl32::Condition arm_cond = vixl32::Condition::None();
2653 const vixl32::Register left = InputRegisterAt(cond, 0);
2654 const Operand right = InputOperandAt(cond, 1);
2655
Scott Wakelingfe885462016-09-22 10:24:38 +01002656 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002657 arm_cond = ARMCondition(condition->GetOppositeCondition());
2658 non_fallthrough_target = false_target;
Scott Wakelingfe885462016-09-22 10:24:38 +01002659 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002660 arm_cond = ARMCondition(condition->GetCondition());
2661 non_fallthrough_target = true_target;
2662 }
2663
2664 if (right.IsImmediate() && right.GetImmediate() == 0 && (arm_cond.Is(ne) || arm_cond.Is(eq))) {
2665 if (arm_cond.Is(eq)) {
Anton Kirilovfd522532017-05-10 12:46:57 +01002666 __ CompareAndBranchIfZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002667 } else {
2668 DCHECK(arm_cond.Is(ne));
Anton Kirilovfd522532017-05-10 12:46:57 +01002669 __ CompareAndBranchIfNonZero(left, non_fallthrough_target, far_target);
Donghui Bai426b49c2016-11-08 14:55:38 +08002670 }
2671 } else {
2672 __ Cmp(left, right);
Anton Kirilovfd522532017-05-10 12:46:57 +01002673 __ B(arm_cond, non_fallthrough_target, far_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002674 }
2675 }
2676
2677 // If neither branch falls through (case 3), the conditional branch to `true_target`
2678 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2679 if (true_target != nullptr && false_target != nullptr) {
2680 __ B(false_target);
2681 }
2682}
2683
2684void LocationsBuilderARMVIXL::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002685 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
Scott Wakelingfe885462016-09-22 10:24:38 +01002686 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
2687 locations->SetInAt(0, Location::RequiresRegister());
2688 }
2689}
2690
2691void InstructionCodeGeneratorARMVIXL::VisitIf(HIf* if_instr) {
2692 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2693 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002694 vixl32::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2695 nullptr : codegen_->GetLabelOf(true_successor);
2696 vixl32::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2697 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002698 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Scott Wakelingfe885462016-09-22 10:24:38 +01002699}
2700
Scott Wakelingc34dba72016-10-03 10:14:44 +01002701void LocationsBuilderARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002702 LocationSummary* locations = new (GetGraph()->GetAllocator())
Scott Wakelingc34dba72016-10-03 10:14:44 +01002703 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002704 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2705 RegisterSet caller_saves = RegisterSet::Empty();
2706 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
2707 locations->SetCustomSlowPathCallerSaves(caller_saves);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002708 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
2709 locations->SetInAt(0, Location::RequiresRegister());
2710 }
2711}
2712
2713void InstructionCodeGeneratorARMVIXL::VisitDeoptimize(HDeoptimize* deoptimize) {
2714 SlowPathCodeARMVIXL* slow_path =
2715 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARMVIXL>(deoptimize);
2716 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002717 /* condition_input_index= */ 0,
Scott Wakelingc34dba72016-10-03 10:14:44 +01002718 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002719 /* false_target= */ nullptr);
Scott Wakelingc34dba72016-10-03 10:14:44 +01002720}
2721
Artem Serovd4cc5b22016-11-04 11:19:09 +00002722void LocationsBuilderARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002723 LocationSummary* locations = new (GetGraph()->GetAllocator())
Artem Serovd4cc5b22016-11-04 11:19:09 +00002724 LocationSummary(flag, LocationSummary::kNoCall);
2725 locations->SetOut(Location::RequiresRegister());
2726}
2727
2728void InstructionCodeGeneratorARMVIXL::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2729 GetAssembler()->LoadFromOffset(kLoadWord,
2730 OutputRegister(flag),
2731 sp,
2732 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2733}
2734
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002735void LocationsBuilderARMVIXL::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002736 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002737 const bool is_floating_point = DataType::IsFloatingPointType(select->GetType());
Donghui Bai426b49c2016-11-08 14:55:38 +08002738
2739 if (is_floating_point) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002740 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002741 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002742 } else {
2743 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002744 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002745 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002746
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002747 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002748 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2749 // The code generator handles overlap with the values, but not with the condition.
2750 locations->SetOut(Location::SameAsFirstInput());
2751 } else if (is_floating_point) {
2752 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2753 } else {
2754 if (!locations->InAt(1).IsConstant()) {
2755 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2756 }
2757
2758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002759 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002760}
2761
2762void InstructionCodeGeneratorARMVIXL::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002763 HInstruction* const condition = select->GetCondition();
2764 const LocationSummary* const locations = select->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002765 const DataType::Type type = select->GetType();
Donghui Bai426b49c2016-11-08 14:55:38 +08002766 const Location first = locations->InAt(0);
2767 const Location out = locations->Out();
2768 const Location second = locations->InAt(1);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002769
2770 // In the unlucky case the output of this instruction overlaps
2771 // with an input of an "emitted-at-use-site" condition, and
2772 // the output of this instruction is not one of its inputs, we'll
2773 // need to fallback to branches instead of conditional ARM instructions.
2774 bool output_overlaps_with_condition_inputs =
2775 !IsBooleanValueOrMaterializedCondition(condition) &&
2776 !out.Equals(first) &&
2777 !out.Equals(second) &&
2778 (condition->GetLocations()->InAt(0).Equals(out) ||
2779 condition->GetLocations()->InAt(1).Equals(out));
2780 DCHECK(!output_overlaps_with_condition_inputs || condition->IsCondition());
Donghui Bai426b49c2016-11-08 14:55:38 +08002781 Location src;
2782
2783 if (condition->IsIntConstant()) {
2784 if (condition->AsIntConstant()->IsFalse()) {
2785 src = first;
2786 } else {
2787 src = second;
2788 }
2789
2790 codegen_->MoveLocation(out, src, type);
2791 return;
2792 }
2793
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002794 if (!DataType::IsFloatingPointType(type) && !output_overlaps_with_condition_inputs) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002795 bool invert = false;
2796
2797 if (out.Equals(second)) {
2798 src = first;
2799 invert = true;
2800 } else if (out.Equals(first)) {
2801 src = second;
2802 } else if (second.IsConstant()) {
2803 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2804 src = second;
2805 } else if (first.IsConstant()) {
2806 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2807 src = first;
2808 invert = true;
2809 } else {
2810 src = second;
2811 }
2812
2813 if (CanGenerateConditionalMove(out, src)) {
2814 if (!out.Equals(first) && !out.Equals(second)) {
2815 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2816 }
2817
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002818 std::pair<vixl32::Condition, vixl32::Condition> cond(eq, ne);
2819
2820 if (IsBooleanValueOrMaterializedCondition(condition)) {
2821 __ Cmp(InputRegisterAt(select, 2), 0);
2822 cond = invert ? std::make_pair(eq, ne) : std::make_pair(ne, eq);
2823 } else {
2824 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2825 }
2826
Donghui Bai426b49c2016-11-08 14:55:38 +08002827 const size_t instr_count = out.IsRegisterPair() ? 4 : 2;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002828 // We use the scope because of the IT block that follows.
Donghui Bai426b49c2016-11-08 14:55:38 +08002829 ExactAssemblyScope guard(GetVIXLAssembler(),
2830 instr_count * vixl32::k16BitT32InstructionSizeInBytes,
2831 CodeBufferCheckScope::kExactSize);
2832
2833 if (out.IsRegister()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002834 __ it(cond.first);
2835 __ mov(cond.first, RegisterFrom(out), OperandFrom(src, type));
Donghui Bai426b49c2016-11-08 14:55:38 +08002836 } else {
2837 DCHECK(out.IsRegisterPair());
2838
2839 Operand operand_high(0);
2840 Operand operand_low(0);
2841
2842 if (src.IsConstant()) {
2843 const int64_t value = Int64ConstantFrom(src);
2844
2845 operand_high = High32Bits(value);
2846 operand_low = Low32Bits(value);
2847 } else {
2848 DCHECK(src.IsRegisterPair());
2849 operand_high = HighRegisterFrom(src);
2850 operand_low = LowRegisterFrom(src);
2851 }
2852
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002853 __ it(cond.first);
2854 __ mov(cond.first, LowRegisterFrom(out), operand_low);
2855 __ it(cond.first);
2856 __ mov(cond.first, HighRegisterFrom(out), operand_high);
Donghui Bai426b49c2016-11-08 14:55:38 +08002857 }
2858
2859 return;
2860 }
2861 }
2862
2863 vixl32::Label* false_target = nullptr;
2864 vixl32::Label* true_target = nullptr;
2865 vixl32::Label select_end;
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002866 vixl32::Label other_case;
Donghui Bai426b49c2016-11-08 14:55:38 +08002867 vixl32::Label* const target = codegen_->GetFinalLabel(select, &select_end);
2868
2869 if (out.Equals(second)) {
2870 true_target = target;
2871 src = first;
2872 } else {
2873 false_target = target;
2874 src = second;
2875
2876 if (!out.Equals(first)) {
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002877 if (output_overlaps_with_condition_inputs) {
2878 false_target = &other_case;
2879 } else {
2880 codegen_->MoveLocation(out, first, type);
2881 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002882 }
2883 }
2884
Andreas Gampe3db70682018-12-26 15:12:03 -08002885 GenerateTestAndBranch(select, 2, true_target, false_target, /* far_target= */ false);
Donghui Bai426b49c2016-11-08 14:55:38 +08002886 codegen_->MoveLocation(out, src, type);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002887 if (output_overlaps_with_condition_inputs) {
2888 __ B(target);
2889 __ Bind(&other_case);
2890 codegen_->MoveLocation(out, first, type);
2891 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002892
2893 if (select_end.IsReferenced()) {
2894 __ Bind(&select_end);
2895 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01002896}
2897
Artem Serov551b28f2016-10-18 19:11:30 +01002898void LocationsBuilderARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002899 new (GetGraph()->GetAllocator()) LocationSummary(info);
Artem Serov551b28f2016-10-18 19:11:30 +01002900}
2901
2902void InstructionCodeGeneratorARMVIXL::VisitNativeDebugInfo(HNativeDebugInfo*) {
2903 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
2904}
2905
Scott Wakelingfe885462016-09-22 10:24:38 +01002906void CodeGeneratorARMVIXL::GenerateNop() {
2907 __ Nop();
2908}
2909
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002910// `temp` is an extra temporary register that is used for some conditions;
2911// callers may not specify it, in which case the method will use a scratch
2912// register instead.
2913void CodeGeneratorARMVIXL::GenerateConditionWithZero(IfCondition condition,
2914 vixl32::Register out,
2915 vixl32::Register in,
2916 vixl32::Register temp) {
2917 switch (condition) {
2918 case kCondEQ:
2919 // x <= 0 iff x == 0 when the comparison is unsigned.
2920 case kCondBE:
2921 if (!temp.IsValid() || (out.IsLow() && !out.Is(in))) {
2922 temp = out;
2923 }
2924
2925 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
2926 // different as well.
2927 if (in.IsLow() && temp.IsLow() && !in.Is(temp)) {
2928 // temp = - in; only 0 sets the carry flag.
2929 __ Rsbs(temp, in, 0);
2930
2931 if (out.Is(in)) {
2932 std::swap(in, temp);
2933 }
2934
2935 // out = - in + in + carry = carry
2936 __ Adc(out, temp, in);
2937 } else {
2938 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
2939 __ Clz(out, in);
2940 // Any number less than 32 logically shifted right by 5 bits results in 0;
2941 // the same operation on 32 yields 1.
2942 __ Lsr(out, out, 5);
2943 }
2944
2945 break;
2946 case kCondNE:
2947 // x > 0 iff x != 0 when the comparison is unsigned.
2948 case kCondA: {
2949 UseScratchRegisterScope temps(GetVIXLAssembler());
2950
2951 if (out.Is(in)) {
2952 if (!temp.IsValid() || in.Is(temp)) {
2953 temp = temps.Acquire();
2954 }
2955 } else if (!temp.IsValid() || !temp.IsLow()) {
2956 temp = out;
2957 }
2958
2959 // temp = in - 1; only 0 does not set the carry flag.
2960 __ Subs(temp, in, 1);
2961 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
2962 __ Sbc(out, in, temp);
2963 break;
2964 }
2965 case kCondGE:
2966 __ Mvn(out, in);
2967 in = out;
2968 FALLTHROUGH_INTENDED;
2969 case kCondLT:
2970 // We only care about the sign bit.
2971 __ Lsr(out, in, 31);
2972 break;
2973 case kCondAE:
2974 // Trivially true.
2975 __ Mov(out, 1);
2976 break;
2977 case kCondB:
2978 // Trivially false.
2979 __ Mov(out, 0);
2980 break;
2981 default:
2982 LOG(FATAL) << "Unexpected condition " << condition;
2983 UNREACHABLE();
2984 }
2985}
2986
Scott Wakelingfe885462016-09-22 10:24:38 +01002987void LocationsBuilderARMVIXL::HandleCondition(HCondition* cond) {
2988 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002989 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray7b05c5f2018-09-21 11:31:38 +01002990 const DataType::Type type = cond->InputAt(0)->GetType();
2991 if (DataType::IsFloatingPointType(type)) {
2992 locations->SetInAt(0, Location::RequiresFpuRegister());
2993 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
2994 } else {
2995 locations->SetInAt(0, Location::RequiresRegister());
2996 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
2997 }
2998 if (!cond->IsEmittedAtUseSite()) {
2999 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003000 }
3001}
3002
3003void InstructionCodeGeneratorARMVIXL::HandleCondition(HCondition* cond) {
3004 if (cond->IsEmittedAtUseSite()) {
3005 return;
3006 }
3007
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003008 const DataType::Type type = cond->GetLeft()->GetType();
Scott Wakelingfe885462016-09-22 10:24:38 +01003009
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 if (DataType::IsFloatingPointType(type)) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003011 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003012 return;
Scott Wakelingfe885462016-09-22 10:24:38 +01003013 }
3014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 DCHECK(DataType::IsIntegralType(type) || type == DataType::Type::kReference) << type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003016
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003017 const IfCondition condition = cond->GetCondition();
Scott Wakelingfe885462016-09-22 10:24:38 +01003018
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003019 // A condition with only one boolean input, or two boolean inputs without being equality or
3020 // inequality results from transformations done by the instruction simplifier, and is handled
3021 // as a regular condition with integral inputs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 if (type == DataType::Type::kBool &&
3023 cond->GetRight()->GetType() == DataType::Type::kBool &&
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003024 (condition == kCondEQ || condition == kCondNE)) {
3025 vixl32::Register left = InputRegisterAt(cond, 0);
3026 const vixl32::Register out = OutputRegister(cond);
3027 const Location right_loc = cond->GetLocations()->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003028
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003029 // The constant case is handled by the instruction simplifier.
3030 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003031
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003032 vixl32::Register right = RegisterFrom(right_loc);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003033
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003034 // Avoid 32-bit instructions if possible.
3035 if (out.Is(right)) {
3036 std::swap(left, right);
3037 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003038
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003039 __ Eor(out, left, right);
3040
3041 if (condition == kCondEQ) {
3042 __ Eor(out, out, 1);
3043 }
3044
3045 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003046 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003047
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003048 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Scott Wakelingfe885462016-09-22 10:24:38 +01003049}
3050
3051void LocationsBuilderARMVIXL::VisitEqual(HEqual* comp) {
3052 HandleCondition(comp);
3053}
3054
3055void InstructionCodeGeneratorARMVIXL::VisitEqual(HEqual* comp) {
3056 HandleCondition(comp);
3057}
3058
3059void LocationsBuilderARMVIXL::VisitNotEqual(HNotEqual* comp) {
3060 HandleCondition(comp);
3061}
3062
3063void InstructionCodeGeneratorARMVIXL::VisitNotEqual(HNotEqual* comp) {
3064 HandleCondition(comp);
3065}
3066
3067void LocationsBuilderARMVIXL::VisitLessThan(HLessThan* comp) {
3068 HandleCondition(comp);
3069}
3070
3071void InstructionCodeGeneratorARMVIXL::VisitLessThan(HLessThan* comp) {
3072 HandleCondition(comp);
3073}
3074
3075void LocationsBuilderARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3076 HandleCondition(comp);
3077}
3078
3079void InstructionCodeGeneratorARMVIXL::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3080 HandleCondition(comp);
3081}
3082
3083void LocationsBuilderARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3084 HandleCondition(comp);
3085}
3086
3087void InstructionCodeGeneratorARMVIXL::VisitGreaterThan(HGreaterThan* comp) {
3088 HandleCondition(comp);
3089}
3090
3091void LocationsBuilderARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3092 HandleCondition(comp);
3093}
3094
3095void InstructionCodeGeneratorARMVIXL::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3096 HandleCondition(comp);
3097}
3098
3099void LocationsBuilderARMVIXL::VisitBelow(HBelow* comp) {
3100 HandleCondition(comp);
3101}
3102
3103void InstructionCodeGeneratorARMVIXL::VisitBelow(HBelow* comp) {
3104 HandleCondition(comp);
3105}
3106
3107void LocationsBuilderARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3108 HandleCondition(comp);
3109}
3110
3111void InstructionCodeGeneratorARMVIXL::VisitBelowOrEqual(HBelowOrEqual* comp) {
3112 HandleCondition(comp);
3113}
3114
3115void LocationsBuilderARMVIXL::VisitAbove(HAbove* comp) {
3116 HandleCondition(comp);
3117}
3118
3119void InstructionCodeGeneratorARMVIXL::VisitAbove(HAbove* comp) {
3120 HandleCondition(comp);
3121}
3122
3123void LocationsBuilderARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3124 HandleCondition(comp);
3125}
3126
3127void InstructionCodeGeneratorARMVIXL::VisitAboveOrEqual(HAboveOrEqual* comp) {
3128 HandleCondition(comp);
3129}
3130
3131void LocationsBuilderARMVIXL::VisitIntConstant(HIntConstant* constant) {
3132 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003133 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003134 locations->SetOut(Location::ConstantLocation(constant));
3135}
3136
3137void InstructionCodeGeneratorARMVIXL::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3138 // Will be generated at use site.
3139}
3140
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003141void LocationsBuilderARMVIXL::VisitNullConstant(HNullConstant* constant) {
3142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003143 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003144 locations->SetOut(Location::ConstantLocation(constant));
3145}
3146
3147void InstructionCodeGeneratorARMVIXL::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3148 // Will be generated at use site.
3149}
3150
Scott Wakelingfe885462016-09-22 10:24:38 +01003151void LocationsBuilderARMVIXL::VisitLongConstant(HLongConstant* constant) {
3152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003153 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003154 locations->SetOut(Location::ConstantLocation(constant));
3155}
3156
3157void InstructionCodeGeneratorARMVIXL::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3158 // Will be generated at use site.
3159}
3160
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003161void LocationsBuilderARMVIXL::VisitFloatConstant(HFloatConstant* constant) {
3162 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003163 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003164 locations->SetOut(Location::ConstantLocation(constant));
3165}
3166
Scott Wakelingc34dba72016-10-03 10:14:44 +01003167void InstructionCodeGeneratorARMVIXL::VisitFloatConstant(
3168 HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003169 // Will be generated at use site.
3170}
3171
3172void LocationsBuilderARMVIXL::VisitDoubleConstant(HDoubleConstant* constant) {
3173 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003174 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003175 locations->SetOut(Location::ConstantLocation(constant));
3176}
3177
Scott Wakelingc34dba72016-10-03 10:14:44 +01003178void InstructionCodeGeneratorARMVIXL::VisitDoubleConstant(
3179 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01003180 // Will be generated at use site.
3181}
3182
Igor Murashkind01745e2017-04-05 16:40:31 -07003183void LocationsBuilderARMVIXL::VisitConstructorFence(HConstructorFence* constructor_fence) {
3184 constructor_fence->SetLocations(nullptr);
3185}
3186
3187void InstructionCodeGeneratorARMVIXL::VisitConstructorFence(
3188 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3189 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3190}
3191
Scott Wakelingfe885462016-09-22 10:24:38 +01003192void LocationsBuilderARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3193 memory_barrier->SetLocations(nullptr);
3194}
3195
3196void InstructionCodeGeneratorARMVIXL::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3197 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3198}
3199
3200void LocationsBuilderARMVIXL::VisitReturnVoid(HReturnVoid* ret) {
3201 ret->SetLocations(nullptr);
3202}
3203
3204void InstructionCodeGeneratorARMVIXL::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3205 codegen_->GenerateFrameExit();
3206}
3207
3208void LocationsBuilderARMVIXL::VisitReturn(HReturn* ret) {
3209 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003210 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003211 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
3212}
3213
3214void InstructionCodeGeneratorARMVIXL::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3215 codegen_->GenerateFrameExit();
3216}
3217
Artem Serovcfbe9132016-10-14 15:58:56 +01003218void LocationsBuilderARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3219 // The trampoline uses the same calling convention as dex calling conventions,
3220 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3221 // the method_idx.
3222 HandleInvoke(invoke);
3223}
3224
3225void InstructionCodeGeneratorARMVIXL::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3226 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003227 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 3);
Artem Serovcfbe9132016-10-14 15:58:56 +01003228}
3229
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003230void LocationsBuilderARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3231 // Explicit clinit checks triggered by static invokes must have been pruned by
3232 // art::PrepareForRegisterAllocation.
3233 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3234
Anton Kirilov5ec62182016-10-13 20:16:02 +01003235 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3236 if (intrinsic.TryDispatch(invoke)) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003237 return;
3238 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003239
3240 HandleInvoke(invoke);
3241}
3242
Anton Kirilov5ec62182016-10-13 20:16:02 +01003243static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARMVIXL* codegen) {
3244 if (invoke->GetLocations()->Intrinsified()) {
3245 IntrinsicCodeGeneratorARMVIXL intrinsic(codegen);
3246 intrinsic.Dispatch(invoke);
3247 return true;
3248 }
3249 return false;
3250}
3251
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003252void InstructionCodeGeneratorARMVIXL::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3253 // Explicit clinit checks triggered by static invokes must have been pruned by
3254 // art::PrepareForRegisterAllocation.
3255 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
3256
Anton Kirilov5ec62182016-10-13 20:16:02 +01003257 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003258 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 4);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003259 return;
3260 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003261
3262 LocationSummary* locations = invoke->GetLocations();
Artem Serovd4cc5b22016-11-04 11:19:09 +00003263 codegen_->GenerateStaticOrDirectCall(
3264 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Roland Levillain5daa4952017-07-03 17:23:56 +01003265
Andreas Gampe3db70682018-12-26 15:12:03 -08003266 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 5);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003267}
3268
3269void LocationsBuilderARMVIXL::HandleInvoke(HInvoke* invoke) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00003270 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003271 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3272}
3273
3274void LocationsBuilderARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003275 IntrinsicLocationsBuilderARMVIXL intrinsic(codegen_);
3276 if (intrinsic.TryDispatch(invoke)) {
3277 return;
3278 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003279
3280 HandleInvoke(invoke);
3281}
3282
3283void InstructionCodeGeneratorARMVIXL::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01003284 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08003285 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 6);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003286 return;
3287 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003288
3289 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames374ddf32016-11-04 10:40:49 +00003290 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain5daa4952017-07-03 17:23:56 +01003291
Andreas Gampe3db70682018-12-26 15:12:03 -08003292 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 7);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003293}
3294
Artem Serovcfbe9132016-10-14 15:58:56 +01003295void LocationsBuilderARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3296 HandleInvoke(invoke);
3297 // Add the hidden argument.
3298 invoke->GetLocations()->AddTemp(LocationFrom(r12));
3299}
3300
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003301void CodeGeneratorARMVIXL::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
3302 vixl32::Register klass) {
3303 DCHECK_EQ(r0.GetCode(), klass.GetCode());
Nicolas Geoffray20036d82019-11-28 16:15:00 +00003304 // We know the destination of an intrinsic, so no need to record inline
3305 // caches.
3306 if (!instruction->GetLocations()->Intrinsified() &&
3307 GetCompilerOptions().IsBaseline() &&
3308 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003309 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
3310 ScopedObjectAccess soa(Thread::Current());
3311 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
3312 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
3313 uint32_t address = reinterpret_cast32<uint32_t>(cache);
3314 vixl32::Label done;
3315 UseScratchRegisterScope temps(GetVIXLAssembler());
3316 temps.Exclude(ip);
3317 __ Mov(r4, address);
3318 __ Ldr(ip, MemOperand(r4, InlineCache::ClassesOffset().Int32Value()));
3319 // Fast path for a monomorphic cache.
3320 __ Cmp(klass, ip);
3321 __ B(eq, &done, /* is_far_target= */ false);
3322 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
3323 __ Bind(&done);
3324 }
3325}
3326
Artem Serovcfbe9132016-10-14 15:58:56 +01003327void InstructionCodeGeneratorARMVIXL::VisitInvokeInterface(HInvokeInterface* invoke) {
3328 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3329 LocationSummary* locations = invoke->GetLocations();
3330 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
3331 vixl32::Register hidden_reg = RegisterFrom(locations->GetTemp(1));
3332 Location receiver = locations->InAt(0);
3333 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3334
3335 DCHECK(!receiver.IsStackSlot());
3336
Alexandre Rames374ddf32016-11-04 10:40:49 +00003337 // Ensure the pc position is recorded immediately after the `ldr` instruction.
3338 {
Artem Serov0fb37192016-12-06 18:13:40 +00003339 ExactAssemblyScope aas(GetVIXLAssembler(),
3340 vixl32::kMaxInstructionSizeInBytes,
3341 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00003342 // /* HeapReference<Class> */ temp = receiver->klass_
3343 __ ldr(temp, MemOperand(RegisterFrom(receiver), class_offset));
3344 codegen_->MaybeRecordImplicitNullCheck(invoke);
3345 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003346 // Instead of simply (possibly) unpoisoning `temp` here, we should
3347 // emit a read barrier for the previous class reference load.
3348 // However this is not required in practice, as this is an
3349 // intermediate/temporary reference and because the current
3350 // concurrent copying collector keeps the from-space memory
3351 // intact/accessible until the end of the marking phase (the
3352 // concurrent copying collector may not in the future).
3353 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003354
3355 // If we're compiling baseline, update the inline cache.
3356 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
3357
Artem Serovcfbe9132016-10-14 15:58:56 +01003358 GetAssembler()->LoadFromOffset(kLoadWord,
3359 temp,
3360 temp,
3361 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00003362
Artem Serovcfbe9132016-10-14 15:58:56 +01003363 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3364 invoke->GetImtIndex(), kArmPointerSize));
3365 // temp = temp->GetImtEntryAt(method_offset);
3366 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
3367 uint32_t entry_point =
3368 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
3369 // LR = temp->GetEntryPoint();
3370 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
3371
3372 // Set the hidden (in r12) argument. It is done here, right before a BLX to prevent other
3373 // instruction from clobbering it as they might use r12 as a scratch register.
3374 DCHECK(hidden_reg.Is(r12));
Scott Wakelingb77051e2016-11-21 19:46:00 +00003375
3376 {
3377 // The VIXL macro assembler may clobber any of the scratch registers that are available to it,
3378 // so it checks if the application is using them (by passing them to the macro assembler
3379 // methods). The following application of UseScratchRegisterScope corrects VIXL's notion of
3380 // what is available, and is the opposite of the standard usage: Instead of requesting a
3381 // temporary location, it imposes an external constraint (i.e. a specific register is reserved
3382 // for the hidden argument). Note that this works even if VIXL needs a scratch register itself
3383 // (to materialize the constant), since the destination register becomes available for such use
3384 // internally for the duration of the macro instruction.
3385 UseScratchRegisterScope temps(GetVIXLAssembler());
3386 temps.Exclude(hidden_reg);
3387 __ Mov(hidden_reg, invoke->GetDexMethodIndex());
3388 }
Artem Serovcfbe9132016-10-14 15:58:56 +01003389 {
Alexandre Rames374ddf32016-11-04 10:40:49 +00003390 // Ensure the pc position is recorded immediately after the `blx` instruction.
3391 // 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 +00003392 ExactAssemblyScope aas(GetVIXLAssembler(),
Alexandre Rames374ddf32016-11-04 10:40:49 +00003393 vixl32::k16BitT32InstructionSizeInBytes,
3394 CodeBufferCheckScope::kExactSize);
Artem Serovcfbe9132016-10-14 15:58:56 +01003395 // LR();
3396 __ blx(lr);
Artem Serovcfbe9132016-10-14 15:58:56 +01003397 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames374ddf32016-11-04 10:40:49 +00003398 DCHECK(!codegen_->IsLeafMethod());
Artem Serovcfbe9132016-10-14 15:58:56 +01003399 }
Roland Levillain5daa4952017-07-03 17:23:56 +01003400
Andreas Gampe3db70682018-12-26 15:12:03 -08003401 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 8);
Artem Serovcfbe9132016-10-14 15:58:56 +01003402}
3403
Orion Hodsonac141392017-01-13 11:53:47 +00003404void LocationsBuilderARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3405 HandleInvoke(invoke);
3406}
3407
3408void InstructionCodeGeneratorARMVIXL::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3409 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003410 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 9);
Orion Hodsonac141392017-01-13 11:53:47 +00003411}
3412
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003413void LocationsBuilderARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3414 HandleInvoke(invoke);
3415}
3416
3417void InstructionCodeGeneratorARMVIXL::VisitInvokeCustom(HInvokeCustom* invoke) {
3418 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08003419 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 10);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003420}
3421
Artem Serov02109dd2016-09-23 17:17:54 +01003422void LocationsBuilderARMVIXL::VisitNeg(HNeg* neg) {
3423 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003424 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01003425 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003426 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01003427 locations->SetInAt(0, Location::RequiresRegister());
3428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3429 break;
3430 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003431 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01003432 locations->SetInAt(0, Location::RequiresRegister());
3433 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3434 break;
3435 }
3436
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003437 case DataType::Type::kFloat32:
3438 case DataType::Type::kFloat64:
Artem Serov02109dd2016-09-23 17:17:54 +01003439 locations->SetInAt(0, Location::RequiresFpuRegister());
3440 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3441 break;
3442
3443 default:
3444 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3445 }
3446}
3447
3448void InstructionCodeGeneratorARMVIXL::VisitNeg(HNeg* neg) {
3449 LocationSummary* locations = neg->GetLocations();
3450 Location out = locations->Out();
3451 Location in = locations->InAt(0);
3452 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 case DataType::Type::kInt32:
Artem Serov02109dd2016-09-23 17:17:54 +01003454 __ Rsb(OutputRegister(neg), InputRegisterAt(neg, 0), 0);
3455 break;
3456
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003457 case DataType::Type::kInt64:
Artem Serov02109dd2016-09-23 17:17:54 +01003458 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3459 __ Rsbs(LowRegisterFrom(out), LowRegisterFrom(in), 0);
3460 // We cannot emit an RSC (Reverse Subtract with Carry)
3461 // instruction here, as it does not exist in the Thumb-2
3462 // instruction set. We use the following approach
3463 // using SBC and SUB instead.
3464 //
3465 // out.hi = -C
3466 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(out));
3467 // out.hi = out.hi - in.hi
3468 __ Sub(HighRegisterFrom(out), HighRegisterFrom(out), HighRegisterFrom(in));
3469 break;
3470
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003471 case DataType::Type::kFloat32:
3472 case DataType::Type::kFloat64:
Anton Kirilov644032c2016-12-06 17:51:43 +00003473 __ Vneg(OutputVRegister(neg), InputVRegister(neg));
Artem Serov02109dd2016-09-23 17:17:54 +01003474 break;
3475
3476 default:
3477 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3478 }
3479}
3480
Scott Wakelingfe885462016-09-22 10:24:38 +01003481void LocationsBuilderARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003482 DataType::Type result_type = conversion->GetResultType();
3483 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003484 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3485 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003486
3487 // The float-to-long, double-to-long and long-to-float type conversions
3488 // rely on a call to the runtime.
3489 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 (((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
3491 && result_type == DataType::Type::kInt64)
3492 || (input_type == DataType::Type::kInt64 && result_type == DataType::Type::kFloat32))
Scott Wakelingfe885462016-09-22 10:24:38 +01003493 ? LocationSummary::kCallOnMainOnly
3494 : LocationSummary::kNoCall;
3495 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003496 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01003497
Scott Wakelingfe885462016-09-22 10:24:38 +01003498 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003499 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003501 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003503 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
3504 locations->SetInAt(0, Location::RequiresRegister());
3505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Scott Wakelingfe885462016-09-22 10:24:38 +01003506 break;
3507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003508 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003509 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003511 locations->SetInAt(0, Location::Any());
3512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3513 break;
3514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003515 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003516 locations->SetInAt(0, Location::RequiresFpuRegister());
3517 locations->SetOut(Location::RequiresRegister());
3518 locations->AddTemp(Location::RequiresFpuRegister());
3519 break;
3520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003522 locations->SetInAt(0, Location::RequiresFpuRegister());
3523 locations->SetOut(Location::RequiresRegister());
3524 locations->AddTemp(Location::RequiresFpuRegister());
3525 break;
3526
3527 default:
3528 LOG(FATAL) << "Unexpected type conversion from " << input_type
3529 << " to " << result_type;
3530 }
3531 break;
3532
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003533 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003534 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003535 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003536 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003537 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003538 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003539 case DataType::Type::kInt16:
3540 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003541 locations->SetInAt(0, Location::RequiresRegister());
3542 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3543 break;
3544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003545 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003546 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3547 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3548 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003549 break;
3550 }
3551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003552 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003553 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3554 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0),
3555 calling_convention.GetFpuRegisterAt(1)));
3556 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003557 break;
3558 }
3559
3560 default:
3561 LOG(FATAL) << "Unexpected type conversion from " << input_type
3562 << " to " << result_type;
3563 }
3564 break;
3565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003566 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003567 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003568 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003569 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003570 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003571 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003572 case DataType::Type::kInt16:
3573 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003574 locations->SetInAt(0, Location::RequiresRegister());
3575 locations->SetOut(Location::RequiresFpuRegister());
3576 break;
3577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003579 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3580 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0),
3581 calling_convention.GetRegisterAt(1)));
3582 locations->SetOut(LocationFrom(calling_convention.GetFpuRegisterAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01003583 break;
3584 }
3585
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003586 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003587 locations->SetInAt(0, Location::RequiresFpuRegister());
3588 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3589 break;
3590
3591 default:
3592 LOG(FATAL) << "Unexpected type conversion from " << input_type
3593 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003594 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003595 break;
3596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003597 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003598 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003599 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003600 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003601 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003602 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003603 case DataType::Type::kInt16:
3604 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003605 locations->SetInAt(0, Location::RequiresRegister());
3606 locations->SetOut(Location::RequiresFpuRegister());
3607 break;
3608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003610 locations->SetInAt(0, Location::RequiresRegister());
3611 locations->SetOut(Location::RequiresFpuRegister());
3612 locations->AddTemp(Location::RequiresFpuRegister());
3613 locations->AddTemp(Location::RequiresFpuRegister());
3614 break;
3615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003616 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003617 locations->SetInAt(0, Location::RequiresFpuRegister());
3618 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3619 break;
3620
3621 default:
3622 LOG(FATAL) << "Unexpected type conversion from " << input_type
3623 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003624 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003625 break;
3626
3627 default:
3628 LOG(FATAL) << "Unexpected type conversion from " << input_type
3629 << " to " << result_type;
3630 }
3631}
3632
3633void InstructionCodeGeneratorARMVIXL::VisitTypeConversion(HTypeConversion* conversion) {
3634 LocationSummary* locations = conversion->GetLocations();
3635 Location out = locations->Out();
3636 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003637 DataType::Type result_type = conversion->GetResultType();
3638 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003639 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3640 << input_type << " -> " << result_type;
Scott Wakelingfe885462016-09-22 10:24:38 +01003641 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003642 case DataType::Type::kUint8:
Scott Wakelingfe885462016-09-22 10:24:38 +01003643 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003644 case DataType::Type::kInt8:
3645 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003646 case DataType::Type::kInt16:
3647 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003648 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3649 break;
3650 case DataType::Type::kInt64:
3651 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3652 break;
3653
3654 default:
3655 LOG(FATAL) << "Unexpected type conversion from " << input_type
3656 << " to " << result_type;
3657 }
3658 break;
3659
3660 case DataType::Type::kInt8:
3661 switch (input_type) {
3662 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003664 case DataType::Type::kInt16:
3665 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003666 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 8);
3667 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003668 case DataType::Type::kInt64:
3669 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 8);
3670 break;
3671
3672 default:
3673 LOG(FATAL) << "Unexpected type conversion from " << input_type
3674 << " to " << result_type;
3675 }
3676 break;
3677
3678 case DataType::Type::kUint16:
3679 switch (input_type) {
3680 case DataType::Type::kInt8:
3681 case DataType::Type::kInt16:
3682 case DataType::Type::kInt32:
3683 __ Ubfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3684 break;
3685 case DataType::Type::kInt64:
3686 __ Ubfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3687 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003688
3689 default:
3690 LOG(FATAL) << "Unexpected type conversion from " << input_type
3691 << " to " << result_type;
3692 }
3693 break;
3694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003695 case DataType::Type::kInt16:
Scott Wakelingfe885462016-09-22 10:24:38 +01003696 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003697 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003698 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003699 __ Sbfx(OutputRegister(conversion), InputRegisterAt(conversion, 0), 0, 16);
3700 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003701 case DataType::Type::kInt64:
3702 __ Sbfx(OutputRegister(conversion), LowRegisterFrom(in), 0, 16);
3703 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003704
3705 default:
3706 LOG(FATAL) << "Unexpected type conversion from " << input_type
3707 << " to " << result_type;
3708 }
3709 break;
3710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003712 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003713 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003714 DCHECK(out.IsRegister());
3715 if (in.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003716 __ Mov(OutputRegister(conversion), LowRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003717 } else if (in.IsDoubleStackSlot()) {
3718 GetAssembler()->LoadFromOffset(kLoadWord,
3719 OutputRegister(conversion),
3720 sp,
3721 in.GetStackIndex());
3722 } else {
3723 DCHECK(in.IsConstant());
3724 DCHECK(in.GetConstant()->IsLongConstant());
Vladimir Markoba1a48e2017-04-13 11:50:14 +01003725 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3726 __ Mov(OutputRegister(conversion), static_cast<int32_t>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01003727 }
3728 break;
3729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 case DataType::Type::kFloat32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003731 vixl32::SRegister temp = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003732 __ Vcvt(S32, F32, temp, InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003733 __ Vmov(OutputRegister(conversion), temp);
3734 break;
3735 }
3736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 case DataType::Type::kFloat64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003738 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003739 __ Vcvt(S32, F64, temp_s, DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003740 __ Vmov(OutputRegister(conversion), temp_s);
3741 break;
3742 }
3743
3744 default:
3745 LOG(FATAL) << "Unexpected type conversion from " << input_type
3746 << " to " << result_type;
3747 }
3748 break;
3749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003750 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003751 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003752 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003753 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003755 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003756 case DataType::Type::kInt16:
3757 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003758 DCHECK(out.IsRegisterPair());
3759 DCHECK(in.IsRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003760 __ Mov(LowRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003761 // Sign extension.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003762 __ Asr(HighRegisterFrom(out), LowRegisterFrom(out), 31);
Scott Wakelingfe885462016-09-22 10:24:38 +01003763 break;
3764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003766 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
3767 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
3768 break;
3769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003771 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
3772 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
3773 break;
3774
3775 default:
3776 LOG(FATAL) << "Unexpected type conversion from " << input_type
3777 << " to " << result_type;
3778 }
3779 break;
3780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003781 case DataType::Type::kFloat32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003782 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003783 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003784 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003785 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003786 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kInt16:
3788 case DataType::Type::kInt32:
Scott Wakelingfe885462016-09-22 10:24:38 +01003789 __ Vmov(OutputSRegister(conversion), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003790 __ Vcvt(F32, S32, OutputSRegister(conversion), OutputSRegister(conversion));
Scott Wakelingfe885462016-09-22 10:24:38 +01003791 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003793 case DataType::Type::kInt64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003794 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
3795 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3796 break;
3797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003799 __ Vcvt(F32, F64, OutputSRegister(conversion), DRegisterFrom(in));
Scott Wakelingfe885462016-09-22 10:24:38 +01003800 break;
3801
3802 default:
3803 LOG(FATAL) << "Unexpected type conversion from " << input_type
3804 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003805 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003806 break;
3807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003808 case DataType::Type::kFloat64:
Scott Wakelingfe885462016-09-22 10:24:38 +01003809 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003811 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003812 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003813 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 case DataType::Type::kInt16:
3815 case DataType::Type::kInt32:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003816 __ Vmov(LowSRegisterFrom(out), InputRegisterAt(conversion, 0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003817 __ Vcvt(F64, S32, DRegisterFrom(out), LowSRegisterFrom(out));
Scott Wakelingfe885462016-09-22 10:24:38 +01003818 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003820 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003821 vixl32::Register low = LowRegisterFrom(in);
3822 vixl32::Register high = HighRegisterFrom(in);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003823 vixl32::SRegister out_s = LowSRegisterFrom(out);
Scott Wakelingc34dba72016-10-03 10:14:44 +01003824 vixl32::DRegister out_d = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003825 vixl32::SRegister temp_s = LowSRegisterFrom(locations->GetTemp(0));
Scott Wakelingc34dba72016-10-03 10:14:44 +01003826 vixl32::DRegister temp_d = DRegisterFrom(locations->GetTemp(0));
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003827 vixl32::DRegister constant_d = DRegisterFrom(locations->GetTemp(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003828
3829 // temp_d = int-to-double(high)
3830 __ Vmov(temp_s, high);
Scott Wakelingfb0b7d42016-10-28 16:11:08 +01003831 __ Vcvt(F64, S32, temp_d, temp_s);
Scott Wakelingfe885462016-09-22 10:24:38 +01003832 // constant_d = k2Pow32EncodingForDouble
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003833 __ Vmov(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
Scott Wakelingfe885462016-09-22 10:24:38 +01003834 // out_d = unsigned-to-double(low)
3835 __ Vmov(out_s, low);
3836 __ Vcvt(F64, U32, out_d, out_s);
3837 // out_d += temp_d * constant_d
3838 __ Vmla(F64, out_d, temp_d, constant_d);
3839 break;
3840 }
3841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003842 case DataType::Type::kFloat32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01003843 __ Vcvt(F64, F32, DRegisterFrom(out), InputSRegisterAt(conversion, 0));
Scott Wakelingfe885462016-09-22 10:24:38 +01003844 break;
3845
3846 default:
3847 LOG(FATAL) << "Unexpected type conversion from " << input_type
3848 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003849 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003850 break;
3851
3852 default:
3853 LOG(FATAL) << "Unexpected type conversion from " << input_type
3854 << " to " << result_type;
3855 }
3856}
3857
3858void LocationsBuilderARMVIXL::VisitAdd(HAdd* add) {
3859 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003860 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003861 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003862 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003863 locations->SetInAt(0, Location::RequiresRegister());
3864 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3866 break;
3867 }
3868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003870 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003871 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Scott Wakelingfe885462016-09-22 10:24:38 +01003872 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3873 break;
3874 }
3875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003876 case DataType::Type::kFloat32:
3877 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003878 locations->SetInAt(0, Location::RequiresFpuRegister());
3879 locations->SetInAt(1, Location::RequiresFpuRegister());
3880 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3881 break;
3882 }
3883
3884 default:
3885 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3886 }
3887}
3888
3889void InstructionCodeGeneratorARMVIXL::VisitAdd(HAdd* add) {
3890 LocationSummary* locations = add->GetLocations();
3891 Location out = locations->Out();
3892 Location first = locations->InAt(0);
3893 Location second = locations->InAt(1);
3894
3895 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003897 __ Add(OutputRegister(add), InputRegisterAt(add, 0), InputOperandAt(add, 1));
3898 }
3899 break;
3900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003901 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003902 if (second.IsConstant()) {
3903 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3904 GenerateAddLongConst(out, first, value);
3905 } else {
3906 DCHECK(second.IsRegisterPair());
3907 __ Adds(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3908 __ Adc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3909 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003910 break;
3911 }
3912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003913 case DataType::Type::kFloat32:
3914 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003915 __ Vadd(OutputVRegister(add), InputVRegisterAt(add, 0), InputVRegisterAt(add, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003916 break;
3917
3918 default:
3919 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
3920 }
3921}
3922
3923void LocationsBuilderARMVIXL::VisitSub(HSub* sub) {
3924 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003925 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003926 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003927 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003928 locations->SetInAt(0, Location::RequiresRegister());
3929 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
3930 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3931 break;
3932 }
3933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003935 locations->SetInAt(0, Location::RequiresRegister());
Anton Kirilovdda43962016-11-21 19:55:20 +00003936 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Scott Wakelingfe885462016-09-22 10:24:38 +01003937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3938 break;
3939 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003940 case DataType::Type::kFloat32:
3941 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003942 locations->SetInAt(0, Location::RequiresFpuRegister());
3943 locations->SetInAt(1, Location::RequiresFpuRegister());
3944 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3945 break;
3946 }
3947 default:
3948 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3949 }
3950}
3951
3952void InstructionCodeGeneratorARMVIXL::VisitSub(HSub* sub) {
3953 LocationSummary* locations = sub->GetLocations();
3954 Location out = locations->Out();
3955 Location first = locations->InAt(0);
3956 Location second = locations->InAt(1);
3957 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003958 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003959 __ Sub(OutputRegister(sub), InputRegisterAt(sub, 0), InputOperandAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003960 break;
3961 }
3962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003963 case DataType::Type::kInt64: {
Anton Kirilovdda43962016-11-21 19:55:20 +00003964 if (second.IsConstant()) {
3965 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3966 GenerateAddLongConst(out, first, -value);
3967 } else {
3968 DCHECK(second.IsRegisterPair());
3969 __ Subs(LowRegisterFrom(out), LowRegisterFrom(first), LowRegisterFrom(second));
3970 __ Sbc(HighRegisterFrom(out), HighRegisterFrom(first), HighRegisterFrom(second));
3971 }
Scott Wakelingfe885462016-09-22 10:24:38 +01003972 break;
3973 }
3974
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003975 case DataType::Type::kFloat32:
3976 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01003977 __ Vsub(OutputVRegister(sub), InputVRegisterAt(sub, 0), InputVRegisterAt(sub, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01003978 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01003979
3980 default:
3981 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
3982 }
3983}
3984
3985void LocationsBuilderARMVIXL::VisitMul(HMul* mul) {
3986 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003987 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Scott Wakelingfe885462016-09-22 10:24:38 +01003988 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003989 case DataType::Type::kInt32:
3990 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003991 locations->SetInAt(0, Location::RequiresRegister());
3992 locations->SetInAt(1, Location::RequiresRegister());
3993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3994 break;
3995 }
3996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003997 case DataType::Type::kFloat32:
3998 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01003999 locations->SetInAt(0, Location::RequiresFpuRegister());
4000 locations->SetInAt(1, Location::RequiresFpuRegister());
4001 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4002 break;
4003 }
4004
4005 default:
4006 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4007 }
4008}
4009
4010void InstructionCodeGeneratorARMVIXL::VisitMul(HMul* mul) {
4011 LocationSummary* locations = mul->GetLocations();
4012 Location out = locations->Out();
4013 Location first = locations->InAt(0);
4014 Location second = locations->InAt(1);
4015 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004016 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004017 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4018 break;
4019 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004020 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004021 vixl32::Register out_hi = HighRegisterFrom(out);
4022 vixl32::Register out_lo = LowRegisterFrom(out);
4023 vixl32::Register in1_hi = HighRegisterFrom(first);
4024 vixl32::Register in1_lo = LowRegisterFrom(first);
4025 vixl32::Register in2_hi = HighRegisterFrom(second);
4026 vixl32::Register in2_lo = LowRegisterFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004027
4028 // Extra checks to protect caused by the existence of R1_R2.
4029 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4030 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
Anton Kirilov644032c2016-12-06 17:51:43 +00004031 DCHECK(!out_hi.Is(in1_lo));
4032 DCHECK(!out_hi.Is(in2_lo));
Scott Wakelingfe885462016-09-22 10:24:38 +01004033
4034 // input: in1 - 64 bits, in2 - 64 bits
4035 // output: out
4036 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4037 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4038 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4039
4040 UseScratchRegisterScope temps(GetVIXLAssembler());
4041 vixl32::Register temp = temps.Acquire();
4042 // temp <- in1.lo * in2.hi
4043 __ Mul(temp, in1_lo, in2_hi);
4044 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4045 __ Mla(out_hi, in1_hi, in2_lo, temp);
4046 // out.lo <- (in1.lo * in2.lo)[31:0];
4047 __ Umull(out_lo, temp, in1_lo, in2_lo);
4048 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004049 __ Add(out_hi, out_hi, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004050 break;
4051 }
4052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004053 case DataType::Type::kFloat32:
4054 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004055 __ Vmul(OutputVRegister(mul), InputVRegisterAt(mul, 0), InputVRegisterAt(mul, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004056 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004057
4058 default:
4059 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4060 }
4061}
4062
Scott Wakelingfe885462016-09-22 10:24:38 +01004063void InstructionCodeGeneratorARMVIXL::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4064 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004065 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004066
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004067 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004068 DCHECK(second.IsConstant());
4069
4070 vixl32::Register out = OutputRegister(instruction);
4071 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Anton Kirilov644032c2016-12-06 17:51:43 +00004072 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004073 DCHECK(imm == 1 || imm == -1);
4074
4075 if (instruction->IsRem()) {
4076 __ Mov(out, 0);
4077 } else {
4078 if (imm == 1) {
4079 __ Mov(out, dividend);
4080 } else {
4081 __ Rsb(out, dividend, 0);
4082 }
4083 }
4084}
4085
4086void InstructionCodeGeneratorARMVIXL::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4087 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004088 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004089
4090 LocationSummary* locations = instruction->GetLocations();
4091 Location second = locations->InAt(1);
4092 DCHECK(second.IsConstant());
4093
4094 vixl32::Register out = OutputRegister(instruction);
4095 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004096 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov644032c2016-12-06 17:51:43 +00004097 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004098 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
4099 int ctz_imm = CTZ(abs_imm);
4100
4101 if (ctz_imm == 1) {
4102 __ Lsr(temp, dividend, 32 - ctz_imm);
4103 } else {
4104 __ Asr(temp, dividend, 31);
4105 __ Lsr(temp, temp, 32 - ctz_imm);
4106 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004107 __ Add(out, temp, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004108
4109 if (instruction->IsDiv()) {
4110 __ Asr(out, out, ctz_imm);
4111 if (imm < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004112 __ Rsb(out, out, 0);
Scott Wakelingfe885462016-09-22 10:24:38 +01004113 }
4114 } else {
4115 __ Ubfx(out, out, 0, ctz_imm);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004116 __ Sub(out, out, temp);
Scott Wakelingfe885462016-09-22 10:24:38 +01004117 }
4118}
4119
4120void InstructionCodeGeneratorARMVIXL::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4121 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004123
4124 LocationSummary* locations = instruction->GetLocations();
4125 Location second = locations->InAt(1);
4126 DCHECK(second.IsConstant());
4127
4128 vixl32::Register out = OutputRegister(instruction);
4129 vixl32::Register dividend = InputRegisterAt(instruction, 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004130 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(0));
4131 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(1));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004132 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004133
4134 int64_t magic;
4135 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004136 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Scott Wakelingfe885462016-09-22 10:24:38 +01004137
Anton Kirilovdda43962016-11-21 19:55:20 +00004138 // TODO(VIXL): Change the static cast to Operand::From() after VIXL is fixed.
4139 __ Mov(temp1, static_cast<int32_t>(magic));
Scott Wakelingfe885462016-09-22 10:24:38 +01004140 __ Smull(temp2, temp1, dividend, temp1);
4141
4142 if (imm > 0 && magic < 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004143 __ Add(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004144 } else if (imm < 0 && magic > 0) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004145 __ Sub(temp1, temp1, dividend);
Scott Wakelingfe885462016-09-22 10:24:38 +01004146 }
4147
4148 if (shift != 0) {
4149 __ Asr(temp1, temp1, shift);
4150 }
4151
4152 if (instruction->IsDiv()) {
4153 __ Sub(out, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4154 } else {
4155 __ Sub(temp1, temp1, Operand(temp1, vixl32::Shift(ASR), 31));
4156 // TODO: Strength reduction for mls.
4157 __ Mov(temp2, imm);
4158 __ Mls(out, temp1, temp2, dividend);
4159 }
4160}
4161
4162void InstructionCodeGeneratorARMVIXL::GenerateDivRemConstantIntegral(
4163 HBinaryOperation* instruction) {
4164 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
Scott Wakelingfe885462016-09-22 10:24:38 +01004166
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004167 Location second = instruction->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004168 DCHECK(second.IsConstant());
4169
Anton Kirilov644032c2016-12-06 17:51:43 +00004170 int32_t imm = Int32ConstantFrom(second);
Scott Wakelingfe885462016-09-22 10:24:38 +01004171 if (imm == 0) {
4172 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4173 } else if (imm == 1 || imm == -1) {
4174 DivRemOneOrMinusOne(instruction);
4175 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4176 DivRemByPowerOfTwo(instruction);
4177 } else {
4178 DCHECK(imm <= -2 || imm >= 2);
4179 GenerateDivRemWithAnyConstant(instruction);
4180 }
4181}
4182
4183void LocationsBuilderARMVIXL::VisitDiv(HDiv* div) {
4184 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004185 if (div->GetResultType() == DataType::Type::kInt64) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004186 // pLdiv runtime call.
4187 call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004188 } else if (div->GetResultType() == DataType::Type::kInt32 && div->InputAt(1)->IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004189 // sdiv will be replaced by other instruction sequence.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004190 } else if (div->GetResultType() == DataType::Type::kInt32 &&
Scott Wakelingfe885462016-09-22 10:24:38 +01004191 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4192 // pIdivmod runtime call.
4193 call_kind = LocationSummary::kCallOnMainOnly;
4194 }
4195
Vladimir Markoca6fff82017-10-03 14:49:14 +01004196 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Scott Wakelingfe885462016-09-22 10:24:38 +01004197
4198 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004200 if (div->InputAt(1)->IsConstant()) {
4201 locations->SetInAt(0, Location::RequiresRegister());
4202 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004204 int32_t value = Int32ConstantFrom(div->InputAt(1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004205 if (value == 1 || value == 0 || value == -1) {
4206 // No temp register required.
4207 } else {
4208 locations->AddTemp(Location::RequiresRegister());
4209 if (!IsPowerOfTwo(AbsOrMin(value))) {
4210 locations->AddTemp(Location::RequiresRegister());
4211 }
4212 }
4213 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4214 locations->SetInAt(0, Location::RequiresRegister());
4215 locations->SetInAt(1, Location::RequiresRegister());
4216 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4217 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004218 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4219 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4220 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004221 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004222 // we only need the former.
4223 locations->SetOut(LocationFrom(r0));
Scott Wakelingfe885462016-09-22 10:24:38 +01004224 }
4225 break;
4226 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004227 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004228 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4229 locations->SetInAt(0, LocationFrom(
4230 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4231 locations->SetInAt(1, LocationFrom(
4232 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4233 locations->SetOut(LocationFrom(r0, r1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004234 break;
4235 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004236 case DataType::Type::kFloat32:
4237 case DataType::Type::kFloat64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004238 locations->SetInAt(0, Location::RequiresFpuRegister());
4239 locations->SetInAt(1, Location::RequiresFpuRegister());
4240 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4241 break;
4242 }
4243
4244 default:
4245 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4246 }
4247}
4248
4249void InstructionCodeGeneratorARMVIXL::VisitDiv(HDiv* div) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004250 Location lhs = div->GetLocations()->InAt(0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004251 Location rhs = div->GetLocations()->InAt(1);
Scott Wakelingfe885462016-09-22 10:24:38 +01004252
4253 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004254 case DataType::Type::kInt32: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004255 if (rhs.IsConstant()) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004256 GenerateDivRemConstantIntegral(div);
4257 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4258 __ Sdiv(OutputRegister(div), InputRegisterAt(div, 0), InputRegisterAt(div, 1));
4259 } else {
Artem Serov551b28f2016-10-18 19:11:30 +01004260 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4261 DCHECK(calling_convention.GetRegisterAt(0).Is(RegisterFrom(lhs)));
4262 DCHECK(calling_convention.GetRegisterAt(1).Is(RegisterFrom(rhs)));
4263 DCHECK(r0.Is(OutputRegister(div)));
4264
4265 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
4266 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004267 }
4268 break;
4269 }
4270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004271 case DataType::Type::kInt64: {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01004272 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4273 DCHECK(calling_convention.GetRegisterAt(0).Is(LowRegisterFrom(lhs)));
4274 DCHECK(calling_convention.GetRegisterAt(1).Is(HighRegisterFrom(lhs)));
4275 DCHECK(calling_convention.GetRegisterAt(2).Is(LowRegisterFrom(rhs)));
4276 DCHECK(calling_convention.GetRegisterAt(3).Is(HighRegisterFrom(rhs)));
4277 DCHECK(LowRegisterFrom(div->GetLocations()->Out()).Is(r0));
4278 DCHECK(HighRegisterFrom(div->GetLocations()->Out()).Is(r1));
4279
4280 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
4281 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Scott Wakelingfe885462016-09-22 10:24:38 +01004282 break;
4283 }
4284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kFloat32:
4286 case DataType::Type::kFloat64:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004287 __ Vdiv(OutputVRegister(div), InputVRegisterAt(div, 0), InputVRegisterAt(div, 1));
Scott Wakelingfe885462016-09-22 10:24:38 +01004288 break;
Scott Wakelingfe885462016-09-22 10:24:38 +01004289
4290 default:
4291 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4292 }
4293}
4294
Artem Serov551b28f2016-10-18 19:11:30 +01004295void LocationsBuilderARMVIXL::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004296 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004297
4298 // Most remainders are implemented in the runtime.
4299 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004300 if (rem->GetResultType() == DataType::Type::kInt32 && rem->InputAt(1)->IsConstant()) {
Artem Serov551b28f2016-10-18 19:11:30 +01004301 // sdiv will be replaced by other instruction sequence.
4302 call_kind = LocationSummary::kNoCall;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004303 } else if ((rem->GetResultType() == DataType::Type::kInt32)
Artem Serov551b28f2016-10-18 19:11:30 +01004304 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4305 // Have hardware divide instruction for int, do it with three instructions.
4306 call_kind = LocationSummary::kNoCall;
4307 }
4308
Vladimir Markoca6fff82017-10-03 14:49:14 +01004309 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Artem Serov551b28f2016-10-18 19:11:30 +01004310
4311 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004313 if (rem->InputAt(1)->IsConstant()) {
4314 locations->SetInAt(0, Location::RequiresRegister());
4315 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
4316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov644032c2016-12-06 17:51:43 +00004317 int32_t value = Int32ConstantFrom(rem->InputAt(1));
Artem Serov551b28f2016-10-18 19:11:30 +01004318 if (value == 1 || value == 0 || value == -1) {
4319 // No temp register required.
4320 } else {
4321 locations->AddTemp(Location::RequiresRegister());
4322 if (!IsPowerOfTwo(AbsOrMin(value))) {
4323 locations->AddTemp(Location::RequiresRegister());
4324 }
4325 }
4326 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4327 locations->SetInAt(0, Location::RequiresRegister());
4328 locations->SetInAt(1, Location::RequiresRegister());
4329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4330 locations->AddTemp(Location::RequiresRegister());
4331 } else {
4332 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4333 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4334 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004335 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Artem Serov551b28f2016-10-18 19:11:30 +01004336 // we only need the latter.
4337 locations->SetOut(LocationFrom(r1));
4338 }
4339 break;
4340 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004341 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004342 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4343 locations->SetInAt(0, LocationFrom(
4344 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4345 locations->SetInAt(1, LocationFrom(
4346 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4347 // The runtime helper puts the output in R2,R3.
4348 locations->SetOut(LocationFrom(r2, r3));
4349 break;
4350 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004351 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004352 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4353 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4354 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4355 locations->SetOut(LocationFrom(s0));
4356 break;
4357 }
4358
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004360 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4361 locations->SetInAt(0, LocationFrom(
4362 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4363 locations->SetInAt(1, LocationFrom(
4364 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4365 locations->SetOut(LocationFrom(s0, s1));
4366 break;
4367 }
4368
4369 default:
4370 LOG(FATAL) << "Unexpected rem type " << type;
4371 }
4372}
4373
4374void InstructionCodeGeneratorARMVIXL::VisitRem(HRem* rem) {
4375 LocationSummary* locations = rem->GetLocations();
4376 Location second = locations->InAt(1);
4377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004378 DataType::Type type = rem->GetResultType();
Artem Serov551b28f2016-10-18 19:11:30 +01004379 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004380 case DataType::Type::kInt32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004381 vixl32::Register reg1 = InputRegisterAt(rem, 0);
4382 vixl32::Register out_reg = OutputRegister(rem);
4383 if (second.IsConstant()) {
4384 GenerateDivRemConstantIntegral(rem);
4385 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4386 vixl32::Register reg2 = RegisterFrom(second);
4387 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4388
4389 // temp = reg1 / reg2 (integer division)
4390 // dest = reg1 - temp * reg2
4391 __ Sdiv(temp, reg1, reg2);
4392 __ Mls(out_reg, temp, reg2, reg1);
4393 } else {
4394 InvokeRuntimeCallingConventionARMVIXL calling_convention;
4395 DCHECK(reg1.Is(calling_convention.GetRegisterAt(0)));
4396 DCHECK(RegisterFrom(second).Is(calling_convention.GetRegisterAt(1)));
4397 DCHECK(out_reg.Is(r1));
4398
4399 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
4400 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
4401 }
4402 break;
4403 }
4404
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004405 case DataType::Type::kInt64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004406 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
4407 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4408 break;
4409 }
4410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004411 case DataType::Type::kFloat32: {
Artem Serov551b28f2016-10-18 19:11:30 +01004412 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
4413 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4414 break;
4415 }
4416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004417 case DataType::Type::kFloat64: {
Artem Serov551b28f2016-10-18 19:11:30 +01004418 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
4419 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4420 break;
4421 }
4422
4423 default:
4424 LOG(FATAL) << "Unexpected rem type " << type;
4425 }
4426}
4427
Aart Bik1f8d51b2018-02-15 10:42:37 -08004428static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4429 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4430 switch (minmax->GetResultType()) {
4431 case DataType::Type::kInt32:
4432 locations->SetInAt(0, Location::RequiresRegister());
4433 locations->SetInAt(1, Location::RequiresRegister());
4434 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4435 break;
4436 case DataType::Type::kInt64:
4437 locations->SetInAt(0, Location::RequiresRegister());
4438 locations->SetInAt(1, Location::RequiresRegister());
4439 locations->SetOut(Location::SameAsFirstInput());
4440 break;
4441 case DataType::Type::kFloat32:
4442 locations->SetInAt(0, Location::RequiresFpuRegister());
4443 locations->SetInAt(1, Location::RequiresFpuRegister());
4444 locations->SetOut(Location::SameAsFirstInput());
4445 locations->AddTemp(Location::RequiresRegister());
4446 break;
4447 case DataType::Type::kFloat64:
4448 locations->SetInAt(0, Location::RequiresFpuRegister());
4449 locations->SetInAt(1, Location::RequiresFpuRegister());
4450 locations->SetOut(Location::SameAsFirstInput());
4451 break;
4452 default:
4453 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4454 }
4455}
4456
Aart Bik351df3e2018-03-07 11:54:57 -08004457void InstructionCodeGeneratorARMVIXL::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004458 Location op1_loc = locations->InAt(0);
4459 Location op2_loc = locations->InAt(1);
4460 Location out_loc = locations->Out();
4461
4462 vixl32::Register op1 = RegisterFrom(op1_loc);
4463 vixl32::Register op2 = RegisterFrom(op2_loc);
4464 vixl32::Register out = RegisterFrom(out_loc);
4465
4466 __ Cmp(op1, op2);
4467
4468 {
4469 ExactAssemblyScope aas(GetVIXLAssembler(),
4470 3 * kMaxInstructionSizeInBytes,
4471 CodeBufferCheckScope::kMaximumSize);
4472
4473 __ ite(is_min ? lt : gt);
4474 __ mov(is_min ? lt : gt, out, op1);
4475 __ mov(is_min ? ge : le, out, op2);
4476 }
4477}
4478
4479void InstructionCodeGeneratorARMVIXL::GenerateMinMaxLong(LocationSummary* locations, bool is_min) {
4480 Location op1_loc = locations->InAt(0);
4481 Location op2_loc = locations->InAt(1);
4482 Location out_loc = locations->Out();
4483
4484 // Optimization: don't generate any code if inputs are the same.
4485 if (op1_loc.Equals(op2_loc)) {
4486 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4487 return;
4488 }
4489
4490 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
4491 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
4492 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
4493 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
4494 vixl32::Register out_lo = LowRegisterFrom(out_loc);
4495 vixl32::Register out_hi = HighRegisterFrom(out_loc);
4496 UseScratchRegisterScope temps(GetVIXLAssembler());
4497 const vixl32::Register temp = temps.Acquire();
4498
4499 DCHECK(op1_lo.Is(out_lo));
4500 DCHECK(op1_hi.Is(out_hi));
4501
4502 // Compare op1 >= op2, or op1 < op2.
4503 __ Cmp(out_lo, op2_lo);
4504 __ Sbcs(temp, out_hi, op2_hi);
4505
4506 // Now GE/LT condition code is correct for the long comparison.
4507 {
4508 vixl32::ConditionType cond = is_min ? ge : lt;
4509 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4510 3 * kMaxInstructionSizeInBytes,
4511 CodeBufferCheckScope::kMaximumSize);
4512 __ itt(cond);
4513 __ mov(cond, out_lo, op2_lo);
4514 __ mov(cond, out_hi, op2_hi);
4515 }
4516}
4517
Aart Bik351df3e2018-03-07 11:54:57 -08004518void InstructionCodeGeneratorARMVIXL::GenerateMinMaxFloat(HInstruction* minmax, bool is_min) {
4519 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004520 Location op1_loc = locations->InAt(0);
4521 Location op2_loc = locations->InAt(1);
4522 Location out_loc = locations->Out();
4523
4524 // Optimization: don't generate any code if inputs are the same.
4525 if (op1_loc.Equals(op2_loc)) {
4526 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
4527 return;
4528 }
4529
4530 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
4531 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
4532 vixl32::SRegister out = SRegisterFrom(out_loc);
4533
4534 UseScratchRegisterScope temps(GetVIXLAssembler());
4535 const vixl32::Register temp1 = temps.Acquire();
4536 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(0));
4537 vixl32::Label nan, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004538 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004539
4540 DCHECK(op1.Is(out));
4541
4542 __ Vcmp(op1, op2);
4543 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004544 __ B(vs, &nan, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004545
4546 // op1 <> op2
4547 vixl32::ConditionType cond = is_min ? gt : lt;
4548 {
4549 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4550 2 * kMaxInstructionSizeInBytes,
4551 CodeBufferCheckScope::kMaximumSize);
4552 __ it(cond);
4553 __ vmov(cond, F32, out, op2);
4554 }
4555 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004556 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004557
4558 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
4559 __ Vmov(temp1, op1);
4560 __ Vmov(temp2, op2);
4561 if (is_min) {
4562 __ Orr(temp1, temp1, temp2);
4563 } else {
4564 __ And(temp1, temp1, temp2);
4565 }
4566 __ Vmov(out, temp1);
4567 __ B(final_label);
4568
4569 // handle NaN input.
4570 __ Bind(&nan);
4571 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
4572 __ Vmov(out, temp1);
4573
4574 if (done.IsReferenced()) {
4575 __ Bind(&done);
4576 }
4577}
4578
Aart Bik351df3e2018-03-07 11:54:57 -08004579void InstructionCodeGeneratorARMVIXL::GenerateMinMaxDouble(HInstruction* minmax, bool is_min) {
4580 LocationSummary* locations = minmax->GetLocations();
Aart Bik1f8d51b2018-02-15 10:42:37 -08004581 Location op1_loc = locations->InAt(0);
4582 Location op2_loc = locations->InAt(1);
4583 Location out_loc = locations->Out();
4584
4585 // Optimization: don't generate any code if inputs are the same.
4586 if (op1_loc.Equals(op2_loc)) {
4587 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
4588 return;
4589 }
4590
4591 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
4592 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
4593 vixl32::DRegister out = DRegisterFrom(out_loc);
4594 vixl32::Label handle_nan_eq, done;
Aart Bik351df3e2018-03-07 11:54:57 -08004595 vixl32::Label* final_label = codegen_->GetFinalLabel(minmax, &done);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004596
4597 DCHECK(op1.Is(out));
4598
4599 __ Vcmp(op1, op2);
4600 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
Andreas Gampe3db70682018-12-26 15:12:03 -08004601 __ B(vs, &handle_nan_eq, /* is_far_target= */ false); // if un-ordered, go to NaN handling.
Aart Bik1f8d51b2018-02-15 10:42:37 -08004602
4603 // op1 <> op2
4604 vixl32::ConditionType cond = is_min ? gt : lt;
4605 {
4606 ExactAssemblyScope it_scope(GetVIXLAssembler(),
4607 2 * kMaxInstructionSizeInBytes,
4608 CodeBufferCheckScope::kMaximumSize);
4609 __ it(cond);
4610 __ vmov(cond, F64, out, op2);
4611 }
4612 // for <>(not equal), we've done min/max calculation.
Andreas Gampe3db70682018-12-26 15:12:03 -08004613 __ B(ne, final_label, /* is_far_target= */ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004614
4615 // handle op1 == op2, max(+0.0,-0.0).
4616 if (!is_min) {
4617 __ Vand(F64, out, op1, op2);
4618 __ B(final_label);
4619 }
4620
4621 // handle op1 == op2, min(+0.0,-0.0), NaN input.
4622 __ Bind(&handle_nan_eq);
4623 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
4624
4625 if (done.IsReferenced()) {
4626 __ Bind(&done);
4627 }
4628}
4629
Aart Bik351df3e2018-03-07 11:54:57 -08004630void InstructionCodeGeneratorARMVIXL::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4631 DataType::Type type = minmax->GetResultType();
4632 switch (type) {
4633 case DataType::Type::kInt32:
4634 GenerateMinMaxInt(minmax->GetLocations(), is_min);
4635 break;
4636 case DataType::Type::kInt64:
4637 GenerateMinMaxLong(minmax->GetLocations(), is_min);
4638 break;
4639 case DataType::Type::kFloat32:
4640 GenerateMinMaxFloat(minmax, is_min);
4641 break;
4642 case DataType::Type::kFloat64:
4643 GenerateMinMaxDouble(minmax, is_min);
4644 break;
4645 default:
4646 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4647 }
4648}
4649
Aart Bik1f8d51b2018-02-15 10:42:37 -08004650void LocationsBuilderARMVIXL::VisitMin(HMin* min) {
4651 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4652}
4653
4654void InstructionCodeGeneratorARMVIXL::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004655 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004656}
4657
4658void LocationsBuilderARMVIXL::VisitMax(HMax* max) {
4659 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4660}
4661
4662void InstructionCodeGeneratorARMVIXL::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004663 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004664}
4665
Aart Bik3dad3412018-02-28 12:01:46 -08004666void LocationsBuilderARMVIXL::VisitAbs(HAbs* abs) {
4667 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4668 switch (abs->GetResultType()) {
4669 case DataType::Type::kInt32:
4670 case DataType::Type::kInt64:
4671 locations->SetInAt(0, Location::RequiresRegister());
4672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4673 locations->AddTemp(Location::RequiresRegister());
4674 break;
4675 case DataType::Type::kFloat32:
4676 case DataType::Type::kFloat64:
4677 locations->SetInAt(0, Location::RequiresFpuRegister());
4678 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4679 break;
4680 default:
4681 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4682 }
4683}
4684
4685void InstructionCodeGeneratorARMVIXL::VisitAbs(HAbs* abs) {
4686 LocationSummary* locations = abs->GetLocations();
4687 switch (abs->GetResultType()) {
4688 case DataType::Type::kInt32: {
4689 vixl32::Register in_reg = RegisterFrom(locations->InAt(0));
4690 vixl32::Register out_reg = RegisterFrom(locations->Out());
4691 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4692 __ Asr(mask, in_reg, 31);
4693 __ Add(out_reg, in_reg, mask);
4694 __ Eor(out_reg, out_reg, mask);
4695 break;
4696 }
4697 case DataType::Type::kInt64: {
4698 Location in = locations->InAt(0);
4699 vixl32::Register in_reg_lo = LowRegisterFrom(in);
4700 vixl32::Register in_reg_hi = HighRegisterFrom(in);
4701 Location output = locations->Out();
4702 vixl32::Register out_reg_lo = LowRegisterFrom(output);
4703 vixl32::Register out_reg_hi = HighRegisterFrom(output);
4704 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
4705 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
4706 __ Asr(mask, in_reg_hi, 31);
4707 __ Adds(out_reg_lo, in_reg_lo, mask);
4708 __ Adc(out_reg_hi, in_reg_hi, mask);
4709 __ Eor(out_reg_lo, out_reg_lo, mask);
4710 __ Eor(out_reg_hi, out_reg_hi, mask);
4711 break;
4712 }
4713 case DataType::Type::kFloat32:
4714 case DataType::Type::kFloat64:
4715 __ Vabs(OutputVRegister(abs), InputVRegisterAt(abs, 0));
4716 break;
4717 default:
4718 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
4719 }
4720}
Artem Serov551b28f2016-10-18 19:11:30 +01004721
Scott Wakelingfe885462016-09-22 10:24:38 +01004722void LocationsBuilderARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00004723 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004724 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Scott Wakelingfe885462016-09-22 10:24:38 +01004725}
4726
4727void InstructionCodeGeneratorARMVIXL::VisitDivZeroCheck(HDivZeroCheck* instruction) {
4728 DivZeroCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004729 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARMVIXL(instruction);
Scott Wakelingfe885462016-09-22 10:24:38 +01004730 codegen_->AddSlowPath(slow_path);
4731
4732 LocationSummary* locations = instruction->GetLocations();
4733 Location value = locations->InAt(0);
4734
4735 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004736 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004737 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004738 case DataType::Type::kInt8:
4739 case DataType::Type::kUint16:
4740 case DataType::Type::kInt16:
4741 case DataType::Type::kInt32: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004742 if (value.IsRegister()) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00004743 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelingfe885462016-09-22 10:24:38 +01004744 } else {
4745 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004746 if (Int32ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004747 __ B(slow_path->GetEntryLabel());
4748 }
4749 }
4750 break;
4751 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004752 case DataType::Type::kInt64: {
Scott Wakelingfe885462016-09-22 10:24:38 +01004753 if (value.IsRegisterPair()) {
4754 UseScratchRegisterScope temps(GetVIXLAssembler());
4755 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01004756 __ Orrs(temp, LowRegisterFrom(value), HighRegisterFrom(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01004757 __ B(eq, slow_path->GetEntryLabel());
4758 } else {
4759 DCHECK(value.IsConstant()) << value;
Anton Kirilov644032c2016-12-06 17:51:43 +00004760 if (Int64ConstantFrom(value) == 0) {
Scott Wakelingfe885462016-09-22 10:24:38 +01004761 __ B(slow_path->GetEntryLabel());
4762 }
4763 }
4764 break;
4765 }
4766 default:
4767 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4768 }
4769}
4770
Artem Serov02109dd2016-09-23 17:17:54 +01004771void InstructionCodeGeneratorARMVIXL::HandleIntegerRotate(HRor* ror) {
4772 LocationSummary* locations = ror->GetLocations();
4773 vixl32::Register in = InputRegisterAt(ror, 0);
4774 Location rhs = locations->InAt(1);
4775 vixl32::Register out = OutputRegister(ror);
4776
4777 if (rhs.IsConstant()) {
4778 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4779 // so map all rotations to a +ve. equivalent in that range.
4780 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4781 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4782 if (rot) {
4783 // Rotate, mapping left rotations to right equivalents if necessary.
4784 // (e.g. left by 2 bits == right by 30.)
4785 __ Ror(out, in, rot);
4786 } else if (!out.Is(in)) {
4787 __ Mov(out, in);
4788 }
4789 } else {
4790 __ Ror(out, in, RegisterFrom(rhs));
4791 }
4792}
4793
4794// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4795// rotates by swapping input regs (effectively rotating by the first 32-bits of
4796// a larger rotation) or flipping direction (thus treating larger right/left
4797// rotations as sub-word sized rotations in the other direction) as appropriate.
4798void InstructionCodeGeneratorARMVIXL::HandleLongRotate(HRor* ror) {
4799 LocationSummary* locations = ror->GetLocations();
4800 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
4801 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
4802 Location rhs = locations->InAt(1);
4803 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
4804 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
4805
4806 if (rhs.IsConstant()) {
4807 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4808 // Map all rotations to +ve. equivalents on the interval [0,63].
4809 rot &= kMaxLongShiftDistance;
4810 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4811 // logic below to a simple pair of binary orr.
4812 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4813 if (rot >= kArmBitsPerWord) {
4814 rot -= kArmBitsPerWord;
4815 std::swap(in_reg_hi, in_reg_lo);
4816 }
4817 // Rotate, or mov to out for zero or word size rotations.
4818 if (rot != 0u) {
Scott Wakelingb77051e2016-11-21 19:46:00 +00004819 __ Lsr(out_reg_hi, in_reg_hi, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004820 __ Orr(out_reg_hi, out_reg_hi, Operand(in_reg_lo, ShiftType::LSL, kArmBitsPerWord - rot));
Scott Wakelingb77051e2016-11-21 19:46:00 +00004821 __ Lsr(out_reg_lo, in_reg_lo, Operand::From(rot));
Artem Serov02109dd2016-09-23 17:17:54 +01004822 __ Orr(out_reg_lo, out_reg_lo, Operand(in_reg_hi, ShiftType::LSL, kArmBitsPerWord - rot));
4823 } else {
4824 __ Mov(out_reg_lo, in_reg_lo);
4825 __ Mov(out_reg_hi, in_reg_hi);
4826 }
4827 } else {
4828 vixl32::Register shift_right = RegisterFrom(locations->GetTemp(0));
4829 vixl32::Register shift_left = RegisterFrom(locations->GetTemp(1));
4830 vixl32::Label end;
4831 vixl32::Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004832 vixl32::Label* final_label = codegen_->GetFinalLabel(ror, &end);
Artem Serov02109dd2016-09-23 17:17:54 +01004833
4834 __ And(shift_right, RegisterFrom(rhs), 0x1F);
4835 __ Lsrs(shift_left, RegisterFrom(rhs), 6);
Scott Wakelingbffdc702016-12-07 17:46:03 +00004836 __ Rsb(LeaveFlags, shift_left, shift_right, Operand::From(kArmBitsPerWord));
Andreas Gampe3db70682018-12-26 15:12:03 -08004837 __ B(cc, &shift_by_32_plus_shift_right, /* is_far_target= */ false);
Artem Serov02109dd2016-09-23 17:17:54 +01004838
4839 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4840 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4841 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4842 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4843 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4844 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4845 __ Lsr(shift_left, in_reg_hi, shift_right);
4846 __ Add(out_reg_lo, out_reg_lo, shift_left);
Anton Kirilov6f644202017-02-27 18:29:45 +00004847 __ B(final_label);
Artem Serov02109dd2016-09-23 17:17:54 +01004848
4849 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4850 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4851 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4852 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4853 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4854 __ Add(out_reg_hi, out_reg_hi, out_reg_lo);
4855 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4856 __ Lsl(shift_right, in_reg_hi, shift_left);
4857 __ Add(out_reg_lo, out_reg_lo, shift_right);
4858
Anton Kirilov6f644202017-02-27 18:29:45 +00004859 if (end.IsReferenced()) {
4860 __ Bind(&end);
4861 }
Artem Serov02109dd2016-09-23 17:17:54 +01004862 }
4863}
4864
4865void LocationsBuilderARMVIXL::VisitRor(HRor* ror) {
4866 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004867 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Artem Serov02109dd2016-09-23 17:17:54 +01004868 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004870 locations->SetInAt(0, Location::RequiresRegister());
4871 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4872 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4873 break;
4874 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004875 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004876 locations->SetInAt(0, Location::RequiresRegister());
4877 if (ror->InputAt(1)->IsConstant()) {
4878 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4879 } else {
4880 locations->SetInAt(1, Location::RequiresRegister());
4881 locations->AddTemp(Location::RequiresRegister());
4882 locations->AddTemp(Location::RequiresRegister());
4883 }
4884 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4885 break;
4886 }
4887 default:
4888 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4889 }
4890}
4891
4892void InstructionCodeGeneratorARMVIXL::VisitRor(HRor* ror) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004893 DataType::Type type = ror->GetResultType();
Artem Serov02109dd2016-09-23 17:17:54 +01004894 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004895 case DataType::Type::kInt32: {
Artem Serov02109dd2016-09-23 17:17:54 +01004896 HandleIntegerRotate(ror);
4897 break;
4898 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004899 case DataType::Type::kInt64: {
Artem Serov02109dd2016-09-23 17:17:54 +01004900 HandleLongRotate(ror);
4901 break;
4902 }
4903 default:
4904 LOG(FATAL) << "Unexpected operation type " << type;
4905 UNREACHABLE();
4906 }
4907}
4908
Artem Serov02d37832016-10-25 15:25:33 +01004909void LocationsBuilderARMVIXL::HandleShift(HBinaryOperation* op) {
4910 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4911
4912 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004913 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01004914
4915 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004916 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004917 locations->SetInAt(0, Location::RequiresRegister());
4918 if (op->InputAt(1)->IsConstant()) {
4919 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4921 } else {
4922 locations->SetInAt(1, Location::RequiresRegister());
4923 // Make the output overlap, as it will be used to hold the masked
4924 // second input.
4925 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4926 }
4927 break;
4928 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004929 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004930 locations->SetInAt(0, Location::RequiresRegister());
4931 if (op->InputAt(1)->IsConstant()) {
4932 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4933 // For simplicity, use kOutputOverlap even though we only require that low registers
4934 // don't clash with high registers which the register allocator currently guarantees.
4935 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4936 } else {
4937 locations->SetInAt(1, Location::RequiresRegister());
4938 locations->AddTemp(Location::RequiresRegister());
4939 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4940 }
4941 break;
4942 }
4943 default:
4944 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4945 }
4946}
4947
4948void InstructionCodeGeneratorARMVIXL::HandleShift(HBinaryOperation* op) {
4949 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4950
4951 LocationSummary* locations = op->GetLocations();
4952 Location out = locations->Out();
4953 Location first = locations->InAt(0);
4954 Location second = locations->InAt(1);
4955
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004956 DataType::Type type = op->GetResultType();
Artem Serov02d37832016-10-25 15:25:33 +01004957 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004958 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01004959 vixl32::Register out_reg = OutputRegister(op);
4960 vixl32::Register first_reg = InputRegisterAt(op, 0);
4961 if (second.IsRegister()) {
4962 vixl32::Register second_reg = RegisterFrom(second);
4963 // ARM doesn't mask the shift count so we need to do it ourselves.
4964 __ And(out_reg, second_reg, kMaxIntShiftDistance);
4965 if (op->IsShl()) {
4966 __ Lsl(out_reg, first_reg, out_reg);
4967 } else if (op->IsShr()) {
4968 __ Asr(out_reg, first_reg, out_reg);
4969 } else {
4970 __ Lsr(out_reg, first_reg, out_reg);
4971 }
4972 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00004973 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01004974 uint32_t shift_value = cst & kMaxIntShiftDistance;
4975 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
4976 __ Mov(out_reg, first_reg);
4977 } else if (op->IsShl()) {
4978 __ Lsl(out_reg, first_reg, shift_value);
4979 } else if (op->IsShr()) {
4980 __ Asr(out_reg, first_reg, shift_value);
4981 } else {
4982 __ Lsr(out_reg, first_reg, shift_value);
4983 }
4984 }
4985 break;
4986 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004987 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01004988 vixl32::Register o_h = HighRegisterFrom(out);
4989 vixl32::Register o_l = LowRegisterFrom(out);
4990
4991 vixl32::Register high = HighRegisterFrom(first);
4992 vixl32::Register low = LowRegisterFrom(first);
4993
4994 if (second.IsRegister()) {
4995 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
4996
4997 vixl32::Register second_reg = RegisterFrom(second);
4998
4999 if (op->IsShl()) {
5000 __ And(o_l, second_reg, kMaxLongShiftDistance);
5001 // Shift the high part
5002 __ Lsl(o_h, high, o_l);
5003 // Shift the low part and `or` what overflew on the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005004 __ Rsb(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005005 __ Lsr(temp, low, temp);
5006 __ Orr(o_h, o_h, temp);
5007 // If the shift is > 32 bits, override the high part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005008 __ Subs(temp, o_l, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005009 {
Artem Serov0fb37192016-12-06 18:13:40 +00005010 ExactAssemblyScope guard(GetVIXLAssembler(),
5011 2 * vixl32::kMaxInstructionSizeInBytes,
5012 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005013 __ it(pl);
5014 __ lsl(pl, o_h, low, temp);
5015 }
5016 // Shift the low part
5017 __ Lsl(o_l, low, o_l);
5018 } else if (op->IsShr()) {
5019 __ And(o_h, second_reg, kMaxLongShiftDistance);
5020 // Shift the low part
5021 __ Lsr(o_l, low, o_h);
5022 // Shift the high part and `or` what underflew on the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005023 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005024 __ Lsl(temp, high, temp);
5025 __ Orr(o_l, o_l, temp);
5026 // If the shift is > 32 bits, override the low part
Scott Wakelingb77051e2016-11-21 19:46:00 +00005027 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005028 {
Artem Serov0fb37192016-12-06 18:13:40 +00005029 ExactAssemblyScope guard(GetVIXLAssembler(),
5030 2 * vixl32::kMaxInstructionSizeInBytes,
5031 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005032 __ it(pl);
5033 __ asr(pl, o_l, high, temp);
5034 }
5035 // Shift the high part
5036 __ Asr(o_h, high, o_h);
5037 } else {
5038 __ And(o_h, second_reg, kMaxLongShiftDistance);
5039 // same as Shr except we use `Lsr`s and not `Asr`s
5040 __ Lsr(o_l, low, o_h);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005041 __ Rsb(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005042 __ Lsl(temp, high, temp);
5043 __ Orr(o_l, o_l, temp);
Scott Wakelingb77051e2016-11-21 19:46:00 +00005044 __ Subs(temp, o_h, Operand::From(kArmBitsPerWord));
Artem Serov02d37832016-10-25 15:25:33 +01005045 {
Artem Serov0fb37192016-12-06 18:13:40 +00005046 ExactAssemblyScope guard(GetVIXLAssembler(),
5047 2 * vixl32::kMaxInstructionSizeInBytes,
5048 CodeBufferCheckScope::kMaximumSize);
Artem Serov02d37832016-10-25 15:25:33 +01005049 __ it(pl);
5050 __ lsr(pl, o_l, high, temp);
5051 }
5052 __ Lsr(o_h, high, o_h);
5053 }
5054 } else {
5055 // Register allocator doesn't create partial overlap.
5056 DCHECK(!o_l.Is(high));
5057 DCHECK(!o_h.Is(low));
Anton Kirilov644032c2016-12-06 17:51:43 +00005058 int32_t cst = Int32ConstantFrom(second);
Artem Serov02d37832016-10-25 15:25:33 +01005059 uint32_t shift_value = cst & kMaxLongShiftDistance;
5060 if (shift_value > 32) {
5061 if (op->IsShl()) {
5062 __ Lsl(o_h, low, shift_value - 32);
5063 __ Mov(o_l, 0);
5064 } else if (op->IsShr()) {
5065 __ Asr(o_l, high, shift_value - 32);
5066 __ Asr(o_h, high, 31);
5067 } else {
5068 __ Lsr(o_l, high, shift_value - 32);
5069 __ Mov(o_h, 0);
5070 }
5071 } else if (shift_value == 32) {
5072 if (op->IsShl()) {
5073 __ Mov(o_h, low);
5074 __ Mov(o_l, 0);
5075 } else if (op->IsShr()) {
5076 __ Mov(o_l, high);
5077 __ Asr(o_h, high, 31);
5078 } else {
5079 __ Mov(o_l, high);
5080 __ Mov(o_h, 0);
5081 }
5082 } else if (shift_value == 1) {
5083 if (op->IsShl()) {
5084 __ Lsls(o_l, low, 1);
5085 __ Adc(o_h, high, high);
5086 } else if (op->IsShr()) {
5087 __ Asrs(o_h, high, 1);
5088 __ Rrx(o_l, low);
5089 } else {
5090 __ Lsrs(o_h, high, 1);
5091 __ Rrx(o_l, low);
5092 }
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005093 } else if (shift_value == 0) {
5094 __ Mov(o_l, low);
5095 __ Mov(o_h, high);
Artem Serov02d37832016-10-25 15:25:33 +01005096 } else {
Nicolas Geoffray9b195cc2019-04-02 08:29:00 +01005097 DCHECK(0 < shift_value && shift_value < 32) << shift_value;
Artem Serov02d37832016-10-25 15:25:33 +01005098 if (op->IsShl()) {
5099 __ Lsl(o_h, high, shift_value);
5100 __ Orr(o_h, o_h, Operand(low, ShiftType::LSR, 32 - shift_value));
5101 __ Lsl(o_l, low, shift_value);
5102 } else if (op->IsShr()) {
5103 __ Lsr(o_l, low, shift_value);
5104 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5105 __ Asr(o_h, high, shift_value);
5106 } else {
5107 __ Lsr(o_l, low, shift_value);
5108 __ Orr(o_l, o_l, Operand(high, ShiftType::LSL, 32 - shift_value));
5109 __ Lsr(o_h, high, shift_value);
5110 }
5111 }
5112 }
5113 break;
5114 }
5115 default:
5116 LOG(FATAL) << "Unexpected operation type " << type;
5117 UNREACHABLE();
5118 }
5119}
5120
5121void LocationsBuilderARMVIXL::VisitShl(HShl* shl) {
5122 HandleShift(shl);
5123}
5124
5125void InstructionCodeGeneratorARMVIXL::VisitShl(HShl* shl) {
5126 HandleShift(shl);
5127}
5128
5129void LocationsBuilderARMVIXL::VisitShr(HShr* shr) {
5130 HandleShift(shr);
5131}
5132
5133void InstructionCodeGeneratorARMVIXL::VisitShr(HShr* shr) {
5134 HandleShift(shr);
5135}
5136
5137void LocationsBuilderARMVIXL::VisitUShr(HUShr* ushr) {
5138 HandleShift(ushr);
5139}
5140
5141void InstructionCodeGeneratorARMVIXL::VisitUShr(HUShr* ushr) {
5142 HandleShift(ushr);
5143}
5144
5145void LocationsBuilderARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005146 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5147 instruction, LocationSummary::kCallOnMainOnly);
Alex Lightd109e302018-06-27 10:25:41 -07005148 InvokeRuntimeCallingConventionARMVIXL calling_convention;
5149 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Artem Serov02d37832016-10-25 15:25:33 +01005150 locations->SetOut(LocationFrom(r0));
5151}
5152
5153void InstructionCodeGeneratorARMVIXL::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005154 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5155 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005156 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 11);
Artem Serov02d37832016-10-25 15:25:33 +01005157}
5158
5159void LocationsBuilderARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005160 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5161 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01005162 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Artem Serov02d37832016-10-25 15:25:33 +01005163 locations->SetOut(LocationFrom(r0));
Nicolas Geoffray8c7c4f12017-01-26 10:13:11 +00005164 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5165 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Artem Serov02d37832016-10-25 15:25:33 +01005166}
5167
5168void InstructionCodeGeneratorARMVIXL::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005169 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5170 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Artem Serov7b3672e2017-02-03 17:30:34 +00005171 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005172 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Artem Serov7b3672e2017-02-03 17:30:34 +00005173 DCHECK(!codegen_->IsLeafMethod());
Andreas Gampe3db70682018-12-26 15:12:03 -08005174 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 12);
Artem Serov02d37832016-10-25 15:25:33 +01005175}
5176
5177void LocationsBuilderARMVIXL::VisitParameterValue(HParameterValue* instruction) {
5178 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005179 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005180 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5181 if (location.IsStackSlot()) {
5182 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5183 } else if (location.IsDoubleStackSlot()) {
5184 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5185 }
5186 locations->SetOut(location);
5187}
5188
5189void InstructionCodeGeneratorARMVIXL::VisitParameterValue(
5190 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5191 // Nothing to do, the parameter is already at its location.
5192}
5193
5194void LocationsBuilderARMVIXL::VisitCurrentMethod(HCurrentMethod* instruction) {
5195 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005196 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005197 locations->SetOut(LocationFrom(kMethodRegister));
5198}
5199
5200void InstructionCodeGeneratorARMVIXL::VisitCurrentMethod(
5201 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5202 // Nothing to do, the method is already at its location.
5203}
5204
5205void LocationsBuilderARMVIXL::VisitNot(HNot* not_) {
5206 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005207 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005208 locations->SetInAt(0, Location::RequiresRegister());
5209 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5210}
5211
5212void InstructionCodeGeneratorARMVIXL::VisitNot(HNot* not_) {
5213 LocationSummary* locations = not_->GetLocations();
5214 Location out = locations->Out();
5215 Location in = locations->InAt(0);
5216 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 case DataType::Type::kInt32:
Artem Serov02d37832016-10-25 15:25:33 +01005218 __ Mvn(OutputRegister(not_), InputRegisterAt(not_, 0));
5219 break;
5220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005221 case DataType::Type::kInt64:
Artem Serov02d37832016-10-25 15:25:33 +01005222 __ Mvn(LowRegisterFrom(out), LowRegisterFrom(in));
5223 __ Mvn(HighRegisterFrom(out), HighRegisterFrom(in));
5224 break;
5225
5226 default:
5227 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5228 }
5229}
5230
Scott Wakelingc34dba72016-10-03 10:14:44 +01005231void LocationsBuilderARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005233 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005234 locations->SetInAt(0, Location::RequiresRegister());
5235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5236}
5237
5238void InstructionCodeGeneratorARMVIXL::VisitBooleanNot(HBooleanNot* bool_not) {
5239 __ Eor(OutputRegister(bool_not), InputRegister(bool_not), 1);
5240}
5241
Artem Serov02d37832016-10-25 15:25:33 +01005242void LocationsBuilderARMVIXL::VisitCompare(HCompare* compare) {
5243 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005244 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005245 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005246 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005247 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005249 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005250 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005251 case DataType::Type::kInt32:
5252 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005253 locations->SetInAt(0, Location::RequiresRegister());
5254 locations->SetInAt(1, Location::RequiresRegister());
5255 // Output overlaps because it is written before doing the low comparison.
5256 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5257 break;
5258 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005259 case DataType::Type::kFloat32:
5260 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005261 locations->SetInAt(0, Location::RequiresFpuRegister());
5262 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
5263 locations->SetOut(Location::RequiresRegister());
5264 break;
5265 }
5266 default:
5267 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5268 }
5269}
5270
5271void InstructionCodeGeneratorARMVIXL::VisitCompare(HCompare* compare) {
5272 LocationSummary* locations = compare->GetLocations();
5273 vixl32::Register out = OutputRegister(compare);
5274 Location left = locations->InAt(0);
5275 Location right = locations->InAt(1);
5276
5277 vixl32::Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005278 vixl32::Label* final_label = codegen_->GetFinalLabel(compare, &done);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005279 DataType::Type type = compare->InputAt(0)->GetType();
Vladimir Marko33bff252017-11-01 14:35:42 +00005280 vixl32::Condition less_cond = vixl32::Condition::None();
Artem Serov02d37832016-10-25 15:25:33 +01005281 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005283 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005284 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005285 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005286 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005287 case DataType::Type::kInt32: {
Artem Serov02d37832016-10-25 15:25:33 +01005288 // Emit move to `out` before the `Cmp`, as `Mov` might affect the status flags.
5289 __ Mov(out, 0);
5290 __ Cmp(RegisterFrom(left), RegisterFrom(right)); // Signed compare.
5291 less_cond = lt;
5292 break;
5293 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005294 case DataType::Type::kInt64: {
Artem Serov02d37832016-10-25 15:25:33 +01005295 __ Cmp(HighRegisterFrom(left), HighRegisterFrom(right)); // Signed compare.
Andreas Gampe3db70682018-12-26 15:12:03 -08005296 __ B(lt, &less, /* is_far_target= */ false);
5297 __ B(gt, &greater, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005298 // Emit move to `out` before the last `Cmp`, as `Mov` might affect the status flags.
5299 __ Mov(out, 0);
5300 __ Cmp(LowRegisterFrom(left), LowRegisterFrom(right)); // Unsigned compare.
5301 less_cond = lo;
5302 break;
5303 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005304 case DataType::Type::kFloat32:
5305 case DataType::Type::kFloat64: {
Artem Serov02d37832016-10-25 15:25:33 +01005306 __ Mov(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005307 GenerateVcmp(compare, codegen_);
Artem Serov02d37832016-10-25 15:25:33 +01005308 // To branch on the FP compare result we transfer FPSCR to APSR (encoded as PC in VMRS).
5309 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
5310 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
5311 break;
5312 }
5313 default:
5314 LOG(FATAL) << "Unexpected compare type " << type;
5315 UNREACHABLE();
5316 }
5317
Andreas Gampe3db70682018-12-26 15:12:03 -08005318 __ B(eq, final_label, /* is_far_target= */ false);
5319 __ B(less_cond, &less, /* is_far_target= */ false);
Artem Serov02d37832016-10-25 15:25:33 +01005320
5321 __ Bind(&greater);
5322 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005323 __ B(final_label);
Artem Serov02d37832016-10-25 15:25:33 +01005324
5325 __ Bind(&less);
5326 __ Mov(out, -1);
5327
Anton Kirilov6f644202017-02-27 18:29:45 +00005328 if (done.IsReferenced()) {
5329 __ Bind(&done);
5330 }
Artem Serov02d37832016-10-25 15:25:33 +01005331}
5332
5333void LocationsBuilderARMVIXL::VisitPhi(HPhi* instruction) {
5334 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005335 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005336 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
5337 locations->SetInAt(i, Location::Any());
5338 }
5339 locations->SetOut(Location::Any());
5340}
5341
5342void InstructionCodeGeneratorARMVIXL::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5343 LOG(FATAL) << "Unreachable";
5344}
5345
5346void CodeGeneratorARMVIXL::GenerateMemoryBarrier(MemBarrierKind kind) {
5347 // TODO (ported from quick): revisit ARM barrier kinds.
5348 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
5349 switch (kind) {
5350 case MemBarrierKind::kAnyStore:
5351 case MemBarrierKind::kLoadAny:
5352 case MemBarrierKind::kAnyAny: {
5353 flavor = DmbOptions::ISH;
5354 break;
5355 }
5356 case MemBarrierKind::kStoreStore: {
5357 flavor = DmbOptions::ISHST;
5358 break;
5359 }
5360 default:
5361 LOG(FATAL) << "Unexpected memory barrier " << kind;
5362 }
5363 __ Dmb(flavor);
5364}
5365
5366void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicLoad(vixl32::Register addr,
5367 uint32_t offset,
5368 vixl32::Register out_lo,
5369 vixl32::Register out_hi) {
5370 UseScratchRegisterScope temps(GetVIXLAssembler());
5371 if (offset != 0) {
5372 vixl32::Register temp = temps.Acquire();
5373 __ Add(temp, addr, offset);
5374 addr = temp;
5375 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005376 __ Ldrexd(out_lo, out_hi, MemOperand(addr));
Artem Serov02d37832016-10-25 15:25:33 +01005377}
5378
5379void InstructionCodeGeneratorARMVIXL::GenerateWideAtomicStore(vixl32::Register addr,
5380 uint32_t offset,
5381 vixl32::Register value_lo,
5382 vixl32::Register value_hi,
5383 vixl32::Register temp1,
5384 vixl32::Register temp2,
5385 HInstruction* instruction) {
5386 UseScratchRegisterScope temps(GetVIXLAssembler());
5387 vixl32::Label fail;
5388 if (offset != 0) {
5389 vixl32::Register temp = temps.Acquire();
5390 __ Add(temp, addr, offset);
5391 addr = temp;
5392 }
5393 __ Bind(&fail);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005394 {
5395 // Ensure the pc position is recorded immediately after the `ldrexd` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005396 ExactAssemblyScope aas(GetVIXLAssembler(),
5397 vixl32::kMaxInstructionSizeInBytes,
5398 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00005399 // We need a load followed by store. (The address used in a STREX instruction must
5400 // be the same as the address in the most recently executed LDREX instruction.)
5401 __ ldrexd(temp1, temp2, MemOperand(addr));
5402 codegen_->MaybeRecordImplicitNullCheck(instruction);
5403 }
Scott Wakelingb77051e2016-11-21 19:46:00 +00005404 __ Strexd(temp1, value_lo, value_hi, MemOperand(addr));
xueliang.zhongf51bc622016-11-04 09:23:32 +00005405 __ CompareAndBranchIfNonZero(temp1, &fail);
Artem Serov02d37832016-10-25 15:25:33 +01005406}
Artem Serov02109dd2016-09-23 17:17:54 +01005407
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005408void LocationsBuilderARMVIXL::HandleFieldSet(
5409 HInstruction* instruction, const FieldInfo& field_info) {
5410 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5411
5412 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005413 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005414 locations->SetInAt(0, Location::RequiresRegister());
5415
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005416 DataType::Type field_type = field_info.GetFieldType();
5417 if (DataType::IsFloatingPointType(field_type)) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005418 locations->SetInAt(1, Location::RequiresFpuRegister());
5419 } else {
5420 locations->SetInAt(1, Location::RequiresRegister());
5421 }
5422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005423 bool is_wide = field_type == DataType::Type::kInt64 || field_type == DataType::Type::kFloat64;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005424 bool generate_volatile = field_info.IsVolatile()
5425 && is_wide
5426 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5427 bool needs_write_barrier =
5428 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5429 // Temporary registers for the write barrier.
5430 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
5431 if (needs_write_barrier) {
5432 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
5433 locations->AddTemp(Location::RequiresRegister());
5434 } else if (generate_volatile) {
5435 // ARM encoding have some additional constraints for ldrexd/strexd:
5436 // - registers need to be consecutive
5437 // - the first register should be even but not R14.
5438 // We don't test for ARM yet, and the assertion makes sure that we
5439 // revisit this if we ever enable ARM encoding.
5440 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5441
5442 locations->AddTemp(Location::RequiresRegister());
5443 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005444 if (field_type == DataType::Type::kFloat64) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005445 // For doubles we need two more registers to copy the value.
5446 locations->AddTemp(LocationFrom(r2));
5447 locations->AddTemp(LocationFrom(r3));
5448 }
5449 }
5450}
5451
5452void InstructionCodeGeneratorARMVIXL::HandleFieldSet(HInstruction* instruction,
5453 const FieldInfo& field_info,
5454 bool value_can_be_null) {
5455 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5456
5457 LocationSummary* locations = instruction->GetLocations();
5458 vixl32::Register base = InputRegisterAt(instruction, 0);
5459 Location value = locations->InAt(1);
5460
5461 bool is_volatile = field_info.IsVolatile();
5462 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005463 DataType::Type field_type = field_info.GetFieldType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005464 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5465 bool needs_write_barrier =
5466 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
5467
5468 if (is_volatile) {
5469 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5470 }
5471
5472 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005473 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005474 case DataType::Type::kUint8:
5475 case DataType::Type::kInt8:
5476 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005477 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005478 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005479 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5480 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005481 StoreOperandType operand_type = GetStoreOperandType(field_type);
5482 GetAssembler()->StoreToOffset(operand_type, RegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005484 break;
5485 }
5486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005487 case DataType::Type::kReference: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005488 vixl32::Register value_reg = RegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005489 if (kPoisonHeapReferences && needs_write_barrier) {
5490 // Note that in the case where `value` is a null reference,
5491 // we do not enter this block, as a null reference does not
5492 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005493 DCHECK_EQ(field_type, DataType::Type::kReference);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005494 value_reg = RegisterFrom(locations->GetTemp(0));
5495 __ Mov(value_reg, RegisterFrom(value));
5496 GetAssembler()->PoisonHeapReference(value_reg);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005497 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005498 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5499 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5500 GetAssembler()->StoreToOffset(kStoreWord, value_reg, base, offset);
5501 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005502 break;
5503 }
5504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005505 case DataType::Type::kInt64: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005506 if (is_volatile && !atomic_ldrd_strd) {
5507 GenerateWideAtomicStore(base,
5508 offset,
5509 LowRegisterFrom(value),
5510 HighRegisterFrom(value),
5511 RegisterFrom(locations->GetTemp(0)),
5512 RegisterFrom(locations->GetTemp(1)),
5513 instruction);
5514 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005515 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5516 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005517 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), base, offset);
5518 codegen_->MaybeRecordImplicitNullCheck(instruction);
5519 }
5520 break;
5521 }
5522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005524 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5525 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005526 GetAssembler()->StoreSToOffset(SRegisterFrom(value), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005527 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005528 break;
5529 }
5530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005531 case DataType::Type::kFloat64: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01005532 vixl32::DRegister value_reg = DRegisterFrom(value);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005533 if (is_volatile && !atomic_ldrd_strd) {
5534 vixl32::Register value_reg_lo = RegisterFrom(locations->GetTemp(0));
5535 vixl32::Register value_reg_hi = RegisterFrom(locations->GetTemp(1));
5536
5537 __ Vmov(value_reg_lo, value_reg_hi, value_reg);
5538
5539 GenerateWideAtomicStore(base,
5540 offset,
5541 value_reg_lo,
5542 value_reg_hi,
5543 RegisterFrom(locations->GetTemp(2)),
5544 RegisterFrom(locations->GetTemp(3)),
5545 instruction);
5546 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005547 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
5548 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005549 GetAssembler()->StoreDToOffset(value_reg, base, offset);
5550 codegen_->MaybeRecordImplicitNullCheck(instruction);
5551 }
5552 break;
5553 }
5554
Aart Bik66c158e2018-01-31 12:55:04 -08005555 case DataType::Type::kUint32:
5556 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005557 case DataType::Type::kVoid:
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005558 LOG(FATAL) << "Unreachable type " << field_type;
5559 UNREACHABLE();
5560 }
5561
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005562 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5563 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
5564 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
5565 codegen_->MarkGCCard(temp, card, base, RegisterFrom(value), value_can_be_null);
5566 }
5567
5568 if (is_volatile) {
5569 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5570 }
5571}
5572
Artem Serov02d37832016-10-25 15:25:33 +01005573void LocationsBuilderARMVIXL::HandleFieldGet(HInstruction* instruction,
5574 const FieldInfo& field_info) {
5575 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5576
5577 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005578 kEmitCompilerReadBarrier && (field_info.GetFieldType() == DataType::Type::kReference);
Artem Serov02d37832016-10-25 15:25:33 +01005579 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005580 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5581 object_field_get_with_read_barrier
5582 ? LocationSummary::kCallOnSlowPath
5583 : LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01005584 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5585 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5586 }
5587 locations->SetInAt(0, Location::RequiresRegister());
5588
5589 bool volatile_for_double = field_info.IsVolatile()
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005590 && (field_info.GetFieldType() == DataType::Type::kFloat64)
Artem Serov02d37832016-10-25 15:25:33 +01005591 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
5592 // The output overlaps in case of volatile long: we don't want the
5593 // code generated by GenerateWideAtomicLoad to overwrite the
5594 // object's location. Likewise, in the case of an object field get
5595 // with read barriers enabled, we do not want the load to overwrite
5596 // the object's location, as we need it to emit the read barrier.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 bool overlap =
5598 (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) ||
Artem Serov02d37832016-10-25 15:25:33 +01005599 object_field_get_with_read_barrier;
5600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 if (DataType::IsFloatingPointType(instruction->GetType())) {
Artem Serov02d37832016-10-25 15:25:33 +01005602 locations->SetOut(Location::RequiresFpuRegister());
5603 } else {
5604 locations->SetOut(Location::RequiresRegister(),
5605 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5606 }
5607 if (volatile_for_double) {
5608 // ARM encoding have some additional constraints for ldrexd/strexd:
5609 // - registers need to be consecutive
5610 // - the first register should be even but not R14.
5611 // We don't test for ARM yet, and the assertion makes sure that we
5612 // revisit this if we ever enable ARM encoding.
5613 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5614 locations->AddTemp(Location::RequiresRegister());
5615 locations->AddTemp(Location::RequiresRegister());
5616 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01005617 // We need a temporary register for the read barrier load in
5618 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
5619 // only if the offset is too big.
5620 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005621 locations->AddTemp(Location::RequiresRegister());
5622 }
Artem Serov02d37832016-10-25 15:25:33 +01005623 }
5624}
5625
5626Location LocationsBuilderARMVIXL::ArithmeticZeroOrFpuRegister(HInstruction* input) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005627 DCHECK(DataType::IsFloatingPointType(input->GetType())) << input->GetType();
Artem Serov02d37832016-10-25 15:25:33 +01005628 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5629 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5630 return Location::ConstantLocation(input->AsConstant());
5631 } else {
5632 return Location::RequiresFpuRegister();
5633 }
5634}
5635
Artem Serov02109dd2016-09-23 17:17:54 +01005636Location LocationsBuilderARMVIXL::ArmEncodableConstantOrRegister(HInstruction* constant,
5637 Opcode opcode) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 DCHECK(!DataType::IsFloatingPointType(constant->GetType()));
Artem Serov02109dd2016-09-23 17:17:54 +01005639 if (constant->IsConstant() &&
5640 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5641 return Location::ConstantLocation(constant->AsConstant());
5642 }
5643 return Location::RequiresRegister();
5644}
5645
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005646static bool CanEncode32BitConstantAsImmediate(
5647 CodeGeneratorARMVIXL* codegen,
5648 uint32_t value,
5649 Opcode opcode,
5650 vixl32::FlagsUpdate flags_update = vixl32::FlagsUpdate::DontCare) {
5651 ArmVIXLAssembler* assembler = codegen->GetAssembler();
5652 if (assembler->ShifterOperandCanHold(opcode, value, flags_update)) {
Artem Serov02109dd2016-09-23 17:17:54 +01005653 return true;
5654 }
5655 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005656 uint32_t neg_value = 0;
Artem Serov02109dd2016-09-23 17:17:54 +01005657 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005658 case AND: neg_opcode = BIC; neg_value = ~value; break;
5659 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5660 case ADD: neg_opcode = SUB; neg_value = -value; break;
5661 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5662 case SUB: neg_opcode = ADD; neg_value = -value; break;
5663 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5664 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Artem Serov02109dd2016-09-23 17:17:54 +01005665 default:
5666 return false;
5667 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005668
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005669 if (assembler->ShifterOperandCanHold(neg_opcode, neg_value, flags_update)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005670 return true;
5671 }
5672
5673 return opcode == AND && IsPowerOfTwo(value + 1);
Artem Serov02109dd2016-09-23 17:17:54 +01005674}
5675
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00005676bool LocationsBuilderARMVIXL::CanEncodeConstantAsImmediate(HConstant* input_cst, Opcode opcode) {
5677 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5678 if (DataType::Is64BitType(input_cst->GetType())) {
5679 Opcode high_opcode = opcode;
5680 vixl32::FlagsUpdate low_flags_update = vixl32::FlagsUpdate::DontCare;
5681 switch (opcode) {
5682 case SUB:
5683 // Flip the operation to an ADD.
5684 value = -value;
5685 opcode = ADD;
5686 FALLTHROUGH_INTENDED;
5687 case ADD:
5688 if (Low32Bits(value) == 0u) {
5689 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), opcode);
5690 }
5691 high_opcode = ADC;
5692 low_flags_update = vixl32::FlagsUpdate::SetFlags;
5693 break;
5694 default:
5695 break;
5696 }
5697 return CanEncode32BitConstantAsImmediate(codegen_, High32Bits(value), high_opcode) &&
5698 CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode, low_flags_update);
5699 } else {
5700 return CanEncode32BitConstantAsImmediate(codegen_, Low32Bits(value), opcode);
5701 }
5702}
5703
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005704void InstructionCodeGeneratorARMVIXL::HandleFieldGet(HInstruction* instruction,
5705 const FieldInfo& field_info) {
5706 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
5707
5708 LocationSummary* locations = instruction->GetLocations();
5709 vixl32::Register base = InputRegisterAt(instruction, 0);
5710 Location out = locations->Out();
5711 bool is_volatile = field_info.IsVolatile();
5712 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Vladimir Marko61b92282017-10-11 13:23:17 +01005713 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5714 DataType::Type load_type = instruction->GetType();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005715 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5716
Vladimir Marko61b92282017-10-11 13:23:17 +01005717 switch (load_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005718 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005719 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005721 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005722 case DataType::Type::kInt16:
5723 case DataType::Type::kInt32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005724 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5725 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01005726 LoadOperandType operand_type = GetLoadOperandType(load_type);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005727 GetAssembler()->LoadFromOffset(operand_type, RegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005728 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005729 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005730 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005732 case DataType::Type::kReference: {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005733 // /* HeapReference<Object> */ out = *(base + offset)
5734 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markodcd117e2018-04-19 11:54:00 +01005735 Location maybe_temp = (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005736 // Note that a potential implicit null check is handled in this
5737 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier call.
5738 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005739 instruction, out, base, offset, maybe_temp, /* needs_null_check= */ true);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00005740 if (is_volatile) {
5741 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5742 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005743 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005744 {
5745 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5746 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5747 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(out), base, offset);
5748 codegen_->MaybeRecordImplicitNullCheck(instruction);
5749 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005750 if (is_volatile) {
5751 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5752 }
5753 // If read barriers are enabled, emit read barriers other than
5754 // Baker's using a slow path (and also unpoison the loaded
5755 // reference, if heap poisoning is enabled).
5756 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, locations->InAt(0), offset);
5757 }
5758 break;
5759 }
5760
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005761 case DataType::Type::kInt64: {
5762 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5763 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005764 if (is_volatile && !atomic_ldrd_strd) {
5765 GenerateWideAtomicLoad(base, offset, LowRegisterFrom(out), HighRegisterFrom(out));
5766 } else {
5767 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out), base, offset);
5768 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005769 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005770 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005771 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005772
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005773 case DataType::Type::kFloat32: {
5774 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5775 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005776 GetAssembler()->LoadSFromOffset(SRegisterFrom(out), base, offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005777 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005778 break;
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005779 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01005782 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5783 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005784 vixl32::DRegister out_dreg = DRegisterFrom(out);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005785 if (is_volatile && !atomic_ldrd_strd) {
5786 vixl32::Register lo = RegisterFrom(locations->GetTemp(0));
5787 vixl32::Register hi = RegisterFrom(locations->GetTemp(1));
5788 GenerateWideAtomicLoad(base, offset, lo, hi);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005789 codegen_->MaybeRecordImplicitNullCheck(instruction);
5790 __ Vmov(out_dreg, lo, hi);
5791 } else {
5792 GetAssembler()->LoadDFromOffset(out_dreg, base, offset);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005793 codegen_->MaybeRecordImplicitNullCheck(instruction);
5794 }
5795 break;
5796 }
5797
Aart Bik66c158e2018-01-31 12:55:04 -08005798 case DataType::Type::kUint32:
5799 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005801 LOG(FATAL) << "Unreachable type " << load_type;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005802 UNREACHABLE();
5803 }
5804
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005805 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005806 if (load_type == DataType::Type::kReference) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005807 // Memory barriers, in the case of references, are also handled
5808 // in the previous switch statement.
5809 } else {
5810 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5811 }
5812 }
5813}
5814
5815void LocationsBuilderARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5816 HandleFieldSet(instruction, instruction->GetFieldInfo());
5817}
5818
5819void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5820 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5821}
5822
5823void LocationsBuilderARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5824 HandleFieldGet(instruction, instruction->GetFieldInfo());
5825}
5826
5827void InstructionCodeGeneratorARMVIXL::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5828 HandleFieldGet(instruction, instruction->GetFieldInfo());
5829}
5830
5831void LocationsBuilderARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5832 HandleFieldGet(instruction, instruction->GetFieldInfo());
5833}
5834
5835void InstructionCodeGeneratorARMVIXL::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5836 HandleFieldGet(instruction, instruction->GetFieldInfo());
5837}
5838
Scott Wakelingc34dba72016-10-03 10:14:44 +01005839void LocationsBuilderARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5840 HandleFieldSet(instruction, instruction->GetFieldInfo());
5841}
5842
5843void InstructionCodeGeneratorARMVIXL::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5844 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
5845}
5846
Vladimir Marko552a1342017-10-31 10:56:47 +00005847void LocationsBuilderARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5848 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(r0));
5849}
5850
5851void InstructionCodeGeneratorARMVIXL::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5852 __ Mov(r0, instruction->GetFormat()->GetValue());
5853 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5854}
5855
Artem Serovcfbe9132016-10-14 15:58:56 +01005856void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldGet(
5857 HUnresolvedInstanceFieldGet* instruction) {
5858 FieldAccessCallingConventionARMVIXL calling_convention;
5859 codegen_->CreateUnresolvedFieldLocationSummary(
5860 instruction, instruction->GetFieldType(), calling_convention);
5861}
5862
5863void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldGet(
5864 HUnresolvedInstanceFieldGet* instruction) {
5865 FieldAccessCallingConventionARMVIXL calling_convention;
5866 codegen_->GenerateUnresolvedFieldAccess(instruction,
5867 instruction->GetFieldType(),
5868 instruction->GetFieldIndex(),
5869 instruction->GetDexPc(),
5870 calling_convention);
5871}
5872
5873void LocationsBuilderARMVIXL::VisitUnresolvedInstanceFieldSet(
5874 HUnresolvedInstanceFieldSet* instruction) {
5875 FieldAccessCallingConventionARMVIXL calling_convention;
5876 codegen_->CreateUnresolvedFieldLocationSummary(
5877 instruction, instruction->GetFieldType(), calling_convention);
5878}
5879
5880void InstructionCodeGeneratorARMVIXL::VisitUnresolvedInstanceFieldSet(
5881 HUnresolvedInstanceFieldSet* instruction) {
5882 FieldAccessCallingConventionARMVIXL calling_convention;
5883 codegen_->GenerateUnresolvedFieldAccess(instruction,
5884 instruction->GetFieldType(),
5885 instruction->GetFieldIndex(),
5886 instruction->GetDexPc(),
5887 calling_convention);
5888}
5889
5890void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldGet(
5891 HUnresolvedStaticFieldGet* instruction) {
5892 FieldAccessCallingConventionARMVIXL calling_convention;
5893 codegen_->CreateUnresolvedFieldLocationSummary(
5894 instruction, instruction->GetFieldType(), calling_convention);
5895}
5896
5897void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldGet(
5898 HUnresolvedStaticFieldGet* instruction) {
5899 FieldAccessCallingConventionARMVIXL calling_convention;
5900 codegen_->GenerateUnresolvedFieldAccess(instruction,
5901 instruction->GetFieldType(),
5902 instruction->GetFieldIndex(),
5903 instruction->GetDexPc(),
5904 calling_convention);
5905}
5906
5907void LocationsBuilderARMVIXL::VisitUnresolvedStaticFieldSet(
5908 HUnresolvedStaticFieldSet* instruction) {
5909 FieldAccessCallingConventionARMVIXL calling_convention;
5910 codegen_->CreateUnresolvedFieldLocationSummary(
5911 instruction, instruction->GetFieldType(), calling_convention);
5912}
5913
5914void InstructionCodeGeneratorARMVIXL::VisitUnresolvedStaticFieldSet(
5915 HUnresolvedStaticFieldSet* instruction) {
5916 FieldAccessCallingConventionARMVIXL calling_convention;
5917 codegen_->GenerateUnresolvedFieldAccess(instruction,
5918 instruction->GetFieldType(),
5919 instruction->GetFieldIndex(),
5920 instruction->GetDexPc(),
5921 calling_convention);
5922}
5923
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005924void LocationsBuilderARMVIXL::VisitNullCheck(HNullCheck* instruction) {
Artem Serov657022c2016-11-23 14:19:38 +00005925 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005926 locations->SetInAt(0, Location::RequiresRegister());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005927}
5928
5929void CodeGeneratorARMVIXL::GenerateImplicitNullCheck(HNullCheck* instruction) {
5930 if (CanMoveNullCheckToUser(instruction)) {
5931 return;
5932 }
5933
5934 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames374ddf32016-11-04 10:40:49 +00005935 // Ensure the pc position is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00005936 ExactAssemblyScope aas(GetVIXLAssembler(),
5937 vixl32::kMaxInstructionSizeInBytes,
5938 CodeBufferCheckScope::kMaximumSize);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005939 __ ldr(temps.Acquire(), MemOperand(InputRegisterAt(instruction, 0)));
5940 RecordPcInfo(instruction, instruction->GetDexPc());
5941}
5942
5943void CodeGeneratorARMVIXL::GenerateExplicitNullCheck(HNullCheck* instruction) {
5944 NullCheckSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005945 new (GetScopedAllocator()) NullCheckSlowPathARMVIXL(instruction);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005946 AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00005947 __ CompareAndBranchIfZero(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01005948}
5949
5950void InstructionCodeGeneratorARMVIXL::VisitNullCheck(HNullCheck* instruction) {
5951 codegen_->GenerateNullCheck(instruction);
5952}
5953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954void CodeGeneratorARMVIXL::LoadFromShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005955 Location out_loc,
5956 vixl32::Register base,
5957 vixl32::Register reg_index,
5958 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005959 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005960 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5961
5962 switch (type) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005963 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005964 case DataType::Type::kUint8:
Vladimir Marko61b92282017-10-11 13:23:17 +01005965 __ Ldrb(cond, RegisterFrom(out_loc), mem_address);
5966 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005968 __ Ldrsb(cond, RegisterFrom(out_loc), mem_address);
5969 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 case DataType::Type::kUint16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005971 __ Ldrh(cond, RegisterFrom(out_loc), mem_address);
5972 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005973 case DataType::Type::kInt16:
5974 __ Ldrsh(cond, RegisterFrom(out_loc), mem_address);
5975 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 case DataType::Type::kReference:
5977 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005978 __ Ldr(cond, RegisterFrom(out_loc), mem_address);
5979 break;
5980 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 case DataType::Type::kInt64:
5982 case DataType::Type::kFloat32:
5983 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01005984 default:
5985 LOG(FATAL) << "Unreachable type " << type;
5986 UNREACHABLE();
5987 }
5988}
5989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005990void CodeGeneratorARMVIXL::StoreToShiftedRegOffset(DataType::Type type,
Scott Wakelingc34dba72016-10-03 10:14:44 +01005991 Location loc,
5992 vixl32::Register base,
5993 vixl32::Register reg_index,
5994 vixl32::Condition cond) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 uint32_t shift_count = DataType::SizeShift(type);
Scott Wakelingc34dba72016-10-03 10:14:44 +01005996 MemOperand mem_address(base, reg_index, vixl32::LSL, shift_count);
5997
5998 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006000 case DataType::Type::kUint8:
6001 case DataType::Type::kInt8:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006002 __ Strb(cond, RegisterFrom(loc), mem_address);
6003 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006005 case DataType::Type::kInt16:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006006 __ Strh(cond, RegisterFrom(loc), mem_address);
6007 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008 case DataType::Type::kReference:
6009 case DataType::Type::kInt32:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006010 __ Str(cond, RegisterFrom(loc), mem_address);
6011 break;
6012 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006013 case DataType::Type::kInt64:
6014 case DataType::Type::kFloat32:
6015 case DataType::Type::kFloat64:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006016 default:
6017 LOG(FATAL) << "Unreachable type " << type;
6018 UNREACHABLE();
6019 }
6020}
6021
6022void LocationsBuilderARMVIXL::VisitArrayGet(HArrayGet* instruction) {
6023 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006025 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006026 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6027 object_array_get_with_read_barrier
6028 ? LocationSummary::kCallOnSlowPath
6029 : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006030 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006031 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelingc34dba72016-10-03 10:14:44 +01006032 }
6033 locations->SetInAt(0, Location::RequiresRegister());
6034 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 if (DataType::IsFloatingPointType(instruction->GetType())) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006036 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6037 } else {
6038 // The output overlaps in the case of an object array get with
6039 // read barriers enabled: we do not want the move to overwrite the
6040 // array's location, as we need it to emit the read barrier.
6041 locations->SetOut(
6042 Location::RequiresRegister(),
6043 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6044 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006045 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006046 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006047 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01006048 // We need a temporary register for the read barrier load in
6049 // CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier()
6050 // only if the offset is too big.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006051 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6052 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006053 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006054 if (offset >= kReferenceLoadMinFarOffset) {
6055 locations->AddTemp(Location::RequiresRegister());
6056 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006057 } else {
Vladimir Marko008e09f32018-08-06 15:42:43 +01006058 // We need a non-scratch temporary for the array data pointer in
6059 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier().
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006060 locations->AddTemp(Location::RequiresRegister());
6061 }
6062 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6063 // Also need a temporary for String compression feature.
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006064 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006065 }
6066}
6067
6068void InstructionCodeGeneratorARMVIXL::VisitArrayGet(HArrayGet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006069 LocationSummary* locations = instruction->GetLocations();
6070 Location obj_loc = locations->InAt(0);
6071 vixl32::Register obj = InputRegisterAt(instruction, 0);
6072 Location index = locations->InAt(1);
6073 Location out_loc = locations->Out();
6074 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006075 DataType::Type type = instruction->GetType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006076 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6077 instruction->IsStringCharAt();
6078 HInstruction* array_instr = instruction->GetArray();
6079 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006080
6081 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006082 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006083 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006084 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006086 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006087 case DataType::Type::kInt32: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006088 vixl32::Register length;
6089 if (maybe_compressed_char_at) {
6090 length = RegisterFrom(locations->GetTemp(0));
6091 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006092 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6093 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006094 GetAssembler()->LoadFromOffset(kLoadWord, length, obj, count_offset);
6095 codegen_->MaybeRecordImplicitNullCheck(instruction);
6096 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006097 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006098 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006099 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006100 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006101 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006102 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6103 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6104 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006105 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006106 GetAssembler()->LoadFromOffset(kLoadUnsignedByte,
6107 RegisterFrom(out_loc),
6108 obj,
6109 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006110 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006111 __ Bind(&uncompressed_load);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 GetAssembler()->LoadFromOffset(GetLoadOperandType(DataType::Type::kUint16),
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006113 RegisterFrom(out_loc),
6114 obj,
6115 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006116 if (done.IsReferenced()) {
6117 __ Bind(&done);
6118 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006119 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006120 uint32_t full_offset = data_offset + (const_index << DataType::SizeShift(type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006121
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006122 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6123 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006124 LoadOperandType load_type = GetLoadOperandType(type);
6125 GetAssembler()->LoadFromOffset(load_type, RegisterFrom(out_loc), obj, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006126 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006127 }
6128 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006129 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006130 vixl32::Register temp = temps.Acquire();
6131
6132 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006133 // We do not need to compute the intermediate address from the array: the
6134 // input instruction has done it already. See the comment in
6135 // `TryExtractArrayAccessAddress()`.
6136 if (kIsDebugBuild) {
6137 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006138 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006139 }
6140 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006141 } else {
6142 __ Add(temp, obj, data_offset);
6143 }
6144 if (maybe_compressed_char_at) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006145 vixl32::Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006146 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006147 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6148 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6149 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe3db70682018-12-26 15:12:03 -08006150 __ B(cs, &uncompressed_load, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006151 __ Ldrb(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006152 __ B(final_label);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006153 __ Bind(&uncompressed_load);
6154 __ Ldrh(RegisterFrom(out_loc), MemOperand(temp, RegisterFrom(index), vixl32::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006155 if (done.IsReferenced()) {
6156 __ Bind(&done);
6157 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006158 } else {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006159 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6160 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006161 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006162 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006163 }
6164 }
6165 break;
6166 }
6167
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006168 case DataType::Type::kReference: {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006169 // The read barrier instrumentation of object ArrayGet
6170 // instructions does not support the HIntermediateAddress
6171 // instruction.
6172 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6173
Scott Wakelingc34dba72016-10-03 10:14:44 +01006174 static_assert(
6175 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6176 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6177 // /* HeapReference<Object> */ out =
6178 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6179 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006180 // Note that a potential implicit null check is handled in this
6181 // CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006182 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6183 if (index.IsConstant()) {
6184 // Array load with a constant index can be treated as a field load.
Vladimir Markodcd117e2018-04-19 11:54:00 +01006185 Location maybe_temp =
6186 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006187 data_offset += Int32ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006188 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6189 out_loc,
6190 obj,
6191 data_offset,
Vladimir Markodcd117e2018-04-19 11:54:00 +01006192 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006193 /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006194 } else {
Vladimir Markodcd117e2018-04-19 11:54:00 +01006195 Location temp = locations->GetTemp(0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006196 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006197 out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006198 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006199 } else {
6200 vixl32::Register out = OutputRegister(instruction);
6201 if (index.IsConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006202 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6203 {
6204 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6205 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6206 GetAssembler()->LoadFromOffset(kLoadWord, out, obj, offset);
6207 codegen_->MaybeRecordImplicitNullCheck(instruction);
6208 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006209 // If read barriers are enabled, emit read barriers other than
6210 // Baker's using a slow path (and also unpoison the loaded
6211 // reference, if heap poisoning is enabled).
6212 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6213 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006214 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006215 vixl32::Register temp = temps.Acquire();
6216
6217 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006218 // We do not need to compute the intermediate address from the array: the
6219 // input instruction has done it already. See the comment in
6220 // `TryExtractArrayAccessAddress()`.
6221 if (kIsDebugBuild) {
6222 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006223 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006224 }
6225 temp = obj;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006226 } else {
6227 __ Add(temp, obj, data_offset);
6228 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006229 {
6230 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6231 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6232 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, RegisterFrom(index));
6233 temps.Close();
6234 codegen_->MaybeRecordImplicitNullCheck(instruction);
6235 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006236 // If read barriers are enabled, emit read barriers other than
6237 // Baker's using a slow path (and also unpoison the loaded
6238 // reference, if heap poisoning is enabled).
6239 codegen_->MaybeGenerateReadBarrierSlow(
6240 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6241 }
6242 }
6243 break;
6244 }
6245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006247 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6248 // As two macro instructions can be emitted the max size is doubled.
6249 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006250 if (index.IsConstant()) {
6251 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006252 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006253 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), obj, offset);
6254 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006255 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006256 vixl32::Register temp = temps.Acquire();
6257 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6258 GetAssembler()->LoadFromOffset(kLoadWordPair, LowRegisterFrom(out_loc), temp, data_offset);
6259 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006260 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006261 break;
6262 }
6263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006265 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6266 // As two macro instructions can be emitted the max size is doubled.
6267 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006268 vixl32::SRegister out = SRegisterFrom(out_loc);
6269 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006270 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006271 GetAssembler()->LoadSFromOffset(out, obj, 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 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6276 GetAssembler()->LoadSFromOffset(out, temp, data_offset);
6277 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006278 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006279 break;
6280 }
6281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006283 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
6284 // As two macro instructions can be emitted the max size is doubled.
6285 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006286 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006287 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006288 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), obj, offset);
6289 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006290 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006291 vixl32::Register temp = temps.Acquire();
6292 __ Add(temp, obj, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6293 GetAssembler()->LoadDFromOffset(DRegisterFrom(out_loc), temp, data_offset);
6294 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006295 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006296 break;
6297 }
6298
Aart Bik66c158e2018-01-31 12:55:04 -08006299 case DataType::Type::kUint32:
6300 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006302 LOG(FATAL) << "Unreachable type " << type;
6303 UNREACHABLE();
6304 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006305}
6306
6307void LocationsBuilderARMVIXL::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006308 DataType::Type value_type = instruction->GetComponentType();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006309
6310 bool needs_write_barrier =
6311 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006312 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006313
Vladimir Markoca6fff82017-10-03 14:49:14 +01006314 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Scott Wakelingc34dba72016-10-03 10:14:44 +01006315 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006316 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006317
6318 locations->SetInAt(0, Location::RequiresRegister());
6319 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006320 if (DataType::IsFloatingPointType(value_type)) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006321 locations->SetInAt(2, Location::RequiresFpuRegister());
6322 } else {
6323 locations->SetInAt(2, Location::RequiresRegister());
6324 }
6325 if (needs_write_barrier) {
6326 // Temporary registers for the write barrier.
6327 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6328 locations->AddTemp(Location::RequiresRegister());
6329 }
6330}
6331
6332void InstructionCodeGeneratorARMVIXL::VisitArraySet(HArraySet* instruction) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006333 LocationSummary* locations = instruction->GetLocations();
6334 vixl32::Register array = InputRegisterAt(instruction, 0);
6335 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006336 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006337 bool needs_type_check = instruction->NeedsTypeCheck();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006338 bool needs_write_barrier =
6339 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
6340 uint32_t data_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006341 mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006342 Location value_loc = locations->InAt(2);
6343 HInstruction* array_instr = instruction->GetArray();
6344 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006345
6346 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006347 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006348 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006349 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006350 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006351 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006352 case DataType::Type::kInt32: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006353 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006354 int32_t const_index = Int32ConstantFrom(index);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006355 uint32_t full_offset =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006356 data_offset + (const_index << DataType::SizeShift(value_type));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006357 StoreOperandType store_type = GetStoreOperandType(value_type);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006358 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6359 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006360 GetAssembler()->StoreToOffset(store_type, RegisterFrom(value_loc), array, full_offset);
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006361 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006362 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006363 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006364 vixl32::Register temp = temps.Acquire();
6365
6366 if (has_intermediate_address) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006367 // We do not need to compute the intermediate address from the array: the
6368 // input instruction has done it already. See the comment in
6369 // `TryExtractArrayAccessAddress()`.
6370 if (kIsDebugBuild) {
6371 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
Anton Kirilov644032c2016-12-06 17:51:43 +00006372 DCHECK_EQ(Uint64ConstantFrom(tmp->GetOffset()), data_offset);
Artem Serov2bbc9532016-10-21 11:51:50 +01006373 }
6374 temp = array;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006375 } else {
6376 __ Add(temp, array, data_offset);
6377 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006378 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6379 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006380 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006381 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006382 }
6383 break;
6384 }
6385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006386 case DataType::Type::kReference: {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006387 vixl32::Register value = RegisterFrom(value_loc);
6388 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6389 // See the comment in instruction_simplifier_shared.cc.
6390 DCHECK(!has_intermediate_address);
6391
6392 if (instruction->InputAt(2)->IsNullConstant()) {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006393 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6394 // As two macro instructions can be emitted the max size is doubled.
6395 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006396 // Just setting null.
6397 if (index.IsConstant()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006398 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006399 GetAssembler()->StoreToOffset(kStoreWord, value, array, offset);
6400 } else {
6401 DCHECK(index.IsRegister()) << index;
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006402 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006403 vixl32::Register temp = temps.Acquire();
6404 __ Add(temp, array, data_offset);
6405 codegen_->StoreToShiftedRegOffset(value_type, value_loc, temp, RegisterFrom(index));
6406 }
6407 codegen_->MaybeRecordImplicitNullCheck(instruction);
6408 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006409 DCHECK(!needs_type_check);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006410 break;
6411 }
6412
6413 DCHECK(needs_write_barrier);
6414 Location temp1_loc = locations->GetTemp(0);
6415 vixl32::Register temp1 = RegisterFrom(temp1_loc);
6416 Location temp2_loc = locations->GetTemp(1);
6417 vixl32::Register temp2 = RegisterFrom(temp2_loc);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006418
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006419 bool can_value_be_null = instruction->GetValueCanBeNull();
6420 vixl32::Label do_store;
6421 if (can_value_be_null) {
6422 __ CompareAndBranchIfZero(value, &do_store, /* is_far_target= */ false);
6423 }
6424
6425 SlowPathCodeARMVIXL* slow_path = nullptr;
6426 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006427 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARMVIXL(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006428 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006429
6430 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6431 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6432 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Scott Wakelingc34dba72016-10-03 10:14:44 +01006433
6434 // Note that when read barriers are enabled, the type checks
6435 // are performed without read barriers. This is fine, even in
6436 // the case where a class object is in the from-space after
6437 // the flip, as a comparison involving such a type would not
6438 // produce a false positive; it may of course produce a false
6439 // negative, in which case we would take the ArraySet slow
6440 // path.
6441
Alexandre Rames374ddf32016-11-04 10:40:49 +00006442 {
6443 // Ensure we record the pc position immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00006444 ExactAssemblyScope aas(GetVIXLAssembler(),
6445 vixl32::kMaxInstructionSizeInBytes,
6446 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006447 // /* HeapReference<Class> */ temp1 = array->klass_
6448 __ ldr(temp1, MemOperand(array, class_offset));
6449 codegen_->MaybeRecordImplicitNullCheck(instruction);
6450 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006451 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6452
6453 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6454 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6455 // /* HeapReference<Class> */ temp2 = value->klass_
6456 GetAssembler()->LoadFromOffset(kLoadWord, temp2, value, class_offset);
6457 // If heap poisoning is enabled, no need to unpoison `temp1`
6458 // nor `temp2`, as we are comparing two poisoned references.
6459 __ Cmp(temp1, temp2);
6460
6461 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006462 vixl32::Label do_put;
6463 __ B(eq, &do_put, /* is_far_target= */ false);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006464 // If heap poisoning is enabled, the `temp1` reference has
6465 // not been unpoisoned yet; unpoison it now.
6466 GetAssembler()->MaybeUnpoisonHeapReference(temp1);
6467
6468 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6469 GetAssembler()->LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6470 // If heap poisoning is enabled, no need to unpoison
6471 // `temp1`, as we are comparing against null below.
xueliang.zhongf51bc622016-11-04 09:23:32 +00006472 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006473 __ Bind(&do_put);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006474 } else {
6475 __ B(ne, slow_path->GetEntryLabel());
6476 }
6477 }
6478
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006479 codegen_->MarkGCCard(temp1, temp2, array, value, /* can_be_null= */ false);
6480
6481 if (can_value_be_null) {
6482 DCHECK(do_store.IsReferenced());
6483 __ Bind(&do_store);
6484 }
6485
Scott Wakelingc34dba72016-10-03 10:14:44 +01006486 vixl32::Register source = value;
6487 if (kPoisonHeapReferences) {
6488 // Note that in the case where `value` is a null reference,
6489 // we do not enter this block, as a null reference does not
6490 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006491 DCHECK_EQ(value_type, DataType::Type::kReference);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006492 __ Mov(temp1, value);
6493 GetAssembler()->PoisonHeapReference(temp1);
6494 source = temp1;
6495 }
6496
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006497 {
6498 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6499 // As two macro instructions can be emitted the max size is doubled.
6500 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
6501 if (index.IsConstant()) {
6502 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
6503 GetAssembler()->StoreToOffset(kStoreWord, source, array, offset);
6504 } else {
6505 DCHECK(index.IsRegister()) << index;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006506
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006507 UseScratchRegisterScope temps(GetVIXLAssembler());
6508 vixl32::Register temp = temps.Acquire();
6509 __ Add(temp, array, data_offset);
6510 codegen_->StoreToShiftedRegOffset(value_type,
6511 LocationFrom(source),
6512 temp,
6513 RegisterFrom(index));
6514 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006515
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006516 if (can_value_be_null || !needs_type_check) {
6517 codegen_->MaybeRecordImplicitNullCheck(instruction);
6518 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006519 }
6520
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006521 if (slow_path != nullptr) {
6522 __ Bind(slow_path->GetExitLabel());
6523 }
6524
Scott Wakelingc34dba72016-10-03 10:14:44 +01006525 break;
6526 }
6527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006528 case DataType::Type::kInt64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006529 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6530 // As two macro instructions can be emitted the max size is doubled.
6531 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006532 Location value = locations->InAt(2);
6533 if (index.IsConstant()) {
6534 size_t offset =
Anton Kirilov644032c2016-12-06 17:51:43 +00006535 (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006536 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), array, offset);
6537 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006538 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006539 vixl32::Register temp = temps.Acquire();
6540 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6541 GetAssembler()->StoreToOffset(kStoreWordPair, LowRegisterFrom(value), temp, data_offset);
6542 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006543 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006544 break;
6545 }
6546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006547 case DataType::Type::kFloat32: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006548 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6549 // As two macro instructions can be emitted the max size is doubled.
6550 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006551 Location value = locations->InAt(2);
6552 DCHECK(value.IsFpuRegister());
6553 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006554 size_t offset = (Int32ConstantFrom(index) << TIMES_4) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006555 GetAssembler()->StoreSToOffset(SRegisterFrom(value), array, offset);
6556 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006557 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006558 vixl32::Register temp = temps.Acquire();
6559 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_4));
6560 GetAssembler()->StoreSToOffset(SRegisterFrom(value), temp, data_offset);
6561 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006562 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006563 break;
6564 }
6565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006566 case DataType::Type::kFloat64: {
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006567 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
6568 // As two macro instructions can be emitted the max size is doubled.
6569 EmissionCheckScope guard(GetVIXLAssembler(), 2 * kMaxMacroInstructionSizeInBytes);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006570 Location value = locations->InAt(2);
6571 DCHECK(value.IsFpuRegisterPair());
6572 if (index.IsConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006573 size_t offset = (Int32ConstantFrom(index) << TIMES_8) + data_offset;
Scott Wakelingc34dba72016-10-03 10:14:44 +01006574 GetAssembler()->StoreDToOffset(DRegisterFrom(value), array, offset);
6575 } else {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006576 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelingc34dba72016-10-03 10:14:44 +01006577 vixl32::Register temp = temps.Acquire();
6578 __ Add(temp, array, Operand(RegisterFrom(index), vixl32::LSL, TIMES_8));
6579 GetAssembler()->StoreDToOffset(DRegisterFrom(value), temp, data_offset);
6580 }
Evgeny Astigeevich98416bf2019-09-09 14:52:12 +01006581 codegen_->MaybeRecordImplicitNullCheck(instruction);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006582 break;
6583 }
6584
Aart Bik66c158e2018-01-31 12:55:04 -08006585 case DataType::Type::kUint32:
6586 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006587 case DataType::Type::kVoid:
Scott Wakelingc34dba72016-10-03 10:14:44 +01006588 LOG(FATAL) << "Unreachable type " << value_type;
6589 UNREACHABLE();
6590 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006591}
6592
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006593void LocationsBuilderARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6594 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006595 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006596 locations->SetInAt(0, Location::RequiresRegister());
6597 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6598}
6599
6600void InstructionCodeGeneratorARMVIXL::VisitArrayLength(HArrayLength* instruction) {
6601 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
6602 vixl32::Register obj = InputRegisterAt(instruction, 0);
6603 vixl32::Register out = OutputRegister(instruction);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006604 {
Artem Serov0fb37192016-12-06 18:13:40 +00006605 ExactAssemblyScope aas(GetVIXLAssembler(),
6606 vixl32::kMaxInstructionSizeInBytes,
6607 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00006608 __ ldr(out, MemOperand(obj, offset));
6609 codegen_->MaybeRecordImplicitNullCheck(instruction);
6610 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006611 // Mask out compression flag from String's array length.
6612 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006613 __ Lsr(out, out, 1u);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01006614 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006615}
6616
Artem Serov2bbc9532016-10-21 11:51:50 +01006617void LocationsBuilderARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov2bbc9532016-10-21 11:51:50 +01006618 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006619 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01006620
6621 locations->SetInAt(0, Location::RequiresRegister());
6622 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6623 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6624}
6625
6626void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6627 vixl32::Register out = OutputRegister(instruction);
6628 vixl32::Register first = InputRegisterAt(instruction, 0);
6629 Location second = instruction->GetLocations()->InAt(1);
6630
Artem Serov2bbc9532016-10-21 11:51:50 +01006631 if (second.IsRegister()) {
6632 __ Add(out, first, RegisterFrom(second));
6633 } else {
Anton Kirilov644032c2016-12-06 17:51:43 +00006634 __ Add(out, first, Int32ConstantFrom(second));
Artem Serov2bbc9532016-10-21 11:51:50 +01006635 }
6636}
6637
Artem Serove1811ed2017-04-27 16:50:47 +01006638void LocationsBuilderARMVIXL::VisitIntermediateAddressIndex(
6639 HIntermediateAddressIndex* instruction) {
6640 LOG(FATAL) << "Unreachable " << instruction->GetId();
6641}
6642
6643void InstructionCodeGeneratorARMVIXL::VisitIntermediateAddressIndex(
6644 HIntermediateAddressIndex* instruction) {
6645 LOG(FATAL) << "Unreachable " << instruction->GetId();
6646}
6647
Scott Wakelingc34dba72016-10-03 10:14:44 +01006648void LocationsBuilderARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
6649 RegisterSet caller_saves = RegisterSet::Empty();
6650 InvokeRuntimeCallingConventionARMVIXL calling_convention;
6651 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(0)));
6652 caller_saves.Add(LocationFrom(calling_convention.GetRegisterAt(1)));
6653 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006654
6655 HInstruction* index = instruction->InputAt(0);
6656 HInstruction* length = instruction->InputAt(1);
6657 // If both index and length are constants we can statically check the bounds. But if at least one
6658 // of them is not encodable ArmEncodableConstantOrRegister will create
6659 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6660 // locations.
6661 bool both_const = index->IsConstant() && length->IsConstant();
6662 locations->SetInAt(0, both_const
6663 ? Location::ConstantLocation(index->AsConstant())
6664 : ArmEncodableConstantOrRegister(index, CMP));
6665 locations->SetInAt(1, both_const
6666 ? Location::ConstantLocation(length->AsConstant())
6667 : ArmEncodableConstantOrRegister(length, CMP));
Scott Wakelingc34dba72016-10-03 10:14:44 +01006668}
6669
6670void InstructionCodeGeneratorARMVIXL::VisitBoundsCheck(HBoundsCheck* instruction) {
Artem Serov2dd053d2017-03-08 14:54:06 +00006671 LocationSummary* locations = instruction->GetLocations();
6672 Location index_loc = locations->InAt(0);
6673 Location length_loc = locations->InAt(1);
Scott Wakelingc34dba72016-10-03 10:14:44 +01006674
Artem Serov2dd053d2017-03-08 14:54:06 +00006675 if (length_loc.IsConstant()) {
6676 int32_t length = Int32ConstantFrom(length_loc);
6677 if (index_loc.IsConstant()) {
6678 // BCE will remove the bounds check if we are guaranteed to pass.
6679 int32_t index = Int32ConstantFrom(index_loc);
6680 if (index < 0 || index >= length) {
6681 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006682 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006683 codegen_->AddSlowPath(slow_path);
6684 __ B(slow_path->GetEntryLabel());
6685 } else {
6686 // Some optimization after BCE may have generated this, and we should not
6687 // generate a bounds check if it is a valid range.
6688 }
6689 return;
6690 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006691
Artem Serov2dd053d2017-03-08 14:54:06 +00006692 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006693 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006694 __ Cmp(RegisterFrom(index_loc), length);
6695 codegen_->AddSlowPath(slow_path);
6696 __ B(hs, slow_path->GetEntryLabel());
6697 } else {
6698 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006699 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARMVIXL(instruction);
Artem Serov2dd053d2017-03-08 14:54:06 +00006700 __ Cmp(RegisterFrom(length_loc), InputOperandAt(instruction, 0));
6701 codegen_->AddSlowPath(slow_path);
6702 __ B(ls, slow_path->GetEntryLabel());
6703 }
Scott Wakelingc34dba72016-10-03 10:14:44 +01006704}
6705
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006706void CodeGeneratorARMVIXL::MarkGCCard(vixl32::Register temp,
6707 vixl32::Register card,
6708 vixl32::Register object,
6709 vixl32::Register value,
6710 bool can_be_null) {
6711 vixl32::Label is_null;
6712 if (can_be_null) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006713 __ CompareAndBranchIfZero(value, &is_null);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006714 }
Roland Levillainc73f0522018-08-14 15:16:50 +01006715 // Load the address of the card table into `card`.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006716 GetAssembler()->LoadFromOffset(
6717 kLoadWord, card, tr, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01006718 // Calculate the offset (in the card table) of the card corresponding to
6719 // `object`.
Scott Wakelingb77051e2016-11-21 19:46:00 +00006720 __ Lsr(temp, object, Operand::From(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01006721 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
6722 // `object`'s card.
6723 //
6724 // Register `card` contains the address of the card table. Note that the card
6725 // table's base is biased during its creation so that it always starts at an
6726 // address whose least-significant byte is equal to `kCardDirty` (see
6727 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
6728 // below writes the `kCardDirty` (byte) value into the `object`'s card
6729 // (located at `card + object >> kCardShift`).
6730 //
6731 // This dual use of the value in register `card` (1. to calculate the location
6732 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
6733 // (no need to explicitly load `kCardDirty` as an immediate value).
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006734 __ Strb(card, MemOperand(card, temp));
6735 if (can_be_null) {
6736 __ Bind(&is_null);
6737 }
6738}
6739
Scott Wakelingfe885462016-09-22 10:24:38 +01006740void LocationsBuilderARMVIXL::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6741 LOG(FATAL) << "Unreachable";
6742}
6743
6744void InstructionCodeGeneratorARMVIXL::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006745 if (instruction->GetNext()->IsSuspendCheck() &&
6746 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6747 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6748 // The back edge will generate the suspend check.
6749 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6750 }
6751
Scott Wakelingfe885462016-09-22 10:24:38 +01006752 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6753}
6754
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006755void LocationsBuilderARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006756 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6757 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov657022c2016-11-23 14:19:38 +00006758 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006759}
6760
6761void InstructionCodeGeneratorARMVIXL::VisitSuspendCheck(HSuspendCheck* instruction) {
6762 HBasicBlock* block = instruction->GetBlock();
6763 if (block->GetLoopInformation() != nullptr) {
6764 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6765 // The back edge will generate the suspend check.
6766 return;
6767 }
6768 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6769 // The goto will generate the suspend check.
6770 return;
6771 }
6772 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08006773 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 13);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006774}
6775
6776void InstructionCodeGeneratorARMVIXL::GenerateSuspendCheck(HSuspendCheck* instruction,
6777 HBasicBlock* successor) {
6778 SuspendCheckSlowPathARMVIXL* slow_path =
6779 down_cast<SuspendCheckSlowPathARMVIXL*>(instruction->GetSlowPath());
6780 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006781 slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006782 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARMVIXL(instruction, successor);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006783 instruction->SetSlowPath(slow_path);
6784 codegen_->AddSlowPath(slow_path);
6785 if (successor != nullptr) {
6786 DCHECK(successor->IsLoopHeader());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006787 }
6788 } else {
6789 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6790 }
6791
Anton Kirilovedb2ac32016-11-30 15:14:10 +00006792 UseScratchRegisterScope temps(GetVIXLAssembler());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006793 vixl32::Register temp = temps.Acquire();
6794 GetAssembler()->LoadFromOffset(
6795 kLoadUnsignedHalfword, temp, tr, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
6796 if (successor == nullptr) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006797 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006798 __ Bind(slow_path->GetReturnLabel());
6799 } else {
xueliang.zhongf51bc622016-11-04 09:23:32 +00006800 __ CompareAndBranchIfZero(temp, codegen_->GetLabelOf(successor));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006801 __ B(slow_path->GetEntryLabel());
6802 }
6803}
6804
Scott Wakelingfe885462016-09-22 10:24:38 +01006805ArmVIXLAssembler* ParallelMoveResolverARMVIXL::GetAssembler() const {
6806 return codegen_->GetAssembler();
6807}
6808
6809void ParallelMoveResolverARMVIXL::EmitMove(size_t index) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006810 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Scott Wakelingfe885462016-09-22 10:24:38 +01006811 MoveOperands* move = moves_[index];
6812 Location source = move->GetSource();
6813 Location destination = move->GetDestination();
6814
6815 if (source.IsRegister()) {
6816 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006817 __ Mov(RegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006818 } else if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006819 __ Vmov(SRegisterFrom(destination), RegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006820 } else {
6821 DCHECK(destination.IsStackSlot());
6822 GetAssembler()->StoreToOffset(kStoreWord,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006823 RegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006824 sp,
6825 destination.GetStackIndex());
6826 }
6827 } else if (source.IsStackSlot()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006828 if (destination.IsRegister()) {
6829 GetAssembler()->LoadFromOffset(kLoadWord,
6830 RegisterFrom(destination),
6831 sp,
6832 source.GetStackIndex());
6833 } else if (destination.IsFpuRegister()) {
6834 GetAssembler()->LoadSFromOffset(SRegisterFrom(destination), sp, source.GetStackIndex());
6835 } else {
6836 DCHECK(destination.IsStackSlot());
6837 vixl32::Register temp = temps.Acquire();
6838 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, source.GetStackIndex());
6839 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6840 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006841 } else if (source.IsFpuRegister()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006842 if (destination.IsRegister()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006843 __ Vmov(RegisterFrom(destination), SRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006844 } else if (destination.IsFpuRegister()) {
6845 __ Vmov(SRegisterFrom(destination), SRegisterFrom(source));
6846 } else {
6847 DCHECK(destination.IsStackSlot());
6848 GetAssembler()->StoreSToOffset(SRegisterFrom(source), sp, destination.GetStackIndex());
6849 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006850 } else if (source.IsDoubleStackSlot()) {
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006851 if (destination.IsDoubleStackSlot()) {
6852 vixl32::DRegister temp = temps.AcquireD();
6853 GetAssembler()->LoadDFromOffset(temp, sp, source.GetStackIndex());
6854 GetAssembler()->StoreDToOffset(temp, sp, destination.GetStackIndex());
6855 } else if (destination.IsRegisterPair()) {
6856 DCHECK(ExpectedPairLayout(destination));
6857 GetAssembler()->LoadFromOffset(
6858 kLoadWordPair, LowRegisterFrom(destination), sp, source.GetStackIndex());
6859 } else {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006860 DCHECK(destination.IsFpuRegisterPair()) << destination;
6861 GetAssembler()->LoadDFromOffset(DRegisterFrom(destination), sp, source.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006862 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006863 } else if (source.IsRegisterPair()) {
6864 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006865 __ Mov(LowRegisterFrom(destination), LowRegisterFrom(source));
6866 __ Mov(HighRegisterFrom(destination), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006867 } else if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006868 __ Vmov(DRegisterFrom(destination), LowRegisterFrom(source), HighRegisterFrom(source));
Scott Wakelingfe885462016-09-22 10:24:38 +01006869 } else {
6870 DCHECK(destination.IsDoubleStackSlot()) << destination;
6871 DCHECK(ExpectedPairLayout(source));
6872 GetAssembler()->StoreToOffset(kStoreWordPair,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006873 LowRegisterFrom(source),
Scott Wakelingfe885462016-09-22 10:24:38 +01006874 sp,
6875 destination.GetStackIndex());
6876 }
6877 } else if (source.IsFpuRegisterPair()) {
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006878 if (destination.IsRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006879 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), DRegisterFrom(source));
Alexandre Ramesb45fbaa52016-10-17 14:57:13 +01006880 } else if (destination.IsFpuRegisterPair()) {
6881 __ Vmov(DRegisterFrom(destination), DRegisterFrom(source));
6882 } else {
6883 DCHECK(destination.IsDoubleStackSlot()) << destination;
6884 GetAssembler()->StoreDToOffset(DRegisterFrom(source), sp, destination.GetStackIndex());
6885 }
Scott Wakelingfe885462016-09-22 10:24:38 +01006886 } else {
6887 DCHECK(source.IsConstant()) << source;
6888 HConstant* constant = source.GetConstant();
6889 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6890 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
6891 if (destination.IsRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006892 __ Mov(RegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006893 } else {
6894 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006895 vixl32::Register temp = temps.Acquire();
6896 __ Mov(temp, value);
6897 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6898 }
6899 } else if (constant->IsLongConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00006900 int64_t value = Int64ConstantFrom(source);
Scott Wakelingfe885462016-09-22 10:24:38 +01006901 if (destination.IsRegisterPair()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006902 __ Mov(LowRegisterFrom(destination), Low32Bits(value));
6903 __ Mov(HighRegisterFrom(destination), High32Bits(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006904 } else {
6905 DCHECK(destination.IsDoubleStackSlot()) << destination;
Scott Wakelingfe885462016-09-22 10:24:38 +01006906 vixl32::Register temp = temps.Acquire();
6907 __ Mov(temp, Low32Bits(value));
6908 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6909 __ Mov(temp, High32Bits(value));
6910 GetAssembler()->StoreToOffset(kStoreWord,
6911 temp,
6912 sp,
6913 destination.GetHighStackIndex(kArmWordSize));
6914 }
6915 } else if (constant->IsDoubleConstant()) {
6916 double value = constant->AsDoubleConstant()->GetValue();
6917 if (destination.IsFpuRegisterPair()) {
Scott Wakelingc34dba72016-10-03 10:14:44 +01006918 __ Vmov(DRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006919 } else {
6920 DCHECK(destination.IsDoubleStackSlot()) << destination;
6921 uint64_t int_value = bit_cast<uint64_t, double>(value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006922 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006923 __ Mov(temp, Low32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006924 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006925 __ Mov(temp, High32Bits(int_value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006926 GetAssembler()->StoreToOffset(kStoreWord,
6927 temp,
6928 sp,
6929 destination.GetHighStackIndex(kArmWordSize));
6930 }
6931 } else {
6932 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
6933 float value = constant->AsFloatConstant()->GetValue();
6934 if (destination.IsFpuRegister()) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006935 __ Vmov(SRegisterFrom(destination), value);
Scott Wakelingfe885462016-09-22 10:24:38 +01006936 } else {
6937 DCHECK(destination.IsStackSlot());
Scott Wakelingfe885462016-09-22 10:24:38 +01006938 vixl32::Register temp = temps.Acquire();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01006939 __ Mov(temp, bit_cast<int32_t, float>(value));
Scott Wakelingfe885462016-09-22 10:24:38 +01006940 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, destination.GetStackIndex());
6941 }
6942 }
6943 }
6944}
6945
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006946void ParallelMoveResolverARMVIXL::Exchange(vixl32::Register reg, int mem) {
6947 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6948 vixl32::Register temp = temps.Acquire();
6949 __ Mov(temp, reg);
6950 GetAssembler()->LoadFromOffset(kLoadWord, reg, sp, mem);
6951 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Scott Wakelingfe885462016-09-22 10:24:38 +01006952}
6953
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006954void ParallelMoveResolverARMVIXL::Exchange(int mem1, int mem2) {
6955 // TODO(VIXL32): Double check the performance of this implementation.
6956 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006957 vixl32::Register temp1 = temps.Acquire();
6958 ScratchRegisterScope ensure_scratch(
6959 this, temp1.GetCode(), r0.GetCode(), codegen_->GetNumberOfCoreRegisters());
6960 vixl32::Register temp2(ensure_scratch.GetRegister());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006961
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006962 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6963 GetAssembler()->LoadFromOffset(kLoadWord, temp1, sp, mem1 + stack_offset);
6964 GetAssembler()->LoadFromOffset(kLoadWord, temp2, sp, mem2 + stack_offset);
6965 GetAssembler()->StoreToOffset(kStoreWord, temp1, sp, mem2 + stack_offset);
6966 GetAssembler()->StoreToOffset(kStoreWord, temp2, sp, mem1 + stack_offset);
Scott Wakelingfe885462016-09-22 10:24:38 +01006967}
6968
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006969void ParallelMoveResolverARMVIXL::EmitSwap(size_t index) {
6970 MoveOperands* move = moves_[index];
6971 Location source = move->GetSource();
6972 Location destination = move->GetDestination();
6973 UseScratchRegisterScope temps(GetAssembler()->GetVIXLAssembler());
6974
6975 if (source.IsRegister() && destination.IsRegister()) {
6976 vixl32::Register temp = temps.Acquire();
6977 DCHECK(!RegisterFrom(source).Is(temp));
6978 DCHECK(!RegisterFrom(destination).Is(temp));
6979 __ Mov(temp, RegisterFrom(destination));
6980 __ Mov(RegisterFrom(destination), RegisterFrom(source));
6981 __ Mov(RegisterFrom(source), temp);
6982 } else if (source.IsRegister() && destination.IsStackSlot()) {
6983 Exchange(RegisterFrom(source), destination.GetStackIndex());
6984 } else if (source.IsStackSlot() && destination.IsRegister()) {
6985 Exchange(RegisterFrom(destination), source.GetStackIndex());
6986 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00006987 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006988 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00006989 vixl32::Register temp = temps.Acquire();
Anton Kirilovdda43962016-11-21 19:55:20 +00006990 __ Vmov(temp, SRegisterFrom(source));
6991 __ Vmov(SRegisterFrom(source), SRegisterFrom(destination));
6992 __ Vmov(SRegisterFrom(destination), temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01006993 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
6994 vixl32::DRegister temp = temps.AcquireD();
6995 __ Vmov(temp, LowRegisterFrom(source), HighRegisterFrom(source));
6996 __ Mov(LowRegisterFrom(source), LowRegisterFrom(destination));
6997 __ Mov(HighRegisterFrom(source), HighRegisterFrom(destination));
6998 __ Vmov(LowRegisterFrom(destination), HighRegisterFrom(destination), temp);
6999 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
7000 vixl32::Register low_reg = LowRegisterFrom(source.IsRegisterPair() ? source : destination);
7001 int mem = source.IsRegisterPair() ? destination.GetStackIndex() : source.GetStackIndex();
7002 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
7003 vixl32::DRegister temp = temps.AcquireD();
7004 __ Vmov(temp, low_reg, vixl32::Register(low_reg.GetCode() + 1));
7005 GetAssembler()->LoadFromOffset(kLoadWordPair, low_reg, sp, mem);
7006 GetAssembler()->StoreDToOffset(temp, sp, mem);
7007 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007008 vixl32::DRegister first = DRegisterFrom(source);
7009 vixl32::DRegister second = DRegisterFrom(destination);
7010 vixl32::DRegister temp = temps.AcquireD();
7011 __ Vmov(temp, first);
7012 __ Vmov(first, second);
7013 __ Vmov(second, temp);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007014 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007015 vixl32::DRegister reg = source.IsFpuRegisterPair()
7016 ? DRegisterFrom(source)
7017 : DRegisterFrom(destination);
7018 int mem = source.IsFpuRegisterPair()
7019 ? destination.GetStackIndex()
7020 : source.GetStackIndex();
7021 vixl32::DRegister temp = temps.AcquireD();
7022 __ Vmov(temp, reg);
7023 GetAssembler()->LoadDFromOffset(reg, sp, mem);
7024 GetAssembler()->StoreDToOffset(temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007025 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
Anton Kirilovdda43962016-11-21 19:55:20 +00007026 vixl32::SRegister reg = source.IsFpuRegister()
7027 ? SRegisterFrom(source)
7028 : SRegisterFrom(destination);
7029 int mem = source.IsFpuRegister()
7030 ? destination.GetStackIndex()
7031 : source.GetStackIndex();
7032 vixl32::Register temp = temps.Acquire();
7033 __ Vmov(temp, reg);
7034 GetAssembler()->LoadSFromOffset(reg, sp, mem);
7035 GetAssembler()->StoreToOffset(kStoreWord, temp, sp, mem);
Alexandre Rames9c19bd62016-10-24 11:50:32 +01007036 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
7037 vixl32::DRegister temp1 = temps.AcquireD();
7038 vixl32::DRegister temp2 = temps.AcquireD();
7039 __ Vldr(temp1, MemOperand(sp, source.GetStackIndex()));
7040 __ Vldr(temp2, MemOperand(sp, destination.GetStackIndex()));
7041 __ Vstr(temp1, MemOperand(sp, destination.GetStackIndex()));
7042 __ Vstr(temp2, MemOperand(sp, source.GetStackIndex()));
7043 } else {
7044 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
7045 }
Scott Wakelingfe885462016-09-22 10:24:38 +01007046}
7047
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007048void ParallelMoveResolverARMVIXL::SpillScratch(int reg) {
7049 __ Push(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007050}
7051
Nicolas Geoffray13a797b2017-03-15 16:41:31 +00007052void ParallelMoveResolverARMVIXL::RestoreScratch(int reg) {
7053 __ Pop(vixl32::Register(reg));
Scott Wakelingfe885462016-09-22 10:24:38 +01007054}
7055
Artem Serov02d37832016-10-25 15:25:33 +01007056HLoadClass::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadClassKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007057 HLoadClass::LoadKind desired_class_load_kind) {
7058 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007059 case HLoadClass::LoadKind::kInvalid:
7060 LOG(FATAL) << "UNREACHABLE";
7061 UNREACHABLE();
Artem Serovd4cc5b22016-11-04 11:19:09 +00007062 case HLoadClass::LoadKind::kReferrersClass:
7063 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007064 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007065 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007066 case HLoadClass::LoadKind::kBssEntry:
7067 DCHECK(!Runtime::Current()->UseJitCompilation());
7068 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007069 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007070 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007071 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007072 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007073 case HLoadClass::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007074 break;
7075 }
7076 return desired_class_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007077}
7078
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007079void LocationsBuilderARMVIXL::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007080 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007081 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007082 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007083 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007084 cls,
7085 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007086 LocationFrom(r0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007087 DCHECK(calling_convention.GetRegisterAt(0).Is(r0));
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007088 return;
7089 }
Vladimir Marko41559982017-01-06 14:04:23 +00007090 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelingfe885462016-09-22 10:24:38 +01007091
Artem Serovd4cc5b22016-11-04 11:19:09 +00007092 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7093 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007094 ? LocationSummary::kCallOnSlowPath
7095 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007096 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007097 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00007098 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Artem Serovd4cc5b22016-11-04 11:19:09 +00007099 }
7100
Vladimir Marko41559982017-01-06 14:04:23 +00007101 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007102 locations->SetInAt(0, Location::RequiresRegister());
7103 }
7104 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007105 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7106 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7107 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007108 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007109 } else {
7110 // For non-Baker read barrier we have a temp-clobbering call.
7111 }
7112 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007113}
7114
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007115// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7116// move.
7117void InstructionCodeGeneratorARMVIXL::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007118 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007119 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007120 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007121 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 14);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007122 return;
7123 }
Vladimir Marko41559982017-01-06 14:04:23 +00007124 DCHECK(!cls->NeedsAccessCheck());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007125
Vladimir Marko41559982017-01-06 14:04:23 +00007126 LocationSummary* locations = cls->GetLocations();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007127 Location out_loc = locations->Out();
7128 vixl32::Register out = OutputRegister(cls);
7129
Artem Serovd4cc5b22016-11-04 11:19:09 +00007130 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7131 ? kWithoutReadBarrier
7132 : kCompilerReadBarrierOption;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007133 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007134 switch (load_kind) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007135 case HLoadClass::LoadKind::kReferrersClass: {
7136 DCHECK(!cls->CanCallRuntime());
7137 DCHECK(!cls->MustGenerateClinitCheck());
7138 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7139 vixl32::Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00007140 codegen_->GenerateGcRootFieldLoad(cls,
7141 out_loc,
7142 current_method,
7143 ArtMethod::DeclaringClassOffset().Int32Value(),
7144 read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007145 break;
7146 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007147 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007148 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7149 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007150 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7151 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007152 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007153 codegen_->EmitMovwMovtPlaceholder(labels, out);
7154 break;
7155 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007156 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007157 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7158 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007159 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007160 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007161 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007162 break;
7163 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007164 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007165 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007166 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007167 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007168 // All aligned loads are implicitly atomic consume operations on ARM.
Andreas Gampe3db70682018-12-26 15:12:03 -08007169 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007170 generate_null_check = true;
7171 break;
7172 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007173 case HLoadClass::LoadKind::kJitBootImageAddress: {
7174 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7175 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7176 DCHECK_NE(address, 0u);
7177 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7178 break;
7179 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007180 case HLoadClass::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007181 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7182 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007183 cls->GetClass()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007184 // /* GcRoot<mirror::Class> */ out = *out
Andreas Gampe3db70682018-12-26 15:12:03 -08007185 codegen_->GenerateGcRootFieldLoad(cls, out_loc, out, /* offset= */ 0, read_barrier_option);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007186 break;
7187 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007188 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007189 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007190 LOG(FATAL) << "UNREACHABLE";
7191 UNREACHABLE();
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007192 }
7193
7194 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7195 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007196 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007197 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(cls, cls);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007198 codegen_->AddSlowPath(slow_path);
7199 if (generate_null_check) {
xueliang.zhongf51bc622016-11-04 09:23:32 +00007200 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007201 }
7202 if (cls->MustGenerateClinitCheck()) {
7203 GenerateClassInitializationCheck(slow_path, out);
7204 } else {
7205 __ Bind(slow_path->GetExitLabel());
7206 }
Andreas Gampe3db70682018-12-26 15:12:03 -08007207 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 15);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01007208 }
7209}
7210
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007211void LocationsBuilderARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7212 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7213 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7214 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7215}
7216
7217void InstructionCodeGeneratorARMVIXL::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7218 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7219}
7220
Orion Hodson18259d72018-04-12 11:18:23 +01007221void LocationsBuilderARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7222 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7223 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
7224 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7225}
7226
7227void InstructionCodeGeneratorARMVIXL::VisitLoadMethodType(HLoadMethodType* load) {
7228 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7229}
7230
Artem Serov02d37832016-10-25 15:25:33 +01007231void LocationsBuilderARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007233 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Artem Serov02d37832016-10-25 15:25:33 +01007234 locations->SetInAt(0, Location::RequiresRegister());
7235 if (check->HasUses()) {
7236 locations->SetOut(Location::SameAsFirstInput());
7237 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007238 // Rely on the type initialization to save everything we need.
7239 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serov02d37832016-10-25 15:25:33 +01007240}
7241
7242void InstructionCodeGeneratorARMVIXL::VisitClinitCheck(HClinitCheck* check) {
7243 // We assume the class is not null.
7244 LoadClassSlowPathARMVIXL* slow_path =
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007245 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARMVIXL(check->GetLoadClass(), check);
Artem Serov02d37832016-10-25 15:25:33 +01007246 codegen_->AddSlowPath(slow_path);
7247 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
7248}
7249
7250void InstructionCodeGeneratorARMVIXL::GenerateClassInitializationCheck(
7251 LoadClassSlowPathARMVIXL* slow_path, vixl32::Register class_reg) {
7252 UseScratchRegisterScope temps(GetVIXLAssembler());
7253 vixl32::Register temp = temps.Acquire();
Vladimir Markodc682aa2018-01-04 18:42:57 +00007254 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Markobf121912019-06-04 13:49:05 +01007255 constexpr uint32_t shifted_visibly_initialized_value =
7256 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << status_lsb_position;
Vladimir Markodc682aa2018-01-04 18:42:57 +00007257
Vladimir Markobf121912019-06-04 13:49:05 +01007258 const size_t status_offset = mirror::Class::StatusOffset().SizeValue();
7259 GetAssembler()->LoadFromOffset(kLoadWord, temp, class_reg, status_offset);
7260 __ Cmp(temp, shifted_visibly_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00007261 __ B(lo, slow_path->GetEntryLabel());
Artem Serov02d37832016-10-25 15:25:33 +01007262 __ Bind(slow_path->GetExitLabel());
7263}
7264
Vladimir Marko175e7862018-03-27 09:03:13 +00007265void InstructionCodeGeneratorARMVIXL::GenerateBitstringTypeCheckCompare(
7266 HTypeCheckInstruction* check,
7267 vixl32::Register temp,
7268 vixl32::FlagsUpdate flags_update) {
7269 uint32_t path_to_root = check->GetBitstringPathToRoot();
7270 uint32_t mask = check->GetBitstringMask();
7271 DCHECK(IsPowerOfTwo(mask + 1));
7272 size_t mask_bits = WhichPowerOf2(mask + 1);
7273
7274 // Note that HInstanceOf shall check for zero value in `temp` but HCheckCast needs
7275 // the Z flag for BNE. This is indicated by the `flags_update` parameter.
7276 if (mask_bits == 16u) {
7277 // Load only the bitstring part of the status word.
7278 __ Ldrh(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7279 // Check if the bitstring bits are equal to `path_to_root`.
7280 if (flags_update == SetFlags) {
7281 __ Cmp(temp, path_to_root);
7282 } else {
7283 __ Sub(temp, temp, path_to_root);
7284 }
7285 } else {
7286 // /* uint32_t */ temp = temp->status_
7287 __ Ldr(temp, MemOperand(temp, mirror::Class::StatusOffset().Int32Value()));
7288 if (GetAssembler()->ShifterOperandCanHold(SUB, path_to_root)) {
7289 // Compare the bitstring bits using SUB.
7290 __ Sub(temp, temp, path_to_root);
7291 // Shift out bits that do not contribute to the comparison.
7292 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7293 } else if (IsUint<16>(path_to_root)) {
7294 if (temp.IsLow()) {
7295 // Note: Optimized for size but contains one more dependent instruction than necessary.
7296 // MOVW+SUB(register) would be 8 bytes unless we find a low-reg temporary but the
7297 // macro assembler would use the high reg IP for the constant by default.
7298 // Compare the bitstring bits using SUB.
7299 __ Sub(temp, temp, path_to_root & 0x00ffu); // 16-bit SUB (immediate) T2
7300 __ Sub(temp, temp, path_to_root & 0xff00u); // 32-bit SUB (immediate) T3
7301 // Shift out bits that do not contribute to the comparison.
7302 __ Lsl(flags_update, temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7303 } else {
7304 // Extract the bitstring bits.
7305 __ Ubfx(temp, temp, 0, mask_bits);
7306 // Check if the bitstring bits are equal to `path_to_root`.
7307 if (flags_update == SetFlags) {
7308 __ Cmp(temp, path_to_root);
7309 } else {
7310 __ Sub(temp, temp, path_to_root);
7311 }
7312 }
7313 } else {
7314 // Shift out bits that do not contribute to the comparison.
7315 __ Lsl(temp, temp, dchecked_integral_cast<uint32_t>(32u - mask_bits));
7316 // Check if the shifted bitstring bits are equal to `path_to_root << (32u - mask_bits)`.
7317 if (flags_update == SetFlags) {
7318 __ Cmp(temp, path_to_root << (32u - mask_bits));
7319 } else {
7320 __ Sub(temp, temp, path_to_root << (32u - mask_bits));
7321 }
7322 }
7323 }
7324}
7325
Artem Serov02d37832016-10-25 15:25:33 +01007326HLoadString::LoadKind CodeGeneratorARMVIXL::GetSupportedLoadStringKind(
Artem Serovd4cc5b22016-11-04 11:19:09 +00007327 HLoadString::LoadKind desired_string_load_kind) {
7328 switch (desired_string_load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007329 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007330 case HLoadString::LoadKind::kBootImageRelRo:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007331 case HLoadString::LoadKind::kBssEntry:
7332 DCHECK(!Runtime::Current()->UseJitCompilation());
7333 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007334 case HLoadString::LoadKind::kJitBootImageAddress:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007335 case HLoadString::LoadKind::kJitTableAddress:
7336 DCHECK(Runtime::Current()->UseJitCompilation());
Artem Serovc5fcb442016-12-02 19:19:58 +00007337 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007338 case HLoadString::LoadKind::kRuntimeCall:
Artem Serovd4cc5b22016-11-04 11:19:09 +00007339 break;
7340 }
7341 return desired_string_load_kind;
Artem Serov02d37832016-10-25 15:25:33 +01007342}
7343
7344void LocationsBuilderARMVIXL::VisitLoadString(HLoadString* load) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007345 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007346 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Artem Serov02d37832016-10-25 15:25:33 +01007347 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007348 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Artem Serov02d37832016-10-25 15:25:33 +01007349 locations->SetOut(LocationFrom(r0));
7350 } else {
7351 locations->SetOut(Location::RequiresRegister());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007352 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7353 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007354 // Rely on the pResolveString and marking to save everything we need, including temps.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007355 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007356 } else {
7357 // For non-Baker read barrier we have a temp-clobbering call.
7358 }
7359 }
Artem Serov02d37832016-10-25 15:25:33 +01007360 }
7361}
7362
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007363// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7364// move.
7365void InstructionCodeGeneratorARMVIXL::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007366 LocationSummary* locations = load->GetLocations();
7367 Location out_loc = locations->Out();
7368 vixl32::Register out = OutputRegister(load);
7369 HLoadString::LoadKind load_kind = load->GetLoadKind();
7370
7371 switch (load_kind) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007372 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007373 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7374 codegen_->GetCompilerOptions().IsBootImageExtension());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007375 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007376 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Artem Serovd4cc5b22016-11-04 11:19:09 +00007377 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007378 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007379 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007380 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007381 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7382 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007383 codegen_->NewBootImageRelRoPatch(codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007384 codegen_->EmitMovwMovtPlaceholder(labels, out);
Andreas Gampe3db70682018-12-26 15:12:03 -08007385 __ Ldr(out, MemOperand(out, /* offset= */ 0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007386 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007387 }
7388 case HLoadString::LoadKind::kBssEntry: {
Artem Serovd4cc5b22016-11-04 11:19:09 +00007389 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007390 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markof3c52b42017-11-17 17:32:12 +00007391 codegen_->EmitMovwMovtPlaceholder(labels, out);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007392 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Markoca1e0382018-04-11 09:58:41 +00007393 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007394 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007395 LoadStringSlowPathARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007396 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARMVIXL(load);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007397 codegen_->AddSlowPath(slow_path);
7398 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7399 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08007400 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 16);
Artem Serovd4cc5b22016-11-04 11:19:09 +00007401 return;
7402 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007403 case HLoadString::LoadKind::kJitBootImageAddress: {
7404 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7405 DCHECK_NE(address, 0u);
7406 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7407 return;
7408 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00007409 case HLoadString::LoadKind::kJitTableAddress: {
Artem Serovc5fcb442016-12-02 19:19:58 +00007410 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007411 load->GetStringIndex(),
7412 load->GetString()));
Artem Serovc5fcb442016-12-02 19:19:58 +00007413 // /* GcRoot<mirror::String> */ out = *out
Vladimir Markoca1e0382018-04-11 09:58:41 +00007414 codegen_->GenerateGcRootFieldLoad(
Andreas Gampe3db70682018-12-26 15:12:03 -08007415 load, out_loc, out, /* offset= */ 0, kCompilerReadBarrierOption);
Artem Serovc5fcb442016-12-02 19:19:58 +00007416 return;
Artem Serovd4cc5b22016-11-04 11:19:09 +00007417 }
7418 default:
7419 break;
7420 }
Artem Serov02d37832016-10-25 15:25:33 +01007421
7422 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007423 DCHECK_EQ(load->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Artem Serov02d37832016-10-25 15:25:33 +01007424 InvokeRuntimeCallingConventionARMVIXL calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007425 __ Mov(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Artem Serov02d37832016-10-25 15:25:33 +01007426 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7427 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08007428 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 17);
Artem Serov02d37832016-10-25 15:25:33 +01007429}
7430
7431static int32_t GetExceptionTlsOffset() {
7432 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
7433}
7434
7435void LocationsBuilderARMVIXL::VisitLoadException(HLoadException* load) {
7436 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007437 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007438 locations->SetOut(Location::RequiresRegister());
7439}
7440
7441void InstructionCodeGeneratorARMVIXL::VisitLoadException(HLoadException* load) {
7442 vixl32::Register out = OutputRegister(load);
7443 GetAssembler()->LoadFromOffset(kLoadWord, out, tr, GetExceptionTlsOffset());
7444}
7445
7446
7447void LocationsBuilderARMVIXL::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007448 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Artem Serov02d37832016-10-25 15:25:33 +01007449}
7450
7451void InstructionCodeGeneratorARMVIXL::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7452 UseScratchRegisterScope temps(GetVIXLAssembler());
7453 vixl32::Register temp = temps.Acquire();
7454 __ Mov(temp, 0);
7455 GetAssembler()->StoreToOffset(kStoreWord, temp, tr, GetExceptionTlsOffset());
7456}
7457
7458void LocationsBuilderARMVIXL::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007459 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7460 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov02d37832016-10-25 15:25:33 +01007461 InvokeRuntimeCallingConventionARMVIXL calling_convention;
7462 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
7463}
7464
7465void InstructionCodeGeneratorARMVIXL::VisitThrow(HThrow* instruction) {
7466 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
7467 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7468}
7469
Artem Serov657022c2016-11-23 14:19:38 +00007470// Temp is used for read barrier.
7471static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7472 if (kEmitCompilerReadBarrier &&
7473 (kUseBakerReadBarrier ||
7474 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7475 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7476 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7477 return 1;
7478 }
7479 return 0;
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007480}
7481
Artem Serov657022c2016-11-23 14:19:38 +00007482// Interface case has 3 temps, one for holding the number of interfaces, one for the current
7483// interface pointer, one for loading the current interface.
7484// The other checks have one temp for loading the object's class.
7485static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7486 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7487 return 3;
7488 }
7489 return 1 + NumberOfInstanceOfTemps(type_check_kind);
7490}
Artem Serovcfbe9132016-10-14 15:58:56 +01007491
7492void LocationsBuilderARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7493 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7494 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7495 bool baker_read_barrier_slow_path = false;
7496 switch (type_check_kind) {
7497 case TypeCheckKind::kExactCheck:
7498 case TypeCheckKind::kAbstractClassCheck:
7499 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007500 case TypeCheckKind::kArrayObjectCheck: {
7501 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7502 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7503 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Artem Serovcfbe9132016-10-14 15:58:56 +01007504 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007505 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007506 case TypeCheckKind::kArrayCheck:
7507 case TypeCheckKind::kUnresolvedCheck:
7508 case TypeCheckKind::kInterfaceCheck:
7509 call_kind = LocationSummary::kCallOnSlowPath;
7510 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007511 case TypeCheckKind::kBitstringCheck:
7512 break;
Artem Serovcfbe9132016-10-14 15:58:56 +01007513 }
7514
Vladimir Markoca6fff82017-10-03 14:49:14 +01007515 LocationSummary* locations =
7516 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Artem Serovcfbe9132016-10-14 15:58:56 +01007517 if (baker_read_barrier_slow_path) {
7518 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7519 }
7520 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007521 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7522 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7523 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7524 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7525 } else {
7526 locations->SetInAt(1, Location::RequiresRegister());
7527 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007528 // The "out" register is used as a temporary, so it overlaps with the inputs.
7529 // Note that TypeCheckSlowPathARM uses this register too.
7530 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Artem Serov657022c2016-11-23 14:19:38 +00007531 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Artem Serovcfbe9132016-10-14 15:58:56 +01007532}
7533
7534void InstructionCodeGeneratorARMVIXL::VisitInstanceOf(HInstanceOf* instruction) {
7535 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7536 LocationSummary* locations = instruction->GetLocations();
7537 Location obj_loc = locations->InAt(0);
7538 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007539 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7540 ? vixl32::Register()
7541 : InputRegisterAt(instruction, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007542 Location out_loc = locations->Out();
7543 vixl32::Register out = OutputRegister(instruction);
Artem Serov657022c2016-11-23 14:19:38 +00007544 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7545 DCHECK_LE(num_temps, 1u);
7546 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Artem Serovcfbe9132016-10-14 15:58:56 +01007547 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7548 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7549 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7550 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007551 vixl32::Label done;
7552 vixl32::Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovcfbe9132016-10-14 15:58:56 +01007553 SlowPathCodeARMVIXL* slow_path = nullptr;
7554
7555 // Return 0 if `obj` is null.
7556 // avoid null check if we know obj is not null.
7557 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007558 DCHECK(!out.Is(obj));
7559 __ Mov(out, 0);
Andreas Gampe3db70682018-12-26 15:12:03 -08007560 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007561 }
7562
Artem Serovcfbe9132016-10-14 15:58:56 +01007563 switch (type_check_kind) {
7564 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007565 ReadBarrierOption read_barrier_option =
7566 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007567 // /* HeapReference<Class> */ out = obj->klass_
7568 GenerateReferenceLoadTwoRegisters(instruction,
7569 out_loc,
7570 obj_loc,
7571 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007572 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007573 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007574 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007575 __ Cmp(out, cls);
7576 // We speculatively set the result to false without changing the condition
7577 // flags, which allows us to avoid some branching later.
7578 __ Mov(LeaveFlags, out, 0);
7579
7580 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7581 // we check that the output is in a low register, so that a 16-bit MOV
7582 // encoding can be used.
7583 if (out.IsLow()) {
7584 // We use the scope because of the IT block that follows.
7585 ExactAssemblyScope guard(GetVIXLAssembler(),
7586 2 * vixl32::k16BitT32InstructionSizeInBytes,
7587 CodeBufferCheckScope::kExactSize);
7588
7589 __ it(eq);
7590 __ mov(eq, out, 1);
7591 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007592 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007593 __ Mov(out, 1);
7594 }
7595
Artem Serovcfbe9132016-10-14 15:58:56 +01007596 break;
7597 }
7598
7599 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007600 ReadBarrierOption read_barrier_option =
7601 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007602 // /* HeapReference<Class> */ out = obj->klass_
7603 GenerateReferenceLoadTwoRegisters(instruction,
7604 out_loc,
7605 obj_loc,
7606 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007607 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007608 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007609 // If the class is abstract, we eagerly fetch the super class of the
7610 // object to avoid doing a comparison we know will fail.
7611 vixl32::Label loop;
7612 __ Bind(&loop);
7613 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007614 GenerateReferenceLoadOneRegister(instruction,
7615 out_loc,
7616 super_offset,
7617 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007618 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007619 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007620 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007621 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007622 __ B(ne, &loop, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007623 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007624 break;
7625 }
7626
7627 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007628 ReadBarrierOption read_barrier_option =
7629 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007630 // /* HeapReference<Class> */ out = obj->klass_
7631 GenerateReferenceLoadTwoRegisters(instruction,
7632 out_loc,
7633 obj_loc,
7634 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007635 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007636 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007637 // Walk over the class hierarchy to find a match.
7638 vixl32::Label loop, success;
7639 __ Bind(&loop);
7640 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007641 __ B(eq, &success, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007642 // /* HeapReference<Class> */ out = out->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007643 GenerateReferenceLoadOneRegister(instruction,
7644 out_loc,
7645 super_offset,
7646 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007647 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007648 // This is essentially a null check, but it sets the condition flags to the
7649 // proper value for the code that follows the loop, i.e. not `eq`.
7650 __ Cmp(out, 1);
Andreas Gampe3db70682018-12-26 15:12:03 -08007651 __ B(hs, &loop, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007652
7653 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7654 // we check that the output is in a low register, so that a 16-bit MOV
7655 // encoding can be used.
7656 if (out.IsLow()) {
7657 // If `out` is null, we use it for the result, and the condition flags
7658 // have already been set to `ne`, so the IT block that comes afterwards
7659 // (and which handles the successful case) turns into a NOP (instead of
7660 // overwriting `out`).
7661 __ Bind(&success);
7662
7663 // We use the scope because of the IT block that follows.
7664 ExactAssemblyScope guard(GetVIXLAssembler(),
7665 2 * vixl32::k16BitT32InstructionSizeInBytes,
7666 CodeBufferCheckScope::kExactSize);
7667
7668 // There is only one branch to the `success` label (which is bound to this
7669 // IT block), and it has the same condition, `eq`, so in that case the MOV
7670 // is executed.
7671 __ it(eq);
7672 __ mov(eq, out, 1);
7673 } else {
7674 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007675 __ B(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007676 __ Bind(&success);
7677 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007678 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007679
Artem Serovcfbe9132016-10-14 15:58:56 +01007680 break;
7681 }
7682
7683 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007684 ReadBarrierOption read_barrier_option =
7685 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007686 // /* HeapReference<Class> */ out = obj->klass_
7687 GenerateReferenceLoadTwoRegisters(instruction,
7688 out_loc,
7689 obj_loc,
7690 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007691 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007692 read_barrier_option);
Artem Serovcfbe9132016-10-14 15:58:56 +01007693 // Do an exact check.
7694 vixl32::Label exact_check;
7695 __ Cmp(out, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007696 __ B(eq, &exact_check, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007697 // Otherwise, we need to check that the object's class is a non-primitive array.
7698 // /* HeapReference<Class> */ out = out->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007699 GenerateReferenceLoadOneRegister(instruction,
7700 out_loc,
7701 component_offset,
7702 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007703 read_barrier_option);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007704 // If `out` is null, we use it for the result, and jump to the final label.
Andreas Gampe3db70682018-12-26 15:12:03 -08007705 __ CompareAndBranchIfZero(out, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007706 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7707 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007708 __ Cmp(out, 0);
7709 // We speculatively set the result to false without changing the condition
7710 // flags, which allows us to avoid some branching later.
7711 __ Mov(LeaveFlags, out, 0);
7712
7713 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7714 // we check that the output is in a low register, so that a 16-bit MOV
7715 // encoding can be used.
7716 if (out.IsLow()) {
7717 __ Bind(&exact_check);
7718
7719 // We use the scope because of the IT block that follows.
7720 ExactAssemblyScope guard(GetVIXLAssembler(),
7721 2 * vixl32::k16BitT32InstructionSizeInBytes,
7722 CodeBufferCheckScope::kExactSize);
7723
7724 __ it(eq);
7725 __ mov(eq, out, 1);
7726 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007727 __ B(ne, final_label, /* is_far_target= */ false);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007728 __ Bind(&exact_check);
7729 __ Mov(out, 1);
7730 }
7731
Artem Serovcfbe9132016-10-14 15:58:56 +01007732 break;
7733 }
7734
7735 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007736 // No read barrier since the slow path will retry upon failure.
Mathieu Chartier6beced42016-11-15 15:51:31 -08007737 // /* HeapReference<Class> */ out = obj->klass_
7738 GenerateReferenceLoadTwoRegisters(instruction,
7739 out_loc,
7740 obj_loc,
7741 class_offset,
Artem Serov657022c2016-11-23 14:19:38 +00007742 maybe_temp_loc,
7743 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007744 __ Cmp(out, cls);
7745 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007746 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08007747 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007748 codegen_->AddSlowPath(slow_path);
7749 __ B(ne, slow_path->GetEntryLabel());
7750 __ Mov(out, 1);
Artem Serovcfbe9132016-10-14 15:58:56 +01007751 break;
7752 }
7753
7754 case TypeCheckKind::kUnresolvedCheck:
7755 case TypeCheckKind::kInterfaceCheck: {
7756 // Note that we indeed only call on slow path, but we always go
7757 // into the slow path for the unresolved and interface check
7758 // cases.
7759 //
7760 // We cannot directly call the InstanceofNonTrivial runtime
7761 // entry point without resorting to a type checking slow path
7762 // here (i.e. by calling InvokeRuntime directly), as it would
7763 // require to assign fixed registers for the inputs of this
7764 // HInstanceOf instruction (following the runtime calling
7765 // convention), which might be cluttered by the potential first
7766 // read barrier emission at the beginning of this method.
7767 //
7768 // TODO: Introduce a new runtime entry point taking the object
7769 // to test (instead of its class) as argument, and let it deal
7770 // with the read barrier issues. This will let us refactor this
7771 // case of the `switch` code as it was previously (with a direct
7772 // call to the runtime not using a type checking slow path).
7773 // This should also be beneficial for the other cases above.
7774 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007775 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
Andreas Gampe3db70682018-12-26 15:12:03 -08007776 instruction, /* is_fatal= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007777 codegen_->AddSlowPath(slow_path);
7778 __ B(slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007779 break;
7780 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007781
7782 case TypeCheckKind::kBitstringCheck: {
7783 // /* HeapReference<Class> */ temp = obj->klass_
7784 GenerateReferenceLoadTwoRegisters(instruction,
7785 out_loc,
7786 obj_loc,
7787 class_offset,
7788 maybe_temp_loc,
7789 kWithoutReadBarrier);
7790
7791 GenerateBitstringTypeCheckCompare(instruction, out, DontCare);
7792 // If `out` is a low reg and we would have another low reg temp, we could
7793 // optimize this as RSBS+ADC, see GenerateConditionWithZero().
7794 //
7795 // Also, in some cases when `out` is a low reg and we're loading a constant to IP
7796 // it would make sense to use CMP+MOV+IT+MOV instead of SUB+CLZ+LSR as the code size
7797 // would be the same and we would have fewer direct data dependencies.
7798 codegen_->GenerateConditionWithZero(kCondEQ, out, out); // CLZ+LSR
7799 break;
7800 }
Artem Serovcfbe9132016-10-14 15:58:56 +01007801 }
7802
Artem Serovcfbe9132016-10-14 15:58:56 +01007803 if (done.IsReferenced()) {
7804 __ Bind(&done);
7805 }
7806
7807 if (slow_path != nullptr) {
7808 __ Bind(slow_path->GetExitLabel());
7809 }
7810}
7811
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007812void LocationsBuilderARMVIXL::VisitCheckCast(HCheckCast* instruction) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007813 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007814 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007815 LocationSummary* locations =
7816 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007817 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007818 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7819 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7820 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7821 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7822 } else {
7823 locations->SetInAt(1, Location::RequiresRegister());
7824 }
Artem Serov657022c2016-11-23 14:19:38 +00007825 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007826}
7827
7828void InstructionCodeGeneratorARMVIXL::VisitCheckCast(HCheckCast* instruction) {
7829 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7830 LocationSummary* locations = instruction->GetLocations();
7831 Location obj_loc = locations->InAt(0);
7832 vixl32::Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00007833 vixl32::Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
7834 ? vixl32::Register()
7835 : InputRegisterAt(instruction, 1);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007836 Location temp_loc = locations->GetTemp(0);
7837 vixl32::Register temp = RegisterFrom(temp_loc);
Artem Serov657022c2016-11-23 14:19:38 +00007838 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7839 DCHECK_LE(num_temps, 3u);
7840 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7841 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7842 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7843 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7844 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7845 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7846 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7847 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7848 const uint32_t object_array_data_offset =
7849 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007850
Vladimir Marko87584542017-12-12 17:47:52 +00007851 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007852 SlowPathCodeARMVIXL* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007853 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARMVIXL(
7854 instruction, is_type_check_slow_path_fatal);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007855 codegen_->AddSlowPath(type_check_slow_path);
7856
7857 vixl32::Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007858 vixl32::Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007859 // Avoid null check if we know obj is not null.
7860 if (instruction->MustDoNullCheck()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08007861 __ CompareAndBranchIfZero(obj, final_label, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007862 }
7863
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007864 switch (type_check_kind) {
7865 case TypeCheckKind::kExactCheck:
7866 case TypeCheckKind::kArrayCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007867 // /* HeapReference<Class> */ temp = obj->klass_
7868 GenerateReferenceLoadTwoRegisters(instruction,
7869 temp_loc,
7870 obj_loc,
7871 class_offset,
7872 maybe_temp2_loc,
7873 kWithoutReadBarrier);
7874
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007875 __ Cmp(temp, cls);
7876 // Jump to slow path for throwing the exception or doing a
7877 // more involved array check.
7878 __ B(ne, type_check_slow_path->GetEntryLabel());
7879 break;
7880 }
7881
7882 case TypeCheckKind::kAbstractClassCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007883 // /* HeapReference<Class> */ temp = obj->klass_
7884 GenerateReferenceLoadTwoRegisters(instruction,
7885 temp_loc,
7886 obj_loc,
7887 class_offset,
7888 maybe_temp2_loc,
7889 kWithoutReadBarrier);
7890
Artem Serovcfbe9132016-10-14 15:58:56 +01007891 // If the class is abstract, we eagerly fetch the super class of the
7892 // object to avoid doing a comparison we know will fail.
7893 vixl32::Label loop;
7894 __ Bind(&loop);
7895 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007896 GenerateReferenceLoadOneRegister(instruction,
7897 temp_loc,
7898 super_offset,
7899 maybe_temp2_loc,
7900 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007901
7902 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7903 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007904 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007905
7906 // Otherwise, compare the classes.
7907 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007908 __ B(ne, &loop, /* is_far_target= */ false);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007909 break;
7910 }
7911
7912 case TypeCheckKind::kClassHierarchyCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007913 // /* HeapReference<Class> */ temp = obj->klass_
7914 GenerateReferenceLoadTwoRegisters(instruction,
7915 temp_loc,
7916 obj_loc,
7917 class_offset,
7918 maybe_temp2_loc,
7919 kWithoutReadBarrier);
7920
Artem Serovcfbe9132016-10-14 15:58:56 +01007921 // Walk over the class hierarchy to find a match.
7922 vixl32::Label loop;
7923 __ Bind(&loop);
7924 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007925 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007926
7927 // /* HeapReference<Class> */ temp = temp->super_class_
Artem Serov657022c2016-11-23 14:19:38 +00007928 GenerateReferenceLoadOneRegister(instruction,
7929 temp_loc,
7930 super_offset,
7931 maybe_temp2_loc,
7932 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007933
7934 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7935 // exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007936 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007937 // Otherwise, jump to the beginning of the loop.
7938 __ B(&loop);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007939 break;
7940 }
7941
Artem Serovcfbe9132016-10-14 15:58:56 +01007942 case TypeCheckKind::kArrayObjectCheck: {
Artem Serov657022c2016-11-23 14:19:38 +00007943 // /* HeapReference<Class> */ temp = obj->klass_
7944 GenerateReferenceLoadTwoRegisters(instruction,
7945 temp_loc,
7946 obj_loc,
7947 class_offset,
7948 maybe_temp2_loc,
7949 kWithoutReadBarrier);
7950
Artem Serovcfbe9132016-10-14 15:58:56 +01007951 // Do an exact check.
7952 __ Cmp(temp, cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08007953 __ B(eq, final_label, /* is_far_target= */ false);
Artem Serovcfbe9132016-10-14 15:58:56 +01007954
7955 // Otherwise, we need to check that the object's class is a non-primitive array.
7956 // /* HeapReference<Class> */ temp = temp->component_type_
Artem Serov657022c2016-11-23 14:19:38 +00007957 GenerateReferenceLoadOneRegister(instruction,
7958 temp_loc,
7959 component_offset,
7960 maybe_temp2_loc,
7961 kWithoutReadBarrier);
Artem Serovcfbe9132016-10-14 15:58:56 +01007962 // If the component type is null, jump to the slow path to throw the exception.
xueliang.zhongf51bc622016-11-04 09:23:32 +00007963 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Artem Serovcfbe9132016-10-14 15:58:56 +01007964 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7965 // to further check that this component type is not a primitive type.
7966 GetAssembler()->LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007967 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00007968 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007969 break;
7970 }
7971
7972 case TypeCheckKind::kUnresolvedCheck:
Artem Serov657022c2016-11-23 14:19:38 +00007973 // We always go into the type check slow path for the unresolved check case.
Artem Serovcfbe9132016-10-14 15:58:56 +01007974 // We cannot directly call the CheckCast runtime entry point
7975 // without resorting to a type checking slow path here (i.e. by
7976 // calling InvokeRuntime directly), as it would require to
7977 // assign fixed registers for the inputs of this HInstanceOf
7978 // instruction (following the runtime calling convention), which
7979 // might be cluttered by the potential first read barrier
7980 // emission at the beginning of this method.
Artem Serov657022c2016-11-23 14:19:38 +00007981
Artem Serovcfbe9132016-10-14 15:58:56 +01007982 __ B(type_check_slow_path->GetEntryLabel());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01007983 break;
Artem Serov657022c2016-11-23 14:19:38 +00007984
7985 case TypeCheckKind::kInterfaceCheck: {
7986 // Avoid read barriers to improve performance of the fast path. We can not get false
7987 // positives by doing this.
7988 // /* HeapReference<Class> */ temp = obj->klass_
7989 GenerateReferenceLoadTwoRegisters(instruction,
7990 temp_loc,
7991 obj_loc,
7992 class_offset,
7993 maybe_temp2_loc,
7994 kWithoutReadBarrier);
7995
7996 // /* HeapReference<Class> */ temp = temp->iftable_
7997 GenerateReferenceLoadTwoRegisters(instruction,
7998 temp_loc,
7999 temp_loc,
8000 iftable_offset,
8001 maybe_temp2_loc,
8002 kWithoutReadBarrier);
8003 // Iftable is never null.
8004 __ Ldr(RegisterFrom(maybe_temp2_loc), MemOperand(temp, array_length_offset));
8005 // Loop through the iftable and check if any class matches.
8006 vixl32::Label start_loop;
8007 __ Bind(&start_loop);
8008 __ CompareAndBranchIfZero(RegisterFrom(maybe_temp2_loc),
8009 type_check_slow_path->GetEntryLabel());
8010 __ Ldr(RegisterFrom(maybe_temp3_loc), MemOperand(temp, object_array_data_offset));
8011 GetAssembler()->MaybeUnpoisonHeapReference(RegisterFrom(maybe_temp3_loc));
8012 // Go to next interface.
8013 __ Add(temp, temp, Operand::From(2 * kHeapReferenceSize));
8014 __ Sub(RegisterFrom(maybe_temp2_loc), RegisterFrom(maybe_temp2_loc), 2);
8015 // Compare the classes and continue the loop if they do not match.
8016 __ Cmp(cls, RegisterFrom(maybe_temp3_loc));
Andreas Gampe3db70682018-12-26 15:12:03 -08008017 __ B(ne, &start_loop, /* is_far_target= */ false);
Artem Serov657022c2016-11-23 14:19:38 +00008018 break;
8019 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008020
8021 case TypeCheckKind::kBitstringCheck: {
8022 // /* HeapReference<Class> */ temp = obj->klass_
8023 GenerateReferenceLoadTwoRegisters(instruction,
8024 temp_loc,
8025 obj_loc,
8026 class_offset,
8027 maybe_temp2_loc,
8028 kWithoutReadBarrier);
8029
8030 GenerateBitstringTypeCheckCompare(instruction, temp, SetFlags);
8031 __ B(ne, type_check_slow_path->GetEntryLabel());
8032 break;
8033 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008034 }
Anton Kirilov6f644202017-02-27 18:29:45 +00008035 if (done.IsReferenced()) {
8036 __ Bind(&done);
8037 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008038
8039 __ Bind(type_check_slow_path->GetExitLabel());
8040}
8041
Artem Serov551b28f2016-10-18 19:11:30 +01008042void LocationsBuilderARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008043 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8044 instruction, LocationSummary::kCallOnMainOnly);
Artem Serov551b28f2016-10-18 19:11:30 +01008045 InvokeRuntimeCallingConventionARMVIXL calling_convention;
8046 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
8047}
8048
8049void InstructionCodeGeneratorARMVIXL::VisitMonitorOperation(HMonitorOperation* instruction) {
8050 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8051 instruction,
8052 instruction->GetDexPc());
8053 if (instruction->IsEnter()) {
8054 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8055 } else {
8056 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8057 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008058 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ 18);
Artem Serov551b28f2016-10-18 19:11:30 +01008059}
8060
Artem Serov02109dd2016-09-23 17:17:54 +01008061void LocationsBuilderARMVIXL::VisitAnd(HAnd* instruction) {
8062 HandleBitwiseOperation(instruction, AND);
8063}
8064
8065void LocationsBuilderARMVIXL::VisitOr(HOr* instruction) {
8066 HandleBitwiseOperation(instruction, ORR);
8067}
8068
8069void LocationsBuilderARMVIXL::VisitXor(HXor* instruction) {
8070 HandleBitwiseOperation(instruction, EOR);
8071}
8072
8073void LocationsBuilderARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
8074 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008075 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008076 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8077 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008078 // Note: GVN reorders commutative operations to have the constant on the right hand side.
8079 locations->SetInAt(0, Location::RequiresRegister());
8080 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
8081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8082}
8083
8084void InstructionCodeGeneratorARMVIXL::VisitAnd(HAnd* instruction) {
8085 HandleBitwiseOperation(instruction);
8086}
8087
8088void InstructionCodeGeneratorARMVIXL::VisitOr(HOr* instruction) {
8089 HandleBitwiseOperation(instruction);
8090}
8091
8092void InstructionCodeGeneratorARMVIXL::VisitXor(HXor* instruction) {
8093 HandleBitwiseOperation(instruction);
8094}
8095
Artem Serov2bbc9532016-10-21 11:51:50 +01008096void LocationsBuilderARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8097 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008098 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008099 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8100 || instruction->GetResultType() == DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008101
8102 locations->SetInAt(0, Location::RequiresRegister());
8103 locations->SetInAt(1, Location::RequiresRegister());
8104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8105}
8106
8107void InstructionCodeGeneratorARMVIXL::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8108 LocationSummary* locations = instruction->GetLocations();
8109 Location first = locations->InAt(0);
8110 Location second = locations->InAt(1);
8111 Location out = locations->Out();
8112
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008113 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov2bbc9532016-10-21 11:51:50 +01008114 vixl32::Register first_reg = RegisterFrom(first);
8115 vixl32::Register second_reg = RegisterFrom(second);
8116 vixl32::Register out_reg = RegisterFrom(out);
8117
8118 switch (instruction->GetOpKind()) {
8119 case HInstruction::kAnd:
8120 __ Bic(out_reg, first_reg, second_reg);
8121 break;
8122 case HInstruction::kOr:
8123 __ Orn(out_reg, first_reg, second_reg);
8124 break;
8125 // There is no EON on arm.
8126 case HInstruction::kXor:
8127 default:
8128 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8129 UNREACHABLE();
8130 }
8131 return;
8132
8133 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008134 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov2bbc9532016-10-21 11:51:50 +01008135 vixl32::Register first_low = LowRegisterFrom(first);
8136 vixl32::Register first_high = HighRegisterFrom(first);
8137 vixl32::Register second_low = LowRegisterFrom(second);
8138 vixl32::Register second_high = HighRegisterFrom(second);
8139 vixl32::Register out_low = LowRegisterFrom(out);
8140 vixl32::Register out_high = HighRegisterFrom(out);
8141
8142 switch (instruction->GetOpKind()) {
8143 case HInstruction::kAnd:
8144 __ Bic(out_low, first_low, second_low);
8145 __ Bic(out_high, first_high, second_high);
8146 break;
8147 case HInstruction::kOr:
8148 __ Orn(out_low, first_low, second_low);
8149 __ Orn(out_high, first_high, second_high);
8150 break;
8151 // There is no EON on arm.
8152 case HInstruction::kXor:
8153 default:
8154 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8155 UNREACHABLE();
8156 }
8157 }
8158}
8159
Anton Kirilov74234da2017-01-13 14:42:47 +00008160void LocationsBuilderARMVIXL::VisitDataProcWithShifterOp(
8161 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008162 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
8163 instruction->GetType() == DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008164 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008165 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008166 const bool overlap = instruction->GetType() == DataType::Type::kInt64 &&
Anton Kirilov74234da2017-01-13 14:42:47 +00008167 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8168
8169 locations->SetInAt(0, Location::RequiresRegister());
8170 locations->SetInAt(1, Location::RequiresRegister());
8171 locations->SetOut(Location::RequiresRegister(),
8172 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8173}
8174
8175void InstructionCodeGeneratorARMVIXL::VisitDataProcWithShifterOp(
8176 HDataProcWithShifterOp* instruction) {
8177 const LocationSummary* const locations = instruction->GetLocations();
8178 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8179 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8180
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008181 if (instruction->GetType() == DataType::Type::kInt32) {
Anton Kirilov420ee302017-02-21 18:10:26 +00008182 const vixl32::Register first = InputRegisterAt(instruction, 0);
8183 const vixl32::Register output = OutputRegister(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008184 const vixl32::Register second = instruction->InputAt(1)->GetType() == DataType::Type::kInt64
Anton Kirilov74234da2017-01-13 14:42:47 +00008185 ? LowRegisterFrom(locations->InAt(1))
8186 : InputRegisterAt(instruction, 1);
8187
Anton Kirilov420ee302017-02-21 18:10:26 +00008188 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8189 DCHECK_EQ(kind, HInstruction::kAdd);
8190
8191 switch (op_kind) {
8192 case HDataProcWithShifterOp::kUXTB:
8193 __ Uxtab(output, first, second);
8194 break;
8195 case HDataProcWithShifterOp::kUXTH:
8196 __ Uxtah(output, first, second);
8197 break;
8198 case HDataProcWithShifterOp::kSXTB:
8199 __ Sxtab(output, first, second);
8200 break;
8201 case HDataProcWithShifterOp::kSXTH:
8202 __ Sxtah(output, first, second);
8203 break;
8204 default:
8205 LOG(FATAL) << "Unexpected operation kind: " << op_kind;
8206 UNREACHABLE();
8207 }
8208 } else {
8209 GenerateDataProcInstruction(kind,
8210 output,
8211 first,
8212 Operand(second,
8213 ShiftFromOpKind(op_kind),
8214 instruction->GetShiftAmount()),
8215 codegen_);
8216 }
Anton Kirilov74234da2017-01-13 14:42:47 +00008217 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008218 DCHECK_EQ(instruction->GetType(), DataType::Type::kInt64);
Anton Kirilov74234da2017-01-13 14:42:47 +00008219
8220 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8221 const vixl32::Register second = InputRegisterAt(instruction, 1);
8222
8223 DCHECK(!LowRegisterFrom(locations->Out()).Is(second));
8224 GenerateDataProc(kind,
8225 locations->Out(),
8226 locations->InAt(0),
8227 second,
8228 Operand(second, ShiftType::ASR, 31),
8229 codegen_);
8230 } else {
8231 GenerateLongDataProc(instruction, codegen_);
8232 }
8233 }
8234}
8235
Artem Serov02109dd2016-09-23 17:17:54 +01008236// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8237void InstructionCodeGeneratorARMVIXL::GenerateAndConst(vixl32::Register out,
8238 vixl32::Register first,
8239 uint32_t value) {
8240 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8241 if (value == 0xffffffffu) {
8242 if (!out.Is(first)) {
8243 __ Mov(out, first);
8244 }
8245 return;
8246 }
8247 if (value == 0u) {
8248 __ Mov(out, 0);
8249 return;
8250 }
8251 if (GetAssembler()->ShifterOperandCanHold(AND, value)) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008252 __ And(out, first, value);
8253 } else if (GetAssembler()->ShifterOperandCanHold(BIC, ~value)) {
8254 __ Bic(out, first, ~value);
Artem Serov02109dd2016-09-23 17:17:54 +01008255 } else {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008256 DCHECK(IsPowerOfTwo(value + 1));
8257 __ Ubfx(out, first, 0, WhichPowerOf2(value + 1));
Artem Serov02109dd2016-09-23 17:17:54 +01008258 }
8259}
8260
8261// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8262void InstructionCodeGeneratorARMVIXL::GenerateOrrConst(vixl32::Register out,
8263 vixl32::Register first,
8264 uint32_t value) {
8265 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8266 if (value == 0u) {
8267 if (!out.Is(first)) {
8268 __ Mov(out, first);
8269 }
8270 return;
8271 }
8272 if (value == 0xffffffffu) {
8273 __ Mvn(out, 0);
8274 return;
8275 }
8276 if (GetAssembler()->ShifterOperandCanHold(ORR, value)) {
8277 __ Orr(out, first, value);
8278 } else {
8279 DCHECK(GetAssembler()->ShifterOperandCanHold(ORN, ~value));
8280 __ Orn(out, first, ~value);
8281 }
8282}
8283
8284// TODO(VIXL): Remove optimizations in the helper when they are implemented in vixl.
8285void InstructionCodeGeneratorARMVIXL::GenerateEorConst(vixl32::Register out,
8286 vixl32::Register first,
8287 uint32_t value) {
8288 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8289 if (value == 0u) {
8290 if (!out.Is(first)) {
8291 __ Mov(out, first);
8292 }
8293 return;
8294 }
8295 __ Eor(out, first, value);
8296}
8297
Anton Kirilovdda43962016-11-21 19:55:20 +00008298void InstructionCodeGeneratorARMVIXL::GenerateAddLongConst(Location out,
8299 Location first,
8300 uint64_t value) {
8301 vixl32::Register out_low = LowRegisterFrom(out);
8302 vixl32::Register out_high = HighRegisterFrom(out);
8303 vixl32::Register first_low = LowRegisterFrom(first);
8304 vixl32::Register first_high = HighRegisterFrom(first);
8305 uint32_t value_low = Low32Bits(value);
8306 uint32_t value_high = High32Bits(value);
8307 if (value_low == 0u) {
8308 if (!out_low.Is(first_low)) {
8309 __ Mov(out_low, first_low);
8310 }
8311 __ Add(out_high, first_high, value_high);
8312 return;
8313 }
8314 __ Adds(out_low, first_low, value_low);
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008315 if (GetAssembler()->ShifterOperandCanHold(ADC, value_high)) {
Anton Kirilovdda43962016-11-21 19:55:20 +00008316 __ Adc(out_high, first_high, value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008317 } else {
Vladimir Markof0a6a1d2018-01-08 14:23:56 +00008318 DCHECK(GetAssembler()->ShifterOperandCanHold(SBC, ~value_high));
8319 __ Sbc(out_high, first_high, ~value_high);
Anton Kirilovdda43962016-11-21 19:55:20 +00008320 }
8321}
8322
Artem Serov02109dd2016-09-23 17:17:54 +01008323void InstructionCodeGeneratorARMVIXL::HandleBitwiseOperation(HBinaryOperation* instruction) {
8324 LocationSummary* locations = instruction->GetLocations();
8325 Location first = locations->InAt(0);
8326 Location second = locations->InAt(1);
8327 Location out = locations->Out();
8328
8329 if (second.IsConstant()) {
8330 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8331 uint32_t value_low = Low32Bits(value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008332 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008333 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8334 vixl32::Register out_reg = OutputRegister(instruction);
8335 if (instruction->IsAnd()) {
8336 GenerateAndConst(out_reg, first_reg, value_low);
8337 } else if (instruction->IsOr()) {
8338 GenerateOrrConst(out_reg, first_reg, value_low);
8339 } else {
8340 DCHECK(instruction->IsXor());
8341 GenerateEorConst(out_reg, first_reg, value_low);
8342 }
8343 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008344 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008345 uint32_t value_high = High32Bits(value);
8346 vixl32::Register first_low = LowRegisterFrom(first);
8347 vixl32::Register first_high = HighRegisterFrom(first);
8348 vixl32::Register out_low = LowRegisterFrom(out);
8349 vixl32::Register out_high = HighRegisterFrom(out);
8350 if (instruction->IsAnd()) {
8351 GenerateAndConst(out_low, first_low, value_low);
8352 GenerateAndConst(out_high, first_high, value_high);
8353 } else if (instruction->IsOr()) {
8354 GenerateOrrConst(out_low, first_low, value_low);
8355 GenerateOrrConst(out_high, first_high, value_high);
8356 } else {
8357 DCHECK(instruction->IsXor());
8358 GenerateEorConst(out_low, first_low, value_low);
8359 GenerateEorConst(out_high, first_high, value_high);
8360 }
8361 }
8362 return;
8363 }
8364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008365 if (instruction->GetResultType() == DataType::Type::kInt32) {
Artem Serov02109dd2016-09-23 17:17:54 +01008366 vixl32::Register first_reg = InputRegisterAt(instruction, 0);
8367 vixl32::Register second_reg = InputRegisterAt(instruction, 1);
8368 vixl32::Register out_reg = OutputRegister(instruction);
8369 if (instruction->IsAnd()) {
8370 __ And(out_reg, first_reg, second_reg);
8371 } else if (instruction->IsOr()) {
8372 __ Orr(out_reg, first_reg, second_reg);
8373 } else {
8374 DCHECK(instruction->IsXor());
8375 __ Eor(out_reg, first_reg, second_reg);
8376 }
8377 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008378 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Artem Serov02109dd2016-09-23 17:17:54 +01008379 vixl32::Register first_low = LowRegisterFrom(first);
8380 vixl32::Register first_high = HighRegisterFrom(first);
8381 vixl32::Register second_low = LowRegisterFrom(second);
8382 vixl32::Register second_high = HighRegisterFrom(second);
8383 vixl32::Register out_low = LowRegisterFrom(out);
8384 vixl32::Register out_high = HighRegisterFrom(out);
8385 if (instruction->IsAnd()) {
8386 __ And(out_low, first_low, second_low);
8387 __ And(out_high, first_high, second_high);
8388 } else if (instruction->IsOr()) {
8389 __ Orr(out_low, first_low, second_low);
8390 __ Orr(out_high, first_high, second_high);
8391 } else {
8392 DCHECK(instruction->IsXor());
8393 __ Eor(out_low, first_low, second_low);
8394 __ Eor(out_high, first_high, second_high);
8395 }
8396 }
8397}
8398
Artem Serovcfbe9132016-10-14 15:58:56 +01008399void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadOneRegister(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008400 HInstruction* instruction,
Artem Serovcfbe9132016-10-14 15:58:56 +01008401 Location out,
8402 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008403 Location maybe_temp,
8404 ReadBarrierOption read_barrier_option) {
Artem Serovcfbe9132016-10-14 15:58:56 +01008405 vixl32::Register out_reg = RegisterFrom(out);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008406 if (read_barrier_option == kWithReadBarrier) {
8407 CHECK(kEmitCompilerReadBarrier);
8408 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8409 if (kUseBakerReadBarrier) {
8410 // Load with fast path based Baker's read barrier.
8411 // /* HeapReference<Object> */ out = *(out + offset)
8412 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008413 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008414 } else {
8415 // Load with slow path based read barrier.
8416 // Save the value of `out` into `maybe_temp` before overwriting it
8417 // in the following move operation, as we will need it for the
8418 // read barrier below.
8419 __ Mov(RegisterFrom(maybe_temp), out_reg);
8420 // /* HeapReference<Object> */ out = *(out + offset)
8421 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8422 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
8423 }
Artem Serovcfbe9132016-10-14 15:58:56 +01008424 } else {
8425 // Plain load with no read barrier.
8426 // /* HeapReference<Object> */ out = *(out + offset)
8427 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8428 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8429 }
8430}
8431
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008432void InstructionCodeGeneratorARMVIXL::GenerateReferenceLoadTwoRegisters(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008433 HInstruction* instruction,
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008434 Location out,
8435 Location obj,
8436 uint32_t offset,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008437 Location maybe_temp,
8438 ReadBarrierOption read_barrier_option) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008439 vixl32::Register out_reg = RegisterFrom(out);
8440 vixl32::Register obj_reg = RegisterFrom(obj);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008441 if (read_barrier_option == kWithReadBarrier) {
8442 CHECK(kEmitCompilerReadBarrier);
8443 if (kUseBakerReadBarrier) {
8444 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
8445 // Load with fast path based Baker's read barrier.
8446 // /* HeapReference<Object> */ out = *(obj + offset)
8447 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008448 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check= */ false);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008449 } else {
8450 // Load with slow path based read barrier.
8451 // /* HeapReference<Object> */ out = *(obj + offset)
8452 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8453 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8454 }
Anton Kirilove28d9ae2016-10-25 18:17:23 +01008455 } else {
8456 // Plain load with no read barrier.
8457 // /* HeapReference<Object> */ out = *(obj + offset)
8458 GetAssembler()->LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8459 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
8460 }
8461}
8462
Vladimir Markoca1e0382018-04-11 09:58:41 +00008463void CodeGeneratorARMVIXL::GenerateGcRootFieldLoad(
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008464 HInstruction* instruction,
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008465 Location root,
8466 vixl32::Register obj,
8467 uint32_t offset,
Artem Serovd4cc5b22016-11-04 11:19:09 +00008468 ReadBarrierOption read_barrier_option) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008469 vixl32::Register root_reg = RegisterFrom(root);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008470 if (read_barrier_option == kWithReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008471 DCHECK(kEmitCompilerReadBarrier);
8472 if (kUseBakerReadBarrier) {
8473 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008474 // Baker's read barrier are used.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008475
Vladimir Marko008e09f32018-08-06 15:42:43 +01008476 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
8477 // the Marking Register) to decide whether we need to enter
8478 // the slow path to mark the GC root.
8479 //
8480 // We use shared thunks for the slow path; shared within the method
8481 // for JIT, across methods for AOT. That thunk checks the reference
8482 // and jumps to the entrypoint if needed.
8483 //
8484 // lr = &return_address;
8485 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8486 // if (mr) { // Thread::Current()->GetIsGcMarking()
8487 // goto gc_root_thunk<root_reg>(lr)
8488 // }
8489 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00008490
Vladimir Marko008e09f32018-08-06 15:42:43 +01008491 UseScratchRegisterScope temps(GetVIXLAssembler());
8492 temps.Exclude(ip);
8493 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
8494 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode(), narrow);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008495
Vladimir Markod887ed82018-08-14 13:52:12 +00008496 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u) + /* LDR */ (narrow ? 1u : 0u);
8497 size_t wide_instructions = /* ADR+CMP+LDR+BNE */ 4u - narrow_instructions;
8498 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8499 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8500 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008501 vixl32::Label return_address;
8502 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8503 __ cmp(mr, Operand(0));
8504 // Currently the offset is always within range. If that changes,
8505 // we shall have to split the load the same way as for fields.
8506 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8507 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8508 __ ldr(EncodingSize(narrow ? Narrow : Wide), root_reg, MemOperand(obj, offset));
8509 EmitBakerReadBarrierBne(custom_data);
Vladimir Markod887ed82018-08-14 13:52:12 +00008510 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008511 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8512 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8513 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008514 } else {
8515 // GC root loaded through a slow path for read barriers other
8516 // than Baker's.
8517 // /* GcRoot<mirror::Object>* */ root = obj + offset
8518 __ Add(root_reg, obj, offset);
8519 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00008520 GenerateReadBarrierForRootSlow(instruction, root, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008521 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008522 } else {
8523 // Plain GC root load with no read barrier.
8524 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8525 GetAssembler()->LoadFromOffset(kLoadWord, root_reg, obj, offset);
8526 // Note that GC roots are not affected by heap poisoning, thus we
8527 // do not have to unpoison `root_reg` here.
8528 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008529 MaybeGenerateMarkingRegisterCheck(/* code= */ 19);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008530}
8531
Vladimir Markod887ed82018-08-14 13:52:12 +00008532void CodeGeneratorARMVIXL::GenerateUnsafeCasOldValueAddWithBakerReadBarrier(
8533 vixl::aarch32::Register old_value,
8534 vixl::aarch32::Register adjusted_old_value,
8535 vixl::aarch32::Register expected) {
8536 DCHECK(kEmitCompilerReadBarrier);
8537 DCHECK(kUseBakerReadBarrier);
8538
8539 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with an ADD instead of LDR.
8540 uint32_t custom_data = EncodeBakerReadBarrierUnsafeCasData(old_value.GetCode());
8541
8542 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8543 size_t wide_instructions = /* ADR+CMP+ADD+BNE */ 4u - narrow_instructions;
8544 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8545 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8546 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
8547 vixl32::Label return_address;
8548 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8549 __ cmp(mr, Operand(0));
8550 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8551 __ add(EncodingSize(Wide), old_value, adjusted_old_value, Operand(expected)); // Preserves flags.
8552 EmitBakerReadBarrierBne(custom_data);
8553 __ bind(&return_address);
8554 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8555 BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ADD_OFFSET);
8556}
8557
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008558void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8559 Location ref,
8560 vixl32::Register obj,
Vladimir Marko248141f2018-08-10 10:40:07 +01008561 const vixl32::MemOperand& src,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008562 bool needs_null_check) {
8563 DCHECK(kEmitCompilerReadBarrier);
8564 DCHECK(kUseBakerReadBarrier);
8565
Vladimir Marko008e09f32018-08-06 15:42:43 +01008566 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8567 // Marking Register) to decide whether we need to enter the slow
8568 // path to mark the reference. Then, in the slow path, check the
8569 // gray bit in the lock word of the reference's holder (`obj`) to
8570 // decide whether to mark `ref` or not.
8571 //
8572 // We use shared thunks for the slow path; shared within the method
8573 // for JIT, across methods for AOT. That thunk checks the holder
8574 // and jumps to the entrypoint if needed. If the holder is not gray,
8575 // it creates a fake dependency and returns to the LDR instruction.
8576 //
8577 // lr = &gray_return_address;
8578 // if (mr) { // Thread::Current()->GetIsGcMarking()
8579 // goto field_thunk<holder_reg, base_reg>(lr)
8580 // }
8581 // not_gray_return_address:
8582 // // Original reference load. If the offset is too large to fit
8583 // // into LDR, we use an adjusted base register here.
8584 // HeapReference<mirror::Object> reference = *(obj+offset);
8585 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008586
Vladimir Marko248141f2018-08-10 10:40:07 +01008587 DCHECK(src.GetAddrMode() == vixl32::Offset);
8588 DCHECK_ALIGNED(src.GetOffsetImmediate(), sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko008e09f32018-08-06 15:42:43 +01008589 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko248141f2018-08-10 10:40:07 +01008590 bool narrow = CanEmitNarrowLdr(ref_reg, src.GetBaseRegister(), src.GetOffsetImmediate());
8591
Vladimir Marko008e09f32018-08-06 15:42:43 +01008592 UseScratchRegisterScope temps(GetVIXLAssembler());
8593 temps.Exclude(ip);
Vladimir Marko248141f2018-08-10 10:40:07 +01008594 uint32_t custom_data =
8595 EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode(), narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008596
Vladimir Marko008e09f32018-08-06 15:42:43 +01008597 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008598 size_t narrow_instructions =
8599 /* CMP */ (mr.IsLow() ? 1u : 0u) +
8600 /* LDR+unpoison? */ (narrow ? (kPoisonHeapReferences ? 2u : 1u) : 0u);
8601 size_t wide_instructions =
8602 /* ADR+CMP+LDR+BNE+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8603 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8604 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8605 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008606 vixl32::Label return_address;
8607 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8608 __ cmp(mr, Operand(0));
8609 EmitBakerReadBarrierBne(custom_data);
8610 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
Vladimir Marko248141f2018-08-10 10:40:07 +01008611 __ ldr(EncodingSize(narrow ? Narrow : Wide), ref_reg, src);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008612 if (needs_null_check) {
8613 MaybeRecordImplicitNullCheck(instruction);
8614 }
8615 // Note: We need a specific width for the unpoisoning NEG.
8616 if (kPoisonHeapReferences) {
8617 if (narrow) {
8618 // The only 16-bit encoding is T1 which sets flags outside IT block (i.e. RSBS, not RSB).
8619 __ rsbs(EncodingSize(Narrow), ref_reg, ref_reg, Operand(0));
8620 } else {
8621 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
8622 }
8623 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008624 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008625 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8626 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8627 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
8628 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008629 MaybeGenerateMarkingRegisterCheck(/* code= */ 20, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008630}
8631
Vladimir Marko248141f2018-08-10 10:40:07 +01008632void CodeGeneratorARMVIXL::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8633 Location ref,
8634 vixl32::Register obj,
8635 uint32_t offset,
8636 Location temp,
8637 bool needs_null_check) {
8638 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8639 vixl32::Register base = obj;
8640 if (offset >= kReferenceLoadMinFarOffset) {
8641 base = RegisterFrom(temp);
8642 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8643 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
8644 offset &= (kReferenceLoadMinFarOffset - 1u);
8645 }
8646 GenerateFieldLoadWithBakerReadBarrier(
8647 instruction, ref, obj, MemOperand(base, offset), needs_null_check);
8648}
8649
Vladimir Marko008e09f32018-08-06 15:42:43 +01008650void CodeGeneratorARMVIXL::GenerateArrayLoadWithBakerReadBarrier(Location ref,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008651 vixl32::Register obj,
8652 uint32_t data_offset,
8653 Location index,
8654 Location temp,
8655 bool needs_null_check) {
8656 DCHECK(kEmitCompilerReadBarrier);
8657 DCHECK(kUseBakerReadBarrier);
8658
8659 static_assert(
8660 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8661 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008662 ScaleFactor scale_factor = TIMES_4;
8663
Vladimir Marko008e09f32018-08-06 15:42:43 +01008664 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
8665 // Marking Register) to decide whether we need to enter the slow
8666 // path to mark the reference. Then, in the slow path, check the
8667 // gray bit in the lock word of the reference's holder (`obj`) to
8668 // decide whether to mark `ref` or not.
8669 //
8670 // We use shared thunks for the slow path; shared within the method
8671 // for JIT, across methods for AOT. That thunk checks the holder
8672 // and jumps to the entrypoint if needed. If the holder is not gray,
8673 // it creates a fake dependency and returns to the LDR instruction.
8674 //
8675 // lr = &gray_return_address;
8676 // if (mr) { // Thread::Current()->GetIsGcMarking()
8677 // goto array_thunk<base_reg>(lr)
8678 // }
8679 // not_gray_return_address:
8680 // // Original reference load. If the offset is too large to fit
8681 // // into LDR, we use an adjusted base register here.
8682 // HeapReference<mirror::Object> reference = data[index];
8683 // gray_return_address:
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008684
Vladimir Marko008e09f32018-08-06 15:42:43 +01008685 DCHECK(index.IsValid());
8686 vixl32::Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
8687 vixl32::Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
8688 vixl32::Register data_reg = RegisterFrom(temp, DataType::Type::kInt32); // Raw pointer.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008689
Vladimir Marko008e09f32018-08-06 15:42:43 +01008690 UseScratchRegisterScope temps(GetVIXLAssembler());
8691 temps.Exclude(ip);
8692 uint32_t custom_data = EncodeBakerReadBarrierArrayData(data_reg.GetCode());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008693
Vladimir Marko008e09f32018-08-06 15:42:43 +01008694 __ Add(data_reg, obj, Operand(data_offset));
8695 {
Vladimir Markod887ed82018-08-14 13:52:12 +00008696 size_t narrow_instructions = /* CMP */ (mr.IsLow() ? 1u : 0u);
8697 size_t wide_instructions =
8698 /* ADR+CMP+BNE+LDR+unpoison? */ (kPoisonHeapReferences ? 5u : 4u) - narrow_instructions;
8699 size_t exact_size = wide_instructions * vixl32::k32BitT32InstructionSizeInBytes +
8700 narrow_instructions * vixl32::k16BitT32InstructionSizeInBytes;
8701 ExactAssemblyScope guard(GetVIXLAssembler(), exact_size);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008702 vixl32::Label return_address;
8703 EmitAdrCode adr(GetVIXLAssembler(), lr, &return_address);
8704 __ cmp(mr, Operand(0));
8705 EmitBakerReadBarrierBne(custom_data);
8706 ptrdiff_t old_offset = GetVIXLAssembler()->GetBuffer()->GetCursorOffset();
8707 __ ldr(ref_reg, MemOperand(data_reg, index_reg, vixl32::LSL, scale_factor));
8708 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8709 // Note: We need a Wide NEG for the unpoisoning.
8710 if (kPoisonHeapReferences) {
8711 __ rsb(EncodingSize(Wide), ref_reg, ref_reg, Operand(0));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008712 }
Vladimir Markod887ed82018-08-14 13:52:12 +00008713 __ bind(&return_address);
Vladimir Marko008e09f32018-08-06 15:42:43 +01008714 DCHECK_EQ(old_offset - GetVIXLAssembler()->GetBuffer()->GetCursorOffset(),
8715 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008716 }
Andreas Gampe3db70682018-12-26 15:12:03 -08008717 MaybeGenerateMarkingRegisterCheck(/* code= */ 21, /* temp_loc= */ LocationFrom(ip));
Roland Levillain6070e882016-11-03 17:51:58 +00008718}
8719
Roland Levillain5daa4952017-07-03 17:23:56 +01008720void CodeGeneratorARMVIXL::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
8721 // The following condition is a compile-time one, so it does not have a run-time cost.
8722 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
8723 // The following condition is a run-time one; it is executed after the
8724 // previous compile-time test, to avoid penalizing non-debug builds.
8725 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
8726 UseScratchRegisterScope temps(GetVIXLAssembler());
8727 vixl32::Register temp = temp_loc.IsValid() ? RegisterFrom(temp_loc) : temps.Acquire();
8728 GetAssembler()->GenerateMarkingRegisterCheck(temp,
8729 kMarkingRegisterCheckBreakCodeBaseCode + code);
8730 }
8731 }
8732}
8733
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008734void CodeGeneratorARMVIXL::GenerateReadBarrierSlow(HInstruction* instruction,
8735 Location out,
8736 Location ref,
8737 Location obj,
8738 uint32_t offset,
8739 Location index) {
8740 DCHECK(kEmitCompilerReadBarrier);
8741
8742 // Insert a slow path based read barrier *after* the reference load.
8743 //
8744 // If heap poisoning is enabled, the unpoisoning of the loaded
8745 // reference will be carried out by the runtime within the slow
8746 // path.
8747 //
8748 // Note that `ref` currently does not get unpoisoned (when heap
8749 // poisoning is enabled), which is alright as the `ref` argument is
8750 // not used by the artReadBarrierSlow entry point.
8751 //
8752 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008753 SlowPathCodeARMVIXL* slow_path = new (GetScopedAllocator())
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008754 ReadBarrierForHeapReferenceSlowPathARMVIXL(instruction, out, ref, obj, offset, index);
8755 AddSlowPath(slow_path);
8756
8757 __ B(slow_path->GetEntryLabel());
8758 __ Bind(slow_path->GetExitLabel());
8759}
8760
8761void CodeGeneratorARMVIXL::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
Artem Serov02d37832016-10-25 15:25:33 +01008762 Location out,
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008763 Location ref,
8764 Location obj,
8765 uint32_t offset,
8766 Location index) {
Artem Serov02d37832016-10-25 15:25:33 +01008767 if (kEmitCompilerReadBarrier) {
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008768 // Baker's read barriers shall be handled by the fast path
Roland Levillain9983e302017-07-14 14:34:22 +01008769 // (CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier).
Artem Serov02d37832016-10-25 15:25:33 +01008770 DCHECK(!kUseBakerReadBarrier);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008771 // If heap poisoning is enabled, unpoisoning will be taken care of
8772 // by the runtime within the slow path.
8773 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Artem Serov02d37832016-10-25 15:25:33 +01008774 } else if (kPoisonHeapReferences) {
8775 GetAssembler()->UnpoisonHeapReference(RegisterFrom(out));
8776 }
8777}
8778
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008779void CodeGeneratorARMVIXL::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8780 Location out,
8781 Location root) {
8782 DCHECK(kEmitCompilerReadBarrier);
8783
8784 // Insert a slow path based read barrier *after* the GC root load.
8785 //
8786 // Note that GC roots are not affected by heap poisoning, so we do
8787 // not need to do anything special for this here.
8788 SlowPathCodeARMVIXL* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008789 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARMVIXL(instruction, out, root);
Anton Kirilovedb2ac32016-11-30 15:14:10 +00008790 AddSlowPath(slow_path);
8791
8792 __ B(slow_path->GetEntryLabel());
8793 __ Bind(slow_path->GetExitLabel());
8794}
8795
Artem Serov02d37832016-10-25 15:25:33 +01008796// Check if the desired_dispatch_info is supported. If it is, return it,
8797// otherwise return a fall-back info that should be used instead.
8798HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARMVIXL::GetSupportedInvokeStaticOrDirectDispatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008799 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01008800 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008801 return desired_dispatch_info;
Artem Serov02d37832016-10-25 15:25:33 +01008802}
8803
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008804vixl32::Register CodeGeneratorARMVIXL::GetInvokeStaticOrDirectExtraParameter(
8805 HInvokeStaticOrDirect* invoke, vixl32::Register temp) {
8806 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8807 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8808 if (!invoke->GetLocations()->Intrinsified()) {
8809 return RegisterFrom(location);
8810 }
8811 // For intrinsics we allow any location, so it may be on the stack.
8812 if (!location.IsRegister()) {
8813 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, location.GetStackIndex());
8814 return temp;
8815 }
8816 // For register locations, check if the register was saved. If so, get it from the stack.
8817 // Note: There is a chance that the register was saved but not overwritten, so we could
8818 // save one load. However, since this is just an intrinsic slow path we prefer this
8819 // simple and more robust approach rather that trying to determine if that's the case.
8820 SlowPathCode* slow_path = GetCurrentSlowPath();
Scott Wakelingd5cd4972017-02-03 11:38:35 +00008821 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(RegisterFrom(location).GetCode())) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008822 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(RegisterFrom(location).GetCode());
8823 GetAssembler()->LoadFromOffset(kLoadWord, temp, sp, stack_offset);
8824 return temp;
8825 }
8826 return RegisterFrom(location);
8827}
8828
Vladimir Markod254f5c2017-06-02 15:18:36 +00008829void CodeGeneratorARMVIXL::GenerateStaticOrDirectCall(
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008830 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00008831 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008832 switch (invoke->GetMethodLoadKind()) {
8833 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8834 uint32_t offset =
8835 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
8836 // temp = thread->string_init_entrypoint
Artem Serovd4cc5b22016-11-04 11:19:09 +00008837 GetAssembler()->LoadFromOffset(kLoadWord, RegisterFrom(temp), tr, offset);
8838 break;
8839 }
8840 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
8841 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8842 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008843 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01008844 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008845 PcRelativePatchInfo* labels = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01008846 vixl32::Register temp_reg = RegisterFrom(temp);
8847 EmitMovwMovtPlaceholder(labels, temp_reg);
8848 break;
8849 }
Vladimir Markob066d432018-01-03 13:14:37 +00008850 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008851 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00008852 PcRelativePatchInfo* labels = NewBootImageRelRoPatch(boot_image_offset);
8853 vixl32::Register temp_reg = RegisterFrom(temp);
8854 EmitMovwMovtPlaceholder(labels, temp_reg);
8855 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
8856 break;
8857 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008858 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8859 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8860 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8861 vixl32::Register temp_reg = RegisterFrom(temp);
8862 EmitMovwMovtPlaceholder(labels, temp_reg);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01008863 // All aligned loads are implicitly atomic consume operations on ARM.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008864 GetAssembler()->LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset*/ 0);
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008865 break;
8866 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008867 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
8868 __ Mov(RegisterFrom(temp), Operand::From(invoke->GetMethodAddress()));
8869 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008870 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8871 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8872 return; // No code pointer retrieval; the runtime performs the call directly.
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008873 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008874 }
8875
Artem Serovd4cc5b22016-11-04 11:19:09 +00008876 switch (invoke->GetCodePtrLocation()) {
8877 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008878 {
8879 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8880 ExactAssemblyScope aas(GetVIXLAssembler(),
8881 vixl32::k32BitT32InstructionSizeInBytes,
8882 CodeBufferCheckScope::kMaximumSize);
8883 __ bl(GetFrameEntryLabel());
8884 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8885 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008886 break;
Artem Serovd4cc5b22016-11-04 11:19:09 +00008887 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8888 // LR = callee_method->entry_point_from_quick_compiled_code_
8889 GetAssembler()->LoadFromOffset(
8890 kLoadWord,
8891 lr,
8892 RegisterFrom(callee_method),
8893 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Alexandre Rames374ddf32016-11-04 10:40:49 +00008894 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008895 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Alexandre Rames374ddf32016-11-04 10:40:49 +00008896 // 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 +00008897 ExactAssemblyScope aas(GetVIXLAssembler(),
8898 vixl32::k16BitT32InstructionSizeInBytes,
8899 CodeBufferCheckScope::kExactSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008900 // LR()
8901 __ blx(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008902 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008903 }
Artem Serovd4cc5b22016-11-04 11:19:09 +00008904 break;
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008905 }
8906
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008907 DCHECK(!IsLeafMethod());
8908}
8909
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008910void CodeGeneratorARMVIXL::GenerateVirtualCall(
8911 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008912 vixl32::Register temp = RegisterFrom(temp_location);
8913 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8914 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
8915
8916 // Use the calling convention instead of the location of the receiver, as
8917 // intrinsics may have put the receiver in a different register. In the intrinsics
8918 // slow path, the arguments have been moved to the right place, so here we are
8919 // guaranteed that the receiver is the first register of the calling convention.
8920 InvokeDexCallingConventionARMVIXL calling_convention;
8921 vixl32::Register receiver = calling_convention.GetRegisterAt(0);
8922 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Alexandre Rames374ddf32016-11-04 10:40:49 +00008923 {
8924 // Make sure the pc is recorded immediately after the `ldr` instruction.
Artem Serov0fb37192016-12-06 18:13:40 +00008925 ExactAssemblyScope aas(GetVIXLAssembler(),
8926 vixl32::kMaxInstructionSizeInBytes,
8927 CodeBufferCheckScope::kMaximumSize);
Alexandre Rames374ddf32016-11-04 10:40:49 +00008928 // /* HeapReference<Class> */ temp = receiver->klass_
8929 __ ldr(temp, MemOperand(receiver, class_offset));
8930 MaybeRecordImplicitNullCheck(invoke);
8931 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008932 // Instead of simply (possibly) unpoisoning `temp` here, we should
8933 // emit a read barrier for the previous class reference load.
8934 // However this is not required in practice, as this is an
8935 // intermediate/temporary reference and because the current
8936 // concurrent copying collector keeps the from-space memory
8937 // intact/accessible until the end of the marking phase (the
8938 // concurrent copying collector may not in the future).
8939 GetAssembler()->MaybeUnpoisonHeapReference(temp);
8940
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00008941 // If we're compiling baseline, update the inline cache.
8942 MaybeGenerateInlineCacheCheck(invoke, temp);
8943
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008944 // temp = temp->GetMethodAt(method_offset);
8945 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
8946 kArmPointerSize).Int32Value();
8947 GetAssembler()->LoadFromOffset(kLoadWord, temp, temp, method_offset);
8948 // LR = temp->GetEntryPoint();
8949 GetAssembler()->LoadFromOffset(kLoadWord, lr, temp, entry_point);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008950 {
8951 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
8952 // blx in T32 has only 16bit encoding that's why a stricter check for the scope is used.
8953 ExactAssemblyScope aas(GetVIXLAssembler(),
8954 vixl32::k16BitT32InstructionSizeInBytes,
8955 CodeBufferCheckScope::kExactSize);
8956 // LR();
8957 __ blx(lr);
8958 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8959 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01008960}
8961
Vladimir Marko6fd16062018-06-26 11:02:04 +01008962CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageIntrinsicPatch(
8963 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01008964 return NewPcRelativePatch(/* dex_file= */ nullptr, intrinsic_data, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01008965}
8966
Vladimir Markob066d432018-01-03 13:14:37 +00008967CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageRelRoPatch(
8968 uint32_t boot_image_offset) {
Andreas Gampe3db70682018-12-26 15:12:03 -08008969 return NewPcRelativePatch(/* dex_file= */ nullptr,
Vladimir Markob066d432018-01-03 13:14:37 +00008970 boot_image_offset,
Vladimir Marko2d06e022019-07-08 15:45:19 +01008971 &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00008972}
8973
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008974CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01008975 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008976 return NewPcRelativePatch(
8977 target_method.dex_file, target_method.index, &boot_image_method_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008978}
8979
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008980CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewMethodBssEntryPatch(
8981 MethodReference target_method) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008982 return NewPcRelativePatch(
8983 target_method.dex_file, target_method.index, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008984}
8985
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008986CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageTypePatch(
Artem Serovd4cc5b22016-11-04 11:19:09 +00008987 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008988 return NewPcRelativePatch(&dex_file, type_index.index_, &boot_image_type_patches_);
Artem Serovd4cc5b22016-11-04 11:19:09 +00008989}
8990
Vladimir Marko1998cd02017-01-13 13:02:58 +00008991CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewTypeBssEntryPatch(
8992 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008993 return NewPcRelativePatch(&dex_file, type_index.index_, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00008994}
8995
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008996CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01008997 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008998 return NewPcRelativePatch(&dex_file, string_index.index_, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01008999}
9000
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009001CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewStringBssEntryPatch(
9002 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009003 return NewPcRelativePatch(&dex_file, string_index.index_, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009004}
9005
Artem Serovd4cc5b22016-11-04 11:19:09 +00009006CodeGeneratorARMVIXL::PcRelativePatchInfo* CodeGeneratorARMVIXL::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009007 const DexFile* dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009008 patches->emplace_back(dex_file, offset_or_index);
9009 return &patches->back();
9010}
9011
Vladimir Markof6675082019-05-17 12:05:28 +01009012void CodeGeneratorARMVIXL::EmitEntrypointThunkCall(ThreadOffset32 entrypoint_offset) {
9013 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
9014 DCHECK(!Runtime::Current()->UseJitCompilation());
9015 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
9016 vixl::aarch32::Label* bl_label = &call_entrypoint_patches_.back().label;
9017 __ bind(bl_label);
9018 vixl32::Label placeholder_label;
9019 __ bl(&placeholder_label); // Placeholder, patched at link-time.
9020 __ bind(&placeholder_label);
9021}
9022
Vladimir Marko966b46f2018-08-03 10:20:19 +00009023void CodeGeneratorARMVIXL::EmitBakerReadBarrierBne(uint32_t custom_data) {
Vladimir Markod887ed82018-08-14 13:52:12 +00009024 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko966b46f2018-08-03 10:20:19 +00009025 if (Runtime::Current()->UseJitCompilation()) {
9026 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
9027 vixl::aarch32::Label* slow_path_entry = &it->second.label;
9028 __ b(ne, EncodingSize(Wide), slow_path_entry);
9029 } else {
9030 baker_read_barrier_patches_.emplace_back(custom_data);
9031 vixl::aarch32::Label* patch_label = &baker_read_barrier_patches_.back().label;
9032 __ bind(patch_label);
9033 vixl32::Label placeholder_label;
9034 __ b(ne, EncodingSize(Wide), &placeholder_label); // Placeholder, patched at link-time.
9035 __ bind(&placeholder_label);
9036 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009037}
9038
Artem Serovc5fcb442016-12-02 19:19:58 +00009039VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateBootImageAddressLiteral(uint32_t address) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009040 return DeduplicateUint32Literal(address, &uint32_literals_);
Artem Serovc5fcb442016-12-02 19:19:58 +00009041}
9042
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009043VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitStringLiteral(
9044 const DexFile& dex_file,
9045 dex::StringIndex string_index,
9046 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009047 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009048 return jit_string_patches_.GetOrCreate(
9049 StringReference(&dex_file, string_index),
9050 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009051 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009052 });
9053}
9054
9055VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateJitClassLiteral(const DexFile& dex_file,
9056 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009057 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01009058 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Artem Serovc5fcb442016-12-02 19:19:58 +00009059 return jit_class_patches_.GetOrCreate(
9060 TypeReference(&dex_file, type_index),
9061 [this]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009062 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
Artem Serovc5fcb442016-12-02 19:19:58 +00009063 });
9064}
9065
Vladimir Marko6fd16062018-06-26 11:02:04 +01009066void CodeGeneratorARMVIXL::LoadBootImageAddress(vixl32::Register reg,
9067 uint32_t boot_image_reference) {
9068 if (GetCompilerOptions().IsBootImage()) {
9069 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9070 NewBootImageIntrinsicPatch(boot_image_reference);
9071 EmitMovwMovtPlaceholder(labels, reg);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01009072 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01009073 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels =
9074 NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009075 EmitMovwMovtPlaceholder(labels, reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08009076 __ Ldr(reg, MemOperand(reg, /* offset= */ 0));
Vladimir Markoeebb8212018-06-05 14:57:24 +01009077 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01009078 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01009079 gc::Heap* heap = Runtime::Current()->GetHeap();
9080 DCHECK(!heap->GetBootImageSpaces().empty());
9081 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01009082 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01009083 __ Ldr(reg, DeduplicateBootImageAddressLiteral(dchecked_integral_cast<uint32_t>(address)));
9084 }
9085}
9086
Vladimir Marko6fd16062018-06-26 11:02:04 +01009087void CodeGeneratorARMVIXL::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
9088 uint32_t boot_image_offset) {
9089 DCHECK(invoke->IsStatic());
9090 InvokeRuntimeCallingConventionARMVIXL calling_convention;
9091 vixl32::Register argument = calling_convention.GetRegisterAt(0);
9092 if (GetCompilerOptions().IsBootImage()) {
9093 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
9094 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
9095 MethodReference target_method = invoke->GetTargetMethod();
9096 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
9097 PcRelativePatchInfo* labels = NewBootImageTypePatch(*target_method.dex_file, type_idx);
9098 EmitMovwMovtPlaceholder(labels, argument);
9099 } else {
9100 LoadBootImageAddress(argument, boot_image_offset);
9101 }
9102 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
9103 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
9104}
9105
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009106template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Artem Serovd4cc5b22016-11-04 11:19:09 +00009107inline void CodeGeneratorARMVIXL::EmitPcRelativeLinkerPatches(
9108 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009109 ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009110 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009111 const DexFile* dex_file = info.target_dex_file;
Artem Serovd4cc5b22016-11-04 11:19:09 +00009112 size_t offset_or_index = info.offset_or_index;
9113 DCHECK(info.add_pc_label.IsBound());
9114 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.GetLocation());
9115 // Add MOVW patch.
9116 DCHECK(info.movw_label.IsBound());
9117 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009118 linker_patches->push_back(Factory(movw_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009119 // Add MOVT patch.
9120 DCHECK(info.movt_label.IsBound());
9121 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.GetLocation());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009122 linker_patches->push_back(Factory(movt_offset, dex_file, add_pc_offset, offset_or_index));
Artem Serovd4cc5b22016-11-04 11:19:09 +00009123 }
9124}
9125
Vladimir Marko6fd16062018-06-26 11:02:04 +01009126template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
9127linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
9128 const DexFile* target_dex_file,
9129 uint32_t pc_insn_offset,
9130 uint32_t boot_image_offset) {
9131 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
9132 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00009133}
9134
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009135void CodeGeneratorARMVIXL::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Artem Serovd4cc5b22016-11-04 11:19:09 +00009136 DCHECK(linker_patches->empty());
9137 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009138 /* MOVW+MOVT for each entry */ 2u * boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009139 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009140 /* MOVW+MOVT for each entry */ 2u * boot_image_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009141 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009142 /* MOVW+MOVT for each entry */ 2u * boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01009143 /* MOVW+MOVT for each entry */ 2u * string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01009144 /* MOVW+MOVT for each entry */ 2u * boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01009145 call_entrypoint_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009146 baker_read_barrier_patches_.size();
Artem Serovd4cc5b22016-11-04 11:19:09 +00009147 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01009148 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009149 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009150 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009151 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009152 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009153 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009154 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009155 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01009156 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00009157 DCHECK(boot_image_type_patches_.empty());
9158 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01009159 }
9160 if (GetCompilerOptions().IsBootImage()) {
9161 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
9162 boot_image_other_patches_, linker_patches);
9163 } else {
9164 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
9165 boot_image_other_patches_, linker_patches);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009166 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009167 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
9168 method_bss_entry_patches_, linker_patches);
9169 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
9170 type_bss_entry_patches_, linker_patches);
9171 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
9172 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01009173 for (const PatchInfo<vixl32::Label>& info : call_entrypoint_patches_) {
9174 DCHECK(info.target_dex_file == nullptr);
9175 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
9176 info.label.GetLocation(), info.offset_or_index));
9177 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009178 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01009179 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
9180 info.label.GetLocation(), info.custom_data));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009181 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009182 DCHECK_EQ(size, linker_patches->size());
Artem Serovc5fcb442016-12-02 19:19:58 +00009183}
9184
Vladimir Markoca1e0382018-04-11 09:58:41 +00009185bool CodeGeneratorARMVIXL::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01009186 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
9187 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00009188 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
9189}
9190
9191void CodeGeneratorARMVIXL::EmitThunkCode(const linker::LinkerPatch& patch,
9192 /*out*/ ArenaVector<uint8_t>* code,
9193 /*out*/ std::string* debug_name) {
9194 arm::ArmVIXLAssembler assembler(GetGraph()->GetAllocator());
9195 switch (patch.GetType()) {
Vladimir Markof6675082019-05-17 12:05:28 +01009196 case linker::LinkerPatch::Type::kCallRelative: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009197 // The thunk just uses the entry point in the ArtMethod. This works even for calls
9198 // to the generic JNI and interpreter trampolines.
Vladimir Markof6675082019-05-17 12:05:28 +01009199 MemberOffset offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
9200 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, vixl32::r0, offset.Int32Value());
Vladimir Markoca1e0382018-04-11 09:58:41 +00009201 assembler.GetVIXLAssembler()->Bkpt(0);
9202 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9203 *debug_name = "MethodCallThunk";
9204 }
9205 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009206 }
9207 case linker::LinkerPatch::Type::kCallEntrypoint: {
9208 assembler.LoadFromOffset(arm::kLoadWord, vixl32::pc, tr, patch.EntrypointOffset());
9209 assembler.GetVIXLAssembler()->Bkpt(0);
9210 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
9211 *debug_name = "EntrypointCallThunk_" + std::to_string(patch.EntrypointOffset());
9212 }
9213 break;
9214 }
9215 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009216 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
9217 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
9218 break;
Vladimir Markof6675082019-05-17 12:05:28 +01009219 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009220 default:
9221 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
9222 UNREACHABLE();
9223 }
9224
9225 // Ensure we emit the literal pool if any.
9226 assembler.FinalizeCode();
9227 code->resize(assembler.CodeSize());
9228 MemoryRegion code_region(code->data(), code->size());
9229 assembler.FinalizeInstructions(code_region);
9230}
9231
Artem Serovc5fcb442016-12-02 19:19:58 +00009232VIXLUInt32Literal* CodeGeneratorARMVIXL::DeduplicateUint32Literal(
9233 uint32_t value,
9234 Uint32ToLiteralMap* map) {
9235 return map->GetOrCreate(
9236 value,
9237 [this, value]() {
Andreas Gampe3db70682018-12-26 15:12:03 -08009238 return GetAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ value);
Artem Serovc5fcb442016-12-02 19:19:58 +00009239 });
9240}
9241
Artem Serov2bbc9532016-10-21 11:51:50 +01009242void LocationsBuilderARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9243 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009244 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Serov2bbc9532016-10-21 11:51:50 +01009245 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9246 Location::RequiresRegister());
9247 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9248 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9250}
9251
9252void InstructionCodeGeneratorARMVIXL::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9253 vixl32::Register res = OutputRegister(instr);
9254 vixl32::Register accumulator =
9255 InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
9256 vixl32::Register mul_left =
9257 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
9258 vixl32::Register mul_right =
9259 InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
9260
9261 if (instr->GetOpKind() == HInstruction::kAdd) {
9262 __ Mla(res, mul_left, mul_right, accumulator);
9263 } else {
9264 __ Mls(res, mul_left, mul_right, accumulator);
9265 }
9266}
9267
Artem Serov551b28f2016-10-18 19:11:30 +01009268void LocationsBuilderARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9269 // Nothing to do, this should be removed during prepare for register allocator.
9270 LOG(FATAL) << "Unreachable";
9271}
9272
9273void InstructionCodeGeneratorARMVIXL::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9274 // Nothing to do, this should be removed during prepare for register allocator.
9275 LOG(FATAL) << "Unreachable";
9276}
9277
9278// Simple implementation of packed switch - generate cascaded compare/jumps.
9279void LocationsBuilderARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9280 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009281 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Artem Serov551b28f2016-10-18 19:11:30 +01009282 locations->SetInAt(0, Location::RequiresRegister());
9283 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
9284 codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9285 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9286 if (switch_instr->GetStartValue() != 0) {
9287 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9288 }
9289 }
9290}
9291
9292// TODO(VIXL): Investigate and reach the parity with old arm codegen.
9293void InstructionCodeGeneratorARMVIXL::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9294 int32_t lower_bound = switch_instr->GetStartValue();
9295 uint32_t num_entries = switch_instr->GetNumEntries();
9296 LocationSummary* locations = switch_instr->GetLocations();
9297 vixl32::Register value_reg = InputRegisterAt(switch_instr, 0);
9298 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9299
9300 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
9301 !codegen_->GetAssembler()->GetVIXLAssembler()->IsUsingT32()) {
9302 // Create a series of compare/jumps.
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009303 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009304 vixl32::Register temp_reg = temps.Acquire();
9305 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9306 // the immediate, because IP is used as the destination register. For the other
9307 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9308 // and they can be encoded in the instruction without making use of IP register.
9309 __ Adds(temp_reg, value_reg, -lower_bound);
9310
9311 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9312 // Jump to successors[0] if value == lower_bound.
9313 __ B(eq, codegen_->GetLabelOf(successors[0]));
9314 int32_t last_index = 0;
9315 for (; num_entries - last_index > 2; last_index += 2) {
9316 __ Adds(temp_reg, temp_reg, -2);
9317 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9318 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
9319 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9320 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
9321 }
9322 if (num_entries - last_index == 2) {
9323 // The last missing case_value.
9324 __ Cmp(temp_reg, 1);
9325 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
9326 }
9327
9328 // And the default for any other value.
9329 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9330 __ B(codegen_->GetLabelOf(default_block));
9331 }
9332 } else {
9333 // Create a table lookup.
9334 vixl32::Register table_base = RegisterFrom(locations->GetTemp(0));
9335
9336 JumpTableARMVIXL* jump_table = codegen_->CreateJumpTable(switch_instr);
9337
9338 // Remove the bias.
9339 vixl32::Register key_reg;
9340 if (lower_bound != 0) {
9341 key_reg = RegisterFrom(locations->GetTemp(1));
9342 __ Sub(key_reg, value_reg, lower_bound);
9343 } else {
9344 key_reg = value_reg;
9345 }
9346
9347 // Check whether the value is in the table, jump to default block if not.
9348 __ Cmp(key_reg, num_entries - 1);
9349 __ B(hi, codegen_->GetLabelOf(default_block));
9350
Anton Kirilovedb2ac32016-11-30 15:14:10 +00009351 UseScratchRegisterScope temps(GetVIXLAssembler());
Artem Serov551b28f2016-10-18 19:11:30 +01009352 vixl32::Register jump_offset = temps.Acquire();
9353
9354 // Load jump offset from the table.
Scott Wakeling86e9d262017-01-18 15:59:24 +00009355 {
9356 const size_t jump_size = switch_instr->GetNumEntries() * sizeof(int32_t);
9357 ExactAssemblyScope aas(GetVIXLAssembler(),
9358 (vixl32::kMaxInstructionSizeInBytes * 4) + jump_size,
9359 CodeBufferCheckScope::kMaximumSize);
9360 __ adr(table_base, jump_table->GetTableStartLabel());
9361 __ ldr(jump_offset, MemOperand(table_base, key_reg, vixl32::LSL, 2));
Artem Serov551b28f2016-10-18 19:11:30 +01009362
Scott Wakeling86e9d262017-01-18 15:59:24 +00009363 // Jump to target block by branching to table_base(pc related) + offset.
9364 vixl32::Register target_address = table_base;
9365 __ add(target_address, table_base, jump_offset);
9366 __ bx(target_address);
Artem Serov09a940d2016-11-11 16:15:11 +00009367
Scott Wakeling86e9d262017-01-18 15:59:24 +00009368 jump_table->EmitTable(codegen_);
9369 }
Artem Serov551b28f2016-10-18 19:11:30 +01009370 }
9371}
9372
Artem Serov02d37832016-10-25 15:25:33 +01009373// Copy the result of a call into the given target.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009374void CodeGeneratorARMVIXL::MoveFromReturnRegister(Location trg, DataType::Type type) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009375 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009376 DCHECK_EQ(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009377 return;
9378 }
9379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009380 DCHECK_NE(type, DataType::Type::kVoid);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009381
Artem Serovd4cc5b22016-11-04 11:19:09 +00009382 Location return_loc = InvokeDexCallingConventionVisitorARMVIXL().GetReturnLocation(type);
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009383 if (return_loc.Equals(trg)) {
9384 return;
9385 }
9386
9387 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9388 // with the last branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009389 if (type == DataType::Type::kInt64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009390 TODO_VIXL32(FATAL);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009391 } else if (type == DataType::Type::kFloat64) {
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009392 TODO_VIXL32(FATAL);
9393 } else {
9394 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01009395 HParallelMove parallel_move(GetGraph()->GetAllocator());
Anton Kirilove28d9ae2016-10-25 18:17:23 +01009396 parallel_move.AddMove(return_loc, trg, type, nullptr);
9397 GetMoveResolver()->EmitNativeCode(&parallel_move);
9398 }
Scott Wakelinga7812ae2016-10-17 10:03:36 +01009399}
Scott Wakelingfe885462016-09-22 10:24:38 +01009400
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009401void LocationsBuilderARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9402 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009403 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009404 locations->SetInAt(0, Location::RequiresRegister());
9405 locations->SetOut(Location::RequiresRegister());
Artem Serov551b28f2016-10-18 19:11:30 +01009406}
9407
xueliang.zhong8d2c4592016-11-23 17:05:25 +00009408void InstructionCodeGeneratorARMVIXL::VisitClassTableGet(HClassTableGet* instruction) {
9409 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
9410 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9411 instruction->GetIndex(), kArmPointerSize).SizeValue();
9412 GetAssembler()->LoadFromOffset(kLoadWord,
9413 OutputRegister(instruction),
9414 InputRegisterAt(instruction, 0),
9415 method_offset);
9416 } else {
9417 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
9418 instruction->GetIndex(), kArmPointerSize));
9419 GetAssembler()->LoadFromOffset(kLoadWord,
9420 OutputRegister(instruction),
9421 InputRegisterAt(instruction, 0),
9422 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9423 GetAssembler()->LoadFromOffset(kLoadWord,
9424 OutputRegister(instruction),
9425 OutputRegister(instruction),
9426 method_offset);
9427 }
Artem Serov551b28f2016-10-18 19:11:30 +01009428}
9429
Artem Serovc5fcb442016-12-02 19:19:58 +00009430static void PatchJitRootUse(uint8_t* code,
9431 const uint8_t* roots_data,
9432 VIXLUInt32Literal* literal,
9433 uint64_t index_in_table) {
9434 DCHECK(literal->IsBound());
9435 uint32_t literal_offset = literal->GetLocation();
9436 uintptr_t address =
9437 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9438 uint8_t* data = code + literal_offset;
9439 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9440}
9441
9442void CodeGeneratorARMVIXL::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9443 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009444 const StringReference& string_reference = entry.first;
9445 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009446 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009447 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009448 }
9449 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009450 const TypeReference& type_reference = entry.first;
9451 VIXLUInt32Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01009452 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009453 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Artem Serovc5fcb442016-12-02 19:19:58 +00009454 }
9455}
9456
Artem Serovd4cc5b22016-11-04 11:19:09 +00009457void CodeGeneratorARMVIXL::EmitMovwMovtPlaceholder(
9458 CodeGeneratorARMVIXL::PcRelativePatchInfo* labels,
9459 vixl32::Register out) {
Artem Serov0fb37192016-12-06 18:13:40 +00009460 ExactAssemblyScope aas(GetVIXLAssembler(),
9461 3 * vixl32::kMaxInstructionSizeInBytes,
9462 CodeBufferCheckScope::kMaximumSize);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009463 // TODO(VIXL): Think about using mov instead of movw.
9464 __ bind(&labels->movw_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009465 __ movw(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009466 __ bind(&labels->movt_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08009467 __ movt(out, /* operand= */ 0u);
Artem Serovd4cc5b22016-11-04 11:19:09 +00009468 __ bind(&labels->add_pc_label);
9469 __ add(out, out, pc);
9470}
9471
Scott Wakelingfe885462016-09-22 10:24:38 +01009472#undef __
9473#undef QUICK_ENTRY_POINT
9474#undef TODO_VIXL32
9475
Vladimir Markoca1e0382018-04-11 09:58:41 +00009476#define __ assembler.GetVIXLAssembler()->
9477
9478static void EmitGrayCheckAndFastPath(ArmVIXLAssembler& assembler,
9479 vixl32::Register base_reg,
9480 vixl32::MemOperand& lock_word,
9481 vixl32::Label* slow_path,
Vladimir Marko7a695052018-04-12 10:26:50 +01009482 int32_t raw_ldr_offset,
9483 vixl32::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009484 // Load the lock word containing the rb_state.
9485 __ Ldr(ip, lock_word);
9486 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01009487 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00009488 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
9489 __ Tst(ip, Operand(LockWord::kReadBarrierStateMaskShifted));
Andreas Gampe3db70682018-12-26 15:12:03 -08009490 __ B(ne, slow_path, /* is_far_target= */ false);
Vladimir Marko7a695052018-04-12 10:26:50 +01009491 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
9492 if (throw_npe != nullptr) {
9493 __ Bind(throw_npe);
9494 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00009495 __ Add(lr, lr, raw_ldr_offset);
9496 // Introduce a dependency on the lock_word including rb_state,
9497 // to prevent load-load reordering, and without using
9498 // a memory barrier (which would be more expensive).
9499 __ Add(base_reg, base_reg, Operand(ip, LSR, 32));
9500 __ Bx(lr); // And return back to the function.
9501 // Note: The fake dependency is unnecessary for the slow path.
9502}
9503
9504// Load the read barrier introspection entrypoint in register `entrypoint`
Vladimir Markodcd117e2018-04-19 11:54:00 +01009505static vixl32::Register LoadReadBarrierMarkIntrospectionEntrypoint(ArmVIXLAssembler& assembler) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009506 // The register where the read barrier introspection entrypoint is loaded
Vladimir Markodcd117e2018-04-19 11:54:00 +01009507 // is the marking register. We clobber it here and the entrypoint restores it to 1.
9508 vixl32::Register entrypoint = mr;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009509 // entrypoint = Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
9510 DCHECK_EQ(ip.GetCode(), 12u);
9511 const int32_t entry_point_offset =
9512 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ip.GetCode());
9513 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
Vladimir Markodcd117e2018-04-19 11:54:00 +01009514 return entrypoint;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009515}
9516
9517void CodeGeneratorARMVIXL::CompileBakerReadBarrierThunk(ArmVIXLAssembler& assembler,
9518 uint32_t encoded_data,
9519 /*out*/ std::string* debug_name) {
9520 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
9521 switch (kind) {
9522 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009523 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9524 CheckValidReg(base_reg.GetCode());
9525 vixl32::Register holder_reg(BakerReadBarrierSecondRegField::Decode(encoded_data));
9526 CheckValidReg(holder_reg.GetCode());
9527 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9528 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9529 temps.Exclude(ip);
Roland Levillain988c3912019-09-25 19:33:35 +01009530 // In the case of a field load, if `base_reg` differs from
9531 // `holder_reg`, the offset was too large and we must have emitted (during the construction
9532 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
9533 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
9534 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
9535 // not necessarily do that check before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01009536 vixl32::Label throw_npe_label;
9537 vixl32::Label* throw_npe = nullptr;
9538 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
9539 throw_npe = &throw_npe_label;
Andreas Gampe3db70682018-12-26 15:12:03 -08009540 __ CompareAndBranchIfZero(holder_reg, throw_npe, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009541 }
Vladimir Marko7a695052018-04-12 10:26:50 +01009542 // Check if the holder is gray and, if not, add fake dependency to the base register
9543 // and return to the LDR instruction to load the reference. Otherwise, use introspection
9544 // to load the reference and call the entrypoint that performs further checks on the
9545 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009546 vixl32::Label slow_path;
9547 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
9548 const int32_t raw_ldr_offset = (width == BakerReadBarrierWidth::kWide)
9549 ? BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET
9550 : BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET;
Vladimir Marko7a695052018-04-12 10:26:50 +01009551 EmitGrayCheckAndFastPath(
9552 assembler, base_reg, lock_word, &slow_path, raw_ldr_offset, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009553 __ Bind(&slow_path);
9554 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9555 raw_ldr_offset;
Vladimir Markodcd117e2018-04-19 11:54:00 +01009556 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009557 if (width == BakerReadBarrierWidth::kWide) {
9558 MemOperand ldr_half_address(lr, ldr_offset + 2);
9559 __ Ldrh(ip, ldr_half_address); // Load the LDR immediate half-word with "Rt | imm12".
9560 __ Ubfx(ip, ip, 0, 12); // Extract the offset imm12.
9561 __ Ldr(ip, MemOperand(base_reg, ip)); // Load the reference.
9562 } else {
9563 MemOperand ldr_address(lr, ldr_offset);
9564 __ Ldrh(ip, ldr_address); // Load the LDR immediate, encoding T1.
9565 __ Add(ep_reg, // Adjust the entrypoint address to the entrypoint
9566 ep_reg, // for narrow LDR.
9567 Operand(BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_ENTRYPOINT_OFFSET));
9568 __ Ubfx(ip, ip, 6, 5); // Extract the imm5, i.e. offset / 4.
9569 __ Ldr(ip, MemOperand(base_reg, ip, LSL, 2)); // Load the reference.
9570 }
9571 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
9572 __ Bx(ep_reg); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00009573 break;
9574 }
9575 case BakerReadBarrierKind::kArray: {
9576 vixl32::Register base_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9577 CheckValidReg(base_reg.GetCode());
9578 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9579 BakerReadBarrierSecondRegField::Decode(encoded_data));
9580 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9581 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9582 temps.Exclude(ip);
9583 vixl32::Label slow_path;
9584 int32_t data_offset =
9585 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
9586 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
9587 DCHECK_LT(lock_word.GetOffsetImmediate(), 0);
9588 const int32_t raw_ldr_offset = BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET;
9589 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, raw_ldr_offset);
9590 __ Bind(&slow_path);
9591 const int32_t ldr_offset = /* Thumb state adjustment (LR contains Thumb state). */ -1 +
9592 raw_ldr_offset;
9593 MemOperand ldr_address(lr, ldr_offset + 2);
9594 __ Ldrb(ip, ldr_address); // Load the LDR (register) byte with "00 | imm2 | Rm",
9595 // i.e. Rm+32 because the scale in imm2 is 2.
Vladimir Markodcd117e2018-04-19 11:54:00 +01009596 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009597 __ Bfi(ep_reg, ip, 3, 6); // Insert ip to the entrypoint address to create
9598 // a switch case target based on the index register.
9599 __ Mov(ip, base_reg); // Move the base register to ip0.
9600 __ Bx(ep_reg); // Jump to the entrypoint's array switch case.
9601 break;
9602 }
Vladimir Markod887ed82018-08-14 13:52:12 +00009603 case BakerReadBarrierKind::kGcRoot:
9604 case BakerReadBarrierKind::kUnsafeCas: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009605 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
9606 // and it does not have a forwarding address), call the correct introspection entrypoint;
9607 // otherwise return the reference (or the extracted forwarding address).
9608 // There is no gray bit check for GC roots.
9609 vixl32::Register root_reg(BakerReadBarrierFirstRegField::Decode(encoded_data));
9610 CheckValidReg(root_reg.GetCode());
9611 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9612 BakerReadBarrierSecondRegField::Decode(encoded_data));
9613 BakerReadBarrierWidth width = BakerReadBarrierWidthField::Decode(encoded_data);
9614 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
9615 temps.Exclude(ip);
9616 vixl32::Label return_label, not_marked, forwarding_address;
Andreas Gampe3db70682018-12-26 15:12:03 -08009617 __ CompareAndBranchIfZero(root_reg, &return_label, /* is_far_target= */ false);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009618 MemOperand lock_word(root_reg, mirror::Object::MonitorOffset().Int32Value());
9619 __ Ldr(ip, lock_word);
9620 __ Tst(ip, LockWord::kMarkBitStateMaskShifted);
9621 __ B(eq, &not_marked);
9622 __ Bind(&return_label);
9623 __ Bx(lr);
9624 __ Bind(&not_marked);
9625 static_assert(LockWord::kStateShift == 30 && LockWord::kStateForwardingAddress == 3,
9626 "To use 'CMP ip, #modified-immediate; BHS', we need the lock word state in "
9627 " the highest bits and the 'forwarding address' state to have all bits set");
9628 __ Cmp(ip, Operand(0xc0000000));
9629 __ B(hs, &forwarding_address);
Vladimir Markodcd117e2018-04-19 11:54:00 +01009630 vixl32::Register ep_reg = LoadReadBarrierMarkIntrospectionEntrypoint(assembler);
Vladimir Markoca1e0382018-04-11 09:58:41 +00009631 // Adjust the art_quick_read_barrier_mark_introspection address in kBakerCcEntrypointRegister
Vladimir Markod887ed82018-08-14 13:52:12 +00009632 // to one of art_quick_read_barrier_mark_introspection_{gc_roots_{wide,narrow},unsafe_cas}.
9633 DCHECK(kind != BakerReadBarrierKind::kUnsafeCas || width == BakerReadBarrierWidth::kWide);
9634 int32_t entrypoint_offset =
9635 (kind == BakerReadBarrierKind::kGcRoot)
9636 ? (width == BakerReadBarrierWidth::kWide)
9637 ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_ENTRYPOINT_OFFSET
9638 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_ENTRYPOINT_OFFSET
9639 : BAKER_MARK_INTROSPECTION_UNSAFE_CAS_ENTRYPOINT_OFFSET;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009640 __ Add(ep_reg, ep_reg, Operand(entrypoint_offset));
9641 __ Mov(ip, root_reg);
9642 __ Bx(ep_reg);
9643 __ Bind(&forwarding_address);
9644 __ Lsl(root_reg, ip, LockWord::kForwardingAddressShift);
9645 __ Bx(lr);
9646 break;
9647 }
9648 default:
9649 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
9650 UNREACHABLE();
9651 }
9652
Vladimir Marko966b46f2018-08-03 10:20:19 +00009653 // For JIT, the slow path is considered part of the compiled method,
9654 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
9655 DCHECK(Runtime::Current() == nullptr ||
9656 !Runtime::Current()->UseJitCompilation() ||
9657 debug_name == nullptr);
9658 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00009659 std::ostringstream oss;
9660 oss << "BakerReadBarrierThunk";
9661 switch (kind) {
9662 case BakerReadBarrierKind::kField:
9663 oss << "Field";
9664 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9665 oss << "Wide";
9666 }
9667 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
9668 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
9669 break;
9670 case BakerReadBarrierKind::kArray:
9671 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9672 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9673 BakerReadBarrierSecondRegField::Decode(encoded_data));
9674 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9675 break;
9676 case BakerReadBarrierKind::kGcRoot:
9677 oss << "GcRoot";
9678 if (BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide) {
9679 oss << "Wide";
9680 }
9681 oss << "_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9682 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9683 BakerReadBarrierSecondRegField::Decode(encoded_data));
9684 break;
Vladimir Markod887ed82018-08-14 13:52:12 +00009685 case BakerReadBarrierKind::kUnsafeCas:
9686 oss << "UnsafeCas_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
9687 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
9688 BakerReadBarrierSecondRegField::Decode(encoded_data));
9689 DCHECK(BakerReadBarrierWidthField::Decode(encoded_data) == BakerReadBarrierWidth::kWide);
9690 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00009691 }
9692 *debug_name = oss.str();
9693 }
9694}
9695
9696#undef __
9697
Scott Wakelingfe885462016-09-22 10:24:38 +01009698} // namespace arm
9699} // namespace art