blob: 5a3b22cabdcf1bf8e9e64275eb1374621ed14bd6 [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(),
Nicolas Geoffray756e7222018-08-02 17:53:46 +00001416 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001417 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001418 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001419}
Alexandre Rames5319def2014-10-23 10:03:10 +01001420
Alexandre Rames67555f72014-11-18 10:55:16 +00001421#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001422
Zheng Xu3927c8b2015-11-18 17:46:25 +08001423void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001424 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001425 jump_table->EmitTable(this);
1426 }
1427}
1428
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001429void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001430 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001431 // Ensure we emit the literal pool.
1432 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001433
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001434 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001435
1436 // Verify Baker read barrier linker patches.
1437 if (kIsDebugBuild) {
1438 ArrayRef<const uint8_t> code = allocator->GetMemory();
1439 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1440 DCHECK(info.label.IsBound());
1441 uint32_t literal_offset = info.label.GetLocation();
1442 DCHECK_ALIGNED(literal_offset, 4u);
1443
1444 auto GetInsn = [&code](uint32_t offset) {
1445 DCHECK_ALIGNED(offset, 4u);
1446 return
1447 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1448 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1449 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1450 (static_cast<uint32_t>(code[offset + 3]) << 24);
1451 };
1452
1453 const uint32_t encoded_data = info.custom_data;
1454 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1455 // Check that the next instruction matches the expected LDR.
1456 switch (kind) {
1457 case BakerReadBarrierKind::kField: {
1458 DCHECK_GE(code.size() - literal_offset, 8u);
1459 uint32_t next_insn = GetInsn(literal_offset + 4u);
1460 // LDR (immediate) with correct base_reg.
1461 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1462 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1463 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1464 break;
1465 }
1466 case BakerReadBarrierKind::kArray: {
1467 DCHECK_GE(code.size() - literal_offset, 8u);
1468 uint32_t next_insn = GetInsn(literal_offset + 4u);
1469 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1470 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1471 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1472 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1473 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1474 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1475 break;
1476 }
1477 case BakerReadBarrierKind::kGcRoot: {
1478 DCHECK_GE(literal_offset, 4u);
1479 uint32_t prev_insn = GetInsn(literal_offset - 4u);
1480 // LDR (immediate) with correct root_reg.
1481 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1482 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg);
1483 break;
1484 }
1485 default:
1486 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1487 UNREACHABLE();
1488 }
1489 }
1490 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001491}
1492
Zheng Xuad4450e2015-04-17 18:48:56 +08001493void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1494 // Note: There are 6 kinds of moves:
1495 // 1. constant -> GPR/FPR (non-cycle)
1496 // 2. constant -> stack (non-cycle)
1497 // 3. GPR/FPR -> GPR/FPR
1498 // 4. GPR/FPR -> stack
1499 // 5. stack -> GPR/FPR
1500 // 6. stack -> stack (non-cycle)
1501 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1502 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1503 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1504 // dependency.
1505 vixl_temps_.Open(GetVIXLAssembler());
1506}
1507
1508void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1509 vixl_temps_.Close();
1510}
1511
1512Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001513 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1514 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1515 || kind == Location::kSIMDStackSlot);
1516 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1517 ? Location::kFpuRegister
1518 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001519 Location scratch = GetScratchLocation(kind);
1520 if (!scratch.Equals(Location::NoLocation())) {
1521 return scratch;
1522 }
1523 // Allocate from VIXL temp registers.
1524 if (kind == Location::kRegister) {
1525 scratch = LocationFrom(vixl_temps_.AcquireX());
1526 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001527 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001528 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1529 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1530 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001531 }
1532 AddScratchLocation(scratch);
1533 return scratch;
1534}
1535
1536void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1537 if (loc.IsRegister()) {
1538 vixl_temps_.Release(XRegisterFrom(loc));
1539 } else {
1540 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001541 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001542 }
1543 RemoveScratchLocation(loc);
1544}
1545
Alexandre Rames3e69f162014-12-10 10:36:50 +00001546void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001547 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001548 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001549}
1550
Alexandre Rames5319def2014-10-23 10:03:10 +01001551void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001552 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001553 __ Bind(&frame_entry_label_);
1554
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001555 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1556 UseScratchRegisterScope temps(masm);
1557 Register temp = temps.AcquireX();
1558 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1559 __ Add(temp, temp, 1);
1560 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1561 }
1562
Vladimir Marko33bff252017-11-01 14:35:42 +00001563 bool do_overflow_check =
1564 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001565 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001566 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001567 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001568 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001569 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001570 {
1571 // Ensure that between load and RecordPcInfo there are no pools emitted.
1572 ExactAssemblyScope eas(GetVIXLAssembler(),
1573 kInstructionSize,
1574 CodeBufferCheckScope::kExactSize);
1575 __ ldr(wzr, MemOperand(temp, 0));
1576 RecordPcInfo(nullptr, 0);
1577 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001578 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001579
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001580 if (!HasEmptyFrame()) {
1581 int frame_size = GetFrameSize();
1582 // Stack layout:
1583 // sp[frame_size - 8] : lr.
1584 // ... : other preserved core registers.
1585 // ... : other preserved fp registers.
1586 // ... : reserved frame space.
1587 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001588
1589 // Save the current method if we need it. Note that we do not
1590 // do this in HCurrentMethod, as the instruction might have been removed
1591 // in the SSA graph.
1592 if (RequiresCurrentMethod()) {
1593 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001594 } else {
1595 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001596 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001597 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001598 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1599 frame_size - GetCoreSpillSize());
1600 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1601 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001602
1603 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1604 // Initialize should_deoptimize flag to 0.
1605 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1606 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1607 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001608 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001609
1610 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001611}
1612
1613void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001614 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001615 if (!HasEmptyFrame()) {
1616 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001617 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1618 frame_size - FrameEntrySpillSize());
1619 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1620 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001621 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001622 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001623 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001624 __ Ret();
1625 GetAssembler()->cfi().RestoreState();
1626 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001627}
1628
Scott Wakeling97c72b72016-06-24 16:19:36 +01001629CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001630 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001631 return CPURegList(CPURegister::kRegister, kXRegSize,
1632 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001633}
1634
Scott Wakeling97c72b72016-06-24 16:19:36 +01001635CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001636 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1637 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001638 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1639 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001640}
1641
Alexandre Rames5319def2014-10-23 10:03:10 +01001642void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1643 __ Bind(GetLabelOf(block));
1644}
1645
Calin Juravle175dc732015-08-25 15:42:32 +01001646void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1647 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001648 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001649}
1650
Calin Juravlee460d1d2015-09-29 04:52:17 +01001651void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1652 if (location.IsRegister()) {
1653 locations->AddTemp(location);
1654 } else {
1655 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1656 }
1657}
1658
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001659void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001660 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001661 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001662 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001663 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001664 if (value_can_be_null) {
1665 __ Cbz(value, &done);
1666 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001667 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001668 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001669 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001670 if (value_can_be_null) {
1671 __ Bind(&done);
1672 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001673}
1674
David Brazdil58282f42016-01-14 12:45:10 +00001675void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001676 // Blocked core registers:
1677 // lr : Runtime reserved.
1678 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001679 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001680 // ip1 : VIXL core temp.
1681 // ip0 : VIXL core temp.
1682 //
1683 // Blocked fp registers:
1684 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001685 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1686 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001687 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001688 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001689 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001690
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001691 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001692 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001693 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001694 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001695
David Brazdil58282f42016-01-14 12:45:10 +00001696 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001697 // Stubs do not save callee-save floating point registers. If the graph
1698 // is debuggable, we need to deal with these registers differently. For
1699 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001700 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1701 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001702 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001703 }
1704 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001705}
1706
Alexandre Rames3e69f162014-12-10 10:36:50 +00001707size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1708 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1709 __ Str(reg, MemOperand(sp, stack_index));
1710 return kArm64WordSize;
1711}
1712
1713size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1714 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1715 __ Ldr(reg, MemOperand(sp, stack_index));
1716 return kArm64WordSize;
1717}
1718
1719size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1720 FPRegister reg = FPRegister(reg_id, kDRegSize);
1721 __ Str(reg, MemOperand(sp, stack_index));
1722 return kArm64WordSize;
1723}
1724
1725size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1726 FPRegister reg = FPRegister(reg_id, kDRegSize);
1727 __ Ldr(reg, MemOperand(sp, stack_index));
1728 return kArm64WordSize;
1729}
1730
Alexandre Rames5319def2014-10-23 10:03:10 +01001731void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001732 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001733}
1734
1735void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001736 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001737}
1738
Vladimir Markoa0431112018-06-25 09:32:54 +01001739const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1740 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1741}
1742
Alexandre Rames67555f72014-11-18 10:55:16 +00001743void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001744 if (constant->IsIntConstant()) {
1745 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1746 } else if (constant->IsLongConstant()) {
1747 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1748 } else if (constant->IsNullConstant()) {
1749 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001750 } else if (constant->IsFloatConstant()) {
1751 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1752 } else {
1753 DCHECK(constant->IsDoubleConstant());
1754 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1755 }
1756}
1757
Alexandre Rames3e69f162014-12-10 10:36:50 +00001758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001760 DCHECK(constant.IsConstant());
1761 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001762 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001763 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001764 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1765 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1766 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1767 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001768}
1769
Roland Levillain952b2352017-05-03 19:49:14 +01001770// Allocate a scratch register from the VIXL pool, querying first
1771// the floating-point register pool, and then the core register
1772// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001773// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1774// using a different allocation strategy.
1775static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1776 vixl::aarch64::UseScratchRegisterScope* temps,
1777 int size_in_bits) {
1778 return masm->GetScratchFPRegisterList()->IsEmpty()
1779 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1780 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1781}
1782
Calin Juravlee460d1d2015-09-29 04:52:17 +01001783void CodeGeneratorARM64::MoveLocation(Location destination,
1784 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001785 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001786 if (source.Equals(destination)) {
1787 return;
1788 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001789
1790 // A valid move can always be inferred from the destination and source
1791 // locations. When moving from and to a register, the argument type can be
1792 // used to generate 32bit instead of 64bit moves. In debug mode we also
1793 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001794 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001795
1796 if (destination.IsRegister() || destination.IsFpuRegister()) {
1797 if (unspecified_type) {
1798 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1799 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001800 (src_cst != nullptr && (src_cst->IsIntConstant()
1801 || src_cst->IsFloatConstant()
1802 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001803 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001804 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001805 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001806 // If the source is a double stack slot or a 64bit constant, a 64bit
1807 // type is appropriate. Else the source is a register, and since the
1808 // type has not been specified, we chose a 64bit type to force a 64bit
1809 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001810 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001811 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001812 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001813 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1814 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001815 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001816 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1817 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1818 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001819 } else if (source.IsSIMDStackSlot()) {
1820 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001821 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001822 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001823 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001824 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001825 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001826 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001827 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001828 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 DataType::Type source_type = DataType::Is64BitType(dst_type)
1830 ? DataType::Type::kInt64
1831 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001832 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1833 }
1834 } else {
1835 DCHECK(source.IsFpuRegister());
1836 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001837 DataType::Type source_type = DataType::Is64BitType(dst_type)
1838 ? DataType::Type::kFloat64
1839 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001840 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1841 } else {
1842 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001843 if (GetGraph()->HasSIMD()) {
1844 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1845 } else {
1846 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1847 }
1848 }
1849 }
1850 } else if (destination.IsSIMDStackSlot()) {
1851 if (source.IsFpuRegister()) {
1852 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1853 } else {
1854 DCHECK(source.IsSIMDStackSlot());
1855 UseScratchRegisterScope temps(GetVIXLAssembler());
1856 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1857 Register temp = temps.AcquireX();
1858 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1859 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1860 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1861 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1862 } else {
1863 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1864 __ Ldr(temp, StackOperandFrom(source));
1865 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001866 }
1867 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001868 } else { // The destination is not a register. It must be a stack slot.
1869 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1870 if (source.IsRegister() || source.IsFpuRegister()) {
1871 if (unspecified_type) {
1872 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001874 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001875 dst_type =
1876 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001877 }
1878 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001879 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1880 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001881 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001882 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001883 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1884 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001885 UseScratchRegisterScope temps(GetVIXLAssembler());
1886 HConstant* src_cst = source.GetConstant();
1887 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001888 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001889 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1890 ? Register(xzr)
1891 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001892 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001893 if (src_cst->IsIntConstant()) {
1894 temp = temps.AcquireW();
1895 } else if (src_cst->IsLongConstant()) {
1896 temp = temps.AcquireX();
1897 } else if (src_cst->IsFloatConstant()) {
1898 temp = temps.AcquireS();
1899 } else {
1900 DCHECK(src_cst->IsDoubleConstant());
1901 temp = temps.AcquireD();
1902 }
1903 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001904 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001905 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001906 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001907 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001908 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001909 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001910 // Use any scratch register (a core or a floating-point one)
1911 // from VIXL scratch register pools as a temporary.
1912 //
1913 // We used to only use the FP scratch register pool, but in some
1914 // rare cases the only register from this pool (D31) would
1915 // already be used (e.g. within a ParallelMove instruction, when
1916 // a move is blocked by a another move requiring a scratch FP
1917 // register, which would reserve D31). To prevent this issue, we
1918 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001919 //
1920 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001921 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001922 // use AcquireFPOrCoreCPURegisterOfSize instead of
1923 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1924 // allocates core scratch registers first.
1925 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1926 GetVIXLAssembler(),
1927 &temps,
1928 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001929 __ Ldr(temp, StackOperandFrom(source));
1930 __ Str(temp, StackOperandFrom(destination));
1931 }
1932 }
1933}
1934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001936 CPURegister dst,
1937 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001938 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001939 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001940 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001941 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001942 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001943 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001944 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001945 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001946 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001947 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001948 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001949 case DataType::Type::kInt16:
1950 __ Ldrsh(Register(dst), src);
1951 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 case DataType::Type::kInt32:
1953 case DataType::Type::kReference:
1954 case DataType::Type::kInt64:
1955 case DataType::Type::kFloat32:
1956 case DataType::Type::kFloat64:
1957 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001958 __ Ldr(dst, src);
1959 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001960 case DataType::Type::kUint32:
1961 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001962 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001963 LOG(FATAL) << "Unreachable type " << type;
1964 }
1965}
1966
Calin Juravle77520bc2015-01-12 18:45:46 +00001967void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001968 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001969 const MemOperand& src,
1970 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001971 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001972 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001973 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001974 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001975
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001976 DCHECK(!src.IsPreIndex());
1977 DCHECK(!src.IsPostIndex());
1978
1979 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001980 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001981 {
1982 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1983 MemOperand base = MemOperand(temp_base);
1984 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001985 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001986 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001987 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001988 {
1989 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1990 __ ldarb(Register(dst), base);
1991 if (needs_null_check) {
1992 MaybeRecordImplicitNullCheck(instruction);
1993 }
1994 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001995 if (type == DataType::Type::kInt8) {
1996 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001997 }
1998 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001999 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002001 {
2002 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2003 __ ldarh(Register(dst), base);
2004 if (needs_null_check) {
2005 MaybeRecordImplicitNullCheck(instruction);
2006 }
2007 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002008 if (type == DataType::Type::kInt16) {
2009 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
2010 }
Artem Serov914d7a82017-02-07 14:33:49 +00002011 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002012 case DataType::Type::kInt32:
2013 case DataType::Type::kReference:
2014 case DataType::Type::kInt64:
2015 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002016 {
2017 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2018 __ ldar(Register(dst), base);
2019 if (needs_null_check) {
2020 MaybeRecordImplicitNullCheck(instruction);
2021 }
2022 }
2023 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002024 case DataType::Type::kFloat32:
2025 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00002026 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002028
Artem Serov914d7a82017-02-07 14:33:49 +00002029 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2030 {
2031 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2032 __ ldar(temp, base);
2033 if (needs_null_check) {
2034 MaybeRecordImplicitNullCheck(instruction);
2035 }
2036 }
2037 __ Fmov(FPRegister(dst), temp);
2038 break;
Roland Levillain44015862016-01-22 11:47:17 +00002039 }
Aart Bik66c158e2018-01-31 12:55:04 -08002040 case DataType::Type::kUint32:
2041 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00002043 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002044 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002045 }
2046}
2047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002049 CPURegister src,
2050 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002051 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002053 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002054 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002055 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002056 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 case DataType::Type::kUint16:
2058 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002059 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002060 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 case DataType::Type::kInt32:
2062 case DataType::Type::kReference:
2063 case DataType::Type::kInt64:
2064 case DataType::Type::kFloat32:
2065 case DataType::Type::kFloat64:
2066 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002067 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002068 break;
Aart Bik66c158e2018-01-31 12:55:04 -08002069 case DataType::Type::kUint32:
2070 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002072 LOG(FATAL) << "Unreachable type " << type;
2073 }
2074}
2075
Artem Serov914d7a82017-02-07 14:33:49 +00002076void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002078 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002079 const MemOperand& dst,
2080 bool needs_null_check) {
2081 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002082 UseScratchRegisterScope temps(GetVIXLAssembler());
2083 Register temp_base = temps.AcquireX();
2084
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002085 DCHECK(!dst.IsPreIndex());
2086 DCHECK(!dst.IsPostIndex());
2087
2088 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002089 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002090 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002091 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002092 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002093 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002095 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002097 {
2098 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2099 __ stlrb(Register(src), base);
2100 if (needs_null_check) {
2101 MaybeRecordImplicitNullCheck(instruction);
2102 }
2103 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002104 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002105 case DataType::Type::kUint16:
2106 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002107 {
2108 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2109 __ stlrh(Register(src), base);
2110 if (needs_null_check) {
2111 MaybeRecordImplicitNullCheck(instruction);
2112 }
2113 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002114 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 case DataType::Type::kInt32:
2116 case DataType::Type::kReference:
2117 case DataType::Type::kInt64:
2118 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002119 {
2120 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2121 __ stlr(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::kFloat32:
2128 case DataType::Type::kFloat64: {
2129 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002130 Register temp_src;
2131 if (src.IsZero()) {
2132 // The zero register is used to avoid synthesizing zero constants.
2133 temp_src = Register(src);
2134 } else {
2135 DCHECK(src.IsFPRegister());
2136 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2137 __ Fmov(temp_src, FPRegister(src));
2138 }
Artem Serov914d7a82017-02-07 14:33:49 +00002139 {
2140 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2141 __ stlr(temp_src, base);
2142 if (needs_null_check) {
2143 MaybeRecordImplicitNullCheck(instruction);
2144 }
2145 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002146 break;
2147 }
Aart Bik66c158e2018-01-31 12:55:04 -08002148 case DataType::Type::kUint32:
2149 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002151 LOG(FATAL) << "Unreachable type " << type;
2152 }
2153}
2154
Calin Juravle175dc732015-08-25 15:42:32 +01002155void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2156 HInstruction* instruction,
2157 uint32_t dex_pc,
2158 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002159 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002160
2161 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2162 {
2163 // Ensure the pc position is recorded immediately after the `blr` instruction.
2164 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2165 __ blr(lr);
2166 if (EntrypointRequiresStackMap(entrypoint)) {
2167 RecordPcInfo(instruction, dex_pc, slow_path);
2168 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002169 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002170}
2171
Roland Levillaindec8f632016-07-22 17:10:06 +01002172void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2173 HInstruction* instruction,
2174 SlowPathCode* slow_path) {
2175 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002176 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2177 __ Blr(lr);
2178}
2179
Alexandre Rames67555f72014-11-18 10:55:16 +00002180void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002181 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002182 UseScratchRegisterScope temps(GetVIXLAssembler());
2183 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00002184 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2185 const size_t status_byte_offset =
2186 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2187 constexpr uint32_t shifted_initialized_value =
2188 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002189
Serban Constantinescu02164b32014-11-13 14:05:07 +00002190 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002191 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00002192 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07002193 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00002194 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00002195 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002196 __ Bind(slow_path->GetExitLabel());
2197}
Alexandre Rames5319def2014-10-23 10:03:10 +01002198
Vladimir Marko175e7862018-03-27 09:03:13 +00002199void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
2200 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
2201 uint32_t path_to_root = check->GetBitstringPathToRoot();
2202 uint32_t mask = check->GetBitstringMask();
2203 DCHECK(IsPowerOfTwo(mask + 1));
2204 size_t mask_bits = WhichPowerOf2(mask + 1);
2205
2206 if (mask_bits == 16u) {
2207 // Load only the bitstring part of the status word.
2208 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2209 } else {
2210 // /* uint32_t */ temp = temp->status_
2211 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2212 // Extract the bitstring bits.
2213 __ Ubfx(temp, temp, 0, mask_bits);
2214 }
2215 // Compare the bitstring bits to `path_to_root`.
2216 __ Cmp(temp, path_to_root);
2217}
2218
Roland Levillain44015862016-01-22 11:47:17 +00002219void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002220 BarrierType type = BarrierAll;
2221
2222 switch (kind) {
2223 case MemBarrierKind::kAnyAny:
2224 case MemBarrierKind::kAnyStore: {
2225 type = BarrierAll;
2226 break;
2227 }
2228 case MemBarrierKind::kLoadAny: {
2229 type = BarrierReads;
2230 break;
2231 }
2232 case MemBarrierKind::kStoreStore: {
2233 type = BarrierWrites;
2234 break;
2235 }
2236 default:
2237 LOG(FATAL) << "Unexpected memory barrier " << kind;
2238 }
2239 __ Dmb(InnerShareable, type);
2240}
2241
Serban Constantinescu02164b32014-11-13 14:05:07 +00002242void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2243 HBasicBlock* successor) {
2244 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002245 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2246 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002247 slow_path =
2248 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002249 instruction->SetSlowPath(slow_path);
2250 codegen_->AddSlowPath(slow_path);
2251 if (successor != nullptr) {
2252 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002253 }
2254 } else {
2255 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2256 }
2257
Serban Constantinescu02164b32014-11-13 14:05:07 +00002258 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2259 Register temp = temps.AcquireW();
2260
Andreas Gampe542451c2016-07-26 09:02:02 -07002261 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002262 if (successor == nullptr) {
2263 __ Cbnz(temp, slow_path->GetEntryLabel());
2264 __ Bind(slow_path->GetReturnLabel());
2265 } else {
2266 __ Cbz(temp, codegen_->GetLabelOf(successor));
2267 __ B(slow_path->GetEntryLabel());
2268 // slow_path will return to GetLabelOf(successor).
2269 }
2270}
2271
Alexandre Rames5319def2014-10-23 10:03:10 +01002272InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2273 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002274 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 assembler_(codegen->GetAssembler()),
2276 codegen_(codegen) {}
2277
Alexandre Rames67555f72014-11-18 10:55:16 +00002278void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002279 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002280 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002282 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002283 case DataType::Type::kInt32:
2284 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002285 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002286 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002288 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002290 case DataType::Type::kFloat32:
2291 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002292 locations->SetInAt(0, Location::RequiresFpuRegister());
2293 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002294 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002295 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002296
Alexandre Rames5319def2014-10-23 10:03:10 +01002297 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002298 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002299 }
2300}
2301
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002302void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2303 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002304 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2305
2306 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002308 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002309 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2310 object_field_get_with_read_barrier
2311 ? LocationSummary::kCallOnSlowPath
2312 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002313 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002314 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002315 // We need a temporary register for the read barrier marking slow
2316 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002317 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2318 !Runtime::Current()->UseJitCompilation() &&
2319 !field_info.IsVolatile()) {
2320 // If link-time thunks for the Baker read barrier are enabled, for AOT
2321 // non-volatile loads we need a temporary only if the offset is too big.
2322 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2323 locations->AddTemp(FixedTempLocation());
2324 }
2325 } else {
2326 locations->AddTemp(Location::RequiresRegister());
2327 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002328 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002329 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002330 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002331 locations->SetOut(Location::RequiresFpuRegister());
2332 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002333 // The output overlaps for an object field get when read barriers
2334 // are enabled: we do not want the load to overwrite the object's
2335 // location, as we need it to emit the read barrier.
2336 locations->SetOut(
2337 Location::RequiresRegister(),
2338 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002339 }
2340}
2341
2342void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2343 const FieldInfo& field_info) {
2344 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002345 LocationSummary* locations = instruction->GetLocations();
2346 Location base_loc = locations->InAt(0);
2347 Location out = locations->Out();
2348 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002349 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2350 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002351 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002352
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002354 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002355 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002356 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002358 Location maybe_temp =
2359 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002360 // Note that potential implicit null checks are handled in this
2361 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2362 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2363 instruction,
2364 out,
2365 base,
2366 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002367 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002368 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002369 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002370 } else {
2371 // General case.
2372 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002373 // Note that a potential implicit null check is handled in this
2374 // CodeGeneratorARM64::LoadAcquire call.
2375 // NB: LoadAcquire will record the pc info if needed.
2376 codegen_->LoadAcquire(
2377 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002378 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002379 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2380 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002381 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002382 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002383 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002384 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002385 // If read barriers are enabled, emit read barriers other than
2386 // Baker's using a slow path (and also unpoison the loaded
2387 // reference, if heap poisoning is enabled).
2388 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2389 }
Roland Levillain4d027112015-07-01 15:41:14 +01002390 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002391}
2392
2393void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2394 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002395 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002396 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002397 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2398 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002400 locations->SetInAt(1, Location::RequiresFpuRegister());
2401 } else {
2402 locations->SetInAt(1, Location::RequiresRegister());
2403 }
2404}
2405
2406void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002407 const FieldInfo& field_info,
2408 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002409 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2410
2411 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002412 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002413 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002414 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002416
Roland Levillain4d027112015-07-01 15:41:14 +01002417 {
2418 // We use a block to end the scratch scope before the write barrier, thus
2419 // freeing the temporary registers so they can be used in `MarkGCCard`.
2420 UseScratchRegisterScope temps(GetVIXLAssembler());
2421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002423 DCHECK(value.IsW());
2424 Register temp = temps.AcquireW();
2425 __ Mov(temp, value.W());
2426 GetAssembler()->PoisonHeapReference(temp.W());
2427 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002428 }
Roland Levillain4d027112015-07-01 15:41:14 +01002429
2430 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002431 codegen_->StoreRelease(
2432 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002433 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002434 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2435 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002436 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2437 codegen_->MaybeRecordImplicitNullCheck(instruction);
2438 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002439 }
2440
2441 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002442 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002443 }
2444}
2445
Alexandre Rames67555f72014-11-18 10:55:16 +00002446void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002447 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002448
2449 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002450 case DataType::Type::kInt32:
2451 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002452 Register dst = OutputRegister(instr);
2453 Register lhs = InputRegisterAt(instr, 0);
2454 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002455 if (instr->IsAdd()) {
2456 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002457 } else if (instr->IsAnd()) {
2458 __ And(dst, lhs, rhs);
2459 } else if (instr->IsOr()) {
2460 __ Orr(dst, lhs, rhs);
2461 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002462 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002463 } else if (instr->IsRor()) {
2464 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002465 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002466 __ Ror(dst, lhs, shift);
2467 } else {
2468 // Ensure shift distance is in the same size register as the result. If
2469 // we are rotating a long and the shift comes in a w register originally,
2470 // we don't need to sxtw for use as an x since the shift distances are
2471 // all & reg_bits - 1.
2472 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2473 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002474 } else if (instr->IsMin() || instr->IsMax()) {
2475 __ Cmp(lhs, rhs);
2476 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002477 } else {
2478 DCHECK(instr->IsXor());
2479 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002480 }
2481 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002482 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 case DataType::Type::kFloat32:
2484 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002485 FPRegister dst = OutputFPRegister(instr);
2486 FPRegister lhs = InputFPRegisterAt(instr, 0);
2487 FPRegister rhs = InputFPRegisterAt(instr, 1);
2488 if (instr->IsAdd()) {
2489 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002490 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002491 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002492 } else if (instr->IsMin()) {
2493 __ Fmin(dst, lhs, rhs);
2494 } else if (instr->IsMax()) {
2495 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002496 } else {
2497 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002498 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002499 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002500 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002501 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002502 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002503 }
2504}
2505
Serban Constantinescu02164b32014-11-13 14:05:07 +00002506void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2507 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2508
Vladimir Markoca6fff82017-10-03 14:49:14 +01002509 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002511 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002512 case DataType::Type::kInt32:
2513 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002514 locations->SetInAt(0, Location::RequiresRegister());
2515 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002516 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002517 break;
2518 }
2519 default:
2520 LOG(FATAL) << "Unexpected shift type " << type;
2521 }
2522}
2523
2524void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2525 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002528 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002529 case DataType::Type::kInt32:
2530 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002531 Register dst = OutputRegister(instr);
2532 Register lhs = InputRegisterAt(instr, 0);
2533 Operand rhs = InputOperandAt(instr, 1);
2534 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002535 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002537 if (instr->IsShl()) {
2538 __ Lsl(dst, lhs, shift_value);
2539 } else if (instr->IsShr()) {
2540 __ Asr(dst, lhs, shift_value);
2541 } else {
2542 __ Lsr(dst, lhs, shift_value);
2543 }
2544 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002545 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002546
2547 if (instr->IsShl()) {
2548 __ Lsl(dst, lhs, rhs_reg);
2549 } else if (instr->IsShr()) {
2550 __ Asr(dst, lhs, rhs_reg);
2551 } else {
2552 __ Lsr(dst, lhs, rhs_reg);
2553 }
2554 }
2555 break;
2556 }
2557 default:
2558 LOG(FATAL) << "Unexpected shift operation type " << type;
2559 }
2560}
2561
Alexandre Rames5319def2014-10-23 10:03:10 +01002562void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002563 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002564}
2565
2566void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002567 HandleBinaryOp(instruction);
2568}
2569
2570void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2571 HandleBinaryOp(instruction);
2572}
2573
2574void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2575 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002576}
2577
Artem Serov7fc63502016-02-09 17:15:29 +00002578void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002579 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002580 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002581 locations->SetInAt(0, Location::RequiresRegister());
2582 // There is no immediate variant of negated bitwise instructions in AArch64.
2583 locations->SetInAt(1, Location::RequiresRegister());
2584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2585}
2586
Artem Serov7fc63502016-02-09 17:15:29 +00002587void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002588 Register dst = OutputRegister(instr);
2589 Register lhs = InputRegisterAt(instr, 0);
2590 Register rhs = InputRegisterAt(instr, 1);
2591
2592 switch (instr->GetOpKind()) {
2593 case HInstruction::kAnd:
2594 __ Bic(dst, lhs, rhs);
2595 break;
2596 case HInstruction::kOr:
2597 __ Orn(dst, lhs, rhs);
2598 break;
2599 case HInstruction::kXor:
2600 __ Eon(dst, lhs, rhs);
2601 break;
2602 default:
2603 LOG(FATAL) << "Unreachable";
2604 }
2605}
2606
Anton Kirilov74234da2017-01-13 14:42:47 +00002607void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2608 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002609 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2610 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002611 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002612 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002613 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2614 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2615 } else {
2616 locations->SetInAt(0, Location::RequiresRegister());
2617 }
2618 locations->SetInAt(1, Location::RequiresRegister());
2619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2620}
2621
Anton Kirilov74234da2017-01-13 14:42:47 +00002622void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2623 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002625 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002626 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002627 Register out = OutputRegister(instruction);
2628 Register left;
2629 if (kind != HInstruction::kNeg) {
2630 left = InputRegisterAt(instruction, 0);
2631 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002632 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002633 // shifter operand operation, the IR generating `right_reg` (input to the type
2634 // conversion) can have a different type from the current instruction's type,
2635 // so we manually indicate the type.
2636 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002637 Operand right_operand(0);
2638
Anton Kirilov74234da2017-01-13 14:42:47 +00002639 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2640 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002641 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2642 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002643 right_operand = Operand(right_reg,
2644 helpers::ShiftFromOpKind(op_kind),
2645 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002646 }
2647
2648 // Logical binary operations do not support extension operations in the
2649 // operand. Note that VIXL would still manage if it was passed by generating
2650 // the extension as a separate instruction.
2651 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2652 DCHECK(!right_operand.IsExtendedRegister() ||
2653 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2654 kind != HInstruction::kNeg));
2655 switch (kind) {
2656 case HInstruction::kAdd:
2657 __ Add(out, left, right_operand);
2658 break;
2659 case HInstruction::kAnd:
2660 __ And(out, left, right_operand);
2661 break;
2662 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002663 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002664 __ Neg(out, right_operand);
2665 break;
2666 case HInstruction::kOr:
2667 __ Orr(out, left, right_operand);
2668 break;
2669 case HInstruction::kSub:
2670 __ Sub(out, left, right_operand);
2671 break;
2672 case HInstruction::kXor:
2673 __ Eor(out, left, right_operand);
2674 break;
2675 default:
2676 LOG(FATAL) << "Unexpected operation kind: " << kind;
2677 UNREACHABLE();
2678 }
2679}
2680
Artem Serov328429f2016-07-06 16:23:04 +01002681void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002682 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002683 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002684 locations->SetInAt(0, Location::RequiresRegister());
2685 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002687}
2688
Roland Levillain19c54192016-11-04 13:44:09 +00002689void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002690 __ Add(OutputRegister(instruction),
2691 InputRegisterAt(instruction, 0),
2692 Operand(InputOperandAt(instruction, 1)));
2693}
2694
Artem Serove1811ed2017-04-27 16:50:47 +01002695void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2696 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002697 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002698
2699 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2700
2701 locations->SetInAt(0, Location::RequiresRegister());
2702 // For byte case we don't need to shift the index variable so we can encode the data offset into
2703 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2704 // data offset constant generation out of the loop and reduce the critical path length in the
2705 // loop.
2706 locations->SetInAt(1, shift->GetValue() == 0
2707 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2708 : Location::RequiresRegister());
2709 locations->SetInAt(2, Location::ConstantLocation(shift));
2710 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2711}
2712
2713void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2714 HIntermediateAddressIndex* instruction) {
2715 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002716 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002717 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2718
2719 if (shift == 0) {
2720 __ Add(OutputRegister(instruction), index_reg, offset);
2721 } else {
2722 Register offset_reg = InputRegisterAt(instruction, 1);
2723 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2724 }
2725}
2726
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002727void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002728 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002729 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002730 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2731 if (instr->GetOpKind() == HInstruction::kSub &&
2732 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002733 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002734 // Don't allocate register for Mneg instruction.
2735 } else {
2736 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2737 Location::RequiresRegister());
2738 }
2739 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2740 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2742}
2743
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002744void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002745 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002746 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2747 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002748
2749 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2750 // This fixup should be carried out for all multiply-accumulate instructions:
2751 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002753 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2754 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002755 vixl::aarch64::Instruction* prev =
2756 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002757 if (prev->IsLoadOrStore()) {
2758 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002759 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002760 __ nop();
2761 }
2762 }
2763
2764 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002765 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002766 __ Madd(res, mul_left, mul_right, accumulator);
2767 } else {
2768 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002769 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002770 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002771 __ Mneg(res, mul_left, mul_right);
2772 } else {
2773 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2774 __ Msub(res, mul_left, mul_right, accumulator);
2775 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002776 }
2777}
2778
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002779void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002780 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002781 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002782 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002783 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2784 object_array_get_with_read_barrier
2785 ? LocationSummary::kCallOnSlowPath
2786 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002787 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002788 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002789 // We need a temporary register for the read barrier marking slow
2790 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002791 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2792 !Runtime::Current()->UseJitCompilation() &&
2793 instruction->GetIndex()->IsConstant()) {
2794 // Array loads with constant index are treated as field loads.
2795 // If link-time thunks for the Baker read barrier are enabled, for AOT
2796 // constant index loads we need a temporary only if the offset is too big.
2797 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2798 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002800 if (offset >= kReferenceLoadMinFarOffset) {
2801 locations->AddTemp(FixedTempLocation());
2802 }
2803 } else {
2804 locations->AddTemp(Location::RequiresRegister());
2805 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002806 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002807 locations->SetInAt(0, Location::RequiresRegister());
2808 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002809 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002810 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2811 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002812 // The output overlaps in the case of an object array get with
2813 // read barriers enabled: we do not want the move to overwrite the
2814 // array's location, as we need it to emit the read barrier.
2815 locations->SetOut(
2816 Location::RequiresRegister(),
2817 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002818 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002819}
2820
2821void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002823 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002824 LocationSummary* locations = instruction->GetLocations();
2825 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002826 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002827 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002828 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2829 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002830 MacroAssembler* masm = GetVIXLAssembler();
2831 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002832
Roland Levillain19c54192016-11-04 13:44:09 +00002833 // The read barrier instrumentation of object ArrayGet instructions
2834 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002836 instruction->GetArray()->IsIntermediateAddress() &&
2837 kEmitCompilerReadBarrier));
2838
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002839 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002840 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002841 // Note that a potential implicit null check is handled in the
2842 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002843 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002844 if (index.IsConstant()) {
2845 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002846 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002847 Location maybe_temp =
2848 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2849 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2850 out,
2851 obj.W(),
2852 offset,
2853 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002854 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002855 /* use_load_acquire */ false);
2856 } else {
2857 Register temp = WRegisterFrom(locations->GetTemp(0));
2858 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002859 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002860 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002861 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002862 // General case.
2863 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002864 Register length;
2865 if (maybe_compressed_char_at) {
2866 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2867 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002868 {
2869 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2870 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2871
2872 if (instruction->GetArray()->IsIntermediateAddress()) {
2873 DCHECK_LT(count_offset, offset);
2874 int64_t adjusted_offset =
2875 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2876 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2877 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2878 } else {
2879 __ Ldr(length, HeapOperand(obj, count_offset));
2880 }
2881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002882 }
jessicahandojo05765752016-09-09 19:01:32 -07002883 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002884 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002885 if (maybe_compressed_char_at) {
2886 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002887 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2888 "Expecting 0=compressed, 1=uncompressed");
2889 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002890 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002891 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002892 __ B(&done);
2893 __ Bind(&uncompressed_load);
2894 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002895 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002896 __ Bind(&done);
2897 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002898 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002899 source = HeapOperand(obj, offset);
2900 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002901 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002902 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002903 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002904 // We do not need to compute the intermediate address from the array: the
2905 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002906 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002907 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002908 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002909 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2910 }
2911 temp = obj;
2912 } else {
2913 __ Add(temp, obj, offset);
2914 }
jessicahandojo05765752016-09-09 19:01:32 -07002915 if (maybe_compressed_char_at) {
2916 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002917 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2918 "Expecting 0=compressed, 1=uncompressed");
2919 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002920 __ Ldrb(Register(OutputCPURegister(instruction)),
2921 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2922 __ B(&done);
2923 __ Bind(&uncompressed_load);
2924 __ Ldrh(Register(OutputCPURegister(instruction)),
2925 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2926 __ Bind(&done);
2927 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002929 }
Roland Levillain44015862016-01-22 11:47:17 +00002930 }
jessicahandojo05765752016-09-09 19:01:32 -07002931 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002932 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2933 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002934 codegen_->Load(type, OutputCPURegister(instruction), source);
2935 codegen_->MaybeRecordImplicitNullCheck(instruction);
2936 }
Roland Levillain44015862016-01-22 11:47:17 +00002937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002939 static_assert(
2940 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2941 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2942 Location obj_loc = locations->InAt(0);
2943 if (index.IsConstant()) {
2944 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2945 } else {
2946 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2947 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002948 }
Roland Levillain4d027112015-07-01 15:41:14 +01002949 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002950}
2951
Alexandre Rames5319def2014-10-23 10:03:10 +01002952void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002953 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002954 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002956}
2957
2958void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002959 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002960 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002961 {
2962 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2963 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2964 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2965 codegen_->MaybeRecordImplicitNullCheck(instruction);
2966 }
jessicahandojo05765752016-09-09 19:01:32 -07002967 // Mask out compression flag from String's array length.
2968 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002969 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002970 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002971}
2972
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002973void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002975
2976 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002977 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002978 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002979 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002980 LocationSummary::kCallOnSlowPath :
2981 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002982 locations->SetInAt(0, Location::RequiresRegister());
2983 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002984 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2985 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002987 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002988 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002989 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002990 }
2991}
2992
2993void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002995 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002996 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002997 bool needs_write_barrier =
2998 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002999
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003000 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01003001 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003002 CPURegister source = value;
3003 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003005 MemOperand destination = HeapOperand(array);
3006 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003007
3008 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003009 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003010 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003011 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003012 destination = HeapOperand(array, offset);
3013 } else {
3014 UseScratchRegisterScope temps(masm);
3015 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01003016 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003017 // We do not need to compute the intermediate address from the array: the
3018 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01003019 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003020 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01003021 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003022 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
3023 }
3024 temp = array;
3025 } else {
3026 __ Add(temp, array, offset);
3027 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003028 destination = HeapOperand(temp,
3029 XRegisterFrom(index),
3030 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003031 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003032 }
Artem Serov914d7a82017-02-07 14:33:49 +00003033 {
3034 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3035 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3036 codegen_->Store(value_type, value, destination);
3037 codegen_->MaybeRecordImplicitNullCheck(instruction);
3038 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003039 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003040 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003041 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003042 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003043 {
3044 // We use a block to end the scratch scope before the write barrier, thus
3045 // freeing the temporary registers so they can be used in `MarkGCCard`.
3046 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003047 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003048 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003049 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003050 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003051 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003052 destination = HeapOperand(temp,
3053 XRegisterFrom(index),
3054 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003055 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003056 }
3057
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003058 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3059 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3060 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3061
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003062 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003063 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003064 codegen_->AddSlowPath(slow_path);
3065 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003066 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003067 __ Cbnz(Register(value), &non_zero);
3068 if (!index.IsConstant()) {
3069 __ Add(temp, array, offset);
3070 }
Artem Serov914d7a82017-02-07 14:33:49 +00003071 {
3072 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3073 // emitted.
3074 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3075 __ Str(wzr, destination);
3076 codegen_->MaybeRecordImplicitNullCheck(instruction);
3077 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003078 __ B(&done);
3079 __ Bind(&non_zero);
3080 }
3081
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003082 // Note that when Baker read barriers are enabled, the type
3083 // checks are performed without read barriers. This is fine,
3084 // even in the case where a class object is in the from-space
3085 // after the flip, as a comparison involving such a type would
3086 // not produce a false positive; it may of course produce a
3087 // false negative, in which case we would take the ArraySet
3088 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003089
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003090 Register temp2 = temps.AcquireSameSizeAs(array);
3091 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003092 {
3093 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3094 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3095 __ Ldr(temp, HeapOperand(array, class_offset));
3096 codegen_->MaybeRecordImplicitNullCheck(instruction);
3097 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003098 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003099
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003100 // /* HeapReference<Class> */ temp = temp->component_type_
3101 __ Ldr(temp, HeapOperand(temp, component_offset));
3102 // /* HeapReference<Class> */ temp2 = value->klass_
3103 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3104 // If heap poisoning is enabled, no need to unpoison `temp`
3105 // nor `temp2`, as we are comparing two poisoned references.
3106 __ Cmp(temp, temp2);
3107 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003108
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003109 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3110 vixl::aarch64::Label do_put;
3111 __ B(eq, &do_put);
3112 // If heap poisoning is enabled, the `temp` reference has
3113 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003114 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3115
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003116 // /* HeapReference<Class> */ temp = temp->super_class_
3117 __ Ldr(temp, HeapOperand(temp, super_offset));
3118 // If heap poisoning is enabled, no need to unpoison
3119 // `temp`, as we are comparing against null below.
3120 __ Cbnz(temp, slow_path->GetEntryLabel());
3121 __ Bind(&do_put);
3122 } else {
3123 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003124 }
3125 }
3126
3127 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003128 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003129 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003130 __ Mov(temp2, value.W());
3131 GetAssembler()->PoisonHeapReference(temp2);
3132 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003133 }
3134
3135 if (!index.IsConstant()) {
3136 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003137 } else {
3138 // We no longer need the `temp` here so release it as the store below may
3139 // need a scratch register (if the constant index makes the offset too large)
3140 // and the poisoned `source` could be using the other scratch register.
3141 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003142 }
Artem Serov914d7a82017-02-07 14:33:49 +00003143 {
3144 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3145 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3146 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003147
Artem Serov914d7a82017-02-07 14:33:49 +00003148 if (!may_need_runtime_call_for_type_check) {
3149 codegen_->MaybeRecordImplicitNullCheck(instruction);
3150 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003151 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003152 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003153
3154 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3155
3156 if (done.IsLinked()) {
3157 __ Bind(&done);
3158 }
3159
3160 if (slow_path != nullptr) {
3161 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003162 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003163 }
3164}
3165
Alexandre Rames67555f72014-11-18 10:55:16 +00003166void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003167 RegisterSet caller_saves = RegisterSet::Empty();
3168 InvokeRuntimeCallingConvention calling_convention;
3169 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3170 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3171 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003172 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003173 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003174}
3175
3176void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003177 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003178 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003179 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003180 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3181 __ B(slow_path->GetEntryLabel(), hs);
3182}
3183
Alexandre Rames67555f72014-11-18 10:55:16 +00003184void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003186 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003187 locations->SetInAt(0, Location::RequiresRegister());
3188 if (check->HasUses()) {
3189 locations->SetOut(Location::SameAsFirstInput());
3190 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01003191 // Rely on the type initialization to save everything we need.
3192 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00003193}
3194
3195void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3196 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003197 SlowPathCodeARM64* slow_path =
3198 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00003199 codegen_->AddSlowPath(slow_path);
3200 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3201}
3202
Roland Levillain1a653882016-03-18 18:05:57 +00003203static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3204 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3205 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3206}
3207
3208void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3209 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3210 Location rhs_loc = instruction->GetLocations()->InAt(1);
3211 if (rhs_loc.IsConstant()) {
3212 // 0.0 is the only immediate that can be encoded directly in
3213 // an FCMP instruction.
3214 //
3215 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3216 // specify that in a floating-point comparison, positive zero
3217 // and negative zero are considered equal, so we can use the
3218 // literal 0.0 for both cases here.
3219 //
3220 // Note however that some methods (Float.equal, Float.compare,
3221 // Float.compareTo, Double.equal, Double.compare,
3222 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3223 // StrictMath.min) consider 0.0 to be (strictly) greater than
3224 // -0.0. So if we ever translate calls to these methods into a
3225 // HCompare instruction, we must handle the -0.0 case with
3226 // care here.
3227 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3228 __ Fcmp(lhs_reg, 0.0);
3229 } else {
3230 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3231 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003232}
3233
Serban Constantinescu02164b32014-11-13 14:05:07 +00003234void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003235 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003236 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003237 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003238 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003240 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003242 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003243 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003244 case DataType::Type::kInt32:
3245 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003246 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003247 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3249 break;
3250 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003251 case DataType::Type::kFloat32:
3252 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003253 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003254 locations->SetInAt(1,
3255 IsFloatingPointZeroConstant(compare->InputAt(1))
3256 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3257 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003258 locations->SetOut(Location::RequiresRegister());
3259 break;
3260 }
3261 default:
3262 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3263 }
3264}
3265
3266void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003267 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003268
3269 // 0 if: left == right
3270 // 1 if: left > right
3271 // -1 if: left < right
3272 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003273 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003274 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003275 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003276 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003277 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003278 case DataType::Type::kInt32:
3279 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003280 Register result = OutputRegister(compare);
3281 Register left = InputRegisterAt(compare, 0);
3282 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003283 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003284 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3285 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003286 break;
3287 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003288 case DataType::Type::kFloat32:
3289 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003290 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003291 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003292 __ Cset(result, ne);
3293 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003294 break;
3295 }
3296 default:
3297 LOG(FATAL) << "Unimplemented compare type " << in_type;
3298 }
3299}
3300
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003301void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003302 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003304 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003305 locations->SetInAt(0, Location::RequiresFpuRegister());
3306 locations->SetInAt(1,
3307 IsFloatingPointZeroConstant(instruction->InputAt(1))
3308 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3309 : Location::RequiresFpuRegister());
3310 } else {
3311 // Integer cases.
3312 locations->SetInAt(0, Location::RequiresRegister());
3313 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3314 }
3315
David Brazdilb3e773e2016-01-26 11:28:37 +00003316 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003318 }
3319}
3320
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003321void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003322 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003323 return;
3324 }
3325
3326 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003327 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003328 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003329
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003330 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003331 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003332 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003333 } else {
3334 // Integer cases.
3335 Register lhs = InputRegisterAt(instruction, 0);
3336 Operand rhs = InputOperandAt(instruction, 1);
3337 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003338 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003339 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003340}
3341
3342#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3343 M(Equal) \
3344 M(NotEqual) \
3345 M(LessThan) \
3346 M(LessThanOrEqual) \
3347 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003348 M(GreaterThanOrEqual) \
3349 M(Below) \
3350 M(BelowOrEqual) \
3351 M(Above) \
3352 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003353#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003354void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3355void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003356FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003357#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003358#undef FOR_EACH_CONDITION_INSTRUCTION
3359
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003360void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003361 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003362 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003363 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3364
3365 Register out = OutputRegister(instruction);
3366 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003367
3368 if (abs_imm == 2) {
3369 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3370 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3371 } else {
3372 UseScratchRegisterScope temps(GetVIXLAssembler());
3373 Register temp = temps.AcquireSameSizeAs(out);
3374 __ Add(temp, dividend, abs_imm - 1);
3375 __ Cmp(dividend, 0);
3376 __ Csel(out, temp, dividend, lt);
3377 }
3378
Zheng Xuc6667102015-05-15 16:08:45 +08003379 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003380 if (imm > 0) {
3381 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003382 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003383 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003384 }
3385}
3386
3387void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3388 DCHECK(instruction->IsDiv() || instruction->IsRem());
3389
3390 LocationSummary* locations = instruction->GetLocations();
3391 Location second = locations->InAt(1);
3392 DCHECK(second.IsConstant());
3393
3394 Register out = OutputRegister(instruction);
3395 Register dividend = InputRegisterAt(instruction, 0);
3396 int64_t imm = Int64FromConstant(second.GetConstant());
3397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003398 DataType::Type type = instruction->GetResultType();
3399 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003400
3401 int64_t magic;
3402 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003403 CalculateMagicAndShiftForDivRem(
3404 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003405
3406 UseScratchRegisterScope temps(GetVIXLAssembler());
3407 Register temp = temps.AcquireSameSizeAs(out);
3408
3409 // temp = get_high(dividend * magic)
3410 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003411 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003412 __ Smulh(temp, dividend, temp);
3413 } else {
3414 __ Smull(temp.X(), dividend, temp);
3415 __ Lsr(temp.X(), temp.X(), 32);
3416 }
3417
3418 if (imm > 0 && magic < 0) {
3419 __ Add(temp, temp, dividend);
3420 } else if (imm < 0 && magic > 0) {
3421 __ Sub(temp, temp, dividend);
3422 }
3423
3424 if (shift != 0) {
3425 __ Asr(temp, temp, shift);
3426 }
3427
3428 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003429 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003430 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003431 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003432 // TODO: Strength reduction for msub.
3433 Register temp_imm = temps.AcquireSameSizeAs(out);
3434 __ Mov(temp_imm, imm);
3435 __ Msub(out, temp, temp_imm, dividend);
3436 }
3437}
3438
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003439void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003440 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003441
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003442 if (imm == 0) {
3443 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3444 return;
3445 }
Zheng Xuc6667102015-05-15 16:08:45 +08003446
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003447 if (IsPowerOfTwo(AbsOrMin(imm))) {
3448 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003449 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003450 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3451 DCHECK(imm < -2 || imm > 2) << imm;
3452 GenerateDivRemWithAnyConstant(instruction);
3453 }
3454}
3455
3456void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3457 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3458 << instruction->GetResultType();
3459
3460 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3461 GenerateIntDivForConstDenom(instruction);
3462 } else {
3463 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003464 Register dividend = InputRegisterAt(instruction, 0);
3465 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003466 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003467 }
3468}
3469
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003470void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003472 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003473 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003474 case DataType::Type::kInt32:
3475 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003476 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003477 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3479 break;
3480
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003481 case DataType::Type::kFloat32:
3482 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003483 locations->SetInAt(0, Location::RequiresFpuRegister());
3484 locations->SetInAt(1, Location::RequiresFpuRegister());
3485 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3486 break;
3487
3488 default:
3489 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3490 }
3491}
3492
3493void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003494 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003495 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003496 case DataType::Type::kInt32:
3497 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003498 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003499 break;
3500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kFloat32:
3502 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003503 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3504 break;
3505
3506 default:
3507 LOG(FATAL) << "Unexpected div type " << type;
3508 }
3509}
3510
Alexandre Rames67555f72014-11-18 10:55:16 +00003511void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003512 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003513 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003514}
3515
3516void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3517 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003518 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003519 codegen_->AddSlowPath(slow_path);
3520 Location value = instruction->GetLocations()->InAt(0);
3521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003522 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003523
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003524 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003525 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003526 return;
3527 }
3528
Alexandre Rames67555f72014-11-18 10:55:16 +00003529 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003530 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003531 if (divisor == 0) {
3532 __ B(slow_path->GetEntryLabel());
3533 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003534 // A division by a non-null constant is valid. We don't need to perform
3535 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003536 }
3537 } else {
3538 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3539 }
3540}
3541
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003542void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3543 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003544 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003545 locations->SetOut(Location::ConstantLocation(constant));
3546}
3547
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003548void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3549 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003550 // Will be generated at use site.
3551}
3552
Alexandre Rames5319def2014-10-23 10:03:10 +01003553void LocationsBuilderARM64::VisitExit(HExit* exit) {
3554 exit->SetLocations(nullptr);
3555}
3556
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003557void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003558}
3559
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003560void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3561 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003562 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003563 locations->SetOut(Location::ConstantLocation(constant));
3564}
3565
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003566void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003567 // Will be generated at use site.
3568}
3569
David Brazdilfc6a86a2015-06-26 10:33:45 +00003570void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003571 if (successor->IsExitBlock()) {
3572 DCHECK(got->GetPrevious()->AlwaysThrows());
3573 return; // no code needed
3574 }
3575
Serban Constantinescu02164b32014-11-13 14:05:07 +00003576 HBasicBlock* block = got->GetBlock();
3577 HInstruction* previous = got->GetPrevious();
3578 HLoopInformation* info = block->GetLoopInformation();
3579
David Brazdil46e2a392015-03-16 17:31:52 +00003580 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003581 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3582 UseScratchRegisterScope temps(GetVIXLAssembler());
3583 Register temp1 = temps.AcquireX();
3584 Register temp2 = temps.AcquireX();
3585 __ Ldr(temp1, MemOperand(sp, 0));
3586 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3587 __ Add(temp2, temp2, 1);
3588 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3589 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003590 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3591 return;
3592 }
3593 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3594 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003595 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003596 }
3597 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003598 __ B(codegen_->GetLabelOf(successor));
3599 }
3600}
3601
David Brazdilfc6a86a2015-06-26 10:33:45 +00003602void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3603 got->SetLocations(nullptr);
3604}
3605
3606void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3607 HandleGoto(got, got->GetSuccessor());
3608}
3609
3610void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3611 try_boundary->SetLocations(nullptr);
3612}
3613
3614void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3615 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3616 if (!successor->IsExitBlock()) {
3617 HandleGoto(try_boundary, successor);
3618 }
3619}
3620
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003621void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003622 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003623 vixl::aarch64::Label* true_target,
3624 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003625 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003626
David Brazdil0debae72015-11-12 18:37:00 +00003627 if (true_target == nullptr && false_target == nullptr) {
3628 // Nothing to do. The code always falls through.
3629 return;
3630 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003631 // Constant condition, statically compared against "true" (integer value 1).
3632 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003633 if (true_target != nullptr) {
3634 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003635 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003636 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003637 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003638 if (false_target != nullptr) {
3639 __ B(false_target);
3640 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003641 }
David Brazdil0debae72015-11-12 18:37:00 +00003642 return;
3643 }
3644
3645 // The following code generates these patterns:
3646 // (1) true_target == nullptr && false_target != nullptr
3647 // - opposite condition true => branch to false_target
3648 // (2) true_target != nullptr && false_target == nullptr
3649 // - condition true => branch to true_target
3650 // (3) true_target != nullptr && false_target != nullptr
3651 // - condition true => branch to true_target
3652 // - branch to false_target
3653 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003654 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003655 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003656 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003657 if (true_target == nullptr) {
3658 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3659 } else {
3660 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3661 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003662 } else {
3663 // The condition instruction has not been materialized, use its inputs as
3664 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003665 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 DataType::Type type = condition->InputAt(0)->GetType();
3668 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003669 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003670 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003671 IfCondition opposite_condition = condition->GetOppositeCondition();
3672 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003673 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003674 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003675 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003676 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003677 // Integer cases.
3678 Register lhs = InputRegisterAt(condition, 0);
3679 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003680
3681 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003682 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003683 if (true_target == nullptr) {
3684 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3685 non_fallthrough_target = false_target;
3686 } else {
3687 arm64_cond = ARM64Condition(condition->GetCondition());
3688 non_fallthrough_target = true_target;
3689 }
3690
Aart Bik086d27e2016-01-20 17:02:00 -08003691 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003692 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003693 switch (arm64_cond) {
3694 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003695 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003696 break;
3697 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003698 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003699 break;
3700 case lt:
3701 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003702 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003703 break;
3704 case ge:
3705 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003706 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003707 break;
3708 default:
3709 // Without the `static_cast` the compiler throws an error for
3710 // `-Werror=sign-promo`.
3711 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3712 }
3713 } else {
3714 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003715 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003716 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003717 }
3718 }
David Brazdil0debae72015-11-12 18:37:00 +00003719
3720 // If neither branch falls through (case 3), the conditional branch to `true_target`
3721 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3722 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003723 __ B(false_target);
3724 }
3725}
3726
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003727void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003728 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003729 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003730 locations->SetInAt(0, Location::RequiresRegister());
3731 }
3732}
3733
3734void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003735 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3736 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003737 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3738 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3739 true_target = nullptr;
3740 }
3741 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3742 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3743 false_target = nullptr;
3744 }
David Brazdil0debae72015-11-12 18:37:00 +00003745 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003746}
3747
3748void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003749 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003750 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003751 InvokeRuntimeCallingConvention calling_convention;
3752 RegisterSet caller_saves = RegisterSet::Empty();
3753 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3754 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003755 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003756 locations->SetInAt(0, Location::RequiresRegister());
3757 }
3758}
3759
3760void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003761 SlowPathCodeARM64* slow_path =
3762 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003763 GenerateTestAndBranch(deoptimize,
3764 /* condition_input_index */ 0,
3765 slow_path->GetEntryLabel(),
3766 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003767}
3768
Mingyao Yang063fc772016-08-02 11:02:54 -07003769void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003770 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003771 LocationSummary(flag, LocationSummary::kNoCall);
3772 locations->SetOut(Location::RequiresRegister());
3773}
3774
3775void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3776 __ Ldr(OutputRegister(flag),
3777 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3778}
3779
David Brazdilc0b601b2016-02-08 14:20:45 +00003780static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3781 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003782 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003783}
3784
Alexandre Rames880f1192016-06-13 16:04:50 +01003785static inline Condition GetConditionForSelect(HCondition* condition) {
3786 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003787 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3788 : ARM64Condition(cond);
3789}
3790
David Brazdil74eb1b22015-12-14 11:44:01 +00003791void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003792 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003793 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003794 locations->SetInAt(0, Location::RequiresFpuRegister());
3795 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003796 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003797 } else {
3798 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3799 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3800 bool is_true_value_constant = cst_true_value != nullptr;
3801 bool is_false_value_constant = cst_false_value != nullptr;
3802 // Ask VIXL whether we should synthesize constants in registers.
3803 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3804 Operand true_op = is_true_value_constant ?
3805 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3806 Operand false_op = is_false_value_constant ?
3807 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3808 bool true_value_in_register = false;
3809 bool false_value_in_register = false;
3810 MacroAssembler::GetCselSynthesisInformation(
3811 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3812 true_value_in_register |= !is_true_value_constant;
3813 false_value_in_register |= !is_false_value_constant;
3814
3815 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3816 : Location::ConstantLocation(cst_true_value));
3817 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3818 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003819 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003820 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003821
David Brazdil74eb1b22015-12-14 11:44:01 +00003822 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3823 locations->SetInAt(2, Location::RequiresRegister());
3824 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003825}
3826
3827void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003828 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003829 Condition csel_cond;
3830
3831 if (IsBooleanValueOrMaterializedCondition(cond)) {
3832 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003833 // Use the condition flags set by the previous instruction.
3834 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003835 } else {
3836 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003837 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003838 }
3839 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003840 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003841 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003842 } else {
3843 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003844 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003845 }
3846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003847 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003848 __ Fcsel(OutputFPRegister(select),
3849 InputFPRegisterAt(select, 1),
3850 InputFPRegisterAt(select, 0),
3851 csel_cond);
3852 } else {
3853 __ Csel(OutputRegister(select),
3854 InputOperandAt(select, 1),
3855 InputOperandAt(select, 0),
3856 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003857 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003858}
3859
David Srbecky0cf44932015-12-09 14:09:59 +00003860void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003861 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003862}
3863
David Srbeckyd28f4a02016-03-14 17:14:24 +00003864void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3865 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003866}
3867
3868void CodeGeneratorARM64::GenerateNop() {
3869 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003870}
3871
Alexandre Rames5319def2014-10-23 10:03:10 +01003872void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003873 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003874}
3875
3876void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003877 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003878}
3879
3880void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003881 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003882}
3883
3884void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003885 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003886}
3887
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003888// Temp is used for read barrier.
3889static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3890 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003891 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003892 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3893 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3894 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3895 return 1;
3896 }
3897 return 0;
3898}
3899
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003900// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003901// interface pointer, one for loading the current interface.
3902// The other checks have one temp for loading the object's class.
3903static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3904 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3905 return 3;
3906 }
3907 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003908}
3909
Alexandre Rames67555f72014-11-18 10:55:16 +00003910void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003911 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003912 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003913 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003914 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003915 case TypeCheckKind::kExactCheck:
3916 case TypeCheckKind::kAbstractClassCheck:
3917 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003918 case TypeCheckKind::kArrayObjectCheck: {
3919 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3920 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3921 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003922 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003923 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003924 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003925 case TypeCheckKind::kUnresolvedCheck:
3926 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 call_kind = LocationSummary::kCallOnSlowPath;
3928 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003929 case TypeCheckKind::kBitstringCheck:
3930 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003931 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932
Vladimir Markoca6fff82017-10-03 14:49:14 +01003933 LocationSummary* locations =
3934 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003935 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003936 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003937 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003938 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003939 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3940 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3941 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3942 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3943 } else {
3944 locations->SetInAt(1, Location::RequiresRegister());
3945 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003946 // The "out" register is used as a temporary, so it overlaps with the inputs.
3947 // Note that TypeCheckSlowPathARM64 uses this register too.
3948 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003949 // Add temps if necessary for read barriers.
3950 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003951}
3952
3953void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003954 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003955 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003956 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003957 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003958 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3959 ? Register()
3960 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003961 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003962 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003963 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3964 DCHECK_LE(num_temps, 1u);
3965 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003966 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3967 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3968 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3969 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003970
Scott Wakeling97c72b72016-06-24 16:19:36 +01003971 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003972 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003973
3974 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003975 // Avoid null check if we know `obj` is not null.
3976 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003977 __ Cbz(obj, &zero);
3978 }
3979
Roland Levillain44015862016-01-22 11:47:17 +00003980 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003981 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003982 ReadBarrierOption read_barrier_option =
3983 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003984 // /* HeapReference<Class> */ out = obj->klass_
3985 GenerateReferenceLoadTwoRegisters(instruction,
3986 out_loc,
3987 obj_loc,
3988 class_offset,
3989 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003990 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003991 __ Cmp(out, cls);
3992 __ Cset(out, eq);
3993 if (zero.IsLinked()) {
3994 __ B(&done);
3995 }
3996 break;
3997 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003998
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003999 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004000 ReadBarrierOption read_barrier_option =
4001 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004002 // /* HeapReference<Class> */ out = obj->klass_
4003 GenerateReferenceLoadTwoRegisters(instruction,
4004 out_loc,
4005 obj_loc,
4006 class_offset,
4007 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004008 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004009 // If the class is abstract, we eagerly fetch the super class of the
4010 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004011 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004012 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004014 GenerateReferenceLoadOneRegister(instruction,
4015 out_loc,
4016 super_offset,
4017 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004018 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004019 // If `out` is null, we use it for the result, and jump to `done`.
4020 __ Cbz(out, &done);
4021 __ Cmp(out, cls);
4022 __ B(ne, &loop);
4023 __ Mov(out, 1);
4024 if (zero.IsLinked()) {
4025 __ B(&done);
4026 }
4027 break;
4028 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004029
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004030 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004031 ReadBarrierOption read_barrier_option =
4032 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004033 // /* HeapReference<Class> */ out = obj->klass_
4034 GenerateReferenceLoadTwoRegisters(instruction,
4035 out_loc,
4036 obj_loc,
4037 class_offset,
4038 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004039 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004040 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004041 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004042 __ Bind(&loop);
4043 __ Cmp(out, cls);
4044 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004045 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004046 GenerateReferenceLoadOneRegister(instruction,
4047 out_loc,
4048 super_offset,
4049 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004050 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004051 __ Cbnz(out, &loop);
4052 // If `out` is null, we use it for the result, and jump to `done`.
4053 __ B(&done);
4054 __ Bind(&success);
4055 __ Mov(out, 1);
4056 if (zero.IsLinked()) {
4057 __ B(&done);
4058 }
4059 break;
4060 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004061
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004062 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004063 ReadBarrierOption read_barrier_option =
4064 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004065 // /* HeapReference<Class> */ out = obj->klass_
4066 GenerateReferenceLoadTwoRegisters(instruction,
4067 out_loc,
4068 obj_loc,
4069 class_offset,
4070 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004071 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004072 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004073 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004074 __ Cmp(out, cls);
4075 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004076 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004077 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004078 GenerateReferenceLoadOneRegister(instruction,
4079 out_loc,
4080 component_offset,
4081 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004082 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004083 // If `out` is null, we use it for the result, and jump to `done`.
4084 __ Cbz(out, &done);
4085 __ Ldrh(out, HeapOperand(out, primitive_offset));
4086 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4087 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004088 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004089 __ Mov(out, 1);
4090 __ B(&done);
4091 break;
4092 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004093
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004094 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004095 // No read barrier since the slow path will retry upon failure.
4096 // /* HeapReference<Class> */ out = obj->klass_
4097 GenerateReferenceLoadTwoRegisters(instruction,
4098 out_loc,
4099 obj_loc,
4100 class_offset,
4101 maybe_temp_loc,
4102 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004103 __ Cmp(out, cls);
4104 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004105 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4106 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107 codegen_->AddSlowPath(slow_path);
4108 __ B(ne, slow_path->GetEntryLabel());
4109 __ Mov(out, 1);
4110 if (zero.IsLinked()) {
4111 __ B(&done);
4112 }
4113 break;
4114 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004115
Calin Juravle98893e12015-10-02 21:05:03 +01004116 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117 case TypeCheckKind::kInterfaceCheck: {
4118 // Note that we indeed only call on slow path, but we always go
4119 // into the slow path for the unresolved and interface check
4120 // cases.
4121 //
4122 // We cannot directly call the InstanceofNonTrivial runtime
4123 // entry point without resorting to a type checking slow path
4124 // here (i.e. by calling InvokeRuntime directly), as it would
4125 // require to assign fixed registers for the inputs of this
4126 // HInstanceOf instruction (following the runtime calling
4127 // convention), which might be cluttered by the potential first
4128 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004129 //
4130 // TODO: Introduce a new runtime entry point taking the object
4131 // to test (instead of its class) as argument, and let it deal
4132 // with the read barrier issues. This will let us refactor this
4133 // case of the `switch` code as it was previously (with a direct
4134 // call to the runtime not using a type checking slow path).
4135 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004136 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004137 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4138 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004139 codegen_->AddSlowPath(slow_path);
4140 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004141 if (zero.IsLinked()) {
4142 __ B(&done);
4143 }
4144 break;
4145 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004146
4147 case TypeCheckKind::kBitstringCheck: {
4148 // /* HeapReference<Class> */ temp = obj->klass_
4149 GenerateReferenceLoadTwoRegisters(instruction,
4150 out_loc,
4151 obj_loc,
4152 class_offset,
4153 maybe_temp_loc,
4154 kWithoutReadBarrier);
4155
4156 GenerateBitstringTypeCheckCompare(instruction, out);
4157 __ Cset(out, eq);
4158 if (zero.IsLinked()) {
4159 __ B(&done);
4160 }
4161 break;
4162 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004163 }
4164
4165 if (zero.IsLinked()) {
4166 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004167 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 }
4169
4170 if (done.IsLinked()) {
4171 __ Bind(&done);
4172 }
4173
4174 if (slow_path != nullptr) {
4175 __ Bind(slow_path->GetExitLabel());
4176 }
4177}
4178
4179void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004180 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004181 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004182 LocationSummary* locations =
4183 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004184 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004185 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4186 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4187 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4188 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4189 } else {
4190 locations->SetInAt(1, Location::RequiresRegister());
4191 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004192 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4193 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004194}
4195
4196void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004197 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004198 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004199 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004200 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004201 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4202 ? Register()
4203 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004204 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4205 DCHECK_GE(num_temps, 1u);
4206 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004207 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004208 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4209 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004210 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004211 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4212 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4213 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4214 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4215 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4216 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4217 const uint32_t object_array_data_offset =
4218 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004219
Vladimir Marko87584542017-12-12 17:47:52 +00004220 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004221 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004222 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4223 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004224 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225
Scott Wakeling97c72b72016-06-24 16:19:36 +01004226 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227 // Avoid null check if we know obj is not null.
4228 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004229 __ Cbz(obj, &done);
4230 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004231
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004232 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004233 case TypeCheckKind::kExactCheck:
4234 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004235 // /* HeapReference<Class> */ temp = obj->klass_
4236 GenerateReferenceLoadTwoRegisters(instruction,
4237 temp_loc,
4238 obj_loc,
4239 class_offset,
4240 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004241 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004242
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004243 __ Cmp(temp, cls);
4244 // Jump to slow path for throwing the exception or doing a
4245 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004247 break;
4248 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004249
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004250 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004251 // /* HeapReference<Class> */ temp = obj->klass_
4252 GenerateReferenceLoadTwoRegisters(instruction,
4253 temp_loc,
4254 obj_loc,
4255 class_offset,
4256 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004257 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004258
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004259 // If the class is abstract, we eagerly fetch the super class of the
4260 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004261 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004262 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004263 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004264 GenerateReferenceLoadOneRegister(instruction,
4265 temp_loc,
4266 super_offset,
4267 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004268 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004269
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004270 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4271 // exception.
4272 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4273 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004274 __ Cmp(temp, cls);
4275 __ B(ne, &loop);
4276 break;
4277 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004278
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004279 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004280 // /* HeapReference<Class> */ temp = obj->klass_
4281 GenerateReferenceLoadTwoRegisters(instruction,
4282 temp_loc,
4283 obj_loc,
4284 class_offset,
4285 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004286 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004287
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004288 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004289 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004290 __ Bind(&loop);
4291 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004292 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004293
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004294 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004295 GenerateReferenceLoadOneRegister(instruction,
4296 temp_loc,
4297 super_offset,
4298 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004299 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004300
4301 // If the class reference currently in `temp` is not null, jump
4302 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004303 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004304 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004305 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004306 break;
4307 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004309 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004310 // /* HeapReference<Class> */ temp = obj->klass_
4311 GenerateReferenceLoadTwoRegisters(instruction,
4312 temp_loc,
4313 obj_loc,
4314 class_offset,
4315 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004316 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004317
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004318 // Do an exact check.
4319 __ Cmp(temp, cls);
4320 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004321
4322 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004323 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004324 GenerateReferenceLoadOneRegister(instruction,
4325 temp_loc,
4326 component_offset,
4327 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004328 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004329
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004330 // If the component type is null, jump to the slow path to throw the exception.
4331 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4332 // Otherwise, the object is indeed an array. Further check that this component type is not a
4333 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004334 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4335 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004336 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004337 break;
4338 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004339
Calin Juravle98893e12015-10-02 21:05:03 +01004340 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004341 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004342 //
4343 // We cannot directly call the CheckCast runtime entry point
4344 // without resorting to a type checking slow path here (i.e. by
4345 // calling InvokeRuntime directly), as it would require to
4346 // assign fixed registers for the inputs of this HInstanceOf
4347 // instruction (following the runtime calling convention), which
4348 // might be cluttered by the potential first read barrier
4349 // emission at the beginning of this method.
4350 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004351 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004352 case TypeCheckKind::kInterfaceCheck: {
4353 // /* HeapReference<Class> */ temp = obj->klass_
4354 GenerateReferenceLoadTwoRegisters(instruction,
4355 temp_loc,
4356 obj_loc,
4357 class_offset,
4358 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004359 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004360
4361 // /* HeapReference<Class> */ temp = temp->iftable_
4362 GenerateReferenceLoadTwoRegisters(instruction,
4363 temp_loc,
4364 temp_loc,
4365 iftable_offset,
4366 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004367 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004368 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004369 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004370 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004371 vixl::aarch64::Label start_loop;
4372 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004373 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004374 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4375 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004376 // Go to next interface.
4377 __ Add(temp, temp, 2 * kHeapReferenceSize);
4378 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004379 // Compare the classes and continue the loop if they do not match.
4380 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4381 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004382 break;
4383 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004384
4385 case TypeCheckKind::kBitstringCheck: {
4386 // /* HeapReference<Class> */ temp = obj->klass_
4387 GenerateReferenceLoadTwoRegisters(instruction,
4388 temp_loc,
4389 obj_loc,
4390 class_offset,
4391 maybe_temp2_loc,
4392 kWithoutReadBarrier);
4393
4394 GenerateBitstringTypeCheckCompare(instruction, temp);
4395 __ B(ne, type_check_slow_path->GetEntryLabel());
4396 break;
4397 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004398 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004399 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004400
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004401 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004402}
4403
Alexandre Rames5319def2014-10-23 10:03:10 +01004404void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004405 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004406 locations->SetOut(Location::ConstantLocation(constant));
4407}
4408
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004409void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004410 // Will be generated at use site.
4411}
4412
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004413void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004414 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004415 locations->SetOut(Location::ConstantLocation(constant));
4416}
4417
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004418void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004419 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004420}
4421
Calin Juravle175dc732015-08-25 15:42:32 +01004422void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4423 // The trampoline uses the same calling convention as dex calling conventions,
4424 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4425 // the method_idx.
4426 HandleInvoke(invoke);
4427}
4428
4429void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4430 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004431 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004432}
4433
Alexandre Rames5319def2014-10-23 10:03:10 +01004434void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004435 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004436 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004437}
4438
Alexandre Rames67555f72014-11-18 10:55:16 +00004439void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4440 HandleInvoke(invoke);
4441}
4442
4443void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4444 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004445 LocationSummary* locations = invoke->GetLocations();
4446 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004447 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004448 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004449 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004450
4451 // The register ip1 is required to be used for the hidden argument in
4452 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004453 MacroAssembler* masm = GetVIXLAssembler();
4454 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004455 scratch_scope.Exclude(ip1);
4456 __ Mov(ip1, invoke->GetDexMethodIndex());
4457
Artem Serov914d7a82017-02-07 14:33:49 +00004458 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004459 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004460 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004461 {
4462 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4463 // /* HeapReference<Class> */ temp = temp->klass_
4464 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4465 codegen_->MaybeRecordImplicitNullCheck(invoke);
4466 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004467 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004468 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004469 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004470 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004471 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004472 }
Artem Serov914d7a82017-02-07 14:33:49 +00004473
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004474 // Instead of simply (possibly) unpoisoning `temp` here, we should
4475 // emit a read barrier for the previous class reference load.
4476 // However this is not required in practice, as this is an
4477 // intermediate/temporary reference and because the current
4478 // concurrent copying collector keeps the from-space memory
4479 // intact/accessible until the end of the marking phase (the
4480 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004481 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004482 __ Ldr(temp,
4483 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4484 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004485 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004486 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004487 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004488 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004489 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004490
4491 {
4492 // Ensure the pc position is recorded immediately after the `blr` instruction.
4493 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4494
4495 // lr();
4496 __ blr(lr);
4497 DCHECK(!codegen_->IsLeafMethod());
4498 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4499 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004500
4501 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004502}
4503
4504void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004505 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004506 if (intrinsic.TryDispatch(invoke)) {
4507 return;
4508 }
4509
Alexandre Rames67555f72014-11-18 10:55:16 +00004510 HandleInvoke(invoke);
4511}
4512
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004513void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004514 // Explicit clinit checks triggered by static invokes must have been pruned by
4515 // art::PrepareForRegisterAllocation.
4516 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004517
Vladimir Markoca6fff82017-10-03 14:49:14 +01004518 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004519 if (intrinsic.TryDispatch(invoke)) {
4520 return;
4521 }
4522
Alexandre Rames67555f72014-11-18 10:55:16 +00004523 HandleInvoke(invoke);
4524}
4525
Andreas Gampe878d58c2015-01-15 23:24:00 -08004526static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4527 if (invoke->GetLocations()->Intrinsified()) {
4528 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4529 intrinsic.Dispatch(invoke);
4530 return true;
4531 }
4532 return false;
4533}
4534
Vladimir Markodc151b22015-10-15 18:02:30 +01004535HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4536 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004537 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004538 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004539 return desired_dispatch_info;
4540}
4541
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004542void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4543 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004544 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004545 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4546 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004547 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4548 uint32_t offset =
4549 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004550 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004551 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004552 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004553 }
Vladimir Marko58155012015-08-19 12:49:41 +00004554 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004555 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004556 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004557 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4558 DCHECK(GetCompilerOptions().IsBootImage());
4559 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004560 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004561 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4562 // Add ADD with its PC-relative method patch.
4563 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004564 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004565 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4566 break;
4567 }
Vladimir Markob066d432018-01-03 13:14:37 +00004568 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4569 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004570 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004571 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4572 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4573 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4574 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4575 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4576 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4577 break;
4578 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004579 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004580 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004581 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4582 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004583 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004584 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004585 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004586 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004587 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004588 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004589 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004590 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4591 // Load method address from literal pool.
4592 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4593 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004594 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4595 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4596 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004597 }
4598 }
4599
4600 switch (invoke->GetCodePtrLocation()) {
4601 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004602 {
4603 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4604 ExactAssemblyScope eas(GetVIXLAssembler(),
4605 kInstructionSize,
4606 CodeBufferCheckScope::kExactSize);
4607 __ bl(&frame_entry_label_);
4608 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4609 }
Vladimir Marko58155012015-08-19 12:49:41 +00004610 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004611 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4612 // LR = callee_method->entry_point_from_quick_compiled_code_;
4613 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004614 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004615 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004616 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004617 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004618 ExactAssemblyScope eas(GetVIXLAssembler(),
4619 kInstructionSize,
4620 CodeBufferCheckScope::kExactSize);
4621 // lr()
4622 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004623 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004624 }
Vladimir Marko58155012015-08-19 12:49:41 +00004625 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004626 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004627
Andreas Gampe878d58c2015-01-15 23:24:00 -08004628 DCHECK(!IsLeafMethod());
4629}
4630
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004631void CodeGeneratorARM64::GenerateVirtualCall(
4632 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004633 // Use the calling convention instead of the location of the receiver, as
4634 // intrinsics may have put the receiver in a different register. In the intrinsics
4635 // slow path, the arguments have been moved to the right place, so here we are
4636 // guaranteed that the receiver is the first register of the calling convention.
4637 InvokeDexCallingConvention calling_convention;
4638 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004639 Register temp = XRegisterFrom(temp_in);
4640 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4641 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4642 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004643 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004644
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004645 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004646
4647 {
4648 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4649 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4650 // /* HeapReference<Class> */ temp = receiver->klass_
4651 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4652 MaybeRecordImplicitNullCheck(invoke);
4653 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004654 // Instead of simply (possibly) unpoisoning `temp` here, we should
4655 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004656 // intermediate/temporary reference and because the current
4657 // concurrent copying collector keeps the from-space memory
4658 // intact/accessible until the end of the marking phase (the
4659 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004660 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4661 // temp = temp->GetMethodAt(method_offset);
4662 __ Ldr(temp, MemOperand(temp, method_offset));
4663 // lr = temp->GetEntryPoint();
4664 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004665 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004666 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004667 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4668 // lr();
4669 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004670 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004671 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004672}
4673
Orion Hodsonac141392017-01-13 11:53:47 +00004674void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4675 HandleInvoke(invoke);
4676}
4677
4678void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4679 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004680 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004681}
4682
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004683void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4684 HandleInvoke(invoke);
4685}
4686
4687void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4688 codegen_->GenerateInvokeCustomCall(invoke);
4689 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
4690}
4691
Vladimir Marko6fd16062018-06-26 11:02:04 +01004692vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4693 uint32_t intrinsic_data,
4694 vixl::aarch64::Label* adrp_label) {
4695 return NewPcRelativePatch(
4696 /* dex_file */ nullptr, intrinsic_data, adrp_label, &boot_image_intrinsic_patches_);
4697}
4698
Vladimir Markob066d432018-01-03 13:14:37 +00004699vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4700 uint32_t boot_image_offset,
4701 vixl::aarch64::Label* adrp_label) {
4702 return NewPcRelativePatch(
4703 /* dex_file */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
4704}
4705
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004706vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004707 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004708 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004709 return NewPcRelativePatch(
4710 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004711}
4712
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004713vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4714 MethodReference target_method,
4715 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004716 return NewPcRelativePatch(
4717 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004718}
4719
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004720vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004721 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004722 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004723 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004724 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004725}
4726
Vladimir Marko1998cd02017-01-13 13:02:58 +00004727vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4728 const DexFile& dex_file,
4729 dex::TypeIndex type_index,
4730 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004731 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004732}
4733
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004734vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004735 const DexFile& dex_file,
4736 dex::StringIndex string_index,
4737 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004738 return NewPcRelativePatch(
4739 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004740}
4741
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004742vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4743 const DexFile& dex_file,
4744 dex::StringIndex string_index,
4745 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004746 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004747}
4748
Nicolas Geoffray756e7222018-08-02 17:53:46 +00004749vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4750 baker_read_barrier_patches_.emplace_back(custom_data);
4751 return &baker_read_barrier_patches_.back().label;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004752}
4753
Scott Wakeling97c72b72016-06-24 16:19:36 +01004754vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004755 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004756 uint32_t offset_or_index,
4757 vixl::aarch64::Label* adrp_label,
4758 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004759 // Add a patch entry and return the label.
4760 patches->emplace_back(dex_file, offset_or_index);
4761 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004762 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004763 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4764 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4765 return label;
4766}
4767
Scott Wakeling97c72b72016-06-24 16:19:36 +01004768vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4769 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004770 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004771}
4772
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004773vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004774 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004775 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004776 return jit_string_patches_.GetOrCreate(
4777 StringReference(&dex_file, string_index),
4778 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4779}
4780
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004781vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004782 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004783 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004784 return jit_class_patches_.GetOrCreate(
4785 TypeReference(&dex_file, type_index),
4786 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4787}
4788
Vladimir Markoaad75c62016-10-03 08:46:48 +00004789void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4790 vixl::aarch64::Register reg) {
4791 DCHECK(reg.IsX());
4792 SingleEmissionCheckScope guard(GetVIXLAssembler());
4793 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004794 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004795}
4796
4797void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4798 vixl::aarch64::Register out,
4799 vixl::aarch64::Register base) {
4800 DCHECK(out.IsX());
4801 DCHECK(base.IsX());
4802 SingleEmissionCheckScope guard(GetVIXLAssembler());
4803 __ Bind(fixup_label);
4804 __ add(out, base, Operand(/* offset placeholder */ 0));
4805}
4806
4807void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4808 vixl::aarch64::Register out,
4809 vixl::aarch64::Register base) {
4810 DCHECK(base.IsX());
4811 SingleEmissionCheckScope guard(GetVIXLAssembler());
4812 __ Bind(fixup_label);
4813 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4814}
4815
Vladimir Markoeebb8212018-06-05 14:57:24 +01004816void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004817 uint32_t boot_image_reference) {
4818 if (GetCompilerOptions().IsBootImage()) {
4819 // Add ADRP with its PC-relative type patch.
4820 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4821 EmitAdrpPlaceholder(adrp_label, reg.X());
4822 // Add ADD with its PC-relative type patch.
4823 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4824 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004825 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004826 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004827 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004828 EmitAdrpPlaceholder(adrp_label, reg.X());
4829 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004830 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004831 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4832 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004833 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004834 gc::Heap* heap = Runtime::Current()->GetHeap();
4835 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004836 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004837 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4838 }
4839}
4840
Vladimir Marko6fd16062018-06-26 11:02:04 +01004841void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4842 uint32_t boot_image_offset) {
4843 DCHECK(invoke->IsStatic());
4844 InvokeRuntimeCallingConvention calling_convention;
4845 Register argument = calling_convention.GetRegisterAt(0);
4846 if (GetCompilerOptions().IsBootImage()) {
4847 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4848 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4849 MethodReference target_method = invoke->GetTargetMethod();
4850 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4851 // Add ADRP with its PC-relative type patch.
4852 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4853 EmitAdrpPlaceholder(adrp_label, argument.X());
4854 // Add ADD with its PC-relative type patch.
4855 vixl::aarch64::Label* add_label =
4856 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4857 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4858 } else {
4859 LoadBootImageAddress(argument, boot_image_offset);
4860 }
4861 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4862 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4863}
4864
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004865template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004866inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4867 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004868 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004869 for (const PcRelativePatchInfo& info : infos) {
4870 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004871 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004872 info.pc_insn_label->GetLocation(),
4873 info.offset_or_index));
4874 }
4875}
4876
Vladimir Marko6fd16062018-06-26 11:02:04 +01004877template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4878linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4879 const DexFile* target_dex_file,
4880 uint32_t pc_insn_offset,
4881 uint32_t boot_image_offset) {
4882 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4883 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004884}
4885
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004886void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004887 DCHECK(linker_patches->empty());
4888 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004889 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004890 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004891 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004892 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004893 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004894 string_bss_entry_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01004895 boot_image_intrinsic_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004896 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004897 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004898 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004899 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004900 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004901 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004902 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004903 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004904 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004905 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4906 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004907 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004908 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00004909 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004910 DCHECK(boot_image_type_patches_.empty());
4911 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004912 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004913 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004914 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4915 method_bss_entry_patches_, linker_patches);
4916 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4917 type_bss_entry_patches_, linker_patches);
4918 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4919 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004920 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004921 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4922 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004923 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004924 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004925}
4926
Vladimir Markoca1e0382018-04-11 09:58:41 +00004927bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
4928 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
4929 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4930}
4931
4932void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4933 /*out*/ ArenaVector<uint8_t>* code,
4934 /*out*/ std::string* debug_name) {
4935 Arm64Assembler assembler(GetGraph()->GetAllocator());
4936 switch (patch.GetType()) {
4937 case linker::LinkerPatch::Type::kCallRelative: {
4938 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4939 // to the generic JNI and interpreter trampolines.
4940 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4941 kArm64PointerSize).Int32Value());
4942 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4943 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4944 *debug_name = "MethodCallThunk";
4945 }
4946 break;
4947 }
4948 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4949 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4950 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4951 break;
4952 }
4953 default:
4954 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4955 UNREACHABLE();
4956 }
4957
4958 // Ensure we emit the literal pool if any.
4959 assembler.FinalizeCode();
4960 code->resize(assembler.CodeSize());
4961 MemoryRegion code_region(code->data(), code->size());
4962 assembler.FinalizeInstructions(code_region);
4963}
4964
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004965vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4966 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004967 value,
4968 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4969}
4970
Scott Wakeling97c72b72016-06-24 16:19:36 +01004971vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004972 return uint64_literals_.GetOrCreate(
4973 value,
4974 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004975}
4976
Andreas Gampe878d58c2015-01-15 23:24:00 -08004977void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004978 // Explicit clinit checks triggered by static invokes must have been pruned by
4979 // art::PrepareForRegisterAllocation.
4980 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004981
Andreas Gampe878d58c2015-01-15 23:24:00 -08004982 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004983 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004984 return;
4985 }
4986
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004987 {
4988 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4989 // are no pools emitted.
4990 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4991 LocationSummary* locations = invoke->GetLocations();
4992 codegen_->GenerateStaticOrDirectCall(
4993 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4994 }
4995
4996 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004997}
4998
4999void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08005000 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005001 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08005002 return;
5003 }
5004
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005005 {
5006 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
5007 // are no pools emitted.
5008 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
5009 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
5010 DCHECK(!codegen_->IsLeafMethod());
5011 }
5012
5013 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005014}
5015
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005016HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5017 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005018 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005019 case HLoadClass::LoadKind::kInvalid:
5020 LOG(FATAL) << "UNREACHABLE";
5021 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005022 case HLoadClass::LoadKind::kReferrersClass:
5023 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005024 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005025 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005026 case HLoadClass::LoadKind::kBssEntry:
5027 DCHECK(!Runtime::Current()->UseJitCompilation());
5028 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005029 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005030 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005031 DCHECK(Runtime::Current()->UseJitCompilation());
5032 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005033 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005034 break;
5035 }
5036 return desired_class_load_kind;
5037}
5038
Alexandre Rames67555f72014-11-18 10:55:16 +00005039void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005040 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005041 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005042 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005043 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005044 cls,
5045 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005046 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005047 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005048 return;
5049 }
Vladimir Marko41559982017-01-06 14:04:23 +00005050 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005051
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005052 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5053 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005054 ? LocationSummary::kCallOnSlowPath
5055 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005056 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005057 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005058 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005059 }
5060
Vladimir Marko41559982017-01-06 14:04:23 +00005061 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005062 locations->SetInAt(0, Location::RequiresRegister());
5063 }
5064 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005065 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5066 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5067 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005068 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005069 } else {
5070 // For non-Baker read barrier we have a temp-clobbering call.
5071 }
5072 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005073}
5074
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005075// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5076// move.
5077void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005078 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005079 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005080 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005081 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005082 return;
5083 }
Vladimir Marko41559982017-01-06 14:04:23 +00005084 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005085
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005086 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005087 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005088
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005089 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5090 ? kWithoutReadBarrier
5091 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005092 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005093 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005094 case HLoadClass::LoadKind::kReferrersClass: {
5095 DCHECK(!cls->CanCallRuntime());
5096 DCHECK(!cls->MustGenerateClinitCheck());
5097 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5098 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005099 codegen_->GenerateGcRootFieldLoad(cls,
5100 out_loc,
5101 current_method,
5102 ArtMethod::DeclaringClassOffset().Int32Value(),
5103 /* fixup_label */ nullptr,
5104 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005105 break;
5106 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005107 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005108 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005109 // Add ADRP with its PC-relative type patch.
5110 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005111 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005112 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005113 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005114 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005115 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005116 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005117 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005118 break;
5119 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005120 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005121 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005122 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5123 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5124 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005125 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005126 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005127 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005128 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005129 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005130 break;
5131 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005132 case HLoadClass::LoadKind::kBssEntry: {
5133 // Add ADRP with its PC-relative Class .bss entry patch.
5134 const DexFile& dex_file = cls->GetDexFile();
5135 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005136 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
5137 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5138 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005139 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005140 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005141 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005142 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005143 codegen_->GenerateGcRootFieldLoad(cls,
5144 out_loc,
5145 temp,
5146 /* offset placeholder */ 0u,
5147 ldr_label,
5148 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005149 generate_null_check = true;
5150 break;
5151 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005152 case HLoadClass::LoadKind::kJitBootImageAddress: {
5153 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5154 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5155 DCHECK_NE(address, 0u);
5156 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5157 break;
5158 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005159 case HLoadClass::LoadKind::kJitTableAddress: {
5160 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5161 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005162 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005163 codegen_->GenerateGcRootFieldLoad(cls,
5164 out_loc,
5165 out.X(),
5166 /* offset */ 0,
5167 /* fixup_label */ nullptr,
5168 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005169 break;
5170 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005171 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005172 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005173 LOG(FATAL) << "UNREACHABLE";
5174 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005175 }
5176
Vladimir Markoea4c1262017-02-06 19:59:33 +00005177 bool do_clinit = cls->MustGenerateClinitCheck();
5178 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005179 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01005180 SlowPathCodeARM64* slow_path =
5181 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005182 codegen_->AddSlowPath(slow_path);
5183 if (generate_null_check) {
5184 __ Cbz(out, slow_path->GetEntryLabel());
5185 }
5186 if (cls->MustGenerateClinitCheck()) {
5187 GenerateClassInitializationCheck(slow_path, out);
5188 } else {
5189 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005190 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005191 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005192 }
5193}
5194
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005195void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5196 InvokeRuntimeCallingConvention calling_convention;
5197 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5198 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5199}
5200
5201void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5202 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5203}
5204
Orion Hodson18259d72018-04-12 11:18:23 +01005205void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5206 InvokeRuntimeCallingConvention calling_convention;
5207 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5208 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5209}
5210
5211void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5212 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5213}
5214
David Brazdilcb1c0552015-08-04 16:22:25 +01005215static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005216 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005217}
5218
Alexandre Rames67555f72014-11-18 10:55:16 +00005219void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5220 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005221 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005222 locations->SetOut(Location::RequiresRegister());
5223}
5224
5225void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005226 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5227}
5228
5229void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005230 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005231}
5232
5233void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5234 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005235}
5236
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005237HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5238 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005239 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005240 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005241 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005242 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005243 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005244 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005245 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005246 case HLoadString::LoadKind::kJitTableAddress:
5247 DCHECK(Runtime::Current()->UseJitCompilation());
5248 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005249 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005250 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005251 }
5252 return desired_string_load_kind;
5253}
5254
Alexandre Rames67555f72014-11-18 10:55:16 +00005255void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005256 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005257 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005258 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005259 InvokeRuntimeCallingConvention calling_convention;
5260 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5261 } else {
5262 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005263 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5264 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005265 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005266 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005267 } else {
5268 // For non-Baker read barrier we have a temp-clobbering call.
5269 }
5270 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005271 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005272}
5273
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005274// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5275// move.
5276void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005277 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005278 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005279
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005280 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005281 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005282 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005283 // Add ADRP with its PC-relative String patch.
5284 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005285 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005286 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005287 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005288 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005289 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005290 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005291 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005292 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005293 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005294 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005295 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005296 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5297 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5298 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005299 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005300 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005301 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005302 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005303 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5304 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005305 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005306 case HLoadString::LoadKind::kBssEntry: {
5307 // Add ADRP with its PC-relative String .bss entry patch.
5308 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005309 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005310 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof3c52b42017-11-17 17:32:12 +00005311 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005312 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005313 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005314 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005315 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005316 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005317 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005318 codegen_->GenerateGcRootFieldLoad(load,
5319 out_loc,
5320 temp,
5321 /* offset placeholder */ 0u,
5322 ldr_label,
5323 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005324 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005325 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005326 codegen_->AddSlowPath(slow_path);
5327 __ Cbz(out.X(), slow_path->GetEntryLabel());
5328 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005329 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005330 return;
5331 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005332 case HLoadString::LoadKind::kJitBootImageAddress: {
5333 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5334 DCHECK_NE(address, 0u);
5335 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5336 return;
5337 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005338 case HLoadString::LoadKind::kJitTableAddress: {
5339 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005340 load->GetStringIndex(),
5341 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005342 codegen_->GenerateGcRootFieldLoad(load,
5343 out_loc,
5344 out.X(),
5345 /* offset */ 0,
5346 /* fixup_label */ nullptr,
5347 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005348 return;
5349 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005350 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005351 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005352 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005353
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005354 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005355 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005356 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005357 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005358 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5359 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005360 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005361}
5362
Alexandre Rames5319def2014-10-23 10:03:10 +01005363void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005364 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005365 locations->SetOut(Location::ConstantLocation(constant));
5366}
5367
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005368void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005369 // Will be generated at use site.
5370}
5371
Alexandre Rames67555f72014-11-18 10:55:16 +00005372void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005373 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5374 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005375 InvokeRuntimeCallingConvention calling_convention;
5376 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5377}
5378
5379void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005380 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005381 instruction,
5382 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005383 if (instruction->IsEnter()) {
5384 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5385 } else {
5386 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5387 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005388 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005389}
5390
Alexandre Rames42d641b2014-10-27 14:00:51 +00005391void LocationsBuilderARM64::VisitMul(HMul* mul) {
5392 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005393 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005394 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005395 case DataType::Type::kInt32:
5396 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005397 locations->SetInAt(0, Location::RequiresRegister());
5398 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005400 break;
5401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 case DataType::Type::kFloat32:
5403 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005404 locations->SetInAt(0, Location::RequiresFpuRegister());
5405 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005406 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005407 break;
5408
5409 default:
5410 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5411 }
5412}
5413
5414void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5415 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 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5419 break;
5420
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005421 case DataType::Type::kFloat32:
5422 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005423 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005424 break;
5425
5426 default:
5427 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5428 }
5429}
5430
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005431void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5432 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005433 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005434 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 case DataType::Type::kInt32:
5436 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005437 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005439 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005441 case DataType::Type::kFloat32:
5442 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005443 locations->SetInAt(0, Location::RequiresFpuRegister());
5444 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005445 break;
5446
5447 default:
5448 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5449 }
5450}
5451
5452void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5453 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 case DataType::Type::kInt32:
5455 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005456 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5457 break;
5458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005459 case DataType::Type::kFloat32:
5460 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005461 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005462 break;
5463
5464 default:
5465 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5466 }
5467}
5468
5469void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005470 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5471 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005472 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005473 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005474 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5475 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005476}
5477
5478void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005479 // Note: if heap poisoning is enabled, the entry point takes cares
5480 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005481 QuickEntrypointEnum entrypoint =
5482 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5483 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005484 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005485 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005486}
5487
Alexandre Rames5319def2014-10-23 10:03:10 +01005488void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005489 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5490 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005491 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005492 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005493 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005494}
5495
5496void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005497 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5498 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005499 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005500}
5501
5502void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005503 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005504 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005506}
5507
5508void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005509 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005510 case DataType::Type::kInt32:
5511 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005512 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005513 break;
5514
5515 default:
5516 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5517 }
5518}
5519
David Brazdil66d126e2015-04-03 16:02:44 +01005520void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005521 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005522 locations->SetInAt(0, Location::RequiresRegister());
5523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5524}
5525
5526void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005527 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005528}
5529
Alexandre Rames5319def2014-10-23 10:03:10 +01005530void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005531 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5532 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005533}
5534
Calin Juravle2ae48182016-03-16 14:05:09 +00005535void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5536 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005537 return;
5538 }
Artem Serov914d7a82017-02-07 14:33:49 +00005539 {
5540 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5541 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5542 Location obj = instruction->GetLocations()->InAt(0);
5543 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5544 RecordPcInfo(instruction, instruction->GetDexPc());
5545 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005546}
5547
Calin Juravle2ae48182016-03-16 14:05:09 +00005548void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005549 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005550 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005551
5552 LocationSummary* locations = instruction->GetLocations();
5553 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005554
5555 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005556}
5557
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005558void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005559 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005560}
5561
Alexandre Rames67555f72014-11-18 10:55:16 +00005562void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5563 HandleBinaryOp(instruction);
5564}
5565
5566void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5567 HandleBinaryOp(instruction);
5568}
5569
Alexandre Rames3e69f162014-12-10 10:36:50 +00005570void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5571 LOG(FATAL) << "Unreachable";
5572}
5573
5574void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005575 if (instruction->GetNext()->IsSuspendCheck() &&
5576 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5577 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5578 // The back edge will generate the suspend check.
5579 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5580 }
5581
Alexandre Rames3e69f162014-12-10 10:36:50 +00005582 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5583}
5584
Alexandre Rames5319def2014-10-23 10:03:10 +01005585void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005586 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005587 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5588 if (location.IsStackSlot()) {
5589 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5590 } else if (location.IsDoubleStackSlot()) {
5591 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5592 }
5593 locations->SetOut(location);
5594}
5595
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005596void InstructionCodeGeneratorARM64::VisitParameterValue(
5597 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005598 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005599}
5600
5601void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5602 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005603 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005604 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005605}
5606
5607void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5608 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5609 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005610}
5611
5612void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005613 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005614 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005615 locations->SetInAt(i, Location::Any());
5616 }
5617 locations->SetOut(Location::Any());
5618}
5619
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005620void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005621 LOG(FATAL) << "Unreachable";
5622}
5623
Serban Constantinescu02164b32014-11-13 14:05:07 +00005624void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005626 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005627 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005628 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005629 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005630
5631 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005632 case DataType::Type::kInt32:
5633 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005634 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005635 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005636 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5637 break;
5638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005639 case DataType::Type::kFloat32:
5640 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005641 InvokeRuntimeCallingConvention calling_convention;
5642 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5643 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5644 locations->SetOut(calling_convention.GetReturnLocation(type));
5645
5646 break;
5647 }
5648
Serban Constantinescu02164b32014-11-13 14:05:07 +00005649 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005650 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005651 }
5652}
5653
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005654void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005655 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005656 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5657 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5658
5659 Register out = OutputRegister(instruction);
5660 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005661
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005662 if (abs_imm == 2) {
5663 __ Cmp(dividend, 0);
5664 __ And(out, dividend, 1);
5665 __ Csneg(out, out, out, ge);
5666 } else {
5667 UseScratchRegisterScope temps(GetVIXLAssembler());
5668 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005669
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005670 __ Negs(temp, dividend);
5671 __ And(out, dividend, abs_imm - 1);
5672 __ And(temp, temp, abs_imm - 1);
5673 __ Csneg(out, out, temp, mi);
5674 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005675}
5676
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005677void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005678 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005679
5680 if (imm == 0) {
5681 // Do not generate anything.
5682 // DivZeroCheck would prevent any code to be executed.
5683 return;
5684 }
5685
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005686 if (IsPowerOfTwo(AbsOrMin(imm))) {
5687 // Cases imm == -1 or imm == 1 are handled in constant folding by
5688 // InstructionWithAbsorbingInputSimplifier.
5689 // If the cases have survided till code generation they are handled in
5690 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5691 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005692 GenerateIntRemForPower2Denom(instruction);
5693 } else {
5694 DCHECK(imm < -2 || imm > 2) << imm;
5695 GenerateDivRemWithAnyConstant(instruction);
5696 }
5697}
5698
5699void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5700 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5701 << instruction->GetResultType();
5702
5703 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5704 GenerateIntRemForConstDenom(instruction);
5705 } else {
5706 Register out = OutputRegister(instruction);
5707 Register dividend = InputRegisterAt(instruction, 0);
5708 Register divisor = InputRegisterAt(instruction, 1);
5709 UseScratchRegisterScope temps(GetVIXLAssembler());
5710 Register temp = temps.AcquireSameSizeAs(out);
5711 __ Sdiv(temp, dividend, divisor);
5712 __ Msub(out, temp, divisor, dividend);
5713 }
5714}
5715
Serban Constantinescu02164b32014-11-13 14:05:07 +00005716void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005718
Serban Constantinescu02164b32014-11-13 14:05:07 +00005719 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 case DataType::Type::kInt32:
5721 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005722 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005723 break;
5724 }
5725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005726 case DataType::Type::kFloat32:
5727 case DataType::Type::kFloat64: {
5728 QuickEntrypointEnum entrypoint =
5729 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005730 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005731 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005732 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5733 } else {
5734 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5735 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005736 break;
5737 }
5738
Serban Constantinescu02164b32014-11-13 14:05:07 +00005739 default:
5740 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005741 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005742 }
5743}
5744
Aart Bik1f8d51b2018-02-15 10:42:37 -08005745void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005746 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005747}
5748
Aart Bik1f8d51b2018-02-15 10:42:37 -08005749void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005750 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005751}
5752
5753void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005754 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005755}
5756
5757void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005758 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005759}
5760
Aart Bik3dad3412018-02-28 12:01:46 -08005761void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5762 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5763 switch (abs->GetResultType()) {
5764 case DataType::Type::kInt32:
5765 case DataType::Type::kInt64:
5766 locations->SetInAt(0, Location::RequiresRegister());
5767 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5768 break;
5769 case DataType::Type::kFloat32:
5770 case DataType::Type::kFloat64:
5771 locations->SetInAt(0, Location::RequiresFpuRegister());
5772 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5773 break;
5774 default:
5775 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5776 }
5777}
5778
5779void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5780 switch (abs->GetResultType()) {
5781 case DataType::Type::kInt32:
5782 case DataType::Type::kInt64: {
5783 Register in_reg = InputRegisterAt(abs, 0);
5784 Register out_reg = OutputRegister(abs);
5785 __ Cmp(in_reg, Operand(0));
5786 __ Cneg(out_reg, in_reg, lt);
5787 break;
5788 }
5789 case DataType::Type::kFloat32:
5790 case DataType::Type::kFloat64: {
5791 FPRegister in_reg = InputFPRegisterAt(abs, 0);
5792 FPRegister out_reg = OutputFPRegister(abs);
5793 __ Fabs(out_reg, in_reg);
5794 break;
5795 }
5796 default:
5797 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5798 }
5799}
5800
Igor Murashkind01745e2017-04-05 16:40:31 -07005801void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5802 constructor_fence->SetLocations(nullptr);
5803}
5804
5805void InstructionCodeGeneratorARM64::VisitConstructorFence(
5806 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5807 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5808}
5809
Calin Juravle27df7582015-04-17 19:12:31 +01005810void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5811 memory_barrier->SetLocations(nullptr);
5812}
5813
5814void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005815 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005816}
5817
Alexandre Rames5319def2014-10-23 10:03:10 +01005818void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005819 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005821 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005822}
5823
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005824void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005825 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005826}
5827
5828void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5829 instruction->SetLocations(nullptr);
5830}
5831
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005832void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005833 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005834}
5835
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005836void LocationsBuilderARM64::VisitRor(HRor* ror) {
5837 HandleBinaryOp(ror);
5838}
5839
5840void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5841 HandleBinaryOp(ror);
5842}
5843
Serban Constantinescu02164b32014-11-13 14:05:07 +00005844void LocationsBuilderARM64::VisitShl(HShl* shl) {
5845 HandleShift(shl);
5846}
5847
5848void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5849 HandleShift(shl);
5850}
5851
5852void LocationsBuilderARM64::VisitShr(HShr* shr) {
5853 HandleShift(shr);
5854}
5855
5856void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5857 HandleShift(shr);
5858}
5859
Alexandre Rames5319def2014-10-23 10:03:10 +01005860void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005861 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005862}
5863
5864void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005865 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005866}
5867
Alexandre Rames67555f72014-11-18 10:55:16 +00005868void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005869 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005870}
5871
5872void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005873 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005874}
5875
5876void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005877 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005878}
5879
Alexandre Rames67555f72014-11-18 10:55:16 +00005880void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005881 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005882}
5883
Calin Juravlee460d1d2015-09-29 04:52:17 +01005884void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5885 HUnresolvedInstanceFieldGet* instruction) {
5886 FieldAccessCallingConventionARM64 calling_convention;
5887 codegen_->CreateUnresolvedFieldLocationSummary(
5888 instruction, instruction->GetFieldType(), calling_convention);
5889}
5890
5891void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5892 HUnresolvedInstanceFieldGet* instruction) {
5893 FieldAccessCallingConventionARM64 calling_convention;
5894 codegen_->GenerateUnresolvedFieldAccess(instruction,
5895 instruction->GetFieldType(),
5896 instruction->GetFieldIndex(),
5897 instruction->GetDexPc(),
5898 calling_convention);
5899}
5900
5901void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5902 HUnresolvedInstanceFieldSet* instruction) {
5903 FieldAccessCallingConventionARM64 calling_convention;
5904 codegen_->CreateUnresolvedFieldLocationSummary(
5905 instruction, instruction->GetFieldType(), calling_convention);
5906}
5907
5908void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5909 HUnresolvedInstanceFieldSet* instruction) {
5910 FieldAccessCallingConventionARM64 calling_convention;
5911 codegen_->GenerateUnresolvedFieldAccess(instruction,
5912 instruction->GetFieldType(),
5913 instruction->GetFieldIndex(),
5914 instruction->GetDexPc(),
5915 calling_convention);
5916}
5917
5918void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5919 HUnresolvedStaticFieldGet* instruction) {
5920 FieldAccessCallingConventionARM64 calling_convention;
5921 codegen_->CreateUnresolvedFieldLocationSummary(
5922 instruction, instruction->GetFieldType(), calling_convention);
5923}
5924
5925void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5926 HUnresolvedStaticFieldGet* instruction) {
5927 FieldAccessCallingConventionARM64 calling_convention;
5928 codegen_->GenerateUnresolvedFieldAccess(instruction,
5929 instruction->GetFieldType(),
5930 instruction->GetFieldIndex(),
5931 instruction->GetDexPc(),
5932 calling_convention);
5933}
5934
5935void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5936 HUnresolvedStaticFieldSet* instruction) {
5937 FieldAccessCallingConventionARM64 calling_convention;
5938 codegen_->CreateUnresolvedFieldLocationSummary(
5939 instruction, instruction->GetFieldType(), calling_convention);
5940}
5941
5942void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5943 HUnresolvedStaticFieldSet* instruction) {
5944 FieldAccessCallingConventionARM64 calling_convention;
5945 codegen_->GenerateUnresolvedFieldAccess(instruction,
5946 instruction->GetFieldType(),
5947 instruction->GetFieldIndex(),
5948 instruction->GetDexPc(),
5949 calling_convention);
5950}
5951
Alexandre Rames5319def2014-10-23 10:03:10 +01005952void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005953 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5954 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005955 // In suspend check slow path, usually there are no caller-save registers at all.
5956 // If SIMD instructions are present, however, we force spilling all live SIMD
5957 // registers in full width (since the runtime only saves/restores lower part).
5958 locations->SetCustomSlowPathCallerSaves(
5959 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005960}
5961
5962void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005963 HBasicBlock* block = instruction->GetBlock();
5964 if (block->GetLoopInformation() != nullptr) {
5965 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5966 // The back edge will generate the suspend check.
5967 return;
5968 }
5969 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5970 // The goto will generate the suspend check.
5971 return;
5972 }
5973 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005974 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005975}
5976
Alexandre Rames67555f72014-11-18 10:55:16 +00005977void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005978 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5979 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005980 InvokeRuntimeCallingConvention calling_convention;
5981 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5982}
5983
5984void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005985 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005986 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005987}
5988
5989void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5990 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005991 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005992 DataType::Type input_type = conversion->GetInputType();
5993 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005994 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5995 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005996 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5997 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005998 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5999 }
6000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006001 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006002 locations->SetInAt(0, Location::RequiresFpuRegister());
6003 } else {
6004 locations->SetInAt(0, Location::RequiresRegister());
6005 }
6006
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006007 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00006008 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6009 } else {
6010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6011 }
6012}
6013
6014void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006015 DataType::Type result_type = conversion->GetResultType();
6016 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00006017
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006018 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6019 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006021 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6022 int result_size = DataType::Size(result_type);
6023 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006024 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006025 Register output = OutputRegister(conversion);
6026 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006027 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006028 // 'int' values are used directly as W registers, discarding the top
6029 // bits, so we don't need to sign-extend and can just perform a move.
6030 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6031 // top 32 bits of the target register. We theoretically could leave those
6032 // bits unchanged, but we would have to make sure that no code uses a
6033 // 32bit input value as a 64bit value assuming that the top 32 bits are
6034 // zero.
6035 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006036 } else if (DataType::IsUnsignedType(result_type) ||
6037 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6038 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006039 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006040 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006041 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006043 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006044 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6045 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006046 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006047 } else if (DataType::IsFloatingPointType(result_type) &&
6048 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006049 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6050 } else {
6051 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6052 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006053 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006054}
Alexandre Rames67555f72014-11-18 10:55:16 +00006055
Serban Constantinescu02164b32014-11-13 14:05:07 +00006056void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6057 HandleShift(ushr);
6058}
6059
6060void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6061 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006062}
6063
6064void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6065 HandleBinaryOp(instruction);
6066}
6067
6068void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6069 HandleBinaryOp(instruction);
6070}
6071
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006072void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006073 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006074 LOG(FATAL) << "Unreachable";
6075}
6076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006077void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006078 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006079 LOG(FATAL) << "Unreachable";
6080}
6081
Mark Mendellfe57faa2015-09-18 09:26:15 -04006082// Simple implementation of packed switch - generate cascaded compare/jumps.
6083void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6084 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006085 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006086 locations->SetInAt(0, Location::RequiresRegister());
6087}
6088
6089void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6090 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006091 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006092 Register value_reg = InputRegisterAt(switch_instr, 0);
6093 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6094
Zheng Xu3927c8b2015-11-18 17:46:25 +08006095 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006096 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006097 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6098 // make sure we don't emit it if the target may run out of range.
6099 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6100 // ranges and emit the tables only as required.
6101 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006102
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006103 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006104 // Current instruction id is an upper bound of the number of HIRs in the graph.
6105 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6106 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006107 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6108 Register temp = temps.AcquireW();
6109 __ Subs(temp, value_reg, Operand(lower_bound));
6110
Zheng Xu3927c8b2015-11-18 17:46:25 +08006111 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006112 // Jump to successors[0] if value == lower_bound.
6113 __ B(eq, codegen_->GetLabelOf(successors[0]));
6114 int32_t last_index = 0;
6115 for (; num_entries - last_index > 2; last_index += 2) {
6116 __ Subs(temp, temp, Operand(2));
6117 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6118 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6119 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6120 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6121 }
6122 if (num_entries - last_index == 2) {
6123 // The last missing case_value.
6124 __ Cmp(temp, Operand(1));
6125 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006126 }
6127
6128 // And the default for any other value.
6129 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6130 __ B(codegen_->GetLabelOf(default_block));
6131 }
6132 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006133 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006134
6135 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6136
6137 // Below instructions should use at most one blocked register. Since there are two blocked
6138 // registers, we are free to block one.
6139 Register temp_w = temps.AcquireW();
6140 Register index;
6141 // Remove the bias.
6142 if (lower_bound != 0) {
6143 index = temp_w;
6144 __ Sub(index, value_reg, Operand(lower_bound));
6145 } else {
6146 index = value_reg;
6147 }
6148
6149 // Jump to default block if index is out of the range.
6150 __ Cmp(index, Operand(num_entries));
6151 __ B(hs, codegen_->GetLabelOf(default_block));
6152
6153 // In current VIXL implementation, it won't require any blocked registers to encode the
6154 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6155 // register pressure.
6156 Register table_base = temps.AcquireX();
6157 // Load jump offset from the table.
6158 __ Adr(table_base, jump_table->GetTableStartLabel());
6159 Register jump_offset = temp_w;
6160 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6161
6162 // Jump to target block by branching to table_base(pc related) + offset.
6163 Register target_address = table_base;
6164 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6165 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006166 }
6167}
6168
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006169void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6170 HInstruction* instruction,
6171 Location out,
6172 uint32_t offset,
6173 Location maybe_temp,
6174 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006175 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006176 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006177 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006178 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006179 if (kUseBakerReadBarrier) {
6180 // Load with fast path based Baker's read barrier.
6181 // /* HeapReference<Object> */ out = *(out + offset)
6182 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6183 out,
6184 out_reg,
6185 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006186 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006187 /* needs_null_check */ false,
6188 /* use_load_acquire */ false);
6189 } else {
6190 // Load with slow path based read barrier.
6191 // Save the value of `out` into `maybe_temp` before overwriting it
6192 // in the following move operation, as we will need it for the
6193 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006194 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006195 __ Mov(temp_reg, out_reg);
6196 // /* HeapReference<Object> */ out = *(out + offset)
6197 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6198 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6199 }
6200 } else {
6201 // Plain load with no read barrier.
6202 // /* HeapReference<Object> */ out = *(out + offset)
6203 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6204 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6205 }
6206}
6207
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006208void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6209 HInstruction* instruction,
6210 Location out,
6211 Location obj,
6212 uint32_t offset,
6213 Location maybe_temp,
6214 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006216 Register out_reg = RegisterFrom(out, type);
6217 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006218 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006219 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006220 if (kUseBakerReadBarrier) {
6221 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006222 // /* HeapReference<Object> */ out = *(obj + offset)
6223 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6224 out,
6225 obj_reg,
6226 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006227 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006228 /* needs_null_check */ false,
6229 /* use_load_acquire */ false);
6230 } else {
6231 // Load with slow path based read barrier.
6232 // /* HeapReference<Object> */ out = *(obj + offset)
6233 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6234 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6235 }
6236 } else {
6237 // Plain load with no read barrier.
6238 // /* HeapReference<Object> */ out = *(obj + offset)
6239 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6240 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6241 }
6242}
6243
Vladimir Markoca1e0382018-04-11 09:58:41 +00006244void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006245 HInstruction* instruction,
6246 Location root,
6247 Register obj,
6248 uint32_t offset,
6249 vixl::aarch64::Label* fixup_label,
6250 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006251 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006252 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006253 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006254 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006255 if (kUseBakerReadBarrier) {
6256 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006257 // Baker's read barrier are used.
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006258 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
6259 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006260 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6261 // the Marking Register) to decide whether we need to enter
6262 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006263 //
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006264 // We use link-time generated thunks for the slow path. That thunk
6265 // checks the reference and jumps to the entrypoint if needed.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006266 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006267 // lr = &return_address;
6268 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006269 // if (mr) { // Thread::Current()->GetIsGcMarking()
6270 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006271 // }
6272 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00006273
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006274 UseScratchRegisterScope temps(GetVIXLAssembler());
6275 DCHECK(temps.IsAvailable(ip0));
6276 DCHECK(temps.IsAvailable(ip1));
6277 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006278 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006279 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00006280
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006281 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006282 vixl::aarch64::Label return_address;
6283 __ adr(lr, &return_address);
6284 if (fixup_label != nullptr) {
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006285 __ Bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006286 }
6287 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6288 "GC root LDR must be 2 instruction (8B) before the return address label.");
6289 __ ldr(root_reg, MemOperand(obj.X(), offset));
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006290 __ Bind(cbnz_label);
6291 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6292 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006294 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6295 // the Marking Register) to decide whether we need to enter
6296 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006297 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006298 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006299 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006300 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006301 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6302 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006303 // }
Roland Levillain44015862016-01-22 11:47:17 +00006304
Roland Levillain97c46462017-05-11 14:04:03 +01006305 // Slow path marking the GC root `root`. The entrypoint will
6306 // be loaded by the slow path code.
6307 SlowPathCodeARM64* slow_path =
Vladimir Markoca1e0382018-04-11 09:58:41 +00006308 new (GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
6309 AddSlowPath(slow_path);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006310
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006311 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6312 if (fixup_label == nullptr) {
6313 __ Ldr(root_reg, MemOperand(obj, offset));
6314 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006315 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006316 }
6317 static_assert(
6318 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6319 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6320 "have different sizes.");
6321 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6322 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6323 "have different sizes.");
6324
Roland Levillain97c46462017-05-11 14:04:03 +01006325 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006326 __ Bind(slow_path->GetExitLabel());
6327 }
Roland Levillain44015862016-01-22 11:47:17 +00006328 } else {
6329 // GC root loaded through a slow path for read barriers other
6330 // than Baker's.
6331 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006332 if (fixup_label == nullptr) {
6333 __ Add(root_reg.X(), obj.X(), offset);
6334 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006335 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006336 }
Roland Levillain44015862016-01-22 11:47:17 +00006337 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006338 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006339 }
6340 } else {
6341 // Plain GC root load with no read barrier.
6342 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006343 if (fixup_label == nullptr) {
6344 __ Ldr(root_reg, MemOperand(obj, offset));
6345 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006346 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006347 }
Roland Levillain44015862016-01-22 11:47:17 +00006348 // Note that GC roots are not affected by heap poisoning, thus we
6349 // do not have to unpoison `root_reg` here.
6350 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006351 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006352}
6353
6354void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6355 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006356 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006357 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006358 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006359 bool needs_null_check,
6360 bool use_load_acquire) {
6361 DCHECK(kEmitCompilerReadBarrier);
6362 DCHECK(kUseBakerReadBarrier);
6363
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006364 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6365 !use_load_acquire &&
6366 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006367 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6368 // Marking Register) to decide whether we need to enter the slow
6369 // path to mark the reference. Then, in the slow path, check the
6370 // gray bit in the lock word of the reference's holder (`obj`) to
6371 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006372 //
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006373 // We use link-time generated thunks for the slow path. That thunk checks
6374 // the holder and jumps to the entrypoint if needed. If the holder is not
6375 // gray, it creates a fake dependency and returns to the LDR instruction.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006376 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006377 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006378 // if (mr) { // Thread::Current()->GetIsGcMarking()
6379 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006380 // }
6381 // not_gray_return_address:
6382 // // Original reference load. If the offset is too large to fit
6383 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006384 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006385 // gray_return_address:
6386
6387 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6388 Register base = obj;
6389 if (offset >= kReferenceLoadMinFarOffset) {
6390 DCHECK(maybe_temp.IsRegister());
6391 base = WRegisterFrom(maybe_temp);
6392 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6393 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6394 offset &= (kReferenceLoadMinFarOffset - 1u);
6395 }
6396 UseScratchRegisterScope temps(GetVIXLAssembler());
6397 DCHECK(temps.IsAvailable(ip0));
6398 DCHECK(temps.IsAvailable(ip1));
6399 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006400 uint32_t custom_data = EncodeBakerReadBarrierFieldData(base.GetCode(), obj.GetCode());
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006401 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006402
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006403 {
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006404 EmissionCheckScope guard(GetVIXLAssembler(),
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006405 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6406 vixl::aarch64::Label return_address;
6407 __ adr(lr, &return_address);
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006408 __ Bind(cbnz_label);
6409 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006410 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6411 "Field LDR must be 1 instruction (4B) before the return address label; "
6412 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006413 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006414 __ ldr(ref_reg, MemOperand(base.X(), offset));
6415 if (needs_null_check) {
6416 MaybeRecordImplicitNullCheck(instruction);
6417 }
6418 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006419 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006420 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006421 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006422 return;
6423 }
6424
Roland Levillain44015862016-01-22 11:47:17 +00006425 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006426 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006427 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006428 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006429 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6430 ref,
6431 obj,
6432 offset,
6433 no_index,
6434 no_scale_factor,
6435 temp,
6436 needs_null_check,
6437 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006438}
6439
6440void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6441 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006442 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006443 uint32_t data_offset,
6444 Location index,
6445 Register temp,
6446 bool needs_null_check) {
6447 DCHECK(kEmitCompilerReadBarrier);
6448 DCHECK(kUseBakerReadBarrier);
6449
Vladimir Marko66d691d2017-04-07 17:53:39 +01006450 static_assert(
6451 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6452 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006453 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006454
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006455 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6456 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006457 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6458 // Marking Register) to decide whether we need to enter the slow
6459 // path to mark the reference. Then, in the slow path, check the
6460 // gray bit in the lock word of the reference's holder (`obj`) to
6461 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006462 //
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006463 // We use link-time generated thunks for the slow path. That thunk checks
6464 // the holder and jumps to the entrypoint if needed. If the holder is not
6465 // gray, it creates a fake dependency and returns to the LDR instruction.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006466 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006467 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006468 // if (mr) { // Thread::Current()->GetIsGcMarking()
6469 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006470 // }
6471 // not_gray_return_address:
6472 // // Original reference load. If the offset is too large to fit
6473 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006474 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006475 // gray_return_address:
6476
6477 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006478 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6479 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006480
6481 UseScratchRegisterScope temps(GetVIXLAssembler());
6482 DCHECK(temps.IsAvailable(ip0));
6483 DCHECK(temps.IsAvailable(ip1));
6484 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006485 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006486 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006487
Vladimir Marko66d691d2017-04-07 17:53:39 +01006488 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006489 {
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006490 EmissionCheckScope guard(GetVIXLAssembler(),
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006491 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6492 vixl::aarch64::Label return_address;
6493 __ adr(lr, &return_address);
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006494 __ Bind(cbnz_label);
6495 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006496 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6497 "Array LDR must be 1 instruction (4B) before the return address label; "
6498 " 2 instructions (8B) for heap poisoning.");
6499 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6500 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6501 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006502 __ Bind(&return_address);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006503 }
6504 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006505 return;
6506 }
6507
Roland Levillain44015862016-01-22 11:47:17 +00006508 // Array cells are never volatile variables, therefore array loads
6509 // never use Load-Acquire instructions on ARM64.
6510 const bool use_load_acquire = false;
6511
6512 // /* HeapReference<Object> */ ref =
6513 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006514 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6515 ref,
6516 obj,
6517 data_offset,
6518 index,
6519 scale_factor,
6520 temp,
6521 needs_null_check,
6522 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006523}
6524
6525void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6526 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006527 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006528 uint32_t offset,
6529 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006530 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006531 Register temp,
6532 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006533 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006534 DCHECK(kEmitCompilerReadBarrier);
6535 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006536 // If we are emitting an array load, we should not be using a
6537 // Load Acquire instruction. In other words:
6538 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6539 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006540
Roland Levillain97c46462017-05-11 14:04:03 +01006541 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6542 // Marking Register) to decide whether we need to enter the slow
6543 // path to mark the reference. Then, in the slow path, check the
6544 // gray bit in the lock word of the reference's holder (`obj`) to
6545 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006546 //
Roland Levillain97c46462017-05-11 14:04:03 +01006547 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006548 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006549 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6550 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6551 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6552 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6553 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006554 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6555 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006556 // }
6557 // } else {
6558 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006559 // }
Roland Levillain44015862016-01-22 11:47:17 +00006560
Roland Levillainba650a42017-03-06 13:52:32 +00006561 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006562 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006563 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006564 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006565 instruction,
6566 ref,
6567 obj,
6568 offset,
6569 index,
6570 scale_factor,
6571 needs_null_check,
6572 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006573 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006574 AddSlowPath(slow_path);
6575
Roland Levillain97c46462017-05-11 14:04:03 +01006576 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006577 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006578 GenerateRawReferenceLoad(
6579 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006580 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006581 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006582}
6583
Roland Levillainff487002017-03-07 16:50:01 +00006584void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6585 Location ref,
6586 Register obj,
6587 Location field_offset,
6588 Register temp,
6589 bool needs_null_check,
6590 bool use_load_acquire) {
6591 DCHECK(kEmitCompilerReadBarrier);
6592 DCHECK(kUseBakerReadBarrier);
6593 // If we are emitting an array load, we should not be using a
6594 // Load Acquire instruction. In other words:
6595 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6596 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6597
Roland Levillain97c46462017-05-11 14:04:03 +01006598 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6599 // Marking Register) to decide whether we need to enter the slow
6600 // path to update the reference field within `obj`. Then, in the
6601 // slow path, check the gray bit in the lock word of the reference's
6602 // holder (`obj`) to decide whether to mark `ref` and update the
6603 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006604 //
Roland Levillain97c46462017-05-11 14:04:03 +01006605 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006606 // // Slow path.
6607 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6608 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6609 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6610 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6611 // if (is_gray) {
6612 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006613 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6614 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006615 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6616 // }
6617 // }
6618
6619 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006620 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006621 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006622 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006623 instruction,
6624 ref,
6625 obj,
6626 /* offset */ 0u,
6627 /* index */ field_offset,
6628 /* scale_factor */ 0u /* "times 1" */,
6629 needs_null_check,
6630 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006631 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006632 AddSlowPath(slow_path);
6633
Roland Levillain97c46462017-05-11 14:04:03 +01006634 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006635 // Fast path: the GC is not marking: nothing to do (the field is
6636 // up-to-date, and we don't need to load the reference).
6637 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006638 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006639}
6640
Roland Levillainba650a42017-03-06 13:52:32 +00006641void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6642 Location ref,
6643 Register obj,
6644 uint32_t offset,
6645 Location index,
6646 size_t scale_factor,
6647 bool needs_null_check,
6648 bool use_load_acquire) {
6649 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006651 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006652
Roland Levillainba650a42017-03-06 13:52:32 +00006653 // If needed, vixl::EmissionCheckScope guards are used to ensure
6654 // that no pools are emitted between the load (macro) instruction
6655 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006656
Roland Levillain44015862016-01-22 11:47:17 +00006657 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006658 // Load types involving an "index": ArrayGet,
6659 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6660 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006661 if (use_load_acquire) {
6662 // UnsafeGetObjectVolatile intrinsic case.
6663 // Register `index` is not an index in an object array, but an
6664 // offset to an object reference field within object `obj`.
6665 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6666 DCHECK(instruction->GetLocations()->Intrinsified());
6667 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6668 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006669 DCHECK_EQ(offset, 0u);
6670 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006671 DCHECK_EQ(needs_null_check, false);
6672 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006673 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6674 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006675 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006676 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6677 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006678 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01006679 uint32_t computed_offset = offset + (Int64FromLocation(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006680 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006681 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006682 if (needs_null_check) {
6683 MaybeRecordImplicitNullCheck(instruction);
6684 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006685 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006686 UseScratchRegisterScope temps(GetVIXLAssembler());
6687 Register temp = temps.AcquireW();
6688 __ Add(temp, obj, offset);
6689 {
6690 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6691 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6692 if (needs_null_check) {
6693 MaybeRecordImplicitNullCheck(instruction);
6694 }
6695 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006696 }
Roland Levillain44015862016-01-22 11:47:17 +00006697 }
Roland Levillain44015862016-01-22 11:47:17 +00006698 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006699 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006700 MemOperand field = HeapOperand(obj, offset);
6701 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006702 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6703 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006704 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006705 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006706 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006707 if (needs_null_check) {
6708 MaybeRecordImplicitNullCheck(instruction);
6709 }
Roland Levillain44015862016-01-22 11:47:17 +00006710 }
6711 }
6712
6713 // Object* ref = ref_addr->AsMirrorPtr()
6714 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006715}
6716
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006717void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6718 // The following condition is a compile-time one, so it does not have a run-time cost.
6719 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6720 // The following condition is a run-time one; it is executed after the
6721 // previous compile-time test, to avoid penalizing non-debug builds.
6722 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6723 UseScratchRegisterScope temps(GetVIXLAssembler());
6724 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6725 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6726 }
6727 }
6728}
6729
Roland Levillain44015862016-01-22 11:47:17 +00006730void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6731 Location out,
6732 Location ref,
6733 Location obj,
6734 uint32_t offset,
6735 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006736 DCHECK(kEmitCompilerReadBarrier);
6737
Roland Levillain44015862016-01-22 11:47:17 +00006738 // Insert a slow path based read barrier *after* the reference load.
6739 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006740 // If heap poisoning is enabled, the unpoisoning of the loaded
6741 // reference will be carried out by the runtime within the slow
6742 // path.
6743 //
6744 // Note that `ref` currently does not get unpoisoned (when heap
6745 // poisoning is enabled), which is alright as the `ref` argument is
6746 // not used by the artReadBarrierSlow entry point.
6747 //
6748 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006749 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006750 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6751 AddSlowPath(slow_path);
6752
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006753 __ B(slow_path->GetEntryLabel());
6754 __ Bind(slow_path->GetExitLabel());
6755}
6756
Roland Levillain44015862016-01-22 11:47:17 +00006757void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6758 Location out,
6759 Location ref,
6760 Location obj,
6761 uint32_t offset,
6762 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006763 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006764 // Baker's read barriers shall be handled by the fast path
6765 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6766 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006767 // If heap poisoning is enabled, unpoisoning will be taken care of
6768 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006769 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006770 } else if (kPoisonHeapReferences) {
6771 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6772 }
6773}
6774
Roland Levillain44015862016-01-22 11:47:17 +00006775void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6776 Location out,
6777 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006778 DCHECK(kEmitCompilerReadBarrier);
6779
Roland Levillain44015862016-01-22 11:47:17 +00006780 // Insert a slow path based read barrier *after* the GC root load.
6781 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006782 // Note that GC roots are not affected by heap poisoning, so we do
6783 // not need to do anything special for this here.
6784 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006785 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006786 AddSlowPath(slow_path);
6787
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006788 __ B(slow_path->GetEntryLabel());
6789 __ Bind(slow_path->GetExitLabel());
6790}
6791
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006792void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6793 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006794 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006795 locations->SetInAt(0, Location::RequiresRegister());
6796 locations->SetOut(Location::RequiresRegister());
6797}
6798
6799void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6800 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006801 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006802 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006803 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006804 __ Ldr(XRegisterFrom(locations->Out()),
6805 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006806 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006807 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006808 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006809 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6810 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006811 __ Ldr(XRegisterFrom(locations->Out()),
6812 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006813 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006814}
6815
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006816static void PatchJitRootUse(uint8_t* code,
6817 const uint8_t* roots_data,
6818 vixl::aarch64::Literal<uint32_t>* literal,
6819 uint64_t index_in_table) {
6820 uint32_t literal_offset = literal->GetOffset();
6821 uintptr_t address =
6822 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6823 uint8_t* data = code + literal_offset;
6824 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6825}
6826
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006827void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6828 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006829 const StringReference& string_reference = entry.first;
6830 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006831 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006832 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006833 }
6834 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006835 const TypeReference& type_reference = entry.first;
6836 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006837 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006838 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006839 }
6840}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006841
Alexandre Rames67555f72014-11-18 10:55:16 +00006842#undef __
6843#undef QUICK_ENTRY_POINT
6844
Vladimir Markoca1e0382018-04-11 09:58:41 +00006845#define __ assembler.GetVIXLAssembler()->
6846
6847static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6848 vixl::aarch64::Register base_reg,
6849 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006850 vixl::aarch64::Label* slow_path,
6851 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006852 // Load the lock word containing the rb_state.
6853 __ Ldr(ip0.W(), lock_word);
6854 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6855 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6856 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6857 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6858 static_assert(
6859 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6860 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006861 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6862 if (throw_npe != nullptr) {
6863 __ Bind(throw_npe);
6864 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006865 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6866 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6867 "Field LDR must be 1 instruction (4B) before the return address label; "
6868 " 2 instructions (8B) for heap poisoning.");
6869 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6870 // Introduce a dependency on the lock_word including rb_state,
6871 // to prevent load-load reordering, and without using
6872 // a memory barrier (which would be more expensive).
6873 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6874 __ Br(lr); // And return back to the function.
6875 // Note: The fake dependency is unnecessary for the slow path.
6876}
6877
6878// Load the read barrier introspection entrypoint in register `entrypoint`.
6879static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6880 vixl::aarch64::Register entrypoint) {
6881 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6882 DCHECK_EQ(ip0.GetCode(), 16u);
6883 const int32_t entry_point_offset =
6884 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6885 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6886}
6887
6888void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6889 uint32_t encoded_data,
6890 /*out*/ std::string* debug_name) {
6891 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6892 switch (kind) {
6893 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006894 auto base_reg =
6895 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6896 CheckValidReg(base_reg.GetCode());
6897 auto holder_reg =
6898 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6899 CheckValidReg(holder_reg.GetCode());
6900 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6901 temps.Exclude(ip0, ip1);
Vladimir Marko7a695052018-04-12 10:26:50 +01006902 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
6903 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
6904 // null-check the holder as we do not necessarily do that check before going to the thunk.
6905 vixl::aarch64::Label throw_npe_label;
6906 vixl::aarch64::Label* throw_npe = nullptr;
6907 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
6908 throw_npe = &throw_npe_label;
6909 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006910 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006911 // Check if the holder is gray and, if not, add fake dependency to the base register
6912 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6913 // to load the reference and call the entrypoint that performs further checks on the
6914 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006915 vixl::aarch64::Label slow_path;
6916 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006917 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006918 __ Bind(&slow_path);
6919 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6920 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6921 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6922 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6923 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6924 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6925 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006926 break;
6927 }
6928 case BakerReadBarrierKind::kArray: {
6929 auto base_reg =
6930 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6931 CheckValidReg(base_reg.GetCode());
6932 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6933 BakerReadBarrierSecondRegField::Decode(encoded_data));
6934 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6935 temps.Exclude(ip0, ip1);
6936 vixl::aarch64::Label slow_path;
6937 int32_t data_offset =
6938 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6939 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6940 DCHECK_LT(lock_word.GetOffset(), 0);
6941 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6942 __ Bind(&slow_path);
6943 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6944 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6945 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6946 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6947 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6948 // a switch case target based on the index register.
6949 __ Mov(ip0, base_reg); // Move the base register to ip0.
6950 __ Br(ip1); // Jump to the entrypoint's array switch case.
6951 break;
6952 }
6953 case BakerReadBarrierKind::kGcRoot: {
6954 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6955 // and it does not have a forwarding address), call the correct introspection entrypoint;
6956 // otherwise return the reference (or the extracted forwarding address).
6957 // There is no gray bit check for GC roots.
6958 auto root_reg =
6959 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6960 CheckValidReg(root_reg.GetCode());
6961 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6962 BakerReadBarrierSecondRegField::Decode(encoded_data));
6963 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6964 temps.Exclude(ip0, ip1);
6965 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6966 __ Cbz(root_reg, &return_label);
6967 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6968 __ Ldr(ip0.W(), lock_word);
6969 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6970 __ Bind(&return_label);
6971 __ Br(lr);
6972 __ Bind(&not_marked);
6973 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6974 __ B(&forwarding_address, mi);
6975 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6976 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6977 // art_quick_read_barrier_mark_introspection_gc_roots.
6978 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6979 __ Mov(ip0.W(), root_reg);
6980 __ Br(ip1);
6981 __ Bind(&forwarding_address);
6982 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6983 __ Br(lr);
6984 break;
6985 }
6986 default:
6987 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6988 UNREACHABLE();
6989 }
6990
Nicolas Geoffray756e7222018-08-02 17:53:46 +00006991 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006992 std::ostringstream oss;
6993 oss << "BakerReadBarrierThunk";
6994 switch (kind) {
6995 case BakerReadBarrierKind::kField:
6996 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6997 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6998 break;
6999 case BakerReadBarrierKind::kArray:
7000 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7001 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7002 BakerReadBarrierSecondRegField::Decode(encoded_data));
7003 break;
7004 case BakerReadBarrierKind::kGcRoot:
7005 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
7006 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
7007 BakerReadBarrierSecondRegField::Decode(encoded_data));
7008 break;
7009 }
7010 *debug_name = oss.str();
7011 }
7012}
7013
7014#undef __
7015
Alexandre Rames5319def2014-10-23 10:03:10 +01007016} // namespace arm64
7017} // namespace art