blob: 260920cb0cd751f50d720ef6c022f40056ee69ad [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 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_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.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"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#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"
Andreas Gampe878d58c2015-01-15 23:24:00 -080032#include "intrinsics.h"
33#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "thread.h"
40#include "utils/arm64/assembler_arm64.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000045using vixl::ExactAssemblyScope;
46using vixl::CodeBufferCheckScope;
47using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49#ifdef __
50#error "ARM64 Codegen VIXL macro-assembler macro already defined."
51#endif
52
Alexandre Rames5319def2014-10-23 10:03:10 +010053namespace art {
54
Roland Levillain22ccc3a2015-11-24 13:10:05 +000055template<class MirrorType>
56class GcRoot;
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058namespace arm64 {
59
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::ARM64EncodableConstantOrRegister;
61using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::CPURegisterFrom;
63using helpers::DRegisterFrom;
64using helpers::FPRegisterFrom;
65using helpers::HeapOperand;
66using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010071using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010072using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073using helpers::LocationFrom;
74using helpers::OperandFromMemOperand;
75using helpers::OutputCPURegister;
76using helpers::OutputFPRegister;
77using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010078using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080079using helpers::RegisterFrom;
80using helpers::StackOperandFrom;
81using helpers::VIXLRegCodeFromART;
82using helpers::WRegisterFrom;
83using helpers::XRegisterFrom;
84
Vladimir Markof3e0ee22015-12-17 15:23:13 +000085// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080086// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
87// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000088static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010089
Vladimir Markof4f2daa2017-03-20 18:26:59 +000090// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
91// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
92// For the Baker read barrier implementation using link-generated thunks we need to split
93// the offset explicitly.
94constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
95
96// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010097constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010098constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010099constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000100
101// Some instructions have special requirements for a temporary, for example
102// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
103// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
104// loads with large offsets need a fixed register to limit the number of link-time
105// thunks we generate. For these and similar cases, we want to reserve a specific
106// register that's neither callee-save nor an argument register. We choose x15.
107inline Location FixedTempLocation() {
108 return Location::RegisterLocation(x15.GetCode());
109}
110
Alexandre Rames5319def2014-10-23 10:03:10 +0100111inline Condition ARM64Condition(IfCondition cond) {
112 switch (cond) {
113 case kCondEQ: return eq;
114 case kCondNE: return ne;
115 case kCondLT: return lt;
116 case kCondLE: return le;
117 case kCondGT: return gt;
118 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700119 case kCondB: return lo;
120 case kCondBE: return ls;
121 case kCondA: return hi;
122 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100123 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000124 LOG(FATAL) << "Unreachable";
125 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100126}
127
Vladimir Markod6e069b2016-01-18 11:11:01 +0000128inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
129 // The ARM64 condition codes can express all the necessary branches, see the
130 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
131 // There is no dex instruction or HIR that would need the missing conditions
132 // "equal or unordered" or "not equal".
133 switch (cond) {
134 case kCondEQ: return eq;
135 case kCondNE: return ne /* unordered */;
136 case kCondLT: return gt_bias ? cc : lt /* unordered */;
137 case kCondLE: return gt_bias ? ls : le /* unordered */;
138 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
139 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
140 default:
141 LOG(FATAL) << "UNREACHABLE";
142 UNREACHABLE();
143 }
144}
145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100146Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000147 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
148 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
149 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000151 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100152 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000153 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000155 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100156 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100157 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000158 } else {
159 return LocationFrom(w0);
160 }
161}
162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100163Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000164 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100165}
166
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100167static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
168 InvokeRuntimeCallingConvention calling_convention;
169 RegisterSet caller_saves = RegisterSet::Empty();
170 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
171 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
172 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
173 DataType::Type::kReference).GetCode());
174 return caller_saves;
175}
176
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100177// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
178#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700179#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100180
Zheng Xuda403092015-04-24 17:35:39 +0800181// Calculate memory accessing operand for save/restore live registers.
182static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100183 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800184 int64_t spill_offset,
185 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100186 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
187 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
188 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800189 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100190 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800191 codegen->GetNumberOfFloatingPointRegisters()));
192
Vladimir Marko804b03f2016-09-14 16:26:36 +0100193 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100194 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
195 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800196
197 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
198 UseScratchRegisterScope temps(masm);
199
200 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100201 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
202 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800203 int64_t reg_size = kXRegSizeInBytes;
204 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
205 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100206 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800207 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
208 // If the offset does not fit in the instruction's immediate field, use an alternate register
209 // to compute the base address(float point registers spill base address).
210 Register new_base = temps.AcquireSameSizeAs(base);
211 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
212 base = new_base;
213 spill_offset = -core_spill_size;
214 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
215 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
216 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
217 }
218
219 if (is_save) {
220 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
221 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
222 } else {
223 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
224 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
225 }
226}
227
228void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800229 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100230 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
231 for (uint32_t i : LowToHighBits(core_spills)) {
232 // If the register holds an object, update the stack mask.
233 if (locations->RegisterContainsObject(i)) {
234 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800235 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
237 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
238 saved_core_stack_offsets_[i] = stack_offset;
239 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800240 }
241
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
243 for (uint32_t i : LowToHighBits(fp_spills)) {
244 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
245 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
246 saved_fpu_stack_offsets_[i] = stack_offset;
247 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800248 }
249
Vladimir Marko804b03f2016-09-14 16:26:36 +0100250 SaveRestoreLiveRegistersHelper(codegen,
251 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800252 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
253}
254
255void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100256 SaveRestoreLiveRegistersHelper(codegen,
257 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800258 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
259}
260
Alexandre Rames5319def2014-10-23 10:03:10 +0100261class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
262 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000263 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100264
Alexandre Rames67555f72014-11-18 10:55:16 +0000265 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100266 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000267 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100268
Alexandre Rames5319def2014-10-23 10:03:10 +0100269 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000270 if (instruction_->CanThrowIntoCatchBlock()) {
271 // Live registers will be restored in the catch block if caught.
272 SaveLiveRegisters(codegen, instruction_->GetLocations());
273 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000274 // We're moving two locations to locations that could overlap, so we need a parallel
275 // move resolver.
276 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100277 codegen->EmitParallelMoves(locations->InAt(0),
278 LocationFrom(calling_convention.GetRegisterAt(0)),
279 DataType::Type::kInt32,
280 locations->InAt(1),
281 LocationFrom(calling_convention.GetRegisterAt(1)),
282 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000283 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
284 ? kQuickThrowStringBounds
285 : kQuickThrowArrayBounds;
286 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100287 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800288 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100289 }
290
Alexandre Rames8158f282015-08-07 10:26:17 +0100291 bool IsFatal() const OVERRIDE { return true; }
292
Alexandre Rames9931f312015-06-19 14:47:01 +0100293 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
294
Alexandre Rames5319def2014-10-23 10:03:10 +0100295 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100296 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
297};
298
Alexandre Rames67555f72014-11-18 10:55:16 +0000299class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
300 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000301 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
304 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
305 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000306 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800307 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000308 }
309
Alexandre Rames8158f282015-08-07 10:26:17 +0100310 bool IsFatal() const OVERRIDE { return true; }
311
Alexandre Rames9931f312015-06-19 14:47:01 +0100312 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
313
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
316};
317
318class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
319 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100320 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
321 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100323 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 }
325
326 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000327 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000328 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100329 const uint32_t dex_pc = instruction_->GetDexPc();
330 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
331 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000332
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100333 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000335 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000336
Vladimir Markof3c52b42017-11-17 17:32:12 +0000337 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100338 if (must_resolve_type) {
339 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
340 dex::TypeIndex type_index = cls_->GetTypeIndex();
341 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100342 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
343 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100344 // If we also must_do_clinit, the resolved type is now in the correct register.
345 } else {
346 DCHECK(must_do_clinit);
347 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
348 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
349 source,
350 cls_->GetType());
351 }
352 if (must_do_clinit) {
353 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
354 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800355 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000356
357 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 if (out.IsValid()) {
359 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100360 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000361 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000362 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000363 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 __ B(GetExitLabel());
365 }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
368
Alexandre Rames67555f72014-11-18 10:55:16 +0000369 private:
370 // The class this slow path will load.
371 HLoadClass* const cls_;
372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
374};
375
Vladimir Markoaad75c62016-10-03 08:46:48 +0000376class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
377 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000378 explicit LoadStringSlowPathARM64(HLoadString* instruction)
379 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380
381 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
382 LocationSummary* locations = instruction_->GetLocations();
383 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
384 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
385
386 __ Bind(GetEntryLabel());
387 SaveLiveRegisters(codegen, locations);
388
Vladimir Markof3c52b42017-11-17 17:32:12 +0000389 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000390 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
391 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000392 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
393 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100394 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000395 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
396
397 RestoreLiveRegisters(codegen, locations);
398
Vladimir Markoaad75c62016-10-03 08:46:48 +0000399 __ B(GetExitLabel());
400 }
401
402 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
403
404 private:
405 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
406};
407
Alexandre Rames5319def2014-10-23 10:03:10 +0100408class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
409 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000410 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100411
Alexandre Rames67555f72014-11-18 10:55:16 +0000412 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
413 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000415 if (instruction_->CanThrowIntoCatchBlock()) {
416 // Live registers will be restored in the catch block if caught.
417 SaveLiveRegisters(codegen, instruction_->GetLocations());
418 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000419 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
420 instruction_,
421 instruction_->GetDexPc(),
422 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800423 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 }
425
Alexandre Rames8158f282015-08-07 10:26:17 +0100426 bool IsFatal() const OVERRIDE { return true; }
427
Alexandre Rames9931f312015-06-19 14:47:01 +0100428 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
429
Alexandre Rames5319def2014-10-23 10:03:10 +0100430 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100431 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
432};
433
434class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
435 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100436 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000437 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100438
Alexandre Rames67555f72014-11-18 10:55:16 +0000439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100440 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100442 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100443 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000444 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800445 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100446 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 if (successor_ == nullptr) {
448 __ B(GetReturnLabel());
449 } else {
450 __ B(arm64_codegen->GetLabelOf(successor_));
451 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100452 }
453
Scott Wakeling97c72b72016-06-24 16:19:36 +0100454 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100455 DCHECK(successor_ == nullptr);
456 return &return_label_;
457 }
458
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100459 HBasicBlock* GetSuccessor() const {
460 return successor_;
461 }
462
Alexandre Rames9931f312015-06-19 14:47:01 +0100463 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
464
Alexandre Rames5319def2014-10-23 10:03:10 +0100465 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100466 // If not null, the block to branch to after the suspend check.
467 HBasicBlock* const successor_;
468
469 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100470 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100471
472 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
473};
474
Alexandre Rames67555f72014-11-18 10:55:16 +0000475class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
476 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000478 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000479
480 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800482
Alexandre Rames3e69f162014-12-10 10:36:50 +0000483 DCHECK(instruction_->IsCheckCast()
484 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
485 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100486 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487
Alexandre Rames67555f72014-11-18 10:55:16 +0000488 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000489
Vladimir Marko87584542017-12-12 17:47:52 +0000490 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000491 SaveLiveRegisters(codegen, locations);
492 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493
494 // We're moving two locations to locations that could overlap, so we need a parallel
495 // move resolver.
496 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800497 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800498 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100499 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800500 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800501 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100502 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000503 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000504 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800505 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100506 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
508 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
509 } else {
510 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800511 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
512 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000513 }
514
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000515 if (!is_fatal_) {
516 RestoreLiveRegisters(codegen, locations);
517 __ B(GetExitLabel());
518 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000519 }
520
Alexandre Rames9931f312015-06-19 14:47:01 +0100521 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100522 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100523
Alexandre Rames67555f72014-11-18 10:55:16 +0000524 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000525 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
528};
529
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700530class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
531 public:
Aart Bik42249c32016-01-07 15:33:50 -0800532 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000533 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700534
535 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800536 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700537 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100538 LocationSummary* locations = instruction_->GetLocations();
539 SaveLiveRegisters(codegen, locations);
540 InvokeRuntimeCallingConvention calling_convention;
541 __ Mov(calling_convention.GetRegisterAt(0),
542 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000543 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100544 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700545 }
546
Alexandre Rames9931f312015-06-19 14:47:01 +0100547 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
548
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700549 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700550 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
551};
552
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100553class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
554 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000555 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100556
557 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
558 LocationSummary* locations = instruction_->GetLocations();
559 __ Bind(GetEntryLabel());
560 SaveLiveRegisters(codegen, locations);
561
562 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100563 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564 parallel_move.AddMove(
565 locations->InAt(0),
566 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100567 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100568 nullptr);
569 parallel_move.AddMove(
570 locations->InAt(1),
571 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100572 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100573 nullptr);
574 parallel_move.AddMove(
575 locations->InAt(2),
576 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100577 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100578 nullptr);
579 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
580
581 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000582 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100583 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
584 RestoreLiveRegisters(codegen, locations);
585 __ B(GetExitLabel());
586 }
587
588 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
589
590 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100591 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
592};
593
Zheng Xu3927c8b2015-11-18 17:46:25 +0800594void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
595 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000596 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597
598 // We are about to use the assembler to place literals directly. Make sure we have enough
599 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000600 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
601 num_entries * sizeof(int32_t),
602 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800603
604 __ Bind(&table_start_);
605 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
606 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100607 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800608 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100609 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800610 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
611 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
612 Literal<int32_t> literal(jump_offset);
613 __ place(&literal);
614 }
615}
616
Roland Levillain54f869e2017-03-06 13:54:11 +0000617// Abstract base class for read barrier slow paths marking a reference
618// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000619//
Roland Levillain54f869e2017-03-06 13:54:11 +0000620// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100621// barrier marking runtime entry point to be invoked or an empty
622// location; in the latter case, the read barrier marking runtime
623// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000624class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
625 protected:
626 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
627 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000628 DCHECK(kEmitCompilerReadBarrier);
629 }
630
Roland Levillain54f869e2017-03-06 13:54:11 +0000631 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000632
Roland Levillain54f869e2017-03-06 13:54:11 +0000633 // Generate assembly code calling the read barrier marking runtime
634 // entry point (ReadBarrierMarkRegX).
635 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000636 // No need to save live registers; it's taken care of by the
637 // entrypoint. Also, there is no need to update the stack mask,
638 // as this runtime call will not trigger a garbage collection.
639 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
640 DCHECK_NE(ref_.reg(), LR);
641 DCHECK_NE(ref_.reg(), WSP);
642 DCHECK_NE(ref_.reg(), WZR);
643 // IP0 is used internally by the ReadBarrierMarkRegX entry point
644 // as a temporary, it cannot be the entry point's input/output.
645 DCHECK_NE(ref_.reg(), IP0);
646 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
647 // "Compact" slow path, saving two moves.
648 //
649 // Instead of using the standard runtime calling convention (input
650 // and output in W0):
651 //
652 // W0 <- ref
653 // W0 <- ReadBarrierMark(W0)
654 // ref <- W0
655 //
656 // we just use rX (the register containing `ref`) as input and output
657 // of a dedicated entrypoint:
658 //
659 // rX <- ReadBarrierMarkRegX(rX)
660 //
661 if (entrypoint_.IsValid()) {
662 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
663 __ Blr(XRegisterFrom(entrypoint_));
664 } else {
665 // Entrypoint is not already loaded, load from the thread.
666 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100667 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000668 // This runtime call does not require a stack map.
669 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
670 }
671 }
672
673 // The location (register) of the marked object reference.
674 const Location ref_;
675
676 // The location of the entrypoint if it is already loaded.
677 const Location entrypoint_;
678
Roland Levillain54f869e2017-03-06 13:54:11 +0000679 private:
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
681};
682
Alexandre Rames5319def2014-10-23 10:03:10 +0100683// Slow path marking an object reference `ref` during a read
684// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000685// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100686//
687// This means that after the execution of this slow path, `ref` will
688// always be up-to-date, but `obj.field` may not; i.e., after the
689// flip, `ref` will be a to-space reference, but `obj.field` will
690// probably still be a from-space reference (unless it gets updated by
691// another thread, or if another thread installed another object
692// reference (different from `ref`) in `obj.field`).
693//
Roland Levillain97c46462017-05-11 14:04:03 +0100694// Argument `entrypoint` must be a register location holding the read
695// barrier marking runtime entry point to be invoked or an empty
696// location; in the latter case, the read barrier marking runtime
697// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000698class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100699 public:
700 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
701 Location ref,
702 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000703 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100704 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100705 }
706
707 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
708
709 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000710 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100711 DCHECK(locations->CanCall());
712 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000713 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000714 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
715 << "Unexpected instruction in read barrier marking slow path: "
716 << instruction_->DebugName();
717
718 __ Bind(GetEntryLabel());
719 GenerateReadBarrierMarkRuntimeCall(codegen);
720 __ B(GetExitLabel());
721 }
722
723 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000724 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
725};
726
Roland Levillain54f869e2017-03-06 13:54:11 +0000727// Slow path loading `obj`'s lock word, loading a reference from
728// object `*(obj + offset + (index << scale_factor))` into `ref`, and
729// marking `ref` if `obj` is gray according to the lock word (Baker
730// read barrier). The field `obj.field` in the object `obj` holding
731// this reference does not get updated by this slow path after marking
732// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
733// below for that).
734//
735// This means that after the execution of this slow path, `ref` will
736// always be up-to-date, but `obj.field` may not; i.e., after the
737// flip, `ref` will be a to-space reference, but `obj.field` will
738// probably still be a from-space reference (unless it gets updated by
739// another thread, or if another thread installed another object
740// reference (different from `ref`) in `obj.field`).
741//
742// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100743// barrier marking runtime entry point to be invoked or an empty
744// location; in the latter case, the read barrier marking runtime
745// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000746class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
747 public:
748 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
749 Location ref,
750 Register obj,
751 uint32_t offset,
752 Location index,
753 size_t scale_factor,
754 bool needs_null_check,
755 bool use_load_acquire,
756 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100757 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000758 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
759 obj_(obj),
760 offset_(offset),
761 index_(index),
762 scale_factor_(scale_factor),
763 needs_null_check_(needs_null_check),
764 use_load_acquire_(use_load_acquire),
765 temp_(temp) {
766 DCHECK(kEmitCompilerReadBarrier);
767 DCHECK(kUseBakerReadBarrier);
768 }
769
770 const char* GetDescription() const OVERRIDE {
771 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
772 }
773
774 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
775 LocationSummary* locations = instruction_->GetLocations();
776 DCHECK(locations->CanCall());
777 DCHECK(ref_.IsRegister()) << ref_;
778 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
779 DCHECK(obj_.IsW());
780 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100781 DCHECK(instruction_->IsInstanceFieldGet() ||
782 instruction_->IsStaticFieldGet() ||
783 instruction_->IsArrayGet() ||
784 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100785 instruction_->IsInstanceOf() ||
786 instruction_->IsCheckCast() ||
787 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
788 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
789 << "Unexpected instruction in read barrier marking slow path: "
790 << instruction_->DebugName();
791 // The read barrier instrumentation of object ArrayGet
792 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000793 // instruction.
794 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000795 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
796
Roland Levillain54f869e2017-03-06 13:54:11 +0000797 // Temporary register `temp_`, used to store the lock word, must
798 // not be IP0 nor IP1, as we may use them to emit the reference
799 // load (in the call to GenerateRawReferenceLoad below), and we
800 // need the lock word to still be in `temp_` after the reference
801 // load.
802 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
803 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
804
Alexandre Rames5319def2014-10-23 10:03:10 +0100805 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000806
807 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
808 // inserted after the original load. However, in fast path based
809 // Baker's read barriers, we need to perform the load of
810 // mirror::Object::monitor_ *before* the original reference load.
811 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000812 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100813 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000814 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
815 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
816 // HeapReference<mirror::Object> ref = *src; // Original reference load.
817 // bool is_gray = (rb_state == ReadBarrier::GrayState());
818 // if (is_gray) {
819 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
820 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000821 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000822 // Note: the original implementation in ReadBarrier::Barrier is
823 // slightly more complex as it performs additional checks that we do
824 // not do here for performance reasons.
825
826 // /* int32_t */ monitor = obj->monitor_
827 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
828 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
829 if (needs_null_check_) {
830 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100831 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000832 // /* LockWord */ lock_word = LockWord(monitor)
833 static_assert(sizeof(LockWord) == sizeof(int32_t),
834 "art::LockWord and int32_t have different sizes.");
835
836 // Introduce a dependency on the lock_word including rb_state,
837 // to prevent load-load reordering, and without using
838 // a memory barrier (which would be more expensive).
839 // `obj` is unchanged by this operation, but its value now depends
840 // on `temp`.
841 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
842
843 // The actual reference load.
844 // A possible implicit null check has already been handled above.
845 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
846 arm64_codegen->GenerateRawReferenceLoad(instruction_,
847 ref_,
848 obj_,
849 offset_,
850 index_,
851 scale_factor_,
852 /* needs_null_check */ false,
853 use_load_acquire_);
854
855 // Mark the object `ref` when `obj` is gray.
856 //
857 // if (rb_state == ReadBarrier::GrayState())
858 // ref = ReadBarrier::Mark(ref);
859 //
860 // Given the numeric representation, it's enough to check the low bit of the rb_state.
861 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
862 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
863 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
864 GenerateReadBarrierMarkRuntimeCall(codegen);
865
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000866 __ B(GetExitLabel());
867 }
868
869 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000870 // The register containing the object holding the marked object reference field.
871 Register obj_;
872 // The offset, index and scale factor to access the reference in `obj_`.
873 uint32_t offset_;
874 Location index_;
875 size_t scale_factor_;
876 // Is a null check required?
877 bool needs_null_check_;
878 // Should this reference load use Load-Acquire semantics?
879 bool use_load_acquire_;
880 // A temporary register used to hold the lock word of `obj_`.
881 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000882
Roland Levillain54f869e2017-03-06 13:54:11 +0000883 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000884};
885
Roland Levillain54f869e2017-03-06 13:54:11 +0000886// Slow path loading `obj`'s lock word, loading a reference from
887// object `*(obj + offset + (index << scale_factor))` into `ref`, and
888// marking `ref` if `obj` is gray according to the lock word (Baker
889// read barrier). If needed, this slow path also atomically updates
890// the field `obj.field` in the object `obj` holding this reference
891// after marking (contrary to
892// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
893// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100894//
895// This means that after the execution of this slow path, both `ref`
896// and `obj.field` will be up-to-date; i.e., after the flip, both will
897// hold the same to-space reference (unless another thread installed
898// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000899//
Roland Levillain54f869e2017-03-06 13:54:11 +0000900// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100901// barrier marking runtime entry point to be invoked or an empty
902// location; in the latter case, the read barrier marking runtime
903// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000904class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
905 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100906 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100907 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
908 HInstruction* instruction,
909 Location ref,
910 Register obj,
911 uint32_t offset,
912 Location index,
913 size_t scale_factor,
914 bool needs_null_check,
915 bool use_load_acquire,
916 Register temp,
917 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000918 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100919 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000920 offset_(offset),
921 index_(index),
922 scale_factor_(scale_factor),
923 needs_null_check_(needs_null_check),
924 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000925 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100926 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000927 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100928 }
929
930 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000931 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100932 }
933
934 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
935 LocationSummary* locations = instruction_->GetLocations();
936 Register ref_reg = WRegisterFrom(ref_);
937 DCHECK(locations->CanCall());
938 DCHECK(ref_.IsRegister()) << ref_;
939 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000940 DCHECK(obj_.IsW());
941 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
942
943 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100944 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
945 << "Unexpected instruction in read barrier marking and field updating slow path: "
946 << instruction_->DebugName();
947 DCHECK(instruction_->GetLocations()->Intrinsified());
948 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000949 DCHECK_EQ(offset_, 0u);
950 DCHECK_EQ(scale_factor_, 0u);
951 DCHECK_EQ(use_load_acquire_, false);
952 // The location of the offset of the marked reference field within `obj_`.
953 Location field_offset = index_;
954 DCHECK(field_offset.IsRegister()) << field_offset;
955
956 // Temporary register `temp_`, used to store the lock word, must
957 // not be IP0 nor IP1, as we may use them to emit the reference
958 // load (in the call to GenerateRawReferenceLoad below), and we
959 // need the lock word to still be in `temp_` after the reference
960 // load.
961 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
962 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100963
964 __ Bind(GetEntryLabel());
965
Roland Levillainff487002017-03-07 16:50:01 +0000966 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
967 //
968 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
969 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
970 // HeapReference<mirror::Object> ref = *src; // Original reference load.
971 // bool is_gray = (rb_state == ReadBarrier::GrayState());
972 // if (is_gray) {
973 // old_ref = ref;
974 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
975 // compareAndSwapObject(obj, field_offset, old_ref, ref);
976 // }
977
Roland Levillain54f869e2017-03-06 13:54:11 +0000978 // /* int32_t */ monitor = obj->monitor_
979 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
980 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
981 if (needs_null_check_) {
982 codegen->MaybeRecordImplicitNullCheck(instruction_);
983 }
984 // /* LockWord */ lock_word = LockWord(monitor)
985 static_assert(sizeof(LockWord) == sizeof(int32_t),
986 "art::LockWord and int32_t have different sizes.");
987
988 // Introduce a dependency on the lock_word including rb_state,
989 // to prevent load-load reordering, and without using
990 // a memory barrier (which would be more expensive).
991 // `obj` is unchanged by this operation, but its value now depends
992 // on `temp`.
993 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
994
995 // The actual reference load.
996 // A possible implicit null check has already been handled above.
997 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
998 arm64_codegen->GenerateRawReferenceLoad(instruction_,
999 ref_,
1000 obj_,
1001 offset_,
1002 index_,
1003 scale_factor_,
1004 /* needs_null_check */ false,
1005 use_load_acquire_);
1006
1007 // Mark the object `ref` when `obj` is gray.
1008 //
1009 // if (rb_state == ReadBarrier::GrayState())
1010 // ref = ReadBarrier::Mark(ref);
1011 //
1012 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1013 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1014 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1015 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1016
1017 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001018 // Note that we cannot use IP to save the old reference, as IP is
1019 // used internally by the ReadBarrierMarkRegX entry point, and we
1020 // need the old reference after the call to that entry point.
1021 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1022 __ Mov(temp_.W(), ref_reg);
1023
Roland Levillain54f869e2017-03-06 13:54:11 +00001024 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001025
1026 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001027 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001028 //
1029 // Note that this field could also hold a different object, if
1030 // another thread had concurrently changed it. In that case, the
1031 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1032 // (CAS) operation below would abort the CAS, leaving the field
1033 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001034 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001035 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001036
1037 // Update the the holder's field atomically. This may fail if
1038 // mutator updates before us, but it's OK. This is achieved
1039 // using a strong compare-and-set (CAS) operation with relaxed
1040 // memory synchronization ordering, where the expected value is
1041 // the old reference and the desired value is the new reference.
1042
1043 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1044 UseScratchRegisterScope temps(masm);
1045
1046 // Convenience aliases.
1047 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001048 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001049 Register expected = temp_.W();
1050 Register value = ref_reg;
1051 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1052 Register tmp_value = temps.AcquireW(); // Value in memory.
1053
1054 __ Add(tmp_ptr, base.X(), Operand(offset));
1055
1056 if (kPoisonHeapReferences) {
1057 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1058 if (value.Is(expected)) {
1059 // Do not poison `value`, as it is the same register as
1060 // `expected`, which has just been poisoned.
1061 } else {
1062 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1063 }
1064 }
1065
1066 // do {
1067 // tmp_value = [tmp_ptr] - expected;
1068 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1069
Roland Levillain24a4d112016-10-26 13:10:46 +01001070 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001071 __ Bind(&loop_head);
1072 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1073 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001074 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001075 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1076 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001077 __ B(&exit_loop);
1078 __ Bind(&comparison_failed);
1079 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001080 __ Bind(&exit_loop);
1081
1082 if (kPoisonHeapReferences) {
1083 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1084 if (value.Is(expected)) {
1085 // Do not unpoison `value`, as it is the same register as
1086 // `expected`, which has just been unpoisoned.
1087 } else {
1088 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1089 }
1090 }
1091
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001092 __ B(GetExitLabel());
1093 }
1094
1095 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001096 // The register containing the object holding the marked object reference field.
1097 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001098 // The offset, index and scale factor to access the reference in `obj_`.
1099 uint32_t offset_;
1100 Location index_;
1101 size_t scale_factor_;
1102 // Is a null check required?
1103 bool needs_null_check_;
1104 // Should this reference load use Load-Acquire semantics?
1105 bool use_load_acquire_;
1106 // A temporary register used to hold the lock word of `obj_`; and
1107 // also to hold the original reference value, when the reference is
1108 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001109 const Register temp_;
1110
Roland Levillain54f869e2017-03-06 13:54:11 +00001111 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001112};
1113
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001114// Slow path generating a read barrier for a heap reference.
1115class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1116 public:
1117 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1118 Location out,
1119 Location ref,
1120 Location obj,
1121 uint32_t offset,
1122 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001123 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001124 out_(out),
1125 ref_(ref),
1126 obj_(obj),
1127 offset_(offset),
1128 index_(index) {
1129 DCHECK(kEmitCompilerReadBarrier);
1130 // If `obj` is equal to `out` or `ref`, it means the initial object
1131 // has been overwritten by (or after) the heap object reference load
1132 // to be instrumented, e.g.:
1133 //
1134 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001135 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001136 //
1137 // In that case, we have lost the information about the original
1138 // object, and the emitted read barrier cannot work properly.
1139 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1140 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1141 }
1142
1143 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1144 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1145 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001146 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001147 DCHECK(locations->CanCall());
1148 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001149 DCHECK(instruction_->IsInstanceFieldGet() ||
1150 instruction_->IsStaticFieldGet() ||
1151 instruction_->IsArrayGet() ||
1152 instruction_->IsInstanceOf() ||
1153 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001154 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001155 << "Unexpected instruction in read barrier for heap reference slow path: "
1156 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001157 // The read barrier instrumentation of object ArrayGet
1158 // instructions does not support the HIntermediateAddress
1159 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001160 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001161 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001162
1163 __ Bind(GetEntryLabel());
1164
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001165 SaveLiveRegisters(codegen, locations);
1166
1167 // We may have to change the index's value, but as `index_` is a
1168 // constant member (like other "inputs" of this slow path),
1169 // introduce a copy of it, `index`.
1170 Location index = index_;
1171 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001172 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001173 if (instruction_->IsArrayGet()) {
1174 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001175 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001176 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1177 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1178 // We are about to change the value of `index_reg` (see the
1179 // calls to vixl::MacroAssembler::Lsl and
1180 // vixl::MacroAssembler::Mov below), but it has
1181 // not been saved by the previous call to
1182 // art::SlowPathCode::SaveLiveRegisters, as it is a
1183 // callee-save register --
1184 // art::SlowPathCode::SaveLiveRegisters does not consider
1185 // callee-save registers, as it has been designed with the
1186 // assumption that callee-save registers are supposed to be
1187 // handled by the called function. So, as a callee-save
1188 // register, `index_reg` _would_ eventually be saved onto
1189 // the stack, but it would be too late: we would have
1190 // changed its value earlier. Therefore, we manually save
1191 // it here into another freely available register,
1192 // `free_reg`, chosen of course among the caller-save
1193 // registers (as a callee-save `free_reg` register would
1194 // exhibit the same problem).
1195 //
1196 // Note we could have requested a temporary register from
1197 // the register allocator instead; but we prefer not to, as
1198 // this is a slow path, and we know we can find a
1199 // caller-save register that is available.
1200 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1201 __ Mov(free_reg.W(), index_reg);
1202 index_reg = free_reg;
1203 index = LocationFrom(index_reg);
1204 } else {
1205 // The initial register stored in `index_` has already been
1206 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1207 // (as it is not a callee-save register), so we can freely
1208 // use it.
1209 }
1210 // Shifting the index value contained in `index_reg` by the scale
1211 // factor (2) cannot overflow in practice, as the runtime is
1212 // unable to allocate object arrays with a size larger than
1213 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001215 static_assert(
1216 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1217 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1218 __ Add(index_reg, index_reg, Operand(offset_));
1219 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001220 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1221 // intrinsics, `index_` is not shifted by a scale factor of 2
1222 // (as in the case of ArrayGet), as it is actually an offset
1223 // to an object field within an object.
1224 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001225 DCHECK(instruction_->GetLocations()->Intrinsified());
1226 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1227 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1228 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001229 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001230 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001231 }
1232 }
1233
1234 // We're moving two or three locations to locations that could
1235 // overlap, so we need a parallel move resolver.
1236 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001237 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001238 parallel_move.AddMove(ref_,
1239 LocationFrom(calling_convention.GetRegisterAt(0)),
1240 type,
1241 nullptr);
1242 parallel_move.AddMove(obj_,
1243 LocationFrom(calling_convention.GetRegisterAt(1)),
1244 type,
1245 nullptr);
1246 if (index.IsValid()) {
1247 parallel_move.AddMove(index,
1248 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001249 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001250 nullptr);
1251 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1252 } else {
1253 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1254 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1255 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001256 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001257 instruction_,
1258 instruction_->GetDexPc(),
1259 this);
1260 CheckEntrypointTypes<
1261 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1262 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1263
1264 RestoreLiveRegisters(codegen, locations);
1265
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001266 __ B(GetExitLabel());
1267 }
1268
1269 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1270
1271 private:
1272 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001273 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1274 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001275 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1276 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1277 return Register(VIXLRegCodeFromART(i), kXRegSize);
1278 }
1279 }
1280 // We shall never fail to find a free caller-save register, as
1281 // there are more than two core caller-save registers on ARM64
1282 // (meaning it is possible to find one which is different from
1283 // `ref` and `obj`).
1284 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1285 LOG(FATAL) << "Could not find a free register";
1286 UNREACHABLE();
1287 }
1288
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001289 const Location out_;
1290 const Location ref_;
1291 const Location obj_;
1292 const uint32_t offset_;
1293 // An additional location containing an index to an array.
1294 // Only used for HArrayGet and the UnsafeGetObject &
1295 // UnsafeGetObjectVolatile intrinsics.
1296 const Location index_;
1297
1298 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1299};
1300
1301// Slow path generating a read barrier for a GC root.
1302class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1303 public:
1304 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001305 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001306 DCHECK(kEmitCompilerReadBarrier);
1307 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001308
1309 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1310 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001311 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001312 DCHECK(locations->CanCall());
1313 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001314 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1315 << "Unexpected instruction in read barrier for GC root slow path: "
1316 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001317
1318 __ Bind(GetEntryLabel());
1319 SaveLiveRegisters(codegen, locations);
1320
1321 InvokeRuntimeCallingConvention calling_convention;
1322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1323 // The argument of the ReadBarrierForRootSlow is not a managed
1324 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1325 // thus we need a 64-bit move here, and we cannot use
1326 //
1327 // arm64_codegen->MoveLocation(
1328 // LocationFrom(calling_convention.GetRegisterAt(0)),
1329 // root_,
1330 // type);
1331 //
1332 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001333 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001334 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001335 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001336 instruction_,
1337 instruction_->GetDexPc(),
1338 this);
1339 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1340 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1341
1342 RestoreLiveRegisters(codegen, locations);
1343 __ B(GetExitLabel());
1344 }
1345
1346 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1347
1348 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001349 const Location out_;
1350 const Location root_;
1351
1352 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1353};
1354
Alexandre Rames5319def2014-10-23 10:03:10 +01001355#undef __
1356
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001357Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001358 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001359 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 LOG(FATAL) << "Unreachable type " << type;
1361 }
1362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001363 if (DataType::IsFloatingPointType(type) &&
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001365 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001366 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001367 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1368 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1369 } else {
1370 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001371 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1372 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001373 }
1374
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001375 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001376 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 return next_location;
1378}
1379
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001380Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001381 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001382}
1383
Serban Constantinescu579885a2015-02-22 20:51:33 +00001384CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001385 const CompilerOptions& compiler_options,
1386 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001387 : CodeGenerator(graph,
1388 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001389 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001390 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001391 callee_saved_core_registers.GetList(),
1392 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001393 compiler_options,
1394 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001395 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1396 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001397 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001398 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001399 move_resolver_(graph->GetAllocator(), this),
1400 assembler_(graph->GetAllocator()),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001401 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001402 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001403 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001404 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001405 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001406 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001407 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001408 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001409 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001410 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001411 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001412 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001413 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001414 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001415 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +00001416 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1417 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
1418 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001419 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001420 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001421}
Alexandre Rames5319def2014-10-23 10:03:10 +01001422
Alexandre Rames67555f72014-11-18 10:55:16 +00001423#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001424
Zheng Xu3927c8b2015-11-18 17:46:25 +08001425void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001426 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001427 jump_table->EmitTable(this);
1428 }
1429}
1430
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001431void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001432 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +00001433
1434 // Emit JIT baker read barrier slow paths.
1435 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
1436 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
1437 uint32_t encoded_data = entry.first;
1438 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
1439 __ Bind(slow_path_entry);
1440 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name */ nullptr);
1441 }
1442
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001443 // Ensure we emit the literal pool.
1444 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001445
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001446 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001447
1448 // Verify Baker read barrier linker patches.
1449 if (kIsDebugBuild) {
1450 ArrayRef<const uint8_t> code = allocator->GetMemory();
1451 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1452 DCHECK(info.label.IsBound());
1453 uint32_t literal_offset = info.label.GetLocation();
1454 DCHECK_ALIGNED(literal_offset, 4u);
1455
1456 auto GetInsn = [&code](uint32_t offset) {
1457 DCHECK_ALIGNED(offset, 4u);
1458 return
1459 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1460 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1461 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1462 (static_cast<uint32_t>(code[offset + 3]) << 24);
1463 };
1464
1465 const uint32_t encoded_data = info.custom_data;
1466 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1467 // Check that the next instruction matches the expected LDR.
1468 switch (kind) {
1469 case BakerReadBarrierKind::kField: {
1470 DCHECK_GE(code.size() - literal_offset, 8u);
1471 uint32_t next_insn = GetInsn(literal_offset + 4u);
1472 // LDR (immediate) with correct base_reg.
1473 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1474 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1475 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1476 break;
1477 }
1478 case BakerReadBarrierKind::kArray: {
1479 DCHECK_GE(code.size() - literal_offset, 8u);
1480 uint32_t next_insn = GetInsn(literal_offset + 4u);
1481 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1482 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1483 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1484 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1485 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1486 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1487 break;
1488 }
1489 case BakerReadBarrierKind::kGcRoot: {
1490 DCHECK_GE(literal_offset, 4u);
1491 uint32_t prev_insn = GetInsn(literal_offset - 4u);
1492 // LDR (immediate) with correct root_reg.
1493 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1494 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg);
1495 break;
1496 }
1497 default:
1498 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1499 UNREACHABLE();
1500 }
1501 }
1502 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001503}
1504
Zheng Xuad4450e2015-04-17 18:48:56 +08001505void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1506 // Note: There are 6 kinds of moves:
1507 // 1. constant -> GPR/FPR (non-cycle)
1508 // 2. constant -> stack (non-cycle)
1509 // 3. GPR/FPR -> GPR/FPR
1510 // 4. GPR/FPR -> stack
1511 // 5. stack -> GPR/FPR
1512 // 6. stack -> stack (non-cycle)
1513 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1514 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1515 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1516 // dependency.
1517 vixl_temps_.Open(GetVIXLAssembler());
1518}
1519
1520void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1521 vixl_temps_.Close();
1522}
1523
1524Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001525 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1526 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1527 || kind == Location::kSIMDStackSlot);
1528 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1529 ? Location::kFpuRegister
1530 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001531 Location scratch = GetScratchLocation(kind);
1532 if (!scratch.Equals(Location::NoLocation())) {
1533 return scratch;
1534 }
1535 // Allocate from VIXL temp registers.
1536 if (kind == Location::kRegister) {
1537 scratch = LocationFrom(vixl_temps_.AcquireX());
1538 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001539 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001540 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1541 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1542 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001543 }
1544 AddScratchLocation(scratch);
1545 return scratch;
1546}
1547
1548void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1549 if (loc.IsRegister()) {
1550 vixl_temps_.Release(XRegisterFrom(loc));
1551 } else {
1552 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001553 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001554 }
1555 RemoveScratchLocation(loc);
1556}
1557
Alexandre Rames3e69f162014-12-10 10:36:50 +00001558void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001559 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001561}
1562
Alexandre Rames5319def2014-10-23 10:03:10 +01001563void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001564 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001565 __ Bind(&frame_entry_label_);
1566
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001567 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1568 UseScratchRegisterScope temps(masm);
1569 Register temp = temps.AcquireX();
1570 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1571 __ Add(temp, temp, 1);
1572 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1573 }
1574
Vladimir Marko33bff252017-11-01 14:35:42 +00001575 bool do_overflow_check =
1576 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001577 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001578 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001579 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001580 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001581 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001582 {
1583 // Ensure that between load and RecordPcInfo there are no pools emitted.
1584 ExactAssemblyScope eas(GetVIXLAssembler(),
1585 kInstructionSize,
1586 CodeBufferCheckScope::kExactSize);
1587 __ ldr(wzr, MemOperand(temp, 0));
1588 RecordPcInfo(nullptr, 0);
1589 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001590 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001591
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001592 if (!HasEmptyFrame()) {
1593 int frame_size = GetFrameSize();
1594 // Stack layout:
1595 // sp[frame_size - 8] : lr.
1596 // ... : other preserved core registers.
1597 // ... : other preserved fp registers.
1598 // ... : reserved frame space.
1599 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001600
1601 // Save the current method if we need it. Note that we do not
1602 // do this in HCurrentMethod, as the instruction might have been removed
1603 // in the SSA graph.
1604 if (RequiresCurrentMethod()) {
1605 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001606 } else {
1607 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001608 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001609 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001610 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1611 frame_size - GetCoreSpillSize());
1612 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1613 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001614
1615 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1616 // Initialize should_deoptimize flag to 0.
1617 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1618 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1619 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001620 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001621
1622 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001623}
1624
1625void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001626 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001627 if (!HasEmptyFrame()) {
1628 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001629 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1630 frame_size - FrameEntrySpillSize());
1631 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1632 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001633 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001634 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001635 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001636 __ Ret();
1637 GetAssembler()->cfi().RestoreState();
1638 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001639}
1640
Scott Wakeling97c72b72016-06-24 16:19:36 +01001641CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001642 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001643 return CPURegList(CPURegister::kRegister, kXRegSize,
1644 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001645}
1646
Scott Wakeling97c72b72016-06-24 16:19:36 +01001647CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001648 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1649 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001650 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1651 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001652}
1653
Alexandre Rames5319def2014-10-23 10:03:10 +01001654void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1655 __ Bind(GetLabelOf(block));
1656}
1657
Calin Juravle175dc732015-08-25 15:42:32 +01001658void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1659 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001660 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001661}
1662
Calin Juravlee460d1d2015-09-29 04:52:17 +01001663void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1664 if (location.IsRegister()) {
1665 locations->AddTemp(location);
1666 } else {
1667 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1668 }
1669}
1670
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001671void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001672 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001673 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001674 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001675 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001676 if (value_can_be_null) {
1677 __ Cbz(value, &done);
1678 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001679 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001680 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001681 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001682 if (value_can_be_null) {
1683 __ Bind(&done);
1684 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001685}
1686
David Brazdil58282f42016-01-14 12:45:10 +00001687void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001688 // Blocked core registers:
1689 // lr : Runtime reserved.
1690 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001691 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001692 // ip1 : VIXL core temp.
1693 // ip0 : VIXL core temp.
1694 //
1695 // Blocked fp registers:
1696 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001697 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1698 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001699 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001700 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001701 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001702
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001703 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001704 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001705 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001706 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001707
David Brazdil58282f42016-01-14 12:45:10 +00001708 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001709 // Stubs do not save callee-save floating point registers. If the graph
1710 // is debuggable, we need to deal with these registers differently. For
1711 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001712 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1713 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001714 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001715 }
1716 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001717}
1718
Alexandre Rames3e69f162014-12-10 10:36:50 +00001719size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1720 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1721 __ Str(reg, MemOperand(sp, stack_index));
1722 return kArm64WordSize;
1723}
1724
1725size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1726 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1727 __ Ldr(reg, MemOperand(sp, stack_index));
1728 return kArm64WordSize;
1729}
1730
1731size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1732 FPRegister reg = FPRegister(reg_id, kDRegSize);
1733 __ Str(reg, MemOperand(sp, stack_index));
1734 return kArm64WordSize;
1735}
1736
1737size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1738 FPRegister reg = FPRegister(reg_id, kDRegSize);
1739 __ Ldr(reg, MemOperand(sp, stack_index));
1740 return kArm64WordSize;
1741}
1742
Alexandre Rames5319def2014-10-23 10:03:10 +01001743void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001744 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001745}
1746
1747void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001748 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001749}
1750
Vladimir Markoa0431112018-06-25 09:32:54 +01001751const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1752 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1753}
1754
Alexandre Rames67555f72014-11-18 10:55:16 +00001755void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001756 if (constant->IsIntConstant()) {
1757 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1758 } else if (constant->IsLongConstant()) {
1759 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1760 } else if (constant->IsNullConstant()) {
1761 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001762 } else if (constant->IsFloatConstant()) {
1763 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1764 } else {
1765 DCHECK(constant->IsDoubleConstant());
1766 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1767 }
1768}
1769
Alexandre Rames3e69f162014-12-10 10:36:50 +00001770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001772 DCHECK(constant.IsConstant());
1773 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001774 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001775 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001776 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1777 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1778 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1779 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001780}
1781
Roland Levillain952b2352017-05-03 19:49:14 +01001782// Allocate a scratch register from the VIXL pool, querying first
1783// the floating-point register pool, and then the core register
1784// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001785// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1786// using a different allocation strategy.
1787static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1788 vixl::aarch64::UseScratchRegisterScope* temps,
1789 int size_in_bits) {
1790 return masm->GetScratchFPRegisterList()->IsEmpty()
1791 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1792 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1793}
1794
Calin Juravlee460d1d2015-09-29 04:52:17 +01001795void CodeGeneratorARM64::MoveLocation(Location destination,
1796 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001797 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001798 if (source.Equals(destination)) {
1799 return;
1800 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801
1802 // A valid move can always be inferred from the destination and source
1803 // locations. When moving from and to a register, the argument type can be
1804 // used to generate 32bit instead of 64bit moves. In debug mode we also
1805 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001806 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001807
1808 if (destination.IsRegister() || destination.IsFpuRegister()) {
1809 if (unspecified_type) {
1810 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1811 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001812 (src_cst != nullptr && (src_cst->IsIntConstant()
1813 || src_cst->IsFloatConstant()
1814 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001815 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001817 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001818 // If the source is a double stack slot or a 64bit constant, a 64bit
1819 // type is appropriate. Else the source is a register, and since the
1820 // type has not been specified, we chose a 64bit type to force a 64bit
1821 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001823 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001824 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1826 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001827 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001828 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1829 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1830 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001831 } else if (source.IsSIMDStackSlot()) {
1832 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001833 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001834 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001835 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001836 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001837 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001838 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001839 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001840 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001841 DataType::Type source_type = DataType::Is64BitType(dst_type)
1842 ? DataType::Type::kInt64
1843 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001844 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1845 }
1846 } else {
1847 DCHECK(source.IsFpuRegister());
1848 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 DataType::Type source_type = DataType::Is64BitType(dst_type)
1850 ? DataType::Type::kFloat64
1851 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001852 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1853 } else {
1854 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001855 if (GetGraph()->HasSIMD()) {
1856 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1857 } else {
1858 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1859 }
1860 }
1861 }
1862 } else if (destination.IsSIMDStackSlot()) {
1863 if (source.IsFpuRegister()) {
1864 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1865 } else {
1866 DCHECK(source.IsSIMDStackSlot());
1867 UseScratchRegisterScope temps(GetVIXLAssembler());
1868 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1869 Register temp = temps.AcquireX();
1870 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1871 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1872 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1873 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1874 } else {
1875 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1876 __ Ldr(temp, StackOperandFrom(source));
1877 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001878 }
1879 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001880 } else { // The destination is not a register. It must be a stack slot.
1881 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1882 if (source.IsRegister() || source.IsFpuRegister()) {
1883 if (unspecified_type) {
1884 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001885 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001886 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001887 dst_type =
1888 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001889 }
1890 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001891 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1892 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001893 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001894 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001895 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1896 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001897 UseScratchRegisterScope temps(GetVIXLAssembler());
1898 HConstant* src_cst = source.GetConstant();
1899 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001900 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001901 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1902 ? Register(xzr)
1903 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001904 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001905 if (src_cst->IsIntConstant()) {
1906 temp = temps.AcquireW();
1907 } else if (src_cst->IsLongConstant()) {
1908 temp = temps.AcquireX();
1909 } else if (src_cst->IsFloatConstant()) {
1910 temp = temps.AcquireS();
1911 } else {
1912 DCHECK(src_cst->IsDoubleConstant());
1913 temp = temps.AcquireD();
1914 }
1915 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001916 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001917 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001918 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001919 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001920 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001921 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001922 // Use any scratch register (a core or a floating-point one)
1923 // from VIXL scratch register pools as a temporary.
1924 //
1925 // We used to only use the FP scratch register pool, but in some
1926 // rare cases the only register from this pool (D31) would
1927 // already be used (e.g. within a ParallelMove instruction, when
1928 // a move is blocked by a another move requiring a scratch FP
1929 // register, which would reserve D31). To prevent this issue, we
1930 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001931 //
1932 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001933 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001934 // use AcquireFPOrCoreCPURegisterOfSize instead of
1935 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1936 // allocates core scratch registers first.
1937 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1938 GetVIXLAssembler(),
1939 &temps,
1940 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001941 __ Ldr(temp, StackOperandFrom(source));
1942 __ Str(temp, StackOperandFrom(destination));
1943 }
1944 }
1945}
1946
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001948 CPURegister dst,
1949 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001950 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001952 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001953 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001954 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001955 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001956 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001957 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001958 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001959 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001960 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001961 case DataType::Type::kInt16:
1962 __ Ldrsh(Register(dst), src);
1963 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001964 case DataType::Type::kInt32:
1965 case DataType::Type::kReference:
1966 case DataType::Type::kInt64:
1967 case DataType::Type::kFloat32:
1968 case DataType::Type::kFloat64:
1969 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001970 __ Ldr(dst, src);
1971 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001972 case DataType::Type::kUint32:
1973 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001974 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001975 LOG(FATAL) << "Unreachable type " << type;
1976 }
1977}
1978
Calin Juravle77520bc2015-01-12 18:45:46 +00001979void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001980 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001981 const MemOperand& src,
1982 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001983 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001984 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001985 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001986 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001987
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001988 DCHECK(!src.IsPreIndex());
1989 DCHECK(!src.IsPostIndex());
1990
1991 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001992 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001993 {
1994 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1995 MemOperand base = MemOperand(temp_base);
1996 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001998 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001999 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002000 {
2001 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2002 __ ldarb(Register(dst), base);
2003 if (needs_null_check) {
2004 MaybeRecordImplicitNullCheck(instruction);
2005 }
2006 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002007 if (type == DataType::Type::kInt8) {
2008 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00002009 }
2010 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002011 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002012 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002013 {
2014 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2015 __ ldarh(Register(dst), base);
2016 if (needs_null_check) {
2017 MaybeRecordImplicitNullCheck(instruction);
2018 }
2019 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002020 if (type == DataType::Type::kInt16) {
2021 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
2022 }
Artem Serov914d7a82017-02-07 14:33:49 +00002023 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002024 case DataType::Type::kInt32:
2025 case DataType::Type::kReference:
2026 case DataType::Type::kInt64:
2027 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002028 {
2029 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2030 __ ldar(Register(dst), base);
2031 if (needs_null_check) {
2032 MaybeRecordImplicitNullCheck(instruction);
2033 }
2034 }
2035 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002036 case DataType::Type::kFloat32:
2037 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00002038 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002040
Artem Serov914d7a82017-02-07 14:33:49 +00002041 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2042 {
2043 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2044 __ ldar(temp, base);
2045 if (needs_null_check) {
2046 MaybeRecordImplicitNullCheck(instruction);
2047 }
2048 }
2049 __ Fmov(FPRegister(dst), temp);
2050 break;
Roland Levillain44015862016-01-22 11:47:17 +00002051 }
Aart Bik66c158e2018-01-31 12:55:04 -08002052 case DataType::Type::kUint32:
2053 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002054 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00002055 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002056 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002057 }
2058}
2059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002060void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002061 CPURegister src,
2062 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002063 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002064 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002065 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002067 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002068 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 case DataType::Type::kUint16:
2070 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002071 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002072 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 case DataType::Type::kInt32:
2074 case DataType::Type::kReference:
2075 case DataType::Type::kInt64:
2076 case DataType::Type::kFloat32:
2077 case DataType::Type::kFloat64:
2078 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002079 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002080 break;
Aart Bik66c158e2018-01-31 12:55:04 -08002081 case DataType::Type::kUint32:
2082 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002084 LOG(FATAL) << "Unreachable type " << type;
2085 }
2086}
2087
Artem Serov914d7a82017-02-07 14:33:49 +00002088void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002090 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002091 const MemOperand& dst,
2092 bool needs_null_check) {
2093 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002094 UseScratchRegisterScope temps(GetVIXLAssembler());
2095 Register temp_base = temps.AcquireX();
2096
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002097 DCHECK(!dst.IsPreIndex());
2098 DCHECK(!dst.IsPostIndex());
2099
2100 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002101 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002102 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002103 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002104 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002105 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002107 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002109 {
2110 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2111 __ stlrb(Register(src), base);
2112 if (needs_null_check) {
2113 MaybeRecordImplicitNullCheck(instruction);
2114 }
2115 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002116 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 case DataType::Type::kUint16:
2118 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002119 {
2120 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2121 __ stlrh(Register(src), base);
2122 if (needs_null_check) {
2123 MaybeRecordImplicitNullCheck(instruction);
2124 }
2125 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002126 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 case DataType::Type::kInt32:
2128 case DataType::Type::kReference:
2129 case DataType::Type::kInt64:
2130 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002131 {
2132 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2133 __ stlr(Register(src), base);
2134 if (needs_null_check) {
2135 MaybeRecordImplicitNullCheck(instruction);
2136 }
2137 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002138 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002139 case DataType::Type::kFloat32:
2140 case DataType::Type::kFloat64: {
2141 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002142 Register temp_src;
2143 if (src.IsZero()) {
2144 // The zero register is used to avoid synthesizing zero constants.
2145 temp_src = Register(src);
2146 } else {
2147 DCHECK(src.IsFPRegister());
2148 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2149 __ Fmov(temp_src, FPRegister(src));
2150 }
Artem Serov914d7a82017-02-07 14:33:49 +00002151 {
2152 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2153 __ stlr(temp_src, base);
2154 if (needs_null_check) {
2155 MaybeRecordImplicitNullCheck(instruction);
2156 }
2157 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002158 break;
2159 }
Aart Bik66c158e2018-01-31 12:55:04 -08002160 case DataType::Type::kUint32:
2161 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002162 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002163 LOG(FATAL) << "Unreachable type " << type;
2164 }
2165}
2166
Calin Juravle175dc732015-08-25 15:42:32 +01002167void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2168 HInstruction* instruction,
2169 uint32_t dex_pc,
2170 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002171 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002172
2173 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2174 {
2175 // Ensure the pc position is recorded immediately after the `blr` instruction.
2176 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2177 __ blr(lr);
2178 if (EntrypointRequiresStackMap(entrypoint)) {
2179 RecordPcInfo(instruction, dex_pc, slow_path);
2180 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002181 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002182}
2183
Roland Levillaindec8f632016-07-22 17:10:06 +01002184void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2185 HInstruction* instruction,
2186 SlowPathCode* slow_path) {
2187 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002188 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2189 __ Blr(lr);
2190}
2191
Alexandre Rames67555f72014-11-18 10:55:16 +00002192void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002193 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002194 UseScratchRegisterScope temps(GetVIXLAssembler());
2195 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00002196 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2197 const size_t status_byte_offset =
2198 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2199 constexpr uint32_t shifted_initialized_value =
2200 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002201
Serban Constantinescu02164b32014-11-13 14:05:07 +00002202 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002203 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00002204 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07002205 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00002206 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00002207 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002208 __ Bind(slow_path->GetExitLabel());
2209}
Alexandre Rames5319def2014-10-23 10:03:10 +01002210
Vladimir Marko175e7862018-03-27 09:03:13 +00002211void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
2212 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
2213 uint32_t path_to_root = check->GetBitstringPathToRoot();
2214 uint32_t mask = check->GetBitstringMask();
2215 DCHECK(IsPowerOfTwo(mask + 1));
2216 size_t mask_bits = WhichPowerOf2(mask + 1);
2217
2218 if (mask_bits == 16u) {
2219 // Load only the bitstring part of the status word.
2220 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2221 } else {
2222 // /* uint32_t */ temp = temp->status_
2223 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2224 // Extract the bitstring bits.
2225 __ Ubfx(temp, temp, 0, mask_bits);
2226 }
2227 // Compare the bitstring bits to `path_to_root`.
2228 __ Cmp(temp, path_to_root);
2229}
2230
Roland Levillain44015862016-01-22 11:47:17 +00002231void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002232 BarrierType type = BarrierAll;
2233
2234 switch (kind) {
2235 case MemBarrierKind::kAnyAny:
2236 case MemBarrierKind::kAnyStore: {
2237 type = BarrierAll;
2238 break;
2239 }
2240 case MemBarrierKind::kLoadAny: {
2241 type = BarrierReads;
2242 break;
2243 }
2244 case MemBarrierKind::kStoreStore: {
2245 type = BarrierWrites;
2246 break;
2247 }
2248 default:
2249 LOG(FATAL) << "Unexpected memory barrier " << kind;
2250 }
2251 __ Dmb(InnerShareable, type);
2252}
2253
Serban Constantinescu02164b32014-11-13 14:05:07 +00002254void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2255 HBasicBlock* successor) {
2256 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002257 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2258 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002259 slow_path =
2260 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002261 instruction->SetSlowPath(slow_path);
2262 codegen_->AddSlowPath(slow_path);
2263 if (successor != nullptr) {
2264 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002265 }
2266 } else {
2267 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2268 }
2269
Serban Constantinescu02164b32014-11-13 14:05:07 +00002270 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2271 Register temp = temps.AcquireW();
2272
Andreas Gampe542451c2016-07-26 09:02:02 -07002273 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002274 if (successor == nullptr) {
2275 __ Cbnz(temp, slow_path->GetEntryLabel());
2276 __ Bind(slow_path->GetReturnLabel());
2277 } else {
2278 __ Cbz(temp, codegen_->GetLabelOf(successor));
2279 __ B(slow_path->GetEntryLabel());
2280 // slow_path will return to GetLabelOf(successor).
2281 }
2282}
2283
Alexandre Rames5319def2014-10-23 10:03:10 +01002284InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2285 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002286 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002287 assembler_(codegen->GetAssembler()),
2288 codegen_(codegen) {}
2289
Alexandre Rames67555f72014-11-18 10:55:16 +00002290void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002291 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002294 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 case DataType::Type::kInt32:
2296 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002297 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002298 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002300 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 case DataType::Type::kFloat32:
2303 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002304 locations->SetInAt(0, Location::RequiresFpuRegister());
2305 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002307 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002308
Alexandre Rames5319def2014-10-23 10:03:10 +01002309 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002310 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002311 }
2312}
2313
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002314void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2315 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002316 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2317
2318 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002320 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002321 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2322 object_field_get_with_read_barrier
2323 ? LocationSummary::kCallOnSlowPath
2324 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002325 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002326 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002327 // We need a temporary register for the read barrier marking slow
2328 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002329 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2330 !Runtime::Current()->UseJitCompilation() &&
2331 !field_info.IsVolatile()) {
2332 // If link-time thunks for the Baker read barrier are enabled, for AOT
2333 // non-volatile loads we need a temporary only if the offset is too big.
2334 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2335 locations->AddTemp(FixedTempLocation());
2336 }
2337 } else {
2338 locations->AddTemp(Location::RequiresRegister());
2339 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002340 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002341 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002343 locations->SetOut(Location::RequiresFpuRegister());
2344 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002345 // The output overlaps for an object field get when read barriers
2346 // are enabled: we do not want the load to overwrite the object's
2347 // location, as we need it to emit the read barrier.
2348 locations->SetOut(
2349 Location::RequiresRegister(),
2350 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002351 }
2352}
2353
2354void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2355 const FieldInfo& field_info) {
2356 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002357 LocationSummary* locations = instruction->GetLocations();
2358 Location base_loc = locations->InAt(0);
2359 Location out = locations->Out();
2360 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002361 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2362 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002363 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002365 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002366 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002367 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002368 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002369 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002370 Location maybe_temp =
2371 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002372 // Note that potential implicit null checks are handled in this
2373 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2374 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2375 instruction,
2376 out,
2377 base,
2378 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002379 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002380 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002381 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002382 } else {
2383 // General case.
2384 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002385 // Note that a potential implicit null check is handled in this
2386 // CodeGeneratorARM64::LoadAcquire call.
2387 // NB: LoadAcquire will record the pc info if needed.
2388 codegen_->LoadAcquire(
2389 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002390 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002391 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2392 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002393 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002394 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002395 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002396 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002397 // If read barriers are enabled, emit read barriers other than
2398 // Baker's using a slow path (and also unpoison the loaded
2399 // reference, if heap poisoning is enabled).
2400 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2401 }
Roland Levillain4d027112015-07-01 15:41:14 +01002402 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002403}
2404
2405void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2406 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002407 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002408 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002409 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2410 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002412 locations->SetInAt(1, Location::RequiresFpuRegister());
2413 } else {
2414 locations->SetInAt(1, Location::RequiresRegister());
2415 }
2416}
2417
2418void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002419 const FieldInfo& field_info,
2420 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002421 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2422
2423 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002424 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002425 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002426 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002428
Roland Levillain4d027112015-07-01 15:41:14 +01002429 {
2430 // We use a block to end the scratch scope before the write barrier, thus
2431 // freeing the temporary registers so they can be used in `MarkGCCard`.
2432 UseScratchRegisterScope temps(GetVIXLAssembler());
2433
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002434 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002435 DCHECK(value.IsW());
2436 Register temp = temps.AcquireW();
2437 __ Mov(temp, value.W());
2438 GetAssembler()->PoisonHeapReference(temp.W());
2439 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002440 }
Roland Levillain4d027112015-07-01 15:41:14 +01002441
2442 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002443 codegen_->StoreRelease(
2444 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002445 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002446 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2447 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002448 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2449 codegen_->MaybeRecordImplicitNullCheck(instruction);
2450 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002451 }
2452
2453 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002454 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002455 }
2456}
2457
Alexandre Rames67555f72014-11-18 10:55:16 +00002458void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002459 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002460
2461 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 case DataType::Type::kInt32:
2463 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002464 Register dst = OutputRegister(instr);
2465 Register lhs = InputRegisterAt(instr, 0);
2466 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002467 if (instr->IsAdd()) {
2468 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002469 } else if (instr->IsAnd()) {
2470 __ And(dst, lhs, rhs);
2471 } else if (instr->IsOr()) {
2472 __ Orr(dst, lhs, rhs);
2473 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002474 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002475 } else if (instr->IsRor()) {
2476 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002477 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002478 __ Ror(dst, lhs, shift);
2479 } else {
2480 // Ensure shift distance is in the same size register as the result. If
2481 // we are rotating a long and the shift comes in a w register originally,
2482 // we don't need to sxtw for use as an x since the shift distances are
2483 // all & reg_bits - 1.
2484 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2485 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002486 } else if (instr->IsMin() || instr->IsMax()) {
2487 __ Cmp(lhs, rhs);
2488 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002489 } else {
2490 DCHECK(instr->IsXor());
2491 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002492 }
2493 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002494 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 case DataType::Type::kFloat32:
2496 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002497 FPRegister dst = OutputFPRegister(instr);
2498 FPRegister lhs = InputFPRegisterAt(instr, 0);
2499 FPRegister rhs = InputFPRegisterAt(instr, 1);
2500 if (instr->IsAdd()) {
2501 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002502 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002503 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002504 } else if (instr->IsMin()) {
2505 __ Fmin(dst, lhs, rhs);
2506 } else if (instr->IsMax()) {
2507 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002508 } else {
2509 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002510 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002511 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002512 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002513 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002514 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002515 }
2516}
2517
Serban Constantinescu02164b32014-11-13 14:05:07 +00002518void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2519 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2520
Vladimir Markoca6fff82017-10-03 14:49:14 +01002521 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002523 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002524 case DataType::Type::kInt32:
2525 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002526 locations->SetInAt(0, Location::RequiresRegister());
2527 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002528 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002529 break;
2530 }
2531 default:
2532 LOG(FATAL) << "Unexpected shift type " << type;
2533 }
2534}
2535
2536void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2537 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002539 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002540 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 case DataType::Type::kInt32:
2542 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002543 Register dst = OutputRegister(instr);
2544 Register lhs = InputRegisterAt(instr, 0);
2545 Operand rhs = InputOperandAt(instr, 1);
2546 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002547 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002549 if (instr->IsShl()) {
2550 __ Lsl(dst, lhs, shift_value);
2551 } else if (instr->IsShr()) {
2552 __ Asr(dst, lhs, shift_value);
2553 } else {
2554 __ Lsr(dst, lhs, shift_value);
2555 }
2556 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002557 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002558
2559 if (instr->IsShl()) {
2560 __ Lsl(dst, lhs, rhs_reg);
2561 } else if (instr->IsShr()) {
2562 __ Asr(dst, lhs, rhs_reg);
2563 } else {
2564 __ Lsr(dst, lhs, rhs_reg);
2565 }
2566 }
2567 break;
2568 }
2569 default:
2570 LOG(FATAL) << "Unexpected shift operation type " << type;
2571 }
2572}
2573
Alexandre Rames5319def2014-10-23 10:03:10 +01002574void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002575 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002576}
2577
2578void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002579 HandleBinaryOp(instruction);
2580}
2581
2582void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2583 HandleBinaryOp(instruction);
2584}
2585
2586void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2587 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002588}
2589
Artem Serov7fc63502016-02-09 17:15:29 +00002590void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002592 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002593 locations->SetInAt(0, Location::RequiresRegister());
2594 // There is no immediate variant of negated bitwise instructions in AArch64.
2595 locations->SetInAt(1, Location::RequiresRegister());
2596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2597}
2598
Artem Serov7fc63502016-02-09 17:15:29 +00002599void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002600 Register dst = OutputRegister(instr);
2601 Register lhs = InputRegisterAt(instr, 0);
2602 Register rhs = InputRegisterAt(instr, 1);
2603
2604 switch (instr->GetOpKind()) {
2605 case HInstruction::kAnd:
2606 __ Bic(dst, lhs, rhs);
2607 break;
2608 case HInstruction::kOr:
2609 __ Orn(dst, lhs, rhs);
2610 break;
2611 case HInstruction::kXor:
2612 __ Eon(dst, lhs, rhs);
2613 break;
2614 default:
2615 LOG(FATAL) << "Unreachable";
2616 }
2617}
2618
Anton Kirilov74234da2017-01-13 14:42:47 +00002619void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2620 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2622 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002623 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002624 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002625 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2626 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2627 } else {
2628 locations->SetInAt(0, Location::RequiresRegister());
2629 }
2630 locations->SetInAt(1, Location::RequiresRegister());
2631 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2632}
2633
Anton Kirilov74234da2017-01-13 14:42:47 +00002634void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2635 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002637 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002638 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002639 Register out = OutputRegister(instruction);
2640 Register left;
2641 if (kind != HInstruction::kNeg) {
2642 left = InputRegisterAt(instruction, 0);
2643 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002644 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002645 // shifter operand operation, the IR generating `right_reg` (input to the type
2646 // conversion) can have a different type from the current instruction's type,
2647 // so we manually indicate the type.
2648 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002649 Operand right_operand(0);
2650
Anton Kirilov74234da2017-01-13 14:42:47 +00002651 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2652 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002653 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2654 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002655 right_operand = Operand(right_reg,
2656 helpers::ShiftFromOpKind(op_kind),
2657 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002658 }
2659
2660 // Logical binary operations do not support extension operations in the
2661 // operand. Note that VIXL would still manage if it was passed by generating
2662 // the extension as a separate instruction.
2663 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2664 DCHECK(!right_operand.IsExtendedRegister() ||
2665 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2666 kind != HInstruction::kNeg));
2667 switch (kind) {
2668 case HInstruction::kAdd:
2669 __ Add(out, left, right_operand);
2670 break;
2671 case HInstruction::kAnd:
2672 __ And(out, left, right_operand);
2673 break;
2674 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002675 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002676 __ Neg(out, right_operand);
2677 break;
2678 case HInstruction::kOr:
2679 __ Orr(out, left, right_operand);
2680 break;
2681 case HInstruction::kSub:
2682 __ Sub(out, left, right_operand);
2683 break;
2684 case HInstruction::kXor:
2685 __ Eor(out, left, right_operand);
2686 break;
2687 default:
2688 LOG(FATAL) << "Unexpected operation kind: " << kind;
2689 UNREACHABLE();
2690 }
2691}
2692
Artem Serov328429f2016-07-06 16:23:04 +01002693void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002694 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002695 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002699}
2700
Roland Levillain19c54192016-11-04 13:44:09 +00002701void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002702 __ Add(OutputRegister(instruction),
2703 InputRegisterAt(instruction, 0),
2704 Operand(InputOperandAt(instruction, 1)));
2705}
2706
Artem Serove1811ed2017-04-27 16:50:47 +01002707void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2708 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002709 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002710
2711 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2712
2713 locations->SetInAt(0, Location::RequiresRegister());
2714 // For byte case we don't need to shift the index variable so we can encode the data offset into
2715 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2716 // data offset constant generation out of the loop and reduce the critical path length in the
2717 // loop.
2718 locations->SetInAt(1, shift->GetValue() == 0
2719 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2720 : Location::RequiresRegister());
2721 locations->SetInAt(2, Location::ConstantLocation(shift));
2722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2723}
2724
2725void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2726 HIntermediateAddressIndex* instruction) {
2727 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002728 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002729 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2730
2731 if (shift == 0) {
2732 __ Add(OutputRegister(instruction), index_reg, offset);
2733 } else {
2734 Register offset_reg = InputRegisterAt(instruction, 1);
2735 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2736 }
2737}
2738
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002739void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002740 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002741 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002742 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2743 if (instr->GetOpKind() == HInstruction::kSub &&
2744 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002745 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002746 // Don't allocate register for Mneg instruction.
2747 } else {
2748 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2749 Location::RequiresRegister());
2750 }
2751 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2752 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2754}
2755
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002756void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002757 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002758 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2759 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002760
2761 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2762 // This fixup should be carried out for all multiply-accumulate instructions:
2763 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002765 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2766 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002767 vixl::aarch64::Instruction* prev =
2768 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002769 if (prev->IsLoadOrStore()) {
2770 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002771 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002772 __ nop();
2773 }
2774 }
2775
2776 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002777 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002778 __ Madd(res, mul_left, mul_right, accumulator);
2779 } else {
2780 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002781 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002782 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002783 __ Mneg(res, mul_left, mul_right);
2784 } else {
2785 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2786 __ Msub(res, mul_left, mul_right, accumulator);
2787 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002788 }
2789}
2790
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002791void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002792 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002793 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002794 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002795 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2796 object_array_get_with_read_barrier
2797 ? LocationSummary::kCallOnSlowPath
2798 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002799 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002800 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002801 // We need a temporary register for the read barrier marking slow
2802 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002803 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2804 !Runtime::Current()->UseJitCompilation() &&
2805 instruction->GetIndex()->IsConstant()) {
2806 // Array loads with constant index are treated as field loads.
2807 // If link-time thunks for the Baker read barrier are enabled, for AOT
2808 // constant index loads we need a temporary only if the offset is too big.
2809 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2810 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002812 if (offset >= kReferenceLoadMinFarOffset) {
2813 locations->AddTemp(FixedTempLocation());
2814 }
2815 } else {
2816 locations->AddTemp(Location::RequiresRegister());
2817 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002818 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002819 locations->SetInAt(0, Location::RequiresRegister());
2820 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002822 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2823 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002824 // The output overlaps in the case of an object array get with
2825 // read barriers enabled: we do not want the move to overwrite the
2826 // array's location, as we need it to emit the read barrier.
2827 locations->SetOut(
2828 Location::RequiresRegister(),
2829 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002830 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002831}
2832
2833void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002834 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002835 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002836 LocationSummary* locations = instruction->GetLocations();
2837 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002838 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002839 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002840 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2841 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002842 MacroAssembler* masm = GetVIXLAssembler();
2843 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002844
Roland Levillain19c54192016-11-04 13:44:09 +00002845 // The read barrier instrumentation of object ArrayGet instructions
2846 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002848 instruction->GetArray()->IsIntermediateAddress() &&
2849 kEmitCompilerReadBarrier));
2850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002852 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002853 // Note that a potential implicit null check is handled in the
2854 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002855 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002856 if (index.IsConstant()) {
2857 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002858 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002859 Location maybe_temp =
2860 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2861 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2862 out,
2863 obj.W(),
2864 offset,
2865 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002866 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002867 /* use_load_acquire */ false);
2868 } else {
2869 Register temp = WRegisterFrom(locations->GetTemp(0));
2870 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002871 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002872 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002873 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002874 // General case.
2875 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002876 Register length;
2877 if (maybe_compressed_char_at) {
2878 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2879 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002880 {
2881 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2882 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2883
2884 if (instruction->GetArray()->IsIntermediateAddress()) {
2885 DCHECK_LT(count_offset, offset);
2886 int64_t adjusted_offset =
2887 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2888 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2889 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2890 } else {
2891 __ Ldr(length, HeapOperand(obj, count_offset));
2892 }
2893 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002894 }
jessicahandojo05765752016-09-09 19:01:32 -07002895 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002896 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002897 if (maybe_compressed_char_at) {
2898 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002899 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2900 "Expecting 0=compressed, 1=uncompressed");
2901 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002902 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002903 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002904 __ B(&done);
2905 __ Bind(&uncompressed_load);
2906 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002907 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002908 __ Bind(&done);
2909 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002910 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002911 source = HeapOperand(obj, offset);
2912 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002913 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002914 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002915 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002916 // We do not need to compute the intermediate address from the array: the
2917 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002918 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002919 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002920 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002921 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2922 }
2923 temp = obj;
2924 } else {
2925 __ Add(temp, obj, offset);
2926 }
jessicahandojo05765752016-09-09 19:01:32 -07002927 if (maybe_compressed_char_at) {
2928 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002929 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2930 "Expecting 0=compressed, 1=uncompressed");
2931 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002932 __ Ldrb(Register(OutputCPURegister(instruction)),
2933 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2934 __ B(&done);
2935 __ Bind(&uncompressed_load);
2936 __ Ldrh(Register(OutputCPURegister(instruction)),
2937 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2938 __ Bind(&done);
2939 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002941 }
Roland Levillain44015862016-01-22 11:47:17 +00002942 }
jessicahandojo05765752016-09-09 19:01:32 -07002943 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002944 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2945 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002946 codegen_->Load(type, OutputCPURegister(instruction), source);
2947 codegen_->MaybeRecordImplicitNullCheck(instruction);
2948 }
Roland Levillain44015862016-01-22 11:47:17 +00002949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002951 static_assert(
2952 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2953 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2954 Location obj_loc = locations->InAt(0);
2955 if (index.IsConstant()) {
2956 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2957 } else {
2958 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2959 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002960 }
Roland Levillain4d027112015-07-01 15:41:14 +01002961 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002962}
2963
Alexandre Rames5319def2014-10-23 10:03:10 +01002964void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002965 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002966 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002967 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002968}
2969
2970void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002971 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002972 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002973 {
2974 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2975 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2976 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2977 codegen_->MaybeRecordImplicitNullCheck(instruction);
2978 }
jessicahandojo05765752016-09-09 19:01:32 -07002979 // Mask out compression flag from String's array length.
2980 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002981 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002982 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002983}
2984
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002985void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002987
2988 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002989 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002990 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002991 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002992 LocationSummary::kCallOnSlowPath :
2993 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002994 locations->SetInAt(0, Location::RequiresRegister());
2995 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002996 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2997 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002999 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003000 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003001 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003002 }
3003}
3004
3005void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01003007 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003008 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003009 bool needs_write_barrier =
3010 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01003011
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003012 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01003013 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003014 CPURegister source = value;
3015 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003017 MemOperand destination = HeapOperand(array);
3018 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003019
3020 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003021 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003022 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003023 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003024 destination = HeapOperand(array, offset);
3025 } else {
3026 UseScratchRegisterScope temps(masm);
3027 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01003028 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003029 // We do not need to compute the intermediate address from the array: the
3030 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01003031 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003032 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01003033 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003034 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
3035 }
3036 temp = array;
3037 } else {
3038 __ Add(temp, array, offset);
3039 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003040 destination = HeapOperand(temp,
3041 XRegisterFrom(index),
3042 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003043 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003044 }
Artem Serov914d7a82017-02-07 14:33:49 +00003045 {
3046 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3047 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3048 codegen_->Store(value_type, value, destination);
3049 codegen_->MaybeRecordImplicitNullCheck(instruction);
3050 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003051 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003052 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003053 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003054 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003055 {
3056 // We use a block to end the scratch scope before the write barrier, thus
3057 // freeing the temporary registers so they can be used in `MarkGCCard`.
3058 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003059 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003060 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003061 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003062 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003063 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003064 destination = HeapOperand(temp,
3065 XRegisterFrom(index),
3066 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003067 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003068 }
3069
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003070 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3071 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3072 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3073
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003074 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003075 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003076 codegen_->AddSlowPath(slow_path);
3077 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003078 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003079 __ Cbnz(Register(value), &non_zero);
3080 if (!index.IsConstant()) {
3081 __ Add(temp, array, offset);
3082 }
Artem Serov914d7a82017-02-07 14:33:49 +00003083 {
3084 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3085 // emitted.
3086 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3087 __ Str(wzr, destination);
3088 codegen_->MaybeRecordImplicitNullCheck(instruction);
3089 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003090 __ B(&done);
3091 __ Bind(&non_zero);
3092 }
3093
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003094 // Note that when Baker read barriers are enabled, the type
3095 // checks are performed without read barriers. This is fine,
3096 // even in the case where a class object is in the from-space
3097 // after the flip, as a comparison involving such a type would
3098 // not produce a false positive; it may of course produce a
3099 // false negative, in which case we would take the ArraySet
3100 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003101
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003102 Register temp2 = temps.AcquireSameSizeAs(array);
3103 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003104 {
3105 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3106 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3107 __ Ldr(temp, HeapOperand(array, class_offset));
3108 codegen_->MaybeRecordImplicitNullCheck(instruction);
3109 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003110 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003111
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003112 // /* HeapReference<Class> */ temp = temp->component_type_
3113 __ Ldr(temp, HeapOperand(temp, component_offset));
3114 // /* HeapReference<Class> */ temp2 = value->klass_
3115 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3116 // If heap poisoning is enabled, no need to unpoison `temp`
3117 // nor `temp2`, as we are comparing two poisoned references.
3118 __ Cmp(temp, temp2);
3119 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003120
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003121 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3122 vixl::aarch64::Label do_put;
3123 __ B(eq, &do_put);
3124 // If heap poisoning is enabled, the `temp` reference has
3125 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003126 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3127
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003128 // /* HeapReference<Class> */ temp = temp->super_class_
3129 __ Ldr(temp, HeapOperand(temp, super_offset));
3130 // If heap poisoning is enabled, no need to unpoison
3131 // `temp`, as we are comparing against null below.
3132 __ Cbnz(temp, slow_path->GetEntryLabel());
3133 __ Bind(&do_put);
3134 } else {
3135 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003136 }
3137 }
3138
3139 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003140 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003141 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003142 __ Mov(temp2, value.W());
3143 GetAssembler()->PoisonHeapReference(temp2);
3144 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003145 }
3146
3147 if (!index.IsConstant()) {
3148 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003149 } else {
3150 // We no longer need the `temp` here so release it as the store below may
3151 // need a scratch register (if the constant index makes the offset too large)
3152 // and the poisoned `source` could be using the other scratch register.
3153 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003154 }
Artem Serov914d7a82017-02-07 14:33:49 +00003155 {
3156 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3157 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3158 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003159
Artem Serov914d7a82017-02-07 14:33:49 +00003160 if (!may_need_runtime_call_for_type_check) {
3161 codegen_->MaybeRecordImplicitNullCheck(instruction);
3162 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003163 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003164 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003165
3166 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3167
3168 if (done.IsLinked()) {
3169 __ Bind(&done);
3170 }
3171
3172 if (slow_path != nullptr) {
3173 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003174 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003175 }
3176}
3177
Alexandre Rames67555f72014-11-18 10:55:16 +00003178void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003179 RegisterSet caller_saves = RegisterSet::Empty();
3180 InvokeRuntimeCallingConvention calling_convention;
3181 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3182 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3183 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003184 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003185 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003186}
3187
3188void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003189 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003190 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003191 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003192 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3193 __ B(slow_path->GetEntryLabel(), hs);
3194}
3195
Alexandre Rames67555f72014-11-18 10:55:16 +00003196void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3197 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003198 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003199 locations->SetInAt(0, Location::RequiresRegister());
3200 if (check->HasUses()) {
3201 locations->SetOut(Location::SameAsFirstInput());
3202 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01003203 // Rely on the type initialization to save everything we need.
3204 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00003205}
3206
3207void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3208 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003209 SlowPathCodeARM64* slow_path =
3210 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00003211 codegen_->AddSlowPath(slow_path);
3212 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3213}
3214
Roland Levillain1a653882016-03-18 18:05:57 +00003215static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3216 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3217 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3218}
3219
3220void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3221 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3222 Location rhs_loc = instruction->GetLocations()->InAt(1);
3223 if (rhs_loc.IsConstant()) {
3224 // 0.0 is the only immediate that can be encoded directly in
3225 // an FCMP instruction.
3226 //
3227 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3228 // specify that in a floating-point comparison, positive zero
3229 // and negative zero are considered equal, so we can use the
3230 // literal 0.0 for both cases here.
3231 //
3232 // Note however that some methods (Float.equal, Float.compare,
3233 // Float.compareTo, Double.equal, Double.compare,
3234 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3235 // StrictMath.min) consider 0.0 to be (strictly) greater than
3236 // -0.0. So if we ever translate calls to these methods into a
3237 // HCompare instruction, we must handle the -0.0 case with
3238 // care here.
3239 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3240 __ Fcmp(lhs_reg, 0.0);
3241 } else {
3242 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3243 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003244}
3245
Serban Constantinescu02164b32014-11-13 14:05:07 +00003246void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003247 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003248 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003250 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003251 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003252 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003254 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003255 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003256 case DataType::Type::kInt32:
3257 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003258 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003259 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3261 break;
3262 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 case DataType::Type::kFloat32:
3264 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003265 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003266 locations->SetInAt(1,
3267 IsFloatingPointZeroConstant(compare->InputAt(1))
3268 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3269 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003270 locations->SetOut(Location::RequiresRegister());
3271 break;
3272 }
3273 default:
3274 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3275 }
3276}
3277
3278void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003279 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003280
3281 // 0 if: left == right
3282 // 1 if: left > right
3283 // -1 if: left < right
3284 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003285 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003286 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003288 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003289 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 case DataType::Type::kInt32:
3291 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003292 Register result = OutputRegister(compare);
3293 Register left = InputRegisterAt(compare, 0);
3294 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003295 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003296 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3297 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003298 break;
3299 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003300 case DataType::Type::kFloat32:
3301 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003302 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003303 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003304 __ Cset(result, ne);
3305 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003306 break;
3307 }
3308 default:
3309 LOG(FATAL) << "Unimplemented compare type " << in_type;
3310 }
3311}
3312
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003313void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003314 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003317 locations->SetInAt(0, Location::RequiresFpuRegister());
3318 locations->SetInAt(1,
3319 IsFloatingPointZeroConstant(instruction->InputAt(1))
3320 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3321 : Location::RequiresFpuRegister());
3322 } else {
3323 // Integer cases.
3324 locations->SetInAt(0, Location::RequiresRegister());
3325 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3326 }
3327
David Brazdilb3e773e2016-01-26 11:28:37 +00003328 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003330 }
3331}
3332
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003333void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003334 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003335 return;
3336 }
3337
3338 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003339 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003340 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003343 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003344 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003345 } else {
3346 // Integer cases.
3347 Register lhs = InputRegisterAt(instruction, 0);
3348 Operand rhs = InputOperandAt(instruction, 1);
3349 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003350 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003351 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003352}
3353
3354#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3355 M(Equal) \
3356 M(NotEqual) \
3357 M(LessThan) \
3358 M(LessThanOrEqual) \
3359 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003360 M(GreaterThanOrEqual) \
3361 M(Below) \
3362 M(BelowOrEqual) \
3363 M(Above) \
3364 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003365#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003366void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3367void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003368FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003369#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003370#undef FOR_EACH_CONDITION_INSTRUCTION
3371
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003372void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003373 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003374 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003375 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3376
3377 Register out = OutputRegister(instruction);
3378 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003379
3380 if (abs_imm == 2) {
3381 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3382 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3383 } else {
3384 UseScratchRegisterScope temps(GetVIXLAssembler());
3385 Register temp = temps.AcquireSameSizeAs(out);
3386 __ Add(temp, dividend, abs_imm - 1);
3387 __ Cmp(dividend, 0);
3388 __ Csel(out, temp, dividend, lt);
3389 }
3390
Zheng Xuc6667102015-05-15 16:08:45 +08003391 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003392 if (imm > 0) {
3393 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003394 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003395 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003396 }
3397}
3398
3399void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3400 DCHECK(instruction->IsDiv() || instruction->IsRem());
3401
3402 LocationSummary* locations = instruction->GetLocations();
3403 Location second = locations->InAt(1);
3404 DCHECK(second.IsConstant());
3405
3406 Register out = OutputRegister(instruction);
3407 Register dividend = InputRegisterAt(instruction, 0);
3408 int64_t imm = Int64FromConstant(second.GetConstant());
3409
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003410 DataType::Type type = instruction->GetResultType();
3411 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003412
3413 int64_t magic;
3414 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003415 CalculateMagicAndShiftForDivRem(
3416 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003417
3418 UseScratchRegisterScope temps(GetVIXLAssembler());
3419 Register temp = temps.AcquireSameSizeAs(out);
3420
3421 // temp = get_high(dividend * magic)
3422 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003423 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003424 __ Smulh(temp, dividend, temp);
3425 } else {
3426 __ Smull(temp.X(), dividend, temp);
3427 __ Lsr(temp.X(), temp.X(), 32);
3428 }
3429
3430 if (imm > 0 && magic < 0) {
3431 __ Add(temp, temp, dividend);
3432 } else if (imm < 0 && magic > 0) {
3433 __ Sub(temp, temp, dividend);
3434 }
3435
3436 if (shift != 0) {
3437 __ Asr(temp, temp, shift);
3438 }
3439
3440 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003441 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003442 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003443 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003444 // TODO: Strength reduction for msub.
3445 Register temp_imm = temps.AcquireSameSizeAs(out);
3446 __ Mov(temp_imm, imm);
3447 __ Msub(out, temp, temp_imm, dividend);
3448 }
3449}
3450
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003451void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003452 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003453
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003454 if (imm == 0) {
3455 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3456 return;
3457 }
Zheng Xuc6667102015-05-15 16:08:45 +08003458
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003459 if (IsPowerOfTwo(AbsOrMin(imm))) {
3460 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003461 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003462 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3463 DCHECK(imm < -2 || imm > 2) << imm;
3464 GenerateDivRemWithAnyConstant(instruction);
3465 }
3466}
3467
3468void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3469 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3470 << instruction->GetResultType();
3471
3472 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3473 GenerateIntDivForConstDenom(instruction);
3474 } else {
3475 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003476 Register dividend = InputRegisterAt(instruction, 0);
3477 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003478 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003479 }
3480}
3481
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003482void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3483 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003484 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003485 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 case DataType::Type::kInt32:
3487 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003488 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003489 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3491 break;
3492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003493 case DataType::Type::kFloat32:
3494 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003495 locations->SetInAt(0, Location::RequiresFpuRegister());
3496 locations->SetInAt(1, Location::RequiresFpuRegister());
3497 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3498 break;
3499
3500 default:
3501 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3502 }
3503}
3504
3505void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003506 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003507 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003508 case DataType::Type::kInt32:
3509 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003510 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003511 break;
3512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003513 case DataType::Type::kFloat32:
3514 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003515 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3516 break;
3517
3518 default:
3519 LOG(FATAL) << "Unexpected div type " << type;
3520 }
3521}
3522
Alexandre Rames67555f72014-11-18 10:55:16 +00003523void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003524 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003525 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003526}
3527
3528void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3529 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003530 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003531 codegen_->AddSlowPath(slow_path);
3532 Location value = instruction->GetLocations()->InAt(0);
3533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003534 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003536 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003537 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003538 return;
3539 }
3540
Alexandre Rames67555f72014-11-18 10:55:16 +00003541 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003542 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003543 if (divisor == 0) {
3544 __ B(slow_path->GetEntryLabel());
3545 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003546 // A division by a non-null constant is valid. We don't need to perform
3547 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003548 }
3549 } else {
3550 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3551 }
3552}
3553
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003554void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3555 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003556 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003557 locations->SetOut(Location::ConstantLocation(constant));
3558}
3559
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003560void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3561 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003562 // Will be generated at use site.
3563}
3564
Alexandre Rames5319def2014-10-23 10:03:10 +01003565void LocationsBuilderARM64::VisitExit(HExit* exit) {
3566 exit->SetLocations(nullptr);
3567}
3568
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003569void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003570}
3571
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003572void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3573 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003574 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003575 locations->SetOut(Location::ConstantLocation(constant));
3576}
3577
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003578void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003579 // Will be generated at use site.
3580}
3581
David Brazdilfc6a86a2015-06-26 10:33:45 +00003582void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003583 if (successor->IsExitBlock()) {
3584 DCHECK(got->GetPrevious()->AlwaysThrows());
3585 return; // no code needed
3586 }
3587
Serban Constantinescu02164b32014-11-13 14:05:07 +00003588 HBasicBlock* block = got->GetBlock();
3589 HInstruction* previous = got->GetPrevious();
3590 HLoopInformation* info = block->GetLoopInformation();
3591
David Brazdil46e2a392015-03-16 17:31:52 +00003592 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003593 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3594 UseScratchRegisterScope temps(GetVIXLAssembler());
3595 Register temp1 = temps.AcquireX();
3596 Register temp2 = temps.AcquireX();
3597 __ Ldr(temp1, MemOperand(sp, 0));
3598 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3599 __ Add(temp2, temp2, 1);
3600 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3601 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003602 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3603 return;
3604 }
3605 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3606 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003607 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003608 }
3609 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003610 __ B(codegen_->GetLabelOf(successor));
3611 }
3612}
3613
David Brazdilfc6a86a2015-06-26 10:33:45 +00003614void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3615 got->SetLocations(nullptr);
3616}
3617
3618void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3619 HandleGoto(got, got->GetSuccessor());
3620}
3621
3622void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3623 try_boundary->SetLocations(nullptr);
3624}
3625
3626void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3627 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3628 if (!successor->IsExitBlock()) {
3629 HandleGoto(try_boundary, successor);
3630 }
3631}
3632
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003633void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003634 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003635 vixl::aarch64::Label* true_target,
3636 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003637 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003638
David Brazdil0debae72015-11-12 18:37:00 +00003639 if (true_target == nullptr && false_target == nullptr) {
3640 // Nothing to do. The code always falls through.
3641 return;
3642 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003643 // Constant condition, statically compared against "true" (integer value 1).
3644 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003645 if (true_target != nullptr) {
3646 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003647 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003648 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003649 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003650 if (false_target != nullptr) {
3651 __ B(false_target);
3652 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003653 }
David Brazdil0debae72015-11-12 18:37:00 +00003654 return;
3655 }
3656
3657 // The following code generates these patterns:
3658 // (1) true_target == nullptr && false_target != nullptr
3659 // - opposite condition true => branch to false_target
3660 // (2) true_target != nullptr && false_target == nullptr
3661 // - condition true => branch to true_target
3662 // (3) true_target != nullptr && false_target != nullptr
3663 // - condition true => branch to true_target
3664 // - branch to false_target
3665 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003666 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003667 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003668 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003669 if (true_target == nullptr) {
3670 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3671 } else {
3672 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3673 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003674 } else {
3675 // The condition instruction has not been materialized, use its inputs as
3676 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003677 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003679 DataType::Type type = condition->InputAt(0)->GetType();
3680 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003681 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003682 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003683 IfCondition opposite_condition = condition->GetOppositeCondition();
3684 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003685 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003686 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003687 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003688 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003689 // Integer cases.
3690 Register lhs = InputRegisterAt(condition, 0);
3691 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003692
3693 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003694 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003695 if (true_target == nullptr) {
3696 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3697 non_fallthrough_target = false_target;
3698 } else {
3699 arm64_cond = ARM64Condition(condition->GetCondition());
3700 non_fallthrough_target = true_target;
3701 }
3702
Aart Bik086d27e2016-01-20 17:02:00 -08003703 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003704 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003705 switch (arm64_cond) {
3706 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003707 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003708 break;
3709 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003710 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003711 break;
3712 case lt:
3713 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003714 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003715 break;
3716 case ge:
3717 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003718 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003719 break;
3720 default:
3721 // Without the `static_cast` the compiler throws an error for
3722 // `-Werror=sign-promo`.
3723 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3724 }
3725 } else {
3726 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003727 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003728 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003729 }
3730 }
David Brazdil0debae72015-11-12 18:37:00 +00003731
3732 // If neither branch falls through (case 3), the conditional branch to `true_target`
3733 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3734 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003735 __ B(false_target);
3736 }
3737}
3738
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003739void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003740 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003741 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003742 locations->SetInAt(0, Location::RequiresRegister());
3743 }
3744}
3745
3746void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003747 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3748 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003749 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3750 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3751 true_target = nullptr;
3752 }
3753 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3754 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3755 false_target = nullptr;
3756 }
David Brazdil0debae72015-11-12 18:37:00 +00003757 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003758}
3759
3760void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003761 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003762 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003763 InvokeRuntimeCallingConvention calling_convention;
3764 RegisterSet caller_saves = RegisterSet::Empty();
3765 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3766 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003767 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003768 locations->SetInAt(0, Location::RequiresRegister());
3769 }
3770}
3771
3772void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003773 SlowPathCodeARM64* slow_path =
3774 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003775 GenerateTestAndBranch(deoptimize,
3776 /* condition_input_index */ 0,
3777 slow_path->GetEntryLabel(),
3778 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003779}
3780
Mingyao Yang063fc772016-08-02 11:02:54 -07003781void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003782 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003783 LocationSummary(flag, LocationSummary::kNoCall);
3784 locations->SetOut(Location::RequiresRegister());
3785}
3786
3787void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3788 __ Ldr(OutputRegister(flag),
3789 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3790}
3791
David Brazdilc0b601b2016-02-08 14:20:45 +00003792static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3793 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003795}
3796
Alexandre Rames880f1192016-06-13 16:04:50 +01003797static inline Condition GetConditionForSelect(HCondition* condition) {
3798 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003799 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3800 : ARM64Condition(cond);
3801}
3802
David Brazdil74eb1b22015-12-14 11:44:01 +00003803void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003804 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003805 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003806 locations->SetInAt(0, Location::RequiresFpuRegister());
3807 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003808 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003809 } else {
3810 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3811 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3812 bool is_true_value_constant = cst_true_value != nullptr;
3813 bool is_false_value_constant = cst_false_value != nullptr;
3814 // Ask VIXL whether we should synthesize constants in registers.
3815 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3816 Operand true_op = is_true_value_constant ?
3817 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3818 Operand false_op = is_false_value_constant ?
3819 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3820 bool true_value_in_register = false;
3821 bool false_value_in_register = false;
3822 MacroAssembler::GetCselSynthesisInformation(
3823 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3824 true_value_in_register |= !is_true_value_constant;
3825 false_value_in_register |= !is_false_value_constant;
3826
3827 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3828 : Location::ConstantLocation(cst_true_value));
3829 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3830 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003831 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003832 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003833
David Brazdil74eb1b22015-12-14 11:44:01 +00003834 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3835 locations->SetInAt(2, Location::RequiresRegister());
3836 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003837}
3838
3839void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003840 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003841 Condition csel_cond;
3842
3843 if (IsBooleanValueOrMaterializedCondition(cond)) {
3844 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003845 // Use the condition flags set by the previous instruction.
3846 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003847 } else {
3848 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003849 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003850 }
3851 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003852 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003853 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003854 } else {
3855 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003856 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003857 }
3858
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003859 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003860 __ Fcsel(OutputFPRegister(select),
3861 InputFPRegisterAt(select, 1),
3862 InputFPRegisterAt(select, 0),
3863 csel_cond);
3864 } else {
3865 __ Csel(OutputRegister(select),
3866 InputOperandAt(select, 1),
3867 InputOperandAt(select, 0),
3868 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003869 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003870}
3871
David Srbecky0cf44932015-12-09 14:09:59 +00003872void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003873 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003874}
3875
David Srbeckyd28f4a02016-03-14 17:14:24 +00003876void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3877 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003878}
3879
3880void CodeGeneratorARM64::GenerateNop() {
3881 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003882}
3883
Alexandre Rames5319def2014-10-23 10:03:10 +01003884void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003885 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003886}
3887
3888void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003889 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003890}
3891
3892void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003893 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003894}
3895
3896void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003897 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003898}
3899
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003900// Temp is used for read barrier.
3901static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3902 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003903 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003904 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3905 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3906 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3907 return 1;
3908 }
3909 return 0;
3910}
3911
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003912// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003913// interface pointer, one for loading the current interface.
3914// The other checks have one temp for loading the object's class.
3915static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3916 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3917 return 3;
3918 }
3919 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003920}
3921
Alexandre Rames67555f72014-11-18 10:55:16 +00003922void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003923 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003924 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003925 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003926 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 case TypeCheckKind::kExactCheck:
3928 case TypeCheckKind::kAbstractClassCheck:
3929 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003930 case TypeCheckKind::kArrayObjectCheck: {
3931 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3932 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3933 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003934 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003935 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003936 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937 case TypeCheckKind::kUnresolvedCheck:
3938 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 call_kind = LocationSummary::kCallOnSlowPath;
3940 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003941 case TypeCheckKind::kBitstringCheck:
3942 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003944
Vladimir Markoca6fff82017-10-03 14:49:14 +01003945 LocationSummary* locations =
3946 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003947 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003948 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003949 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003950 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003951 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3952 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3953 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3954 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3955 } else {
3956 locations->SetInAt(1, Location::RequiresRegister());
3957 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003958 // The "out" register is used as a temporary, so it overlaps with the inputs.
3959 // Note that TypeCheckSlowPathARM64 uses this register too.
3960 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003961 // Add temps if necessary for read barriers.
3962 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003963}
3964
3965void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003966 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003967 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003968 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003970 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3971 ? Register()
3972 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003973 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003974 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003975 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3976 DCHECK_LE(num_temps, 1u);
3977 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003978 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3979 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3980 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3981 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003982
Scott Wakeling97c72b72016-06-24 16:19:36 +01003983 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003984 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003985
3986 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003987 // Avoid null check if we know `obj` is not null.
3988 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003989 __ Cbz(obj, &zero);
3990 }
3991
Roland Levillain44015862016-01-22 11:47:17 +00003992 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003993 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003994 ReadBarrierOption read_barrier_option =
3995 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003996 // /* HeapReference<Class> */ out = obj->klass_
3997 GenerateReferenceLoadTwoRegisters(instruction,
3998 out_loc,
3999 obj_loc,
4000 class_offset,
4001 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004002 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004003 __ Cmp(out, cls);
4004 __ Cset(out, eq);
4005 if (zero.IsLinked()) {
4006 __ B(&done);
4007 }
4008 break;
4009 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004010
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004011 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004012 ReadBarrierOption read_barrier_option =
4013 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004014 // /* HeapReference<Class> */ out = obj->klass_
4015 GenerateReferenceLoadTwoRegisters(instruction,
4016 out_loc,
4017 obj_loc,
4018 class_offset,
4019 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004020 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004021 // If the class is abstract, we eagerly fetch the super class of the
4022 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004023 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004024 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004025 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004026 GenerateReferenceLoadOneRegister(instruction,
4027 out_loc,
4028 super_offset,
4029 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004030 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 // If `out` is null, we use it for the result, and jump to `done`.
4032 __ Cbz(out, &done);
4033 __ Cmp(out, cls);
4034 __ B(ne, &loop);
4035 __ Mov(out, 1);
4036 if (zero.IsLinked()) {
4037 __ B(&done);
4038 }
4039 break;
4040 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004041
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004042 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004043 ReadBarrierOption read_barrier_option =
4044 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004045 // /* HeapReference<Class> */ out = obj->klass_
4046 GenerateReferenceLoadTwoRegisters(instruction,
4047 out_loc,
4048 obj_loc,
4049 class_offset,
4050 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004051 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004052 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004053 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004054 __ Bind(&loop);
4055 __ Cmp(out, cls);
4056 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004058 GenerateReferenceLoadOneRegister(instruction,
4059 out_loc,
4060 super_offset,
4061 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004062 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 __ Cbnz(out, &loop);
4064 // If `out` is null, we use it for the result, and jump to `done`.
4065 __ B(&done);
4066 __ Bind(&success);
4067 __ Mov(out, 1);
4068 if (zero.IsLinked()) {
4069 __ B(&done);
4070 }
4071 break;
4072 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004074 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004075 ReadBarrierOption read_barrier_option =
4076 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004077 // /* HeapReference<Class> */ out = obj->klass_
4078 GenerateReferenceLoadTwoRegisters(instruction,
4079 out_loc,
4080 obj_loc,
4081 class_offset,
4082 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004083 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004084 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004085 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004086 __ Cmp(out, cls);
4087 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004089 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004090 GenerateReferenceLoadOneRegister(instruction,
4091 out_loc,
4092 component_offset,
4093 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004094 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 // If `out` is null, we use it for the result, and jump to `done`.
4096 __ Cbz(out, &done);
4097 __ Ldrh(out, HeapOperand(out, primitive_offset));
4098 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4099 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004100 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004101 __ Mov(out, 1);
4102 __ B(&done);
4103 break;
4104 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004106 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004107 // No read barrier since the slow path will retry upon failure.
4108 // /* HeapReference<Class> */ out = obj->klass_
4109 GenerateReferenceLoadTwoRegisters(instruction,
4110 out_loc,
4111 obj_loc,
4112 class_offset,
4113 maybe_temp_loc,
4114 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115 __ Cmp(out, cls);
4116 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004117 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4118 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004119 codegen_->AddSlowPath(slow_path);
4120 __ B(ne, slow_path->GetEntryLabel());
4121 __ Mov(out, 1);
4122 if (zero.IsLinked()) {
4123 __ B(&done);
4124 }
4125 break;
4126 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004127
Calin Juravle98893e12015-10-02 21:05:03 +01004128 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004129 case TypeCheckKind::kInterfaceCheck: {
4130 // Note that we indeed only call on slow path, but we always go
4131 // into the slow path for the unresolved and interface check
4132 // cases.
4133 //
4134 // We cannot directly call the InstanceofNonTrivial runtime
4135 // entry point without resorting to a type checking slow path
4136 // here (i.e. by calling InvokeRuntime directly), as it would
4137 // require to assign fixed registers for the inputs of this
4138 // HInstanceOf instruction (following the runtime calling
4139 // convention), which might be cluttered by the potential first
4140 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004141 //
4142 // TODO: Introduce a new runtime entry point taking the object
4143 // to test (instead of its class) as argument, and let it deal
4144 // with the read barrier issues. This will let us refactor this
4145 // case of the `switch` code as it was previously (with a direct
4146 // call to the runtime not using a type checking slow path).
4147 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004148 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004149 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4150 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004151 codegen_->AddSlowPath(slow_path);
4152 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004153 if (zero.IsLinked()) {
4154 __ B(&done);
4155 }
4156 break;
4157 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004158
4159 case TypeCheckKind::kBitstringCheck: {
4160 // /* HeapReference<Class> */ temp = obj->klass_
4161 GenerateReferenceLoadTwoRegisters(instruction,
4162 out_loc,
4163 obj_loc,
4164 class_offset,
4165 maybe_temp_loc,
4166 kWithoutReadBarrier);
4167
4168 GenerateBitstringTypeCheckCompare(instruction, out);
4169 __ Cset(out, eq);
4170 if (zero.IsLinked()) {
4171 __ B(&done);
4172 }
4173 break;
4174 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004175 }
4176
4177 if (zero.IsLinked()) {
4178 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004179 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004180 }
4181
4182 if (done.IsLinked()) {
4183 __ Bind(&done);
4184 }
4185
4186 if (slow_path != nullptr) {
4187 __ Bind(slow_path->GetExitLabel());
4188 }
4189}
4190
4191void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004192 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004193 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004194 LocationSummary* locations =
4195 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004197 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4198 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4199 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4200 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4201 } else {
4202 locations->SetInAt(1, Location::RequiresRegister());
4203 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004204 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4205 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004206}
4207
4208void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004209 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004210 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004211 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004212 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004213 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4214 ? Register()
4215 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004216 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4217 DCHECK_GE(num_temps, 1u);
4218 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004220 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4221 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004222 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004223 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4224 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4225 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4226 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4227 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4228 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4229 const uint32_t object_array_data_offset =
4230 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004231
Vladimir Marko87584542017-12-12 17:47:52 +00004232 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004233 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004234 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4235 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004236 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004237
Scott Wakeling97c72b72016-06-24 16:19:36 +01004238 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 // Avoid null check if we know obj is not null.
4240 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004241 __ Cbz(obj, &done);
4242 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004243
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004244 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004245 case TypeCheckKind::kExactCheck:
4246 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004247 // /* HeapReference<Class> */ temp = obj->klass_
4248 GenerateReferenceLoadTwoRegisters(instruction,
4249 temp_loc,
4250 obj_loc,
4251 class_offset,
4252 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004253 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004254
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004255 __ Cmp(temp, cls);
4256 // Jump to slow path for throwing the exception or doing a
4257 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004259 break;
4260 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004262 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004263 // /* HeapReference<Class> */ temp = obj->klass_
4264 GenerateReferenceLoadTwoRegisters(instruction,
4265 temp_loc,
4266 obj_loc,
4267 class_offset,
4268 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004269 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004270
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004271 // If the class is abstract, we eagerly fetch the super class of the
4272 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004273 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004274 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004275 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004276 GenerateReferenceLoadOneRegister(instruction,
4277 temp_loc,
4278 super_offset,
4279 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004280 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004282 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4283 // exception.
4284 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4285 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004286 __ Cmp(temp, cls);
4287 __ B(ne, &loop);
4288 break;
4289 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004290
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004291 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292 // /* HeapReference<Class> */ temp = obj->klass_
4293 GenerateReferenceLoadTwoRegisters(instruction,
4294 temp_loc,
4295 obj_loc,
4296 class_offset,
4297 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004298 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004300 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004301 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004302 __ Bind(&loop);
4303 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004304 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004305
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004307 GenerateReferenceLoadOneRegister(instruction,
4308 temp_loc,
4309 super_offset,
4310 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004311 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004312
4313 // If the class reference currently in `temp` is not null, jump
4314 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004315 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004316 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004317 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004318 break;
4319 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004321 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004322 // /* HeapReference<Class> */ temp = obj->klass_
4323 GenerateReferenceLoadTwoRegisters(instruction,
4324 temp_loc,
4325 obj_loc,
4326 class_offset,
4327 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004328 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004329
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004330 // Do an exact check.
4331 __ Cmp(temp, cls);
4332 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004333
4334 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004335 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004336 GenerateReferenceLoadOneRegister(instruction,
4337 temp_loc,
4338 component_offset,
4339 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004340 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004341
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004342 // If the component type is null, jump to the slow path to throw the exception.
4343 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4344 // Otherwise, the object is indeed an array. Further check that this component type is not a
4345 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004346 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4347 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004348 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004349 break;
4350 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004351
Calin Juravle98893e12015-10-02 21:05:03 +01004352 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004353 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004354 //
4355 // We cannot directly call the CheckCast runtime entry point
4356 // without resorting to a type checking slow path here (i.e. by
4357 // calling InvokeRuntime directly), as it would require to
4358 // assign fixed registers for the inputs of this HInstanceOf
4359 // instruction (following the runtime calling convention), which
4360 // might be cluttered by the potential first read barrier
4361 // emission at the beginning of this method.
4362 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004363 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004364 case TypeCheckKind::kInterfaceCheck: {
4365 // /* HeapReference<Class> */ temp = obj->klass_
4366 GenerateReferenceLoadTwoRegisters(instruction,
4367 temp_loc,
4368 obj_loc,
4369 class_offset,
4370 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004371 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004372
4373 // /* HeapReference<Class> */ temp = temp->iftable_
4374 GenerateReferenceLoadTwoRegisters(instruction,
4375 temp_loc,
4376 temp_loc,
4377 iftable_offset,
4378 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004379 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004380 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004381 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004382 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004383 vixl::aarch64::Label start_loop;
4384 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004385 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004386 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4387 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004388 // Go to next interface.
4389 __ Add(temp, temp, 2 * kHeapReferenceSize);
4390 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004391 // Compare the classes and continue the loop if they do not match.
4392 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4393 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004394 break;
4395 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004396
4397 case TypeCheckKind::kBitstringCheck: {
4398 // /* HeapReference<Class> */ temp = obj->klass_
4399 GenerateReferenceLoadTwoRegisters(instruction,
4400 temp_loc,
4401 obj_loc,
4402 class_offset,
4403 maybe_temp2_loc,
4404 kWithoutReadBarrier);
4405
4406 GenerateBitstringTypeCheckCompare(instruction, temp);
4407 __ B(ne, type_check_slow_path->GetEntryLabel());
4408 break;
4409 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004410 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004411 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004412
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004413 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004414}
4415
Alexandre Rames5319def2014-10-23 10:03:10 +01004416void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004417 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004418 locations->SetOut(Location::ConstantLocation(constant));
4419}
4420
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004421void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004422 // Will be generated at use site.
4423}
4424
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004425void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004426 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004427 locations->SetOut(Location::ConstantLocation(constant));
4428}
4429
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004430void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004431 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004432}
4433
Calin Juravle175dc732015-08-25 15:42:32 +01004434void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4435 // The trampoline uses the same calling convention as dex calling conventions,
4436 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4437 // the method_idx.
4438 HandleInvoke(invoke);
4439}
4440
4441void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4442 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004443 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004444}
4445
Alexandre Rames5319def2014-10-23 10:03:10 +01004446void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004447 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004448 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004449}
4450
Alexandre Rames67555f72014-11-18 10:55:16 +00004451void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4452 HandleInvoke(invoke);
4453}
4454
4455void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4456 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004457 LocationSummary* locations = invoke->GetLocations();
4458 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004459 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004460 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004461 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004462
4463 // The register ip1 is required to be used for the hidden argument in
4464 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004465 MacroAssembler* masm = GetVIXLAssembler();
4466 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004467 scratch_scope.Exclude(ip1);
4468 __ Mov(ip1, invoke->GetDexMethodIndex());
4469
Artem Serov914d7a82017-02-07 14:33:49 +00004470 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004471 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004472 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004473 {
4474 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4475 // /* HeapReference<Class> */ temp = temp->klass_
4476 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4477 codegen_->MaybeRecordImplicitNullCheck(invoke);
4478 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004479 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004480 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004481 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004482 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004483 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004484 }
Artem Serov914d7a82017-02-07 14:33:49 +00004485
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004486 // Instead of simply (possibly) unpoisoning `temp` here, we should
4487 // emit a read barrier for the previous class reference load.
4488 // However this is not required in practice, as this is an
4489 // intermediate/temporary reference and because the current
4490 // concurrent copying collector keeps the from-space memory
4491 // intact/accessible until the end of the marking phase (the
4492 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004493 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004494 __ Ldr(temp,
4495 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4496 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004497 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004498 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004499 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004500 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004501 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004502
4503 {
4504 // Ensure the pc position is recorded immediately after the `blr` instruction.
4505 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4506
4507 // lr();
4508 __ blr(lr);
4509 DCHECK(!codegen_->IsLeafMethod());
4510 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4511 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004512
4513 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004514}
4515
4516void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004517 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004518 if (intrinsic.TryDispatch(invoke)) {
4519 return;
4520 }
4521
Alexandre Rames67555f72014-11-18 10:55:16 +00004522 HandleInvoke(invoke);
4523}
4524
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004525void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004526 // Explicit clinit checks triggered by static invokes must have been pruned by
4527 // art::PrepareForRegisterAllocation.
4528 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004529
Vladimir Markoca6fff82017-10-03 14:49:14 +01004530 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004531 if (intrinsic.TryDispatch(invoke)) {
4532 return;
4533 }
4534
Alexandre Rames67555f72014-11-18 10:55:16 +00004535 HandleInvoke(invoke);
4536}
4537
Andreas Gampe878d58c2015-01-15 23:24:00 -08004538static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4539 if (invoke->GetLocations()->Intrinsified()) {
4540 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4541 intrinsic.Dispatch(invoke);
4542 return true;
4543 }
4544 return false;
4545}
4546
Vladimir Markodc151b22015-10-15 18:02:30 +01004547HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4548 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004549 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004550 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004551 return desired_dispatch_info;
4552}
4553
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004554void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4555 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004556 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004557 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4558 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004559 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4560 uint32_t offset =
4561 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004562 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004563 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004564 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004565 }
Vladimir Marko58155012015-08-19 12:49:41 +00004566 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004567 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004568 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004569 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4570 DCHECK(GetCompilerOptions().IsBootImage());
4571 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004572 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004573 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4574 // Add ADD with its PC-relative method patch.
4575 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004576 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004577 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4578 break;
4579 }
Vladimir Markob066d432018-01-03 13:14:37 +00004580 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4581 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004582 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004583 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4584 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4585 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4586 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4587 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4588 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4589 break;
4590 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004591 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004592 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004593 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4594 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004595 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004596 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004597 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004598 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004599 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004600 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004601 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004602 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4603 // Load method address from literal pool.
4604 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4605 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004606 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4607 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4608 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004609 }
4610 }
4611
4612 switch (invoke->GetCodePtrLocation()) {
4613 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004614 {
4615 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4616 ExactAssemblyScope eas(GetVIXLAssembler(),
4617 kInstructionSize,
4618 CodeBufferCheckScope::kExactSize);
4619 __ bl(&frame_entry_label_);
4620 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4621 }
Vladimir Marko58155012015-08-19 12:49:41 +00004622 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004623 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4624 // LR = callee_method->entry_point_from_quick_compiled_code_;
4625 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004626 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004627 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004628 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004629 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004630 ExactAssemblyScope eas(GetVIXLAssembler(),
4631 kInstructionSize,
4632 CodeBufferCheckScope::kExactSize);
4633 // lr()
4634 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004635 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004636 }
Vladimir Marko58155012015-08-19 12:49:41 +00004637 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004638 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004639
Andreas Gampe878d58c2015-01-15 23:24:00 -08004640 DCHECK(!IsLeafMethod());
4641}
4642
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004643void CodeGeneratorARM64::GenerateVirtualCall(
4644 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004645 // Use the calling convention instead of the location of the receiver, as
4646 // intrinsics may have put the receiver in a different register. In the intrinsics
4647 // slow path, the arguments have been moved to the right place, so here we are
4648 // guaranteed that the receiver is the first register of the calling convention.
4649 InvokeDexCallingConvention calling_convention;
4650 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004651 Register temp = XRegisterFrom(temp_in);
4652 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4653 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4654 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004655 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004656
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004657 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004658
4659 {
4660 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4661 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4662 // /* HeapReference<Class> */ temp = receiver->klass_
4663 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4664 MaybeRecordImplicitNullCheck(invoke);
4665 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004666 // Instead of simply (possibly) unpoisoning `temp` here, we should
4667 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004668 // intermediate/temporary reference and because the current
4669 // concurrent copying collector keeps the from-space memory
4670 // intact/accessible until the end of the marking phase (the
4671 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004672 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4673 // temp = temp->GetMethodAt(method_offset);
4674 __ Ldr(temp, MemOperand(temp, method_offset));
4675 // lr = temp->GetEntryPoint();
4676 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004677 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004678 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004679 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4680 // lr();
4681 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004682 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004683 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004684}
4685
Orion Hodsonac141392017-01-13 11:53:47 +00004686void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4687 HandleInvoke(invoke);
4688}
4689
4690void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4691 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004692 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004693}
4694
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004695void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4696 HandleInvoke(invoke);
4697}
4698
4699void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4700 codegen_->GenerateInvokeCustomCall(invoke);
4701 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
4702}
4703
Vladimir Marko6fd16062018-06-26 11:02:04 +01004704vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4705 uint32_t intrinsic_data,
4706 vixl::aarch64::Label* adrp_label) {
4707 return NewPcRelativePatch(
4708 /* dex_file */ nullptr, intrinsic_data, adrp_label, &boot_image_intrinsic_patches_);
4709}
4710
Vladimir Markob066d432018-01-03 13:14:37 +00004711vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4712 uint32_t boot_image_offset,
4713 vixl::aarch64::Label* adrp_label) {
4714 return NewPcRelativePatch(
4715 /* dex_file */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
4716}
4717
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004718vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004719 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004720 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004721 return NewPcRelativePatch(
4722 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004723}
4724
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004725vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4726 MethodReference target_method,
4727 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004728 return NewPcRelativePatch(
4729 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004730}
4731
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004732vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004733 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004734 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004735 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004736 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004737}
4738
Vladimir Marko1998cd02017-01-13 13:02:58 +00004739vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4740 const DexFile& dex_file,
4741 dex::TypeIndex type_index,
4742 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004743 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004744}
4745
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004746vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004747 const DexFile& dex_file,
4748 dex::StringIndex string_index,
4749 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004750 return NewPcRelativePatch(
4751 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004752}
4753
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004754vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4755 const DexFile& dex_file,
4756 dex::StringIndex string_index,
4757 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004758 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004759}
4760
Vladimir Marko966b46f2018-08-03 10:20:19 +00004761void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
4762 ExactAssemblyScope guard(GetVIXLAssembler(), 1 * vixl::aarch64::kInstructionSize);
4763 if (Runtime::Current()->UseJitCompilation()) {
4764 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4765 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4766 __ cbnz(mr, slow_path_entry);
4767 } else {
4768 baker_read_barrier_patches_.emplace_back(custom_data);
4769 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4770 __ bind(cbnz_label);
4771 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4772 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004773}
4774
Scott Wakeling97c72b72016-06-24 16:19:36 +01004775vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004776 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004777 uint32_t offset_or_index,
4778 vixl::aarch64::Label* adrp_label,
4779 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004780 // Add a patch entry and return the label.
4781 patches->emplace_back(dex_file, offset_or_index);
4782 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004783 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004784 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4785 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4786 return label;
4787}
4788
Scott Wakeling97c72b72016-06-24 16:19:36 +01004789vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4790 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004791 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004792}
4793
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004794vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004795 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004796 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004797 return jit_string_patches_.GetOrCreate(
4798 StringReference(&dex_file, string_index),
4799 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4800}
4801
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004802vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004803 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004804 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004805 return jit_class_patches_.GetOrCreate(
4806 TypeReference(&dex_file, type_index),
4807 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4808}
4809
Vladimir Markoaad75c62016-10-03 08:46:48 +00004810void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4811 vixl::aarch64::Register reg) {
4812 DCHECK(reg.IsX());
4813 SingleEmissionCheckScope guard(GetVIXLAssembler());
4814 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004815 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004816}
4817
4818void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4819 vixl::aarch64::Register out,
4820 vixl::aarch64::Register base) {
4821 DCHECK(out.IsX());
4822 DCHECK(base.IsX());
4823 SingleEmissionCheckScope guard(GetVIXLAssembler());
4824 __ Bind(fixup_label);
4825 __ add(out, base, Operand(/* offset placeholder */ 0));
4826}
4827
4828void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4829 vixl::aarch64::Register out,
4830 vixl::aarch64::Register base) {
4831 DCHECK(base.IsX());
4832 SingleEmissionCheckScope guard(GetVIXLAssembler());
4833 __ Bind(fixup_label);
4834 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4835}
4836
Vladimir Markoeebb8212018-06-05 14:57:24 +01004837void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004838 uint32_t boot_image_reference) {
4839 if (GetCompilerOptions().IsBootImage()) {
4840 // Add ADRP with its PC-relative type patch.
4841 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4842 EmitAdrpPlaceholder(adrp_label, reg.X());
4843 // Add ADD with its PC-relative type patch.
4844 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4845 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004846 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004847 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004848 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004849 EmitAdrpPlaceholder(adrp_label, reg.X());
4850 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004851 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004852 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4853 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004854 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004855 gc::Heap* heap = Runtime::Current()->GetHeap();
4856 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004857 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004858 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4859 }
4860}
4861
Vladimir Marko6fd16062018-06-26 11:02:04 +01004862void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4863 uint32_t boot_image_offset) {
4864 DCHECK(invoke->IsStatic());
4865 InvokeRuntimeCallingConvention calling_convention;
4866 Register argument = calling_convention.GetRegisterAt(0);
4867 if (GetCompilerOptions().IsBootImage()) {
4868 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4869 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4870 MethodReference target_method = invoke->GetTargetMethod();
4871 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4872 // Add ADRP with its PC-relative type patch.
4873 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4874 EmitAdrpPlaceholder(adrp_label, argument.X());
4875 // Add ADD with its PC-relative type patch.
4876 vixl::aarch64::Label* add_label =
4877 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4878 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4879 } else {
4880 LoadBootImageAddress(argument, boot_image_offset);
4881 }
4882 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4883 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4884}
4885
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004886template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004887inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4888 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004889 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004890 for (const PcRelativePatchInfo& info : infos) {
4891 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004892 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004893 info.pc_insn_label->GetLocation(),
4894 info.offset_or_index));
4895 }
4896}
4897
Vladimir Marko6fd16062018-06-26 11:02:04 +01004898template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4899linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4900 const DexFile* target_dex_file,
4901 uint32_t pc_insn_offset,
4902 uint32_t boot_image_offset) {
4903 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4904 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004905}
4906
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004907void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004908 DCHECK(linker_patches->empty());
4909 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004910 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004911 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004912 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004913 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004914 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004915 string_bss_entry_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01004916 boot_image_intrinsic_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004917 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004918 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004919 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004920 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004921 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004922 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004923 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004924 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004925 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004926 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4927 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004928 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004929 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00004930 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004931 DCHECK(boot_image_type_patches_.empty());
4932 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004933 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004934 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004935 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4936 method_bss_entry_patches_, linker_patches);
4937 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4938 type_bss_entry_patches_, linker_patches);
4939 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4940 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004941 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004942 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4943 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004944 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004945 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004946}
4947
Vladimir Markoca1e0382018-04-11 09:58:41 +00004948bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
4949 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
4950 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4951}
4952
4953void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4954 /*out*/ ArenaVector<uint8_t>* code,
4955 /*out*/ std::string* debug_name) {
4956 Arm64Assembler assembler(GetGraph()->GetAllocator());
4957 switch (patch.GetType()) {
4958 case linker::LinkerPatch::Type::kCallRelative: {
4959 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4960 // to the generic JNI and interpreter trampolines.
4961 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4962 kArm64PointerSize).Int32Value());
4963 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4964 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4965 *debug_name = "MethodCallThunk";
4966 }
4967 break;
4968 }
4969 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4970 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4971 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4972 break;
4973 }
4974 default:
4975 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4976 UNREACHABLE();
4977 }
4978
4979 // Ensure we emit the literal pool if any.
4980 assembler.FinalizeCode();
4981 code->resize(assembler.CodeSize());
4982 MemoryRegion code_region(code->data(), code->size());
4983 assembler.FinalizeInstructions(code_region);
4984}
4985
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004986vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4987 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004988 value,
4989 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4990}
4991
Scott Wakeling97c72b72016-06-24 16:19:36 +01004992vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004993 return uint64_literals_.GetOrCreate(
4994 value,
4995 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004996}
4997
Andreas Gampe878d58c2015-01-15 23:24:00 -08004998void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004999 // Explicit clinit checks triggered by static invokes must have been pruned by
5000 // art::PrepareForRegisterAllocation.
5001 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005002
Andreas Gampe878d58c2015-01-15 23:24:00 -08005003 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005004 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005005 return;
5006 }
5007
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005008 {
5009 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
5010 // are no pools emitted.
5011 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5012 LocationSummary* locations = invoke->GetLocations();
5013 codegen_->GenerateStaticOrDirectCall(
5014 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
5015 }
5016
5017 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005018}
5019
5020void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005021 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005022 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005023 return;
5024 }
5025
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005026 {
5027 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5028 // are no pools emitted.
5029 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5030 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5031 DCHECK(!codegen_->IsLeafMethod());
5032 }
5033
5034 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005035}
5036
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005037HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5038 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005039 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005040 case HLoadClass::LoadKind::kInvalid:
5041 LOG(FATAL) << "UNREACHABLE";
5042 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005043 case HLoadClass::LoadKind::kReferrersClass:
5044 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005045 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005046 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005047 case HLoadClass::LoadKind::kBssEntry:
5048 DCHECK(!Runtime::Current()->UseJitCompilation());
5049 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005050 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005051 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005052 DCHECK(Runtime::Current()->UseJitCompilation());
5053 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005054 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005055 break;
5056 }
5057 return desired_class_load_kind;
5058}
5059
Alexandre Rames67555f72014-11-18 10:55:16 +00005060void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005061 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005062 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005063 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005064 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005065 cls,
5066 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005067 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005068 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005069 return;
5070 }
Vladimir Marko41559982017-01-06 14:04:23 +00005071 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005072
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005073 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5074 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005075 ? LocationSummary::kCallOnSlowPath
5076 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005077 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005078 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005079 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005080 }
5081
Vladimir Marko41559982017-01-06 14:04:23 +00005082 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005083 locations->SetInAt(0, Location::RequiresRegister());
5084 }
5085 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005086 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5087 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5088 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005089 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005090 } else {
5091 // For non-Baker read barrier we have a temp-clobbering call.
5092 }
5093 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005094}
5095
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005096// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5097// move.
5098void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005099 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005100 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005101 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005102 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005103 return;
5104 }
Vladimir Marko41559982017-01-06 14:04:23 +00005105 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005106
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005107 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005108 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005109
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005110 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5111 ? kWithoutReadBarrier
5112 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005113 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005114 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005115 case HLoadClass::LoadKind::kReferrersClass: {
5116 DCHECK(!cls->CanCallRuntime());
5117 DCHECK(!cls->MustGenerateClinitCheck());
5118 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5119 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005120 codegen_->GenerateGcRootFieldLoad(cls,
5121 out_loc,
5122 current_method,
5123 ArtMethod::DeclaringClassOffset().Int32Value(),
5124 /* fixup_label */ nullptr,
5125 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005126 break;
5127 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005128 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005129 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005130 // Add ADRP with its PC-relative type patch.
5131 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005132 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005133 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005134 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005135 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005136 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005137 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005138 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005139 break;
5140 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005141 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005142 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005143 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5144 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5145 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005146 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005147 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005148 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005149 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005150 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005151 break;
5152 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005153 case HLoadClass::LoadKind::kBssEntry: {
5154 // Add ADRP with its PC-relative Class .bss entry patch.
5155 const DexFile& dex_file = cls->GetDexFile();
5156 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005157 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
5158 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5159 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005160 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005161 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005162 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005163 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005164 codegen_->GenerateGcRootFieldLoad(cls,
5165 out_loc,
5166 temp,
5167 /* offset placeholder */ 0u,
5168 ldr_label,
5169 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005170 generate_null_check = true;
5171 break;
5172 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005173 case HLoadClass::LoadKind::kJitBootImageAddress: {
5174 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5175 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5176 DCHECK_NE(address, 0u);
5177 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5178 break;
5179 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005180 case HLoadClass::LoadKind::kJitTableAddress: {
5181 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5182 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005183 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005184 codegen_->GenerateGcRootFieldLoad(cls,
5185 out_loc,
5186 out.X(),
5187 /* offset */ 0,
5188 /* fixup_label */ nullptr,
5189 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005190 break;
5191 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005192 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005193 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005194 LOG(FATAL) << "UNREACHABLE";
5195 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005196 }
5197
Vladimir Markoea4c1262017-02-06 19:59:33 +00005198 bool do_clinit = cls->MustGenerateClinitCheck();
5199 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005200 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005201 SlowPathCodeARM64* slow_path =
5202 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005203 codegen_->AddSlowPath(slow_path);
5204 if (generate_null_check) {
5205 __ Cbz(out, slow_path->GetEntryLabel());
5206 }
5207 if (cls->MustGenerateClinitCheck()) {
5208 GenerateClassInitializationCheck(slow_path, out);
5209 } else {
5210 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005211 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005212 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005213 }
5214}
5215
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005216void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5217 InvokeRuntimeCallingConvention calling_convention;
5218 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5219 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5220}
5221
5222void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5223 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5224}
5225
Orion Hodson18259d72018-04-12 11:18:23 +01005226void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5227 InvokeRuntimeCallingConvention calling_convention;
5228 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5229 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5230}
5231
5232void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5233 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5234}
5235
David Brazdilcb1c0552015-08-04 16:22:25 +01005236static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005237 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005238}
5239
Alexandre Rames67555f72014-11-18 10:55:16 +00005240void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5241 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005242 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005243 locations->SetOut(Location::RequiresRegister());
5244}
5245
5246void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005247 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5248}
5249
5250void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005251 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005252}
5253
5254void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5255 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005256}
5257
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005258HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5259 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005260 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005261 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005262 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005263 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005264 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005265 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005266 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005267 case HLoadString::LoadKind::kJitTableAddress:
5268 DCHECK(Runtime::Current()->UseJitCompilation());
5269 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005270 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005271 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005272 }
5273 return desired_string_load_kind;
5274}
5275
Alexandre Rames67555f72014-11-18 10:55:16 +00005276void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005277 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005278 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005279 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005280 InvokeRuntimeCallingConvention calling_convention;
5281 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5282 } else {
5283 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005284 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5285 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005286 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005287 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005288 } else {
5289 // For non-Baker read barrier we have a temp-clobbering call.
5290 }
5291 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005292 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005293}
5294
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005295// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5296// move.
5297void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005298 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005299 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005300
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005301 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005302 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005303 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005304 // Add ADRP with its PC-relative String patch.
5305 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005306 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005307 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005308 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005309 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005310 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005311 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005312 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005313 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005314 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005315 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005316 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005317 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5318 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5319 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005320 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005321 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005322 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005323 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005324 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5325 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005326 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005327 case HLoadString::LoadKind::kBssEntry: {
5328 // Add ADRP with its PC-relative String .bss entry patch.
5329 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005330 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005331 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof3c52b42017-11-17 17:32:12 +00005332 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005333 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005334 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005335 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005336 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005337 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005338 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005339 codegen_->GenerateGcRootFieldLoad(load,
5340 out_loc,
5341 temp,
5342 /* offset placeholder */ 0u,
5343 ldr_label,
5344 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005345 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005346 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005347 codegen_->AddSlowPath(slow_path);
5348 __ Cbz(out.X(), slow_path->GetEntryLabel());
5349 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005350 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005351 return;
5352 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005353 case HLoadString::LoadKind::kJitBootImageAddress: {
5354 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5355 DCHECK_NE(address, 0u);
5356 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5357 return;
5358 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005359 case HLoadString::LoadKind::kJitTableAddress: {
5360 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005361 load->GetStringIndex(),
5362 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005363 codegen_->GenerateGcRootFieldLoad(load,
5364 out_loc,
5365 out.X(),
5366 /* offset */ 0,
5367 /* fixup_label */ nullptr,
5368 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005369 return;
5370 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005371 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005372 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005373 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005374
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005375 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005376 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005377 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005378 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005379 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5380 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005381 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005382}
5383
Alexandre Rames5319def2014-10-23 10:03:10 +01005384void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005385 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005386 locations->SetOut(Location::ConstantLocation(constant));
5387}
5388
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005389void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005390 // Will be generated at use site.
5391}
5392
Alexandre Rames67555f72014-11-18 10:55:16 +00005393void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005394 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5395 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005396 InvokeRuntimeCallingConvention calling_convention;
5397 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5398}
5399
5400void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005401 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005402 instruction,
5403 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005404 if (instruction->IsEnter()) {
5405 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5406 } else {
5407 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5408 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005409 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005410}
5411
Alexandre Rames42d641b2014-10-27 14:00:51 +00005412void LocationsBuilderARM64::VisitMul(HMul* mul) {
5413 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005414 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005415 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005416 case DataType::Type::kInt32:
5417 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005418 locations->SetInAt(0, Location::RequiresRegister());
5419 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005421 break;
5422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005423 case DataType::Type::kFloat32:
5424 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005425 locations->SetInAt(0, Location::RequiresFpuRegister());
5426 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005427 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005428 break;
5429
5430 default:
5431 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5432 }
5433}
5434
5435void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5436 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005437 case DataType::Type::kInt32:
5438 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005439 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5440 break;
5441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 case DataType::Type::kFloat32:
5443 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005444 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005445 break;
5446
5447 default:
5448 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5449 }
5450}
5451
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005452void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5453 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005454 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005455 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005456 case DataType::Type::kInt32:
5457 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005458 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005460 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005462 case DataType::Type::kFloat32:
5463 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005464 locations->SetInAt(0, Location::RequiresFpuRegister());
5465 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005466 break;
5467
5468 default:
5469 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5470 }
5471}
5472
5473void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5474 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 case DataType::Type::kInt32:
5476 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005477 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5478 break;
5479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 case DataType::Type::kFloat32:
5481 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005482 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005483 break;
5484
5485 default:
5486 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5487 }
5488}
5489
5490void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005491 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5492 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005493 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005494 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005495 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5496 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005497}
5498
5499void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005500 // Note: if heap poisoning is enabled, the entry point takes cares
5501 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005502 QuickEntrypointEnum entrypoint =
5503 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5504 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005505 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005506 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005507}
5508
Alexandre Rames5319def2014-10-23 10:03:10 +01005509void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005510 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5511 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005512 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005513 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005514 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005515}
5516
5517void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005518 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5519 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005520 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005521}
5522
5523void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005524 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005525 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005526 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005527}
5528
5529void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005530 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005531 case DataType::Type::kInt32:
5532 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005533 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005534 break;
5535
5536 default:
5537 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5538 }
5539}
5540
David Brazdil66d126e2015-04-03 16:02:44 +01005541void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005542 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005543 locations->SetInAt(0, Location::RequiresRegister());
5544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5545}
5546
5547void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005548 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005549}
5550
Alexandre Rames5319def2014-10-23 10:03:10 +01005551void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005552 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5553 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005554}
5555
Calin Juravle2ae48182016-03-16 14:05:09 +00005556void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5557 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005558 return;
5559 }
Artem Serov914d7a82017-02-07 14:33:49 +00005560 {
5561 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5562 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5563 Location obj = instruction->GetLocations()->InAt(0);
5564 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5565 RecordPcInfo(instruction, instruction->GetDexPc());
5566 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005567}
5568
Calin Juravle2ae48182016-03-16 14:05:09 +00005569void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005570 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005571 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005572
5573 LocationSummary* locations = instruction->GetLocations();
5574 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005575
5576 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005577}
5578
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005579void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005580 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005581}
5582
Alexandre Rames67555f72014-11-18 10:55:16 +00005583void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5584 HandleBinaryOp(instruction);
5585}
5586
5587void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5588 HandleBinaryOp(instruction);
5589}
5590
Alexandre Rames3e69f162014-12-10 10:36:50 +00005591void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5592 LOG(FATAL) << "Unreachable";
5593}
5594
5595void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005596 if (instruction->GetNext()->IsSuspendCheck() &&
5597 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5598 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5599 // The back edge will generate the suspend check.
5600 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5601 }
5602
Alexandre Rames3e69f162014-12-10 10:36:50 +00005603 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5604}
5605
Alexandre Rames5319def2014-10-23 10:03:10 +01005606void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005607 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005608 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5609 if (location.IsStackSlot()) {
5610 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5611 } else if (location.IsDoubleStackSlot()) {
5612 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5613 }
5614 locations->SetOut(location);
5615}
5616
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005617void InstructionCodeGeneratorARM64::VisitParameterValue(
5618 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005619 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005620}
5621
5622void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5623 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005624 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005625 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005626}
5627
5628void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5629 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5630 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005631}
5632
5633void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005634 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005635 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005636 locations->SetInAt(i, Location::Any());
5637 }
5638 locations->SetOut(Location::Any());
5639}
5640
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005641void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005642 LOG(FATAL) << "Unreachable";
5643}
5644
Serban Constantinescu02164b32014-11-13 14:05:07 +00005645void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005646 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005647 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005648 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005649 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005650 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005651
5652 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005653 case DataType::Type::kInt32:
5654 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005655 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005656 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005657 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5658 break;
5659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005660 case DataType::Type::kFloat32:
5661 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005662 InvokeRuntimeCallingConvention calling_convention;
5663 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5664 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5665 locations->SetOut(calling_convention.GetReturnLocation(type));
5666
5667 break;
5668 }
5669
Serban Constantinescu02164b32014-11-13 14:05:07 +00005670 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005671 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005672 }
5673}
5674
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005675void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005676 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005677 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5678 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5679
5680 Register out = OutputRegister(instruction);
5681 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005682
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005683 if (abs_imm == 2) {
5684 __ Cmp(dividend, 0);
5685 __ And(out, dividend, 1);
5686 __ Csneg(out, out, out, ge);
5687 } else {
5688 UseScratchRegisterScope temps(GetVIXLAssembler());
5689 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005690
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005691 __ Negs(temp, dividend);
5692 __ And(out, dividend, abs_imm - 1);
5693 __ And(temp, temp, abs_imm - 1);
5694 __ Csneg(out, out, temp, mi);
5695 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005696}
5697
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005698void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005699 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005700
5701 if (imm == 0) {
5702 // Do not generate anything.
5703 // DivZeroCheck would prevent any code to be executed.
5704 return;
5705 }
5706
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005707 if (IsPowerOfTwo(AbsOrMin(imm))) {
5708 // Cases imm == -1 or imm == 1 are handled in constant folding by
5709 // InstructionWithAbsorbingInputSimplifier.
5710 // If the cases have survided till code generation they are handled in
5711 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5712 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005713 GenerateIntRemForPower2Denom(instruction);
5714 } else {
5715 DCHECK(imm < -2 || imm > 2) << imm;
5716 GenerateDivRemWithAnyConstant(instruction);
5717 }
5718}
5719
5720void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5721 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5722 << instruction->GetResultType();
5723
5724 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5725 GenerateIntRemForConstDenom(instruction);
5726 } else {
5727 Register out = OutputRegister(instruction);
5728 Register dividend = InputRegisterAt(instruction, 0);
5729 Register divisor = InputRegisterAt(instruction, 1);
5730 UseScratchRegisterScope temps(GetVIXLAssembler());
5731 Register temp = temps.AcquireSameSizeAs(out);
5732 __ Sdiv(temp, dividend, divisor);
5733 __ Msub(out, temp, divisor, dividend);
5734 }
5735}
5736
Serban Constantinescu02164b32014-11-13 14:05:07 +00005737void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005739
Serban Constantinescu02164b32014-11-13 14:05:07 +00005740 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 case DataType::Type::kInt32:
5742 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005743 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005744 break;
5745 }
5746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 case DataType::Type::kFloat32:
5748 case DataType::Type::kFloat64: {
5749 QuickEntrypointEnum entrypoint =
5750 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005751 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005753 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5754 } else {
5755 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5756 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005757 break;
5758 }
5759
Serban Constantinescu02164b32014-11-13 14:05:07 +00005760 default:
5761 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005762 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005763 }
5764}
5765
Aart Bik1f8d51b2018-02-15 10:42:37 -08005766void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005767 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005768}
5769
Aart Bik1f8d51b2018-02-15 10:42:37 -08005770void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005771 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005772}
5773
5774void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005775 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005776}
5777
5778void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005779 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005780}
5781
Aart Bik3dad3412018-02-28 12:01:46 -08005782void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5783 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5784 switch (abs->GetResultType()) {
5785 case DataType::Type::kInt32:
5786 case DataType::Type::kInt64:
5787 locations->SetInAt(0, Location::RequiresRegister());
5788 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5789 break;
5790 case DataType::Type::kFloat32:
5791 case DataType::Type::kFloat64:
5792 locations->SetInAt(0, Location::RequiresFpuRegister());
5793 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5794 break;
5795 default:
5796 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5797 }
5798}
5799
5800void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5801 switch (abs->GetResultType()) {
5802 case DataType::Type::kInt32:
5803 case DataType::Type::kInt64: {
5804 Register in_reg = InputRegisterAt(abs, 0);
5805 Register out_reg = OutputRegister(abs);
5806 __ Cmp(in_reg, Operand(0));
5807 __ Cneg(out_reg, in_reg, lt);
5808 break;
5809 }
5810 case DataType::Type::kFloat32:
5811 case DataType::Type::kFloat64: {
5812 FPRegister in_reg = InputFPRegisterAt(abs, 0);
5813 FPRegister out_reg = OutputFPRegister(abs);
5814 __ Fabs(out_reg, in_reg);
5815 break;
5816 }
5817 default:
5818 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5819 }
5820}
5821
Igor Murashkind01745e2017-04-05 16:40:31 -07005822void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5823 constructor_fence->SetLocations(nullptr);
5824}
5825
5826void InstructionCodeGeneratorARM64::VisitConstructorFence(
5827 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5828 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5829}
5830
Calin Juravle27df7582015-04-17 19:12:31 +01005831void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5832 memory_barrier->SetLocations(nullptr);
5833}
5834
5835void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005836 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005837}
5838
Alexandre Rames5319def2014-10-23 10:03:10 +01005839void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005840 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005841 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005842 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005843}
5844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005845void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005846 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005847}
5848
5849void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5850 instruction->SetLocations(nullptr);
5851}
5852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005853void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005854 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005855}
5856
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005857void LocationsBuilderARM64::VisitRor(HRor* ror) {
5858 HandleBinaryOp(ror);
5859}
5860
5861void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5862 HandleBinaryOp(ror);
5863}
5864
Serban Constantinescu02164b32014-11-13 14:05:07 +00005865void LocationsBuilderARM64::VisitShl(HShl* shl) {
5866 HandleShift(shl);
5867}
5868
5869void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5870 HandleShift(shl);
5871}
5872
5873void LocationsBuilderARM64::VisitShr(HShr* shr) {
5874 HandleShift(shr);
5875}
5876
5877void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5878 HandleShift(shr);
5879}
5880
Alexandre Rames5319def2014-10-23 10:03:10 +01005881void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005882 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005883}
5884
5885void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005886 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005887}
5888
Alexandre Rames67555f72014-11-18 10:55:16 +00005889void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005890 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005891}
5892
5893void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005894 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005895}
5896
5897void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005898 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005899}
5900
Alexandre Rames67555f72014-11-18 10:55:16 +00005901void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005902 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005903}
5904
Calin Juravlee460d1d2015-09-29 04:52:17 +01005905void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5906 HUnresolvedInstanceFieldGet* instruction) {
5907 FieldAccessCallingConventionARM64 calling_convention;
5908 codegen_->CreateUnresolvedFieldLocationSummary(
5909 instruction, instruction->GetFieldType(), calling_convention);
5910}
5911
5912void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5913 HUnresolvedInstanceFieldGet* instruction) {
5914 FieldAccessCallingConventionARM64 calling_convention;
5915 codegen_->GenerateUnresolvedFieldAccess(instruction,
5916 instruction->GetFieldType(),
5917 instruction->GetFieldIndex(),
5918 instruction->GetDexPc(),
5919 calling_convention);
5920}
5921
5922void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5923 HUnresolvedInstanceFieldSet* instruction) {
5924 FieldAccessCallingConventionARM64 calling_convention;
5925 codegen_->CreateUnresolvedFieldLocationSummary(
5926 instruction, instruction->GetFieldType(), calling_convention);
5927}
5928
5929void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5930 HUnresolvedInstanceFieldSet* instruction) {
5931 FieldAccessCallingConventionARM64 calling_convention;
5932 codegen_->GenerateUnresolvedFieldAccess(instruction,
5933 instruction->GetFieldType(),
5934 instruction->GetFieldIndex(),
5935 instruction->GetDexPc(),
5936 calling_convention);
5937}
5938
5939void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5940 HUnresolvedStaticFieldGet* instruction) {
5941 FieldAccessCallingConventionARM64 calling_convention;
5942 codegen_->CreateUnresolvedFieldLocationSummary(
5943 instruction, instruction->GetFieldType(), calling_convention);
5944}
5945
5946void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5947 HUnresolvedStaticFieldGet* instruction) {
5948 FieldAccessCallingConventionARM64 calling_convention;
5949 codegen_->GenerateUnresolvedFieldAccess(instruction,
5950 instruction->GetFieldType(),
5951 instruction->GetFieldIndex(),
5952 instruction->GetDexPc(),
5953 calling_convention);
5954}
5955
5956void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5957 HUnresolvedStaticFieldSet* instruction) {
5958 FieldAccessCallingConventionARM64 calling_convention;
5959 codegen_->CreateUnresolvedFieldLocationSummary(
5960 instruction, instruction->GetFieldType(), calling_convention);
5961}
5962
5963void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5964 HUnresolvedStaticFieldSet* instruction) {
5965 FieldAccessCallingConventionARM64 calling_convention;
5966 codegen_->GenerateUnresolvedFieldAccess(instruction,
5967 instruction->GetFieldType(),
5968 instruction->GetFieldIndex(),
5969 instruction->GetDexPc(),
5970 calling_convention);
5971}
5972
Alexandre Rames5319def2014-10-23 10:03:10 +01005973void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005974 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5975 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005976 // In suspend check slow path, usually there are no caller-save registers at all.
5977 // If SIMD instructions are present, however, we force spilling all live SIMD
5978 // registers in full width (since the runtime only saves/restores lower part).
5979 locations->SetCustomSlowPathCallerSaves(
5980 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005981}
5982
5983void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005984 HBasicBlock* block = instruction->GetBlock();
5985 if (block->GetLoopInformation() != nullptr) {
5986 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5987 // The back edge will generate the suspend check.
5988 return;
5989 }
5990 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5991 // The goto will generate the suspend check.
5992 return;
5993 }
5994 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005995 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005996}
5997
Alexandre Rames67555f72014-11-18 10:55:16 +00005998void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005999 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6000 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00006001 InvokeRuntimeCallingConvention calling_convention;
6002 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
6003}
6004
6005void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00006006 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08006007 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00006008}
6009
6010void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
6011 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006012 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006013 DataType::Type input_type = conversion->GetInputType();
6014 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006015 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6016 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006017 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6018 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006019 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6020 }
6021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006022 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006023 locations->SetInAt(0, Location::RequiresFpuRegister());
6024 } else {
6025 locations->SetInAt(0, Location::RequiresRegister());
6026 }
6027
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006028 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006029 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6030 } else {
6031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6032 }
6033}
6034
6035void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006036 DataType::Type result_type = conversion->GetResultType();
6037 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006038
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006039 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6040 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006041
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6043 int result_size = DataType::Size(result_type);
6044 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006045 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006046 Register output = OutputRegister(conversion);
6047 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006048 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006049 // 'int' values are used directly as W registers, discarding the top
6050 // bits, so we don't need to sign-extend and can just perform a move.
6051 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6052 // top 32 bits of the target register. We theoretically could leave those
6053 // bits unchanged, but we would have to make sure that no code uses a
6054 // 32bit input value as a 64bit value assuming that the top 32 bits are
6055 // zero.
6056 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006057 } else if (DataType::IsUnsignedType(result_type) ||
6058 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6059 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006060 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006061 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006062 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006063 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006064 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006065 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6066 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006067 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006068 } else if (DataType::IsFloatingPointType(result_type) &&
6069 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006070 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6071 } else {
6072 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6073 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006074 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006075}
Alexandre Rames67555f72014-11-18 10:55:16 +00006076
Serban Constantinescu02164b32014-11-13 14:05:07 +00006077void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6078 HandleShift(ushr);
6079}
6080
6081void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6082 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006083}
6084
6085void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6086 HandleBinaryOp(instruction);
6087}
6088
6089void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6090 HandleBinaryOp(instruction);
6091}
6092
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006093void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006094 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006095 LOG(FATAL) << "Unreachable";
6096}
6097
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006098void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006099 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006100 LOG(FATAL) << "Unreachable";
6101}
6102
Mark Mendellfe57faa2015-09-18 09:26:15 -04006103// Simple implementation of packed switch - generate cascaded compare/jumps.
6104void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6105 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006106 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006107 locations->SetInAt(0, Location::RequiresRegister());
6108}
6109
6110void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6111 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006112 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006113 Register value_reg = InputRegisterAt(switch_instr, 0);
6114 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6115
Zheng Xu3927c8b2015-11-18 17:46:25 +08006116 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006117 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006118 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6119 // make sure we don't emit it if the target may run out of range.
6120 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6121 // ranges and emit the tables only as required.
6122 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006123
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006124 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006125 // Current instruction id is an upper bound of the number of HIRs in the graph.
6126 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6127 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006128 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6129 Register temp = temps.AcquireW();
6130 __ Subs(temp, value_reg, Operand(lower_bound));
6131
Zheng Xu3927c8b2015-11-18 17:46:25 +08006132 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006133 // Jump to successors[0] if value == lower_bound.
6134 __ B(eq, codegen_->GetLabelOf(successors[0]));
6135 int32_t last_index = 0;
6136 for (; num_entries - last_index > 2; last_index += 2) {
6137 __ Subs(temp, temp, Operand(2));
6138 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6139 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6140 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6141 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6142 }
6143 if (num_entries - last_index == 2) {
6144 // The last missing case_value.
6145 __ Cmp(temp, Operand(1));
6146 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006147 }
6148
6149 // And the default for any other value.
6150 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6151 __ B(codegen_->GetLabelOf(default_block));
6152 }
6153 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006154 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006155
6156 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6157
6158 // Below instructions should use at most one blocked register. Since there are two blocked
6159 // registers, we are free to block one.
6160 Register temp_w = temps.AcquireW();
6161 Register index;
6162 // Remove the bias.
6163 if (lower_bound != 0) {
6164 index = temp_w;
6165 __ Sub(index, value_reg, Operand(lower_bound));
6166 } else {
6167 index = value_reg;
6168 }
6169
6170 // Jump to default block if index is out of the range.
6171 __ Cmp(index, Operand(num_entries));
6172 __ B(hs, codegen_->GetLabelOf(default_block));
6173
6174 // In current VIXL implementation, it won't require any blocked registers to encode the
6175 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6176 // register pressure.
6177 Register table_base = temps.AcquireX();
6178 // Load jump offset from the table.
6179 __ Adr(table_base, jump_table->GetTableStartLabel());
6180 Register jump_offset = temp_w;
6181 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6182
6183 // Jump to target block by branching to table_base(pc related) + offset.
6184 Register target_address = table_base;
6185 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6186 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006187 }
6188}
6189
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006190void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6191 HInstruction* instruction,
6192 Location out,
6193 uint32_t offset,
6194 Location maybe_temp,
6195 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006196 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006197 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006198 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006199 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006200 if (kUseBakerReadBarrier) {
6201 // Load with fast path based Baker's read barrier.
6202 // /* HeapReference<Object> */ out = *(out + offset)
6203 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6204 out,
6205 out_reg,
6206 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006207 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006208 /* needs_null_check */ false,
6209 /* use_load_acquire */ false);
6210 } else {
6211 // Load with slow path based read barrier.
6212 // Save the value of `out` into `maybe_temp` before overwriting it
6213 // in the following move operation, as we will need it for the
6214 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006215 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006216 __ Mov(temp_reg, out_reg);
6217 // /* HeapReference<Object> */ out = *(out + offset)
6218 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6219 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6220 }
6221 } else {
6222 // Plain load with no read barrier.
6223 // /* HeapReference<Object> */ out = *(out + offset)
6224 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6225 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6226 }
6227}
6228
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006229void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6230 HInstruction* instruction,
6231 Location out,
6232 Location obj,
6233 uint32_t offset,
6234 Location maybe_temp,
6235 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006237 Register out_reg = RegisterFrom(out, type);
6238 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006239 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006240 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006241 if (kUseBakerReadBarrier) {
6242 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006243 // /* HeapReference<Object> */ out = *(obj + offset)
6244 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6245 out,
6246 obj_reg,
6247 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006248 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006249 /* needs_null_check */ false,
6250 /* use_load_acquire */ false);
6251 } else {
6252 // Load with slow path based read barrier.
6253 // /* HeapReference<Object> */ out = *(obj + offset)
6254 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6255 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6256 }
6257 } else {
6258 // Plain load with no read barrier.
6259 // /* HeapReference<Object> */ out = *(obj + offset)
6260 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6261 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6262 }
6263}
6264
Vladimir Markoca1e0382018-04-11 09:58:41 +00006265void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006266 HInstruction* instruction,
6267 Location root,
6268 Register obj,
6269 uint32_t offset,
6270 vixl::aarch64::Label* fixup_label,
6271 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006272 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006273 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006274 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006275 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006276 if (kUseBakerReadBarrier) {
6277 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006278 // Baker's read barrier are used.
Vladimir Marko966b46f2018-08-03 10:20:19 +00006279 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
Roland Levillain97c46462017-05-11 14:04:03 +01006280 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6281 // the Marking Register) to decide whether we need to enter
6282 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006283 //
Vladimir Marko966b46f2018-08-03 10:20:19 +00006284 // We use shared thunks for the slow path; shared within the method
6285 // for JIT, across methods for AOT. That thunk checks the reference
6286 // and jumps to the entrypoint if needed.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006287 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006288 // lr = &return_address;
6289 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006290 // if (mr) { // Thread::Current()->GetIsGcMarking()
6291 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006292 // }
6293 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00006294
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006295 UseScratchRegisterScope temps(GetVIXLAssembler());
6296 DCHECK(temps.IsAvailable(ip0));
6297 DCHECK(temps.IsAvailable(ip1));
6298 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006299 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillainba650a42017-03-06 13:52:32 +00006300
Vladimir Marko966b46f2018-08-03 10:20:19 +00006301 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006302 vixl::aarch64::Label return_address;
6303 __ adr(lr, &return_address);
6304 if (fixup_label != nullptr) {
Vladimir Marko966b46f2018-08-03 10:20:19 +00006305 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006306 }
6307 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6308 "GC root LDR must be 2 instruction (8B) before the return address label.");
6309 __ ldr(root_reg, MemOperand(obj.X(), offset));
Vladimir Marko966b46f2018-08-03 10:20:19 +00006310 EmitBakerReadBarrierCbnz(custom_data);
6311 __ bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006312 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006313 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6314 // the Marking Register) to decide whether we need to enter
6315 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006316 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006317 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006318 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006319 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006320 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6321 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006322 // }
Roland Levillain44015862016-01-22 11:47:17 +00006323
Roland Levillain97c46462017-05-11 14:04:03 +01006324 // Slow path marking the GC root `root`. The entrypoint will
6325 // be loaded by the slow path code.
6326 SlowPathCodeARM64* slow_path =
Vladimir Markoca1e0382018-04-11 09:58:41 +00006327 new (GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
6328 AddSlowPath(slow_path);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006329
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006330 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6331 if (fixup_label == nullptr) {
6332 __ Ldr(root_reg, MemOperand(obj, offset));
6333 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006334 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006335 }
6336 static_assert(
6337 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6338 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6339 "have different sizes.");
6340 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6341 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6342 "have different sizes.");
6343
Roland Levillain97c46462017-05-11 14:04:03 +01006344 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006345 __ Bind(slow_path->GetExitLabel());
6346 }
Roland Levillain44015862016-01-22 11:47:17 +00006347 } else {
6348 // GC root loaded through a slow path for read barriers other
6349 // than Baker's.
6350 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351 if (fixup_label == nullptr) {
6352 __ Add(root_reg.X(), obj.X(), offset);
6353 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006354 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006355 }
Roland Levillain44015862016-01-22 11:47:17 +00006356 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006357 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006358 }
6359 } else {
6360 // Plain GC root load with no read barrier.
6361 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006362 if (fixup_label == nullptr) {
6363 __ Ldr(root_reg, MemOperand(obj, offset));
6364 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006365 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006366 }
Roland Levillain44015862016-01-22 11:47:17 +00006367 // Note that GC roots are not affected by heap poisoning, thus we
6368 // do not have to unpoison `root_reg` here.
6369 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006370 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006371}
6372
6373void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6374 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006375 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006376 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006377 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006378 bool needs_null_check,
6379 bool use_load_acquire) {
6380 DCHECK(kEmitCompilerReadBarrier);
6381 DCHECK(kUseBakerReadBarrier);
6382
Vladimir Marko966b46f2018-08-03 10:20:19 +00006383 if (kBakerReadBarrierLinkTimeThunksEnableForFields && !use_load_acquire) {
Roland Levillain97c46462017-05-11 14:04:03 +01006384 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6385 // Marking Register) to decide whether we need to enter the slow
6386 // path to mark the reference. Then, in the slow path, check the
6387 // gray bit in the lock word of the reference's holder (`obj`) to
6388 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006389 //
Vladimir Marko966b46f2018-08-03 10:20:19 +00006390 // We use shared thunks for the slow path; shared within the method
6391 // for JIT, across methods for AOT. That thunk checks the holder
6392 // and jumps to the entrypoint if needed. If the holder is not gray,
6393 // it creates a fake dependency and returns to the LDR instruction.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006394 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006395 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006396 // if (mr) { // Thread::Current()->GetIsGcMarking()
6397 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006398 // }
6399 // not_gray_return_address:
6400 // // Original reference load. If the offset is too large to fit
6401 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006402 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006403 // gray_return_address:
6404
6405 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6406 Register base = obj;
6407 if (offset >= kReferenceLoadMinFarOffset) {
6408 DCHECK(maybe_temp.IsRegister());
6409 base = WRegisterFrom(maybe_temp);
6410 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6411 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6412 offset &= (kReferenceLoadMinFarOffset - 1u);
6413 }
6414 UseScratchRegisterScope temps(GetVIXLAssembler());
6415 DCHECK(temps.IsAvailable(ip0));
6416 DCHECK(temps.IsAvailable(ip1));
6417 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006418 uint32_t custom_data = EncodeBakerReadBarrierFieldData(base.GetCode(), obj.GetCode());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006419
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006420 {
Vladimir Marko966b46f2018-08-03 10:20:19 +00006421 ExactAssemblyScope guard(GetVIXLAssembler(),
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006422 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6423 vixl::aarch64::Label return_address;
6424 __ adr(lr, &return_address);
Vladimir Marko966b46f2018-08-03 10:20:19 +00006425 EmitBakerReadBarrierCbnz(custom_data);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006426 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6427 "Field LDR must be 1 instruction (4B) before the return address label; "
6428 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006429 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006430 __ ldr(ref_reg, MemOperand(base.X(), offset));
6431 if (needs_null_check) {
6432 MaybeRecordImplicitNullCheck(instruction);
6433 }
Vladimir Marko966b46f2018-08-03 10:20:19 +00006434 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6435 // macro instructions disallowed in ExactAssemblyScope.
6436 if (kPoisonHeapReferences) {
6437 __ neg(ref_reg, Operand(ref_reg));
6438 }
6439 __ bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006440 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006441 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006442 return;
6443 }
6444
Roland Levillain44015862016-01-22 11:47:17 +00006445 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006446 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006447 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006448 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006449 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6450 ref,
6451 obj,
6452 offset,
6453 no_index,
6454 no_scale_factor,
6455 temp,
6456 needs_null_check,
6457 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006458}
6459
6460void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6461 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006462 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006463 uint32_t data_offset,
6464 Location index,
6465 Register temp,
6466 bool needs_null_check) {
6467 DCHECK(kEmitCompilerReadBarrier);
6468 DCHECK(kUseBakerReadBarrier);
6469
Vladimir Marko66d691d2017-04-07 17:53:39 +01006470 static_assert(
6471 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6472 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006473 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006474
Vladimir Marko966b46f2018-08-03 10:20:19 +00006475 if (kBakerReadBarrierLinkTimeThunksEnableForArrays) {
Roland Levillain97c46462017-05-11 14:04:03 +01006476 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6477 // Marking Register) to decide whether we need to enter the slow
6478 // path to mark the reference. Then, in the slow path, check the
6479 // gray bit in the lock word of the reference's holder (`obj`) to
6480 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006481 //
Vladimir Marko966b46f2018-08-03 10:20:19 +00006482 // We use shared thunks for the slow path; shared within the method
6483 // for JIT, across methods for AOT. That thunk checks the holder
6484 // and jumps to the entrypoint if needed. If the holder is not gray,
6485 // it creates a fake dependency and returns to the LDR instruction.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006486 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006487 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006488 // if (mr) { // Thread::Current()->GetIsGcMarking()
6489 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006490 // }
6491 // not_gray_return_address:
6492 // // Original reference load. If the offset is too large to fit
6493 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006494 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006495 // gray_return_address:
6496
6497 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006498 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6499 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006500
6501 UseScratchRegisterScope temps(GetVIXLAssembler());
6502 DCHECK(temps.IsAvailable(ip0));
6503 DCHECK(temps.IsAvailable(ip1));
6504 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006505 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006506
Vladimir Marko66d691d2017-04-07 17:53:39 +01006507 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006508 {
Vladimir Marko966b46f2018-08-03 10:20:19 +00006509 ExactAssemblyScope guard(GetVIXLAssembler(),
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006510 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6511 vixl::aarch64::Label return_address;
6512 __ adr(lr, &return_address);
Vladimir Marko966b46f2018-08-03 10:20:19 +00006513 EmitBakerReadBarrierCbnz(custom_data);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006514 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6515 "Array LDR must be 1 instruction (4B) before the return address label; "
6516 " 2 instructions (8B) for heap poisoning.");
6517 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6518 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
Vladimir Marko966b46f2018-08-03 10:20:19 +00006519 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6520 // macro instructions disallowed in ExactAssemblyScope.
6521 if (kPoisonHeapReferences) {
6522 __ neg(ref_reg, Operand(ref_reg));
6523 }
6524 __ bind(&return_address);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006525 }
6526 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006527 return;
6528 }
6529
Roland Levillain44015862016-01-22 11:47:17 +00006530 // Array cells are never volatile variables, therefore array loads
6531 // never use Load-Acquire instructions on ARM64.
6532 const bool use_load_acquire = false;
6533
6534 // /* HeapReference<Object> */ ref =
6535 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006536 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6537 ref,
6538 obj,
6539 data_offset,
6540 index,
6541 scale_factor,
6542 temp,
6543 needs_null_check,
6544 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006545}
6546
6547void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6548 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006549 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006550 uint32_t offset,
6551 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006552 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006553 Register temp,
6554 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006555 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006556 DCHECK(kEmitCompilerReadBarrier);
6557 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006558 // If we are emitting an array load, we should not be using a
6559 // Load Acquire instruction. In other words:
6560 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6561 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006562
Roland Levillain97c46462017-05-11 14:04:03 +01006563 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6564 // Marking Register) to decide whether we need to enter the slow
6565 // path to mark the reference. Then, in the slow path, check the
6566 // gray bit in the lock word of the reference's holder (`obj`) to
6567 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006568 //
Roland Levillain97c46462017-05-11 14:04:03 +01006569 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006570 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006571 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6572 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6573 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6574 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6575 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006576 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6577 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006578 // }
6579 // } else {
6580 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006581 // }
Roland Levillain44015862016-01-22 11:47:17 +00006582
Roland Levillainba650a42017-03-06 13:52:32 +00006583 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006584 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006585 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006586 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006587 instruction,
6588 ref,
6589 obj,
6590 offset,
6591 index,
6592 scale_factor,
6593 needs_null_check,
6594 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006595 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006596 AddSlowPath(slow_path);
6597
Roland Levillain97c46462017-05-11 14:04:03 +01006598 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006599 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006600 GenerateRawReferenceLoad(
6601 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006602 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006603 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006604}
6605
Roland Levillainff487002017-03-07 16:50:01 +00006606void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6607 Location ref,
6608 Register obj,
6609 Location field_offset,
6610 Register temp,
6611 bool needs_null_check,
6612 bool use_load_acquire) {
6613 DCHECK(kEmitCompilerReadBarrier);
6614 DCHECK(kUseBakerReadBarrier);
6615 // If we are emitting an array load, we should not be using a
6616 // Load Acquire instruction. In other words:
6617 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6618 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6619
Roland Levillain97c46462017-05-11 14:04:03 +01006620 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6621 // Marking Register) to decide whether we need to enter the slow
6622 // path to update the reference field within `obj`. Then, in the
6623 // slow path, check the gray bit in the lock word of the reference's
6624 // holder (`obj`) to decide whether to mark `ref` and update the
6625 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006626 //
Roland Levillain97c46462017-05-11 14:04:03 +01006627 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006628 // // Slow path.
6629 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6630 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6631 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6632 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6633 // if (is_gray) {
6634 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006635 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6636 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006637 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6638 // }
6639 // }
6640
6641 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006642 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006643 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006644 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006645 instruction,
6646 ref,
6647 obj,
6648 /* offset */ 0u,
6649 /* index */ field_offset,
6650 /* scale_factor */ 0u /* "times 1" */,
6651 needs_null_check,
6652 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006653 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006654 AddSlowPath(slow_path);
6655
Roland Levillain97c46462017-05-11 14:04:03 +01006656 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006657 // Fast path: the GC is not marking: nothing to do (the field is
6658 // up-to-date, and we don't need to load the reference).
6659 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006660 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006661}
6662
Roland Levillainba650a42017-03-06 13:52:32 +00006663void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6664 Location ref,
6665 Register obj,
6666 uint32_t offset,
6667 Location index,
6668 size_t scale_factor,
6669 bool needs_null_check,
6670 bool use_load_acquire) {
6671 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006672 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006673 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006674
Roland Levillainba650a42017-03-06 13:52:32 +00006675 // If needed, vixl::EmissionCheckScope guards are used to ensure
6676 // that no pools are emitted between the load (macro) instruction
6677 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006678
Roland Levillain44015862016-01-22 11:47:17 +00006679 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006680 // Load types involving an "index": ArrayGet,
6681 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6682 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006683 if (use_load_acquire) {
6684 // UnsafeGetObjectVolatile intrinsic case.
6685 // Register `index` is not an index in an object array, but an
6686 // offset to an object reference field within object `obj`.
6687 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6688 DCHECK(instruction->GetLocations()->Intrinsified());
6689 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6690 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006691 DCHECK_EQ(offset, 0u);
6692 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006693 DCHECK_EQ(needs_null_check, false);
6694 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006695 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6696 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006697 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006698 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6699 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006700 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01006701 uint32_t computed_offset = offset + (Int64FromLocation(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006702 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006703 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006704 if (needs_null_check) {
6705 MaybeRecordImplicitNullCheck(instruction);
6706 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006707 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006708 UseScratchRegisterScope temps(GetVIXLAssembler());
6709 Register temp = temps.AcquireW();
6710 __ Add(temp, obj, offset);
6711 {
6712 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6713 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6714 if (needs_null_check) {
6715 MaybeRecordImplicitNullCheck(instruction);
6716 }
6717 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006718 }
Roland Levillain44015862016-01-22 11:47:17 +00006719 }
Roland Levillain44015862016-01-22 11:47:17 +00006720 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006721 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006722 MemOperand field = HeapOperand(obj, offset);
6723 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006724 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6725 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006726 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006727 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006728 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006729 if (needs_null_check) {
6730 MaybeRecordImplicitNullCheck(instruction);
6731 }
Roland Levillain44015862016-01-22 11:47:17 +00006732 }
6733 }
6734
6735 // Object* ref = ref_addr->AsMirrorPtr()
6736 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006737}
6738
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006739void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6740 // The following condition is a compile-time one, so it does not have a run-time cost.
6741 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6742 // The following condition is a run-time one; it is executed after the
6743 // previous compile-time test, to avoid penalizing non-debug builds.
6744 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6745 UseScratchRegisterScope temps(GetVIXLAssembler());
6746 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6747 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6748 }
6749 }
6750}
6751
Roland Levillain44015862016-01-22 11:47:17 +00006752void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6753 Location out,
6754 Location ref,
6755 Location obj,
6756 uint32_t offset,
6757 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006758 DCHECK(kEmitCompilerReadBarrier);
6759
Roland Levillain44015862016-01-22 11:47:17 +00006760 // Insert a slow path based read barrier *after* the reference load.
6761 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006762 // If heap poisoning is enabled, the unpoisoning of the loaded
6763 // reference will be carried out by the runtime within the slow
6764 // path.
6765 //
6766 // Note that `ref` currently does not get unpoisoned (when heap
6767 // poisoning is enabled), which is alright as the `ref` argument is
6768 // not used by the artReadBarrierSlow entry point.
6769 //
6770 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006771 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006772 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6773 AddSlowPath(slow_path);
6774
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006775 __ B(slow_path->GetEntryLabel());
6776 __ Bind(slow_path->GetExitLabel());
6777}
6778
Roland Levillain44015862016-01-22 11:47:17 +00006779void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6780 Location out,
6781 Location ref,
6782 Location obj,
6783 uint32_t offset,
6784 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006785 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006786 // Baker's read barriers shall be handled by the fast path
6787 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6788 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006789 // If heap poisoning is enabled, unpoisoning will be taken care of
6790 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006791 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006792 } else if (kPoisonHeapReferences) {
6793 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6794 }
6795}
6796
Roland Levillain44015862016-01-22 11:47:17 +00006797void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6798 Location out,
6799 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006800 DCHECK(kEmitCompilerReadBarrier);
6801
Roland Levillain44015862016-01-22 11:47:17 +00006802 // Insert a slow path based read barrier *after* the GC root load.
6803 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006804 // Note that GC roots are not affected by heap poisoning, so we do
6805 // not need to do anything special for this here.
6806 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006807 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006808 AddSlowPath(slow_path);
6809
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006810 __ B(slow_path->GetEntryLabel());
6811 __ Bind(slow_path->GetExitLabel());
6812}
6813
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006814void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6815 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006816 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006817 locations->SetInAt(0, Location::RequiresRegister());
6818 locations->SetOut(Location::RequiresRegister());
6819}
6820
6821void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6822 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006823 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006824 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006825 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006826 __ Ldr(XRegisterFrom(locations->Out()),
6827 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006828 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006829 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006830 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006831 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6832 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006833 __ Ldr(XRegisterFrom(locations->Out()),
6834 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006835 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006836}
6837
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006838static void PatchJitRootUse(uint8_t* code,
6839 const uint8_t* roots_data,
6840 vixl::aarch64::Literal<uint32_t>* literal,
6841 uint64_t index_in_table) {
6842 uint32_t literal_offset = literal->GetOffset();
6843 uintptr_t address =
6844 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6845 uint8_t* data = code + literal_offset;
6846 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6847}
6848
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006849void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6850 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006851 const StringReference& string_reference = entry.first;
6852 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006853 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006854 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006855 }
6856 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006857 const TypeReference& type_reference = entry.first;
6858 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006859 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006860 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006861 }
6862}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006863
Alexandre Rames67555f72014-11-18 10:55:16 +00006864#undef __
6865#undef QUICK_ENTRY_POINT
6866
Vladimir Markoca1e0382018-04-11 09:58:41 +00006867#define __ assembler.GetVIXLAssembler()->
6868
6869static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6870 vixl::aarch64::Register base_reg,
6871 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006872 vixl::aarch64::Label* slow_path,
6873 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006874 // Load the lock word containing the rb_state.
6875 __ Ldr(ip0.W(), lock_word);
6876 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6877 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6878 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6879 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6880 static_assert(
6881 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6882 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006883 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6884 if (throw_npe != nullptr) {
6885 __ Bind(throw_npe);
6886 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006887 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6888 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6889 "Field LDR must be 1 instruction (4B) before the return address label; "
6890 " 2 instructions (8B) for heap poisoning.");
6891 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6892 // Introduce a dependency on the lock_word including rb_state,
6893 // to prevent load-load reordering, and without using
6894 // a memory barrier (which would be more expensive).
6895 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6896 __ Br(lr); // And return back to the function.
6897 // Note: The fake dependency is unnecessary for the slow path.
6898}
6899
6900// Load the read barrier introspection entrypoint in register `entrypoint`.
6901static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6902 vixl::aarch64::Register entrypoint) {
6903 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6904 DCHECK_EQ(ip0.GetCode(), 16u);
6905 const int32_t entry_point_offset =
6906 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6907 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6908}
6909
6910void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6911 uint32_t encoded_data,
6912 /*out*/ std::string* debug_name) {
6913 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6914 switch (kind) {
6915 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006916 auto base_reg =
6917 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6918 CheckValidReg(base_reg.GetCode());
6919 auto holder_reg =
6920 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6921 CheckValidReg(holder_reg.GetCode());
6922 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6923 temps.Exclude(ip0, ip1);
Vladimir Marko7a695052018-04-12 10:26:50 +01006924 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
6925 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
6926 // null-check the holder as we do not necessarily do that check before going to the thunk.
6927 vixl::aarch64::Label throw_npe_label;
6928 vixl::aarch64::Label* throw_npe = nullptr;
6929 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
6930 throw_npe = &throw_npe_label;
6931 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006932 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006933 // Check if the holder is gray and, if not, add fake dependency to the base register
6934 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6935 // to load the reference and call the entrypoint that performs further checks on the
6936 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006937 vixl::aarch64::Label slow_path;
6938 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006939 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006940 __ Bind(&slow_path);
6941 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6942 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6943 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6944 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6945 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6946 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6947 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006948 break;
6949 }
6950 case BakerReadBarrierKind::kArray: {
6951 auto base_reg =
6952 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6953 CheckValidReg(base_reg.GetCode());
6954 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6955 BakerReadBarrierSecondRegField::Decode(encoded_data));
6956 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6957 temps.Exclude(ip0, ip1);
6958 vixl::aarch64::Label slow_path;
6959 int32_t data_offset =
6960 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6961 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6962 DCHECK_LT(lock_word.GetOffset(), 0);
6963 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6964 __ Bind(&slow_path);
6965 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6966 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6967 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6968 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6969 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6970 // a switch case target based on the index register.
6971 __ Mov(ip0, base_reg); // Move the base register to ip0.
6972 __ Br(ip1); // Jump to the entrypoint's array switch case.
6973 break;
6974 }
6975 case BakerReadBarrierKind::kGcRoot: {
6976 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6977 // and it does not have a forwarding address), call the correct introspection entrypoint;
6978 // otherwise return the reference (or the extracted forwarding address).
6979 // There is no gray bit check for GC roots.
6980 auto root_reg =
6981 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6982 CheckValidReg(root_reg.GetCode());
6983 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6984 BakerReadBarrierSecondRegField::Decode(encoded_data));
6985 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6986 temps.Exclude(ip0, ip1);
6987 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6988 __ Cbz(root_reg, &return_label);
6989 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6990 __ Ldr(ip0.W(), lock_word);
6991 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6992 __ Bind(&return_label);
6993 __ Br(lr);
6994 __ Bind(&not_marked);
6995 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6996 __ B(&forwarding_address, mi);
6997 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6998 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6999 // art_quick_read_barrier_mark_introspection_gc_roots.
7000 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
7001 __ Mov(ip0.W(), root_reg);
7002 __ Br(ip1);
7003 __ Bind(&forwarding_address);
7004 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
7005 __ Br(lr);
7006 break;
7007 }
7008 default:
7009 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
7010 UNREACHABLE();
7011 }
7012
Vladimir Marko966b46f2018-08-03 10:20:19 +00007013 // For JIT, the slow path is considered part of the compiled method,
7014 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
7015 DCHECK(Runtime::Current() == nullptr ||
7016 !Runtime::Current()->UseJitCompilation() ||
7017 debug_name == nullptr);
7018 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00007019 std::ostringstream oss;
7020 oss << "BakerReadBarrierThunk";
7021 switch (kind) {
7022 case BakerReadBarrierKind::kField:
7023 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
7024 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
7025 break;
7026 case BakerReadBarrierKind::kArray:
7027 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7028 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7029 BakerReadBarrierSecondRegField::Decode(encoded_data));
7030 break;
7031 case BakerReadBarrierKind::kGcRoot:
7032 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7033 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7034 BakerReadBarrierSecondRegField::Decode(encoded_data));
7035 break;
7036 }
7037 *debug_name = oss.str();
7038 }
7039}
7040
7041#undef __
7042
Alexandre Rames5319def2014-10-23 10:03:10 +01007043} // namespace arm64
7044} // namespace art