blob: 760b1dd09b6720c7816ebbb81c4a1a050bf39cc2 [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
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100167// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
168#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700169#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100170
Zheng Xuda403092015-04-24 17:35:39 +0800171// Calculate memory accessing operand for save/restore live registers.
172static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100173 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800174 int64_t spill_offset,
175 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
177 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
178 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800179 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100180 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800181 codegen->GetNumberOfFloatingPointRegisters()));
182
Vladimir Marko804b03f2016-09-14 16:26:36 +0100183 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100184 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
185 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800186
187 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
188 UseScratchRegisterScope temps(masm);
189
190 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100191 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
192 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800193 int64_t reg_size = kXRegSizeInBytes;
194 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
195 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100196 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800197 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
198 // If the offset does not fit in the instruction's immediate field, use an alternate register
199 // to compute the base address(float point registers spill base address).
200 Register new_base = temps.AcquireSameSizeAs(base);
201 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
202 base = new_base;
203 spill_offset = -core_spill_size;
204 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
205 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
206 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
207 }
208
209 if (is_save) {
210 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
211 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
212 } else {
213 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
214 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
215 }
216}
217
218void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800219 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100220 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
221 for (uint32_t i : LowToHighBits(core_spills)) {
222 // If the register holds an object, update the stack mask.
223 if (locations->RegisterContainsObject(i)) {
224 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800225 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100226 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
227 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
228 saved_core_stack_offsets_[i] = stack_offset;
229 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800230 }
231
Vladimir Marko804b03f2016-09-14 16:26:36 +0100232 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
233 for (uint32_t i : LowToHighBits(fp_spills)) {
234 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
235 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
236 saved_fpu_stack_offsets_[i] = stack_offset;
237 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800238 }
239
Vladimir Marko804b03f2016-09-14 16:26:36 +0100240 SaveRestoreLiveRegistersHelper(codegen,
241 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800242 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
243}
244
245void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100246 SaveRestoreLiveRegistersHelper(codegen,
247 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800248 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
249}
250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
252 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000253 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100254
Alexandre Rames67555f72014-11-18 10:55:16 +0000255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100256 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000257 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100258
Alexandre Rames5319def2014-10-23 10:03:10 +0100259 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000260 if (instruction_->CanThrowIntoCatchBlock()) {
261 // Live registers will be restored in the catch block if caught.
262 SaveLiveRegisters(codegen, instruction_->GetLocations());
263 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000264 // We're moving two locations to locations that could overlap, so we need a parallel
265 // move resolver.
266 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100267 codegen->EmitParallelMoves(locations->InAt(0),
268 LocationFrom(calling_convention.GetRegisterAt(0)),
269 DataType::Type::kInt32,
270 locations->InAt(1),
271 LocationFrom(calling_convention.GetRegisterAt(1)),
272 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000273 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
274 ? kQuickThrowStringBounds
275 : kQuickThrowArrayBounds;
276 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100277 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800278 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 }
280
Alexandre Rames8158f282015-08-07 10:26:17 +0100281 bool IsFatal() const OVERRIDE { return true; }
282
Alexandre Rames9931f312015-06-19 14:47:01 +0100283 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
284
Alexandre Rames5319def2014-10-23 10:03:10 +0100285 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100286 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
287};
288
Alexandre Rames67555f72014-11-18 10:55:16 +0000289class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
290 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000291 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000292
293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
294 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
295 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000296 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 }
299
Alexandre Rames8158f282015-08-07 10:26:17 +0100300 bool IsFatal() const OVERRIDE { return true; }
301
Alexandre Rames9931f312015-06-19 14:47:01 +0100302 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
303
Alexandre Rames67555f72014-11-18 10:55:16 +0000304 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000305 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
306};
307
308class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
309 public:
310 LoadClassSlowPathARM64(HLoadClass* cls,
311 HInstruction* at,
312 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000313 bool do_clinit)
Vladimir Markoea4c1262017-02-06 19:59:33 +0000314 : SlowPathCodeARM64(at),
315 cls_(cls),
316 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000317 do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000318 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
319 }
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000322 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000323 Location out = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
325
326 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000327 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
Vladimir Markof3c52b42017-11-17 17:32:12 +0000329 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000330 dex::TypeIndex type_index = cls_->GetTypeIndex();
331 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000332 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
333 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000334 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800335 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100336 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800337 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100338 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800339 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000340
341 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000342 if (out.IsValid()) {
343 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100344 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000345 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000347 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000348 __ B(GetExitLabel());
349 }
350
Alexandre Rames9931f312015-06-19 14:47:01 +0100351 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
352
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 private:
354 // The class this slow path will load.
355 HLoadClass* const cls_;
356
Alexandre Rames67555f72014-11-18 10:55:16 +0000357 // The dex PC of `at_`.
358 const uint32_t dex_pc_;
359
360 // Whether to initialize the class.
361 const bool do_clinit_;
362
363 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
364};
365
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
367 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000368 explicit LoadStringSlowPathARM64(HLoadString* instruction)
369 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
372 LocationSummary* locations = instruction_->GetLocations();
373 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
374 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
375
376 __ Bind(GetEntryLabel());
377 SaveLiveRegisters(codegen, locations);
378
Vladimir Markof3c52b42017-11-17 17:32:12 +0000379 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000380 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
381 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000382 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
383 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100384 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000385 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
386
387 RestoreLiveRegisters(codegen, locations);
388
Vladimir Markoaad75c62016-10-03 08:46:48 +0000389 __ B(GetExitLabel());
390 }
391
392 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
393
394 private:
395 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
396};
397
Alexandre Rames5319def2014-10-23 10:03:10 +0100398class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
399 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000400 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100401
Alexandre Rames67555f72014-11-18 10:55:16 +0000402 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
403 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100404 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000405 if (instruction_->CanThrowIntoCatchBlock()) {
406 // Live registers will be restored in the catch block if caught.
407 SaveLiveRegisters(codegen, instruction_->GetLocations());
408 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000409 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
410 instruction_,
411 instruction_->GetDexPc(),
412 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800413 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 }
415
Alexandre Rames8158f282015-08-07 10:26:17 +0100416 bool IsFatal() const OVERRIDE { return true; }
417
Alexandre Rames9931f312015-06-19 14:47:01 +0100418 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
419
Alexandre Rames5319def2014-10-23 10:03:10 +0100420 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100421 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
422};
423
424class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
425 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100426 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000427 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100428
Alexandre Rames67555f72014-11-18 10:55:16 +0000429 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100430 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000431 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100433 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000434 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800435 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100436 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000437 if (successor_ == nullptr) {
438 __ B(GetReturnLabel());
439 } else {
440 __ B(arm64_codegen->GetLabelOf(successor_));
441 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100442 }
443
Scott Wakeling97c72b72016-06-24 16:19:36 +0100444 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100445 DCHECK(successor_ == nullptr);
446 return &return_label_;
447 }
448
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100449 HBasicBlock* GetSuccessor() const {
450 return successor_;
451 }
452
Alexandre Rames9931f312015-06-19 14:47:01 +0100453 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
454
Alexandre Rames5319def2014-10-23 10:03:10 +0100455 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100456 // If not null, the block to branch to after the suspend check.
457 HBasicBlock* const successor_;
458
459 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100460 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100461
462 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
463};
464
Alexandre Rames67555f72014-11-18 10:55:16 +0000465class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
466 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000467 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000468 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000469
470 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800472
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473 DCHECK(instruction_->IsCheckCast()
474 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
475 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100476 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000477
Alexandre Rames67555f72014-11-18 10:55:16 +0000478 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000479
Vladimir Marko87584542017-12-12 17:47:52 +0000480 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481 SaveLiveRegisters(codegen, locations);
482 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000483
484 // We're moving two locations to locations that could overlap, so we need a parallel
485 // move resolver.
486 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800487 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800488 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100489 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800490 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800491 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000494 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800495 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000497 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
498 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
499 } else {
500 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800501 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
502 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000503 }
504
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505 if (!is_fatal_) {
506 RestoreLiveRegisters(codegen, locations);
507 __ B(GetExitLabel());
508 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000509 }
510
Alexandre Rames9931f312015-06-19 14:47:01 +0100511 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100512 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100513
Alexandre Rames67555f72014-11-18 10:55:16 +0000514 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000515 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000516
Alexandre Rames67555f72014-11-18 10:55:16 +0000517 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
518};
519
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700520class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
521 public:
Aart Bik42249c32016-01-07 15:33:50 -0800522 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000523 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800526 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700527 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100528 LocationSummary* locations = instruction_->GetLocations();
529 SaveLiveRegisters(codegen, locations);
530 InvokeRuntimeCallingConvention calling_convention;
531 __ Mov(calling_convention.GetRegisterAt(0),
532 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000533 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100534 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700535 }
536
Alexandre Rames9931f312015-06-19 14:47:01 +0100537 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
538
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700539 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700540 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
541};
542
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100543class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
544 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000545 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100546
547 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
548 LocationSummary* locations = instruction_->GetLocations();
549 __ Bind(GetEntryLabel());
550 SaveLiveRegisters(codegen, locations);
551
552 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100553 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100554 parallel_move.AddMove(
555 locations->InAt(0),
556 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100557 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100558 nullptr);
559 parallel_move.AddMove(
560 locations->InAt(1),
561 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100563 nullptr);
564 parallel_move.AddMove(
565 locations->InAt(2),
566 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100567 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100568 nullptr);
569 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
570
571 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000572 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100573 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
574 RestoreLiveRegisters(codegen, locations);
575 __ B(GetExitLabel());
576 }
577
578 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
579
580 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100581 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
582};
583
Zheng Xu3927c8b2015-11-18 17:46:25 +0800584void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
585 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000586 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800587
588 // We are about to use the assembler to place literals directly. Make sure we have enough
589 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000590 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
591 num_entries * sizeof(int32_t),
592 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800593
594 __ Bind(&table_start_);
595 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
596 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100597 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800598 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100599 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800600 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
601 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
602 Literal<int32_t> literal(jump_offset);
603 __ place(&literal);
604 }
605}
606
Roland Levillain54f869e2017-03-06 13:54:11 +0000607// Abstract base class for read barrier slow paths marking a reference
608// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000609//
Roland Levillain54f869e2017-03-06 13:54:11 +0000610// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100611// barrier marking runtime entry point to be invoked or an empty
612// location; in the latter case, the read barrier marking runtime
613// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000614class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
615 protected:
616 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
617 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000618 DCHECK(kEmitCompilerReadBarrier);
619 }
620
Roland Levillain54f869e2017-03-06 13:54:11 +0000621 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000622
Roland Levillain54f869e2017-03-06 13:54:11 +0000623 // Generate assembly code calling the read barrier marking runtime
624 // entry point (ReadBarrierMarkRegX).
625 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000626 // No need to save live registers; it's taken care of by the
627 // entrypoint. Also, there is no need to update the stack mask,
628 // as this runtime call will not trigger a garbage collection.
629 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
630 DCHECK_NE(ref_.reg(), LR);
631 DCHECK_NE(ref_.reg(), WSP);
632 DCHECK_NE(ref_.reg(), WZR);
633 // IP0 is used internally by the ReadBarrierMarkRegX entry point
634 // as a temporary, it cannot be the entry point's input/output.
635 DCHECK_NE(ref_.reg(), IP0);
636 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
637 // "Compact" slow path, saving two moves.
638 //
639 // Instead of using the standard runtime calling convention (input
640 // and output in W0):
641 //
642 // W0 <- ref
643 // W0 <- ReadBarrierMark(W0)
644 // ref <- W0
645 //
646 // we just use rX (the register containing `ref`) as input and output
647 // of a dedicated entrypoint:
648 //
649 // rX <- ReadBarrierMarkRegX(rX)
650 //
651 if (entrypoint_.IsValid()) {
652 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
653 __ Blr(XRegisterFrom(entrypoint_));
654 } else {
655 // Entrypoint is not already loaded, load from the thread.
656 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100657 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000658 // This runtime call does not require a stack map.
659 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
660 }
661 }
662
663 // The location (register) of the marked object reference.
664 const Location ref_;
665
666 // The location of the entrypoint if it is already loaded.
667 const Location entrypoint_;
668
Roland Levillain54f869e2017-03-06 13:54:11 +0000669 private:
670 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
671};
672
Alexandre Rames5319def2014-10-23 10:03:10 +0100673// Slow path marking an object reference `ref` during a read
674// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000675// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100676//
677// This means that after the execution of this slow path, `ref` will
678// always be up-to-date, but `obj.field` may not; i.e., after the
679// flip, `ref` will be a to-space reference, but `obj.field` will
680// probably still be a from-space reference (unless it gets updated by
681// another thread, or if another thread installed another object
682// reference (different from `ref`) in `obj.field`).
683//
Roland Levillain97c46462017-05-11 14:04:03 +0100684// Argument `entrypoint` must be a register location holding the read
685// barrier marking runtime entry point to be invoked or an empty
686// location; in the latter case, the read barrier marking runtime
687// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000688class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100689 public:
690 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
691 Location ref,
692 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000693 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100694 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100695 }
696
697 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
698
699 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000700 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100701 DCHECK(locations->CanCall());
702 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000703 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000704 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
705 << "Unexpected instruction in read barrier marking slow path: "
706 << instruction_->DebugName();
707
708 __ Bind(GetEntryLabel());
709 GenerateReadBarrierMarkRuntimeCall(codegen);
710 __ B(GetExitLabel());
711 }
712
713 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000714 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
715};
716
Roland Levillain54f869e2017-03-06 13:54:11 +0000717// Slow path loading `obj`'s lock word, loading a reference from
718// object `*(obj + offset + (index << scale_factor))` into `ref`, and
719// marking `ref` if `obj` is gray according to the lock word (Baker
720// read barrier). The field `obj.field` in the object `obj` holding
721// this reference does not get updated by this slow path after marking
722// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
723// below for that).
724//
725// This means that after the execution of this slow path, `ref` will
726// always be up-to-date, but `obj.field` may not; i.e., after the
727// flip, `ref` will be a to-space reference, but `obj.field` will
728// probably still be a from-space reference (unless it gets updated by
729// another thread, or if another thread installed another object
730// reference (different from `ref`) in `obj.field`).
731//
732// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100733// barrier marking runtime entry point to be invoked or an empty
734// location; in the latter case, the read barrier marking runtime
735// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000736class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
737 public:
738 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
739 Location ref,
740 Register obj,
741 uint32_t offset,
742 Location index,
743 size_t scale_factor,
744 bool needs_null_check,
745 bool use_load_acquire,
746 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100747 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000748 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
749 obj_(obj),
750 offset_(offset),
751 index_(index),
752 scale_factor_(scale_factor),
753 needs_null_check_(needs_null_check),
754 use_load_acquire_(use_load_acquire),
755 temp_(temp) {
756 DCHECK(kEmitCompilerReadBarrier);
757 DCHECK(kUseBakerReadBarrier);
758 }
759
760 const char* GetDescription() const OVERRIDE {
761 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
762 }
763
764 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
765 LocationSummary* locations = instruction_->GetLocations();
766 DCHECK(locations->CanCall());
767 DCHECK(ref_.IsRegister()) << ref_;
768 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
769 DCHECK(obj_.IsW());
770 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100771 DCHECK(instruction_->IsInstanceFieldGet() ||
772 instruction_->IsStaticFieldGet() ||
773 instruction_->IsArrayGet() ||
774 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100775 instruction_->IsInstanceOf() ||
776 instruction_->IsCheckCast() ||
777 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
778 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
779 << "Unexpected instruction in read barrier marking slow path: "
780 << instruction_->DebugName();
781 // The read barrier instrumentation of object ArrayGet
782 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000783 // instruction.
784 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000785 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
786
Roland Levillain54f869e2017-03-06 13:54:11 +0000787 // Temporary register `temp_`, used to store the lock word, must
788 // not be IP0 nor IP1, as we may use them to emit the reference
789 // load (in the call to GenerateRawReferenceLoad below), and we
790 // need the lock word to still be in `temp_` after the reference
791 // load.
792 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
793 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
794
Alexandre Rames5319def2014-10-23 10:03:10 +0100795 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000796
797 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
798 // inserted after the original load. However, in fast path based
799 // Baker's read barriers, we need to perform the load of
800 // mirror::Object::monitor_ *before* the original reference load.
801 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000802 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100803 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000804 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
805 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
806 // HeapReference<mirror::Object> ref = *src; // Original reference load.
807 // bool is_gray = (rb_state == ReadBarrier::GrayState());
808 // if (is_gray) {
809 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
810 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000811 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000812 // Note: the original implementation in ReadBarrier::Barrier is
813 // slightly more complex as it performs additional checks that we do
814 // not do here for performance reasons.
815
816 // /* int32_t */ monitor = obj->monitor_
817 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
818 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
819 if (needs_null_check_) {
820 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100821 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000822 // /* LockWord */ lock_word = LockWord(monitor)
823 static_assert(sizeof(LockWord) == sizeof(int32_t),
824 "art::LockWord and int32_t have different sizes.");
825
826 // Introduce a dependency on the lock_word including rb_state,
827 // to prevent load-load reordering, and without using
828 // a memory barrier (which would be more expensive).
829 // `obj` is unchanged by this operation, but its value now depends
830 // on `temp`.
831 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
832
833 // The actual reference load.
834 // A possible implicit null check has already been handled above.
835 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
836 arm64_codegen->GenerateRawReferenceLoad(instruction_,
837 ref_,
838 obj_,
839 offset_,
840 index_,
841 scale_factor_,
842 /* needs_null_check */ false,
843 use_load_acquire_);
844
845 // Mark the object `ref` when `obj` is gray.
846 //
847 // if (rb_state == ReadBarrier::GrayState())
848 // ref = ReadBarrier::Mark(ref);
849 //
850 // Given the numeric representation, it's enough to check the low bit of the rb_state.
851 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
852 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
853 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
854 GenerateReadBarrierMarkRuntimeCall(codegen);
855
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000856 __ B(GetExitLabel());
857 }
858
859 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000860 // The register containing the object holding the marked object reference field.
861 Register obj_;
862 // The offset, index and scale factor to access the reference in `obj_`.
863 uint32_t offset_;
864 Location index_;
865 size_t scale_factor_;
866 // Is a null check required?
867 bool needs_null_check_;
868 // Should this reference load use Load-Acquire semantics?
869 bool use_load_acquire_;
870 // A temporary register used to hold the lock word of `obj_`.
871 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000872
Roland Levillain54f869e2017-03-06 13:54:11 +0000873 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000874};
875
Roland Levillain54f869e2017-03-06 13:54:11 +0000876// Slow path loading `obj`'s lock word, loading a reference from
877// object `*(obj + offset + (index << scale_factor))` into `ref`, and
878// marking `ref` if `obj` is gray according to the lock word (Baker
879// read barrier). If needed, this slow path also atomically updates
880// the field `obj.field` in the object `obj` holding this reference
881// after marking (contrary to
882// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
883// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100884//
885// This means that after the execution of this slow path, both `ref`
886// and `obj.field` will be up-to-date; i.e., after the flip, both will
887// hold the same to-space reference (unless another thread installed
888// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000889//
Roland Levillain54f869e2017-03-06 13:54:11 +0000890// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100891// barrier marking runtime entry point to be invoked or an empty
892// location; in the latter case, the read barrier marking runtime
893// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000894class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
895 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100896 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100897 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
898 HInstruction* instruction,
899 Location ref,
900 Register obj,
901 uint32_t offset,
902 Location index,
903 size_t scale_factor,
904 bool needs_null_check,
905 bool use_load_acquire,
906 Register temp,
907 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000908 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100909 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000910 offset_(offset),
911 index_(index),
912 scale_factor_(scale_factor),
913 needs_null_check_(needs_null_check),
914 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000915 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100916 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000917 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100918 }
919
920 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000921 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100922 }
923
924 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
925 LocationSummary* locations = instruction_->GetLocations();
926 Register ref_reg = WRegisterFrom(ref_);
927 DCHECK(locations->CanCall());
928 DCHECK(ref_.IsRegister()) << ref_;
929 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 DCHECK(obj_.IsW());
931 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
932
933 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100934 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
935 << "Unexpected instruction in read barrier marking and field updating slow path: "
936 << instruction_->DebugName();
937 DCHECK(instruction_->GetLocations()->Intrinsified());
938 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000939 DCHECK_EQ(offset_, 0u);
940 DCHECK_EQ(scale_factor_, 0u);
941 DCHECK_EQ(use_load_acquire_, false);
942 // The location of the offset of the marked reference field within `obj_`.
943 Location field_offset = index_;
944 DCHECK(field_offset.IsRegister()) << field_offset;
945
946 // Temporary register `temp_`, used to store the lock word, must
947 // not be IP0 nor IP1, as we may use them to emit the reference
948 // load (in the call to GenerateRawReferenceLoad below), and we
949 // need the lock word to still be in `temp_` after the reference
950 // load.
951 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
952 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100953
954 __ Bind(GetEntryLabel());
955
Roland Levillainff487002017-03-07 16:50:01 +0000956 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
957 //
958 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
959 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
960 // HeapReference<mirror::Object> ref = *src; // Original reference load.
961 // bool is_gray = (rb_state == ReadBarrier::GrayState());
962 // if (is_gray) {
963 // old_ref = ref;
964 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
965 // compareAndSwapObject(obj, field_offset, old_ref, ref);
966 // }
967
Roland Levillain54f869e2017-03-06 13:54:11 +0000968 // /* int32_t */ monitor = obj->monitor_
969 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
970 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
971 if (needs_null_check_) {
972 codegen->MaybeRecordImplicitNullCheck(instruction_);
973 }
974 // /* LockWord */ lock_word = LockWord(monitor)
975 static_assert(sizeof(LockWord) == sizeof(int32_t),
976 "art::LockWord and int32_t have different sizes.");
977
978 // Introduce a dependency on the lock_word including rb_state,
979 // to prevent load-load reordering, and without using
980 // a memory barrier (which would be more expensive).
981 // `obj` is unchanged by this operation, but its value now depends
982 // on `temp`.
983 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
984
985 // The actual reference load.
986 // A possible implicit null check has already been handled above.
987 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
988 arm64_codegen->GenerateRawReferenceLoad(instruction_,
989 ref_,
990 obj_,
991 offset_,
992 index_,
993 scale_factor_,
994 /* needs_null_check */ false,
995 use_load_acquire_);
996
997 // Mark the object `ref` when `obj` is gray.
998 //
999 // if (rb_state == ReadBarrier::GrayState())
1000 // ref = ReadBarrier::Mark(ref);
1001 //
1002 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1003 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1004 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1005 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1006
1007 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001008 // Note that we cannot use IP to save the old reference, as IP is
1009 // used internally by the ReadBarrierMarkRegX entry point, and we
1010 // need the old reference after the call to that entry point.
1011 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1012 __ Mov(temp_.W(), ref_reg);
1013
Roland Levillain54f869e2017-03-06 13:54:11 +00001014 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001015
1016 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001017 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001018 //
1019 // Note that this field could also hold a different object, if
1020 // another thread had concurrently changed it. In that case, the
1021 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1022 // (CAS) operation below would abort the CAS, leaving the field
1023 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001024 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001025 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001026
1027 // Update the the holder's field atomically. This may fail if
1028 // mutator updates before us, but it's OK. This is achieved
1029 // using a strong compare-and-set (CAS) operation with relaxed
1030 // memory synchronization ordering, where the expected value is
1031 // the old reference and the desired value is the new reference.
1032
1033 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1034 UseScratchRegisterScope temps(masm);
1035
1036 // Convenience aliases.
1037 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001038 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001039 Register expected = temp_.W();
1040 Register value = ref_reg;
1041 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1042 Register tmp_value = temps.AcquireW(); // Value in memory.
1043
1044 __ Add(tmp_ptr, base.X(), Operand(offset));
1045
1046 if (kPoisonHeapReferences) {
1047 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1048 if (value.Is(expected)) {
1049 // Do not poison `value`, as it is the same register as
1050 // `expected`, which has just been poisoned.
1051 } else {
1052 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1053 }
1054 }
1055
1056 // do {
1057 // tmp_value = [tmp_ptr] - expected;
1058 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1059
Roland Levillain24a4d112016-10-26 13:10:46 +01001060 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001061 __ Bind(&loop_head);
1062 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1063 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001064 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001065 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1066 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001067 __ B(&exit_loop);
1068 __ Bind(&comparison_failed);
1069 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001070 __ Bind(&exit_loop);
1071
1072 if (kPoisonHeapReferences) {
1073 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1074 if (value.Is(expected)) {
1075 // Do not unpoison `value`, as it is the same register as
1076 // `expected`, which has just been unpoisoned.
1077 } else {
1078 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1079 }
1080 }
1081
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001082 __ B(GetExitLabel());
1083 }
1084
1085 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001086 // The register containing the object holding the marked object reference field.
1087 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001088 // The offset, index and scale factor to access the reference in `obj_`.
1089 uint32_t offset_;
1090 Location index_;
1091 size_t scale_factor_;
1092 // Is a null check required?
1093 bool needs_null_check_;
1094 // Should this reference load use Load-Acquire semantics?
1095 bool use_load_acquire_;
1096 // A temporary register used to hold the lock word of `obj_`; and
1097 // also to hold the original reference value, when the reference is
1098 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001099 const Register temp_;
1100
Roland Levillain54f869e2017-03-06 13:54:11 +00001101 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001102};
1103
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001104// Slow path generating a read barrier for a heap reference.
1105class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1106 public:
1107 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1108 Location out,
1109 Location ref,
1110 Location obj,
1111 uint32_t offset,
1112 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001113 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001114 out_(out),
1115 ref_(ref),
1116 obj_(obj),
1117 offset_(offset),
1118 index_(index) {
1119 DCHECK(kEmitCompilerReadBarrier);
1120 // If `obj` is equal to `out` or `ref`, it means the initial object
1121 // has been overwritten by (or after) the heap object reference load
1122 // to be instrumented, e.g.:
1123 //
1124 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001125 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001126 //
1127 // In that case, we have lost the information about the original
1128 // object, and the emitted read barrier cannot work properly.
1129 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1130 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1131 }
1132
1133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1134 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1135 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001136 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001137 DCHECK(locations->CanCall());
1138 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001139 DCHECK(instruction_->IsInstanceFieldGet() ||
1140 instruction_->IsStaticFieldGet() ||
1141 instruction_->IsArrayGet() ||
1142 instruction_->IsInstanceOf() ||
1143 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001144 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001145 << "Unexpected instruction in read barrier for heap reference slow path: "
1146 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001147 // The read barrier instrumentation of object ArrayGet
1148 // instructions does not support the HIntermediateAddress
1149 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001150 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001151 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001152
1153 __ Bind(GetEntryLabel());
1154
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001155 SaveLiveRegisters(codegen, locations);
1156
1157 // We may have to change the index's value, but as `index_` is a
1158 // constant member (like other "inputs" of this slow path),
1159 // introduce a copy of it, `index`.
1160 Location index = index_;
1161 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001162 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001163 if (instruction_->IsArrayGet()) {
1164 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001165 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001166 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1167 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1168 // We are about to change the value of `index_reg` (see the
1169 // calls to vixl::MacroAssembler::Lsl and
1170 // vixl::MacroAssembler::Mov below), but it has
1171 // not been saved by the previous call to
1172 // art::SlowPathCode::SaveLiveRegisters, as it is a
1173 // callee-save register --
1174 // art::SlowPathCode::SaveLiveRegisters does not consider
1175 // callee-save registers, as it has been designed with the
1176 // assumption that callee-save registers are supposed to be
1177 // handled by the called function. So, as a callee-save
1178 // register, `index_reg` _would_ eventually be saved onto
1179 // the stack, but it would be too late: we would have
1180 // changed its value earlier. Therefore, we manually save
1181 // it here into another freely available register,
1182 // `free_reg`, chosen of course among the caller-save
1183 // registers (as a callee-save `free_reg` register would
1184 // exhibit the same problem).
1185 //
1186 // Note we could have requested a temporary register from
1187 // the register allocator instead; but we prefer not to, as
1188 // this is a slow path, and we know we can find a
1189 // caller-save register that is available.
1190 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1191 __ Mov(free_reg.W(), index_reg);
1192 index_reg = free_reg;
1193 index = LocationFrom(index_reg);
1194 } else {
1195 // The initial register stored in `index_` has already been
1196 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1197 // (as it is not a callee-save register), so we can freely
1198 // use it.
1199 }
1200 // Shifting the index value contained in `index_reg` by the scale
1201 // factor (2) cannot overflow in practice, as the runtime is
1202 // unable to allocate object arrays with a size larger than
1203 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001205 static_assert(
1206 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1207 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1208 __ Add(index_reg, index_reg, Operand(offset_));
1209 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001210 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1211 // intrinsics, `index_` is not shifted by a scale factor of 2
1212 // (as in the case of ArrayGet), as it is actually an offset
1213 // to an object field within an object.
1214 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001215 DCHECK(instruction_->GetLocations()->Intrinsified());
1216 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1217 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1218 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001219 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001220 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001221 }
1222 }
1223
1224 // We're moving two or three locations to locations that could
1225 // overlap, so we need a parallel move resolver.
1226 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001227 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001228 parallel_move.AddMove(ref_,
1229 LocationFrom(calling_convention.GetRegisterAt(0)),
1230 type,
1231 nullptr);
1232 parallel_move.AddMove(obj_,
1233 LocationFrom(calling_convention.GetRegisterAt(1)),
1234 type,
1235 nullptr);
1236 if (index.IsValid()) {
1237 parallel_move.AddMove(index,
1238 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001240 nullptr);
1241 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1242 } else {
1243 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1244 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1245 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001246 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001247 instruction_,
1248 instruction_->GetDexPc(),
1249 this);
1250 CheckEntrypointTypes<
1251 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1252 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1253
1254 RestoreLiveRegisters(codegen, locations);
1255
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001256 __ B(GetExitLabel());
1257 }
1258
1259 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1260
1261 private:
1262 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001263 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1264 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001265 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1266 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1267 return Register(VIXLRegCodeFromART(i), kXRegSize);
1268 }
1269 }
1270 // We shall never fail to find a free caller-save register, as
1271 // there are more than two core caller-save registers on ARM64
1272 // (meaning it is possible to find one which is different from
1273 // `ref` and `obj`).
1274 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1275 LOG(FATAL) << "Could not find a free register";
1276 UNREACHABLE();
1277 }
1278
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001279 const Location out_;
1280 const Location ref_;
1281 const Location obj_;
1282 const uint32_t offset_;
1283 // An additional location containing an index to an array.
1284 // Only used for HArrayGet and the UnsafeGetObject &
1285 // UnsafeGetObjectVolatile intrinsics.
1286 const Location index_;
1287
1288 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1289};
1290
1291// Slow path generating a read barrier for a GC root.
1292class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1293 public:
1294 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001295 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001296 DCHECK(kEmitCompilerReadBarrier);
1297 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001298
1299 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1300 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001301 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001302 DCHECK(locations->CanCall());
1303 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001304 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1305 << "Unexpected instruction in read barrier for GC root slow path: "
1306 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001307
1308 __ Bind(GetEntryLabel());
1309 SaveLiveRegisters(codegen, locations);
1310
1311 InvokeRuntimeCallingConvention calling_convention;
1312 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1313 // The argument of the ReadBarrierForRootSlow is not a managed
1314 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1315 // thus we need a 64-bit move here, and we cannot use
1316 //
1317 // arm64_codegen->MoveLocation(
1318 // LocationFrom(calling_convention.GetRegisterAt(0)),
1319 // root_,
1320 // type);
1321 //
1322 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001324 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001325 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001326 instruction_,
1327 instruction_->GetDexPc(),
1328 this);
1329 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1330 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1331
1332 RestoreLiveRegisters(codegen, locations);
1333 __ B(GetExitLabel());
1334 }
1335
1336 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1337
1338 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001339 const Location out_;
1340 const Location root_;
1341
1342 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1343};
1344
Alexandre Rames5319def2014-10-23 10:03:10 +01001345#undef __
1346
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001347Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001348 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001349 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 LOG(FATAL) << "Unreachable type " << type;
1351 }
1352
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001353 if (DataType::IsFloatingPointType(type) &&
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001355 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001356 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001357 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1358 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1359 } else {
1360 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001361 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1362 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001363 }
1364
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001365 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001366 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001367 return next_location;
1368}
1369
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001370Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001371 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001372}
1373
Serban Constantinescu579885a2015-02-22 20:51:33 +00001374CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001375 const CompilerOptions& compiler_options,
1376 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 : CodeGenerator(graph,
1378 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001379 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001380 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001381 callee_saved_core_registers.GetList(),
1382 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001383 compiler_options,
1384 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001385 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1386 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001387 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001388 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001389 move_resolver_(graph->GetAllocator(), this),
1390 assembler_(graph->GetAllocator()),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001391 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001392 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001393 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001394 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001395 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001396 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001397 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001398 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001399 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001400 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001401 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001402 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001403 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001404 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001405 jit_class_patches_(TypeReferenceValueComparator(),
Nicolas Geoffray756e7222018-08-02 17:53:46 +00001406 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001407 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001408 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001409}
Alexandre Rames5319def2014-10-23 10:03:10 +01001410
Alexandre Rames67555f72014-11-18 10:55:16 +00001411#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001412
Zheng Xu3927c8b2015-11-18 17:46:25 +08001413void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001414 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001415 jump_table->EmitTable(this);
1416 }
1417}
1418
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001419void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001420 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001421 // Ensure we emit the literal pool.
1422 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001423
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001424 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001425
1426 // Verify Baker read barrier linker patches.
1427 if (kIsDebugBuild) {
1428 ArrayRef<const uint8_t> code = allocator->GetMemory();
1429 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1430 DCHECK(info.label.IsBound());
1431 uint32_t literal_offset = info.label.GetLocation();
1432 DCHECK_ALIGNED(literal_offset, 4u);
1433
1434 auto GetInsn = [&code](uint32_t offset) {
1435 DCHECK_ALIGNED(offset, 4u);
1436 return
1437 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1438 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1439 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1440 (static_cast<uint32_t>(code[offset + 3]) << 24);
1441 };
1442
1443 const uint32_t encoded_data = info.custom_data;
1444 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1445 // Check that the next instruction matches the expected LDR.
1446 switch (kind) {
1447 case BakerReadBarrierKind::kField: {
1448 DCHECK_GE(code.size() - literal_offset, 8u);
1449 uint32_t next_insn = GetInsn(literal_offset + 4u);
1450 // LDR (immediate) with correct base_reg.
1451 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1452 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1453 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1454 break;
1455 }
1456 case BakerReadBarrierKind::kArray: {
1457 DCHECK_GE(code.size() - literal_offset, 8u);
1458 uint32_t next_insn = GetInsn(literal_offset + 4u);
1459 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1460 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1461 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1462 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1463 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1464 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1465 break;
1466 }
1467 case BakerReadBarrierKind::kGcRoot: {
1468 DCHECK_GE(literal_offset, 4u);
1469 uint32_t prev_insn = GetInsn(literal_offset - 4u);
1470 // LDR (immediate) with correct root_reg.
1471 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1472 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg);
1473 break;
1474 }
1475 default:
1476 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1477 UNREACHABLE();
1478 }
1479 }
1480 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001481}
1482
Zheng Xuad4450e2015-04-17 18:48:56 +08001483void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1484 // Note: There are 6 kinds of moves:
1485 // 1. constant -> GPR/FPR (non-cycle)
1486 // 2. constant -> stack (non-cycle)
1487 // 3. GPR/FPR -> GPR/FPR
1488 // 4. GPR/FPR -> stack
1489 // 5. stack -> GPR/FPR
1490 // 6. stack -> stack (non-cycle)
1491 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1492 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1493 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1494 // dependency.
1495 vixl_temps_.Open(GetVIXLAssembler());
1496}
1497
1498void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1499 vixl_temps_.Close();
1500}
1501
1502Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001503 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1504 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1505 || kind == Location::kSIMDStackSlot);
1506 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1507 ? Location::kFpuRegister
1508 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001509 Location scratch = GetScratchLocation(kind);
1510 if (!scratch.Equals(Location::NoLocation())) {
1511 return scratch;
1512 }
1513 // Allocate from VIXL temp registers.
1514 if (kind == Location::kRegister) {
1515 scratch = LocationFrom(vixl_temps_.AcquireX());
1516 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001517 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001518 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1519 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1520 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001521 }
1522 AddScratchLocation(scratch);
1523 return scratch;
1524}
1525
1526void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1527 if (loc.IsRegister()) {
1528 vixl_temps_.Release(XRegisterFrom(loc));
1529 } else {
1530 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001531 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001532 }
1533 RemoveScratchLocation(loc);
1534}
1535
Alexandre Rames3e69f162014-12-10 10:36:50 +00001536void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001537 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001538 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539}
1540
Alexandre Rames5319def2014-10-23 10:03:10 +01001541void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001542 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001543 __ Bind(&frame_entry_label_);
1544
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001545 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1546 UseScratchRegisterScope temps(masm);
1547 Register temp = temps.AcquireX();
1548 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1549 __ Add(temp, temp, 1);
1550 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1551 }
1552
Vladimir Marko33bff252017-11-01 14:35:42 +00001553 bool do_overflow_check =
1554 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001555 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001556 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001557 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001558 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001559 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001560 {
1561 // Ensure that between load and RecordPcInfo there are no pools emitted.
1562 ExactAssemblyScope eas(GetVIXLAssembler(),
1563 kInstructionSize,
1564 CodeBufferCheckScope::kExactSize);
1565 __ ldr(wzr, MemOperand(temp, 0));
1566 RecordPcInfo(nullptr, 0);
1567 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001568 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001569
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001570 if (!HasEmptyFrame()) {
1571 int frame_size = GetFrameSize();
1572 // Stack layout:
1573 // sp[frame_size - 8] : lr.
1574 // ... : other preserved core registers.
1575 // ... : other preserved fp registers.
1576 // ... : reserved frame space.
1577 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001578
1579 // Save the current method if we need it. Note that we do not
1580 // do this in HCurrentMethod, as the instruction might have been removed
1581 // in the SSA graph.
1582 if (RequiresCurrentMethod()) {
1583 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001584 } else {
1585 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001586 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001587 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001588 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1589 frame_size - GetCoreSpillSize());
1590 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1591 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001592
1593 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1594 // Initialize should_deoptimize flag to 0.
1595 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1596 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1597 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001598 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001599
1600 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001601}
1602
1603void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001604 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001605 if (!HasEmptyFrame()) {
1606 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001607 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1608 frame_size - FrameEntrySpillSize());
1609 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1610 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001611 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001612 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001613 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001614 __ Ret();
1615 GetAssembler()->cfi().RestoreState();
1616 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001617}
1618
Scott Wakeling97c72b72016-06-24 16:19:36 +01001619CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001620 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001621 return CPURegList(CPURegister::kRegister, kXRegSize,
1622 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001623}
1624
Scott Wakeling97c72b72016-06-24 16:19:36 +01001625CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001626 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1627 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001628 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1629 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001630}
1631
Alexandre Rames5319def2014-10-23 10:03:10 +01001632void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1633 __ Bind(GetLabelOf(block));
1634}
1635
Calin Juravle175dc732015-08-25 15:42:32 +01001636void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1637 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001638 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001639}
1640
Calin Juravlee460d1d2015-09-29 04:52:17 +01001641void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1642 if (location.IsRegister()) {
1643 locations->AddTemp(location);
1644 } else {
1645 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1646 }
1647}
1648
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001649void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001650 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001651 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001652 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001653 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001654 if (value_can_be_null) {
1655 __ Cbz(value, &done);
1656 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001657 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001658 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001659 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001660 if (value_can_be_null) {
1661 __ Bind(&done);
1662 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001663}
1664
David Brazdil58282f42016-01-14 12:45:10 +00001665void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001666 // Blocked core registers:
1667 // lr : Runtime reserved.
1668 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001669 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001670 // ip1 : VIXL core temp.
1671 // ip0 : VIXL core temp.
1672 //
1673 // Blocked fp registers:
1674 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001675 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1676 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001677 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001678 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001679 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001680
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001681 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001682 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001683 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001684 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001685
David Brazdil58282f42016-01-14 12:45:10 +00001686 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001687 // Stubs do not save callee-save floating point registers. If the graph
1688 // is debuggable, we need to deal with these registers differently. For
1689 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001690 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1691 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001692 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001693 }
1694 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001695}
1696
Alexandre Rames3e69f162014-12-10 10:36:50 +00001697size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1698 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1699 __ Str(reg, MemOperand(sp, stack_index));
1700 return kArm64WordSize;
1701}
1702
1703size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1704 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1705 __ Ldr(reg, MemOperand(sp, stack_index));
1706 return kArm64WordSize;
1707}
1708
1709size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1710 FPRegister reg = FPRegister(reg_id, kDRegSize);
1711 __ Str(reg, MemOperand(sp, stack_index));
1712 return kArm64WordSize;
1713}
1714
1715size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1716 FPRegister reg = FPRegister(reg_id, kDRegSize);
1717 __ Ldr(reg, MemOperand(sp, stack_index));
1718 return kArm64WordSize;
1719}
1720
Alexandre Rames5319def2014-10-23 10:03:10 +01001721void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001722 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001723}
1724
1725void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001726 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001727}
1728
Vladimir Markoa0431112018-06-25 09:32:54 +01001729const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1730 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1731}
1732
Alexandre Rames67555f72014-11-18 10:55:16 +00001733void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001734 if (constant->IsIntConstant()) {
1735 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1736 } else if (constant->IsLongConstant()) {
1737 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1738 } else if (constant->IsNullConstant()) {
1739 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001740 } else if (constant->IsFloatConstant()) {
1741 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1742 } else {
1743 DCHECK(constant->IsDoubleConstant());
1744 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1745 }
1746}
1747
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001750 DCHECK(constant.IsConstant());
1751 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001752 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001753 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1755 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1756 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1757 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001758}
1759
Roland Levillain952b2352017-05-03 19:49:14 +01001760// Allocate a scratch register from the VIXL pool, querying first
1761// the floating-point register pool, and then the core register
1762// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001763// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1764// using a different allocation strategy.
1765static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1766 vixl::aarch64::UseScratchRegisterScope* temps,
1767 int size_in_bits) {
1768 return masm->GetScratchFPRegisterList()->IsEmpty()
1769 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1770 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1771}
1772
Calin Juravlee460d1d2015-09-29 04:52:17 +01001773void CodeGeneratorARM64::MoveLocation(Location destination,
1774 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001776 if (source.Equals(destination)) {
1777 return;
1778 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001779
1780 // A valid move can always be inferred from the destination and source
1781 // locations. When moving from and to a register, the argument type can be
1782 // used to generate 32bit instead of 64bit moves. In debug mode we also
1783 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001784 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001785
1786 if (destination.IsRegister() || destination.IsFpuRegister()) {
1787 if (unspecified_type) {
1788 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1789 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001790 (src_cst != nullptr && (src_cst->IsIntConstant()
1791 || src_cst->IsFloatConstant()
1792 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001793 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001794 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001795 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001796 // If the source is a double stack slot or a 64bit constant, a 64bit
1797 // type is appropriate. Else the source is a register, and since the
1798 // type has not been specified, we chose a 64bit type to force a 64bit
1799 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001801 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001802 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001803 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1804 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001805 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001806 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1807 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1808 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001809 } else if (source.IsSIMDStackSlot()) {
1810 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001811 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001812 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001813 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001814 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001815 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001816 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001817 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001818 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 DataType::Type source_type = DataType::Is64BitType(dst_type)
1820 ? DataType::Type::kInt64
1821 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001822 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1823 }
1824 } else {
1825 DCHECK(source.IsFpuRegister());
1826 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001827 DataType::Type source_type = DataType::Is64BitType(dst_type)
1828 ? DataType::Type::kFloat64
1829 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001830 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1831 } else {
1832 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001833 if (GetGraph()->HasSIMD()) {
1834 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1835 } else {
1836 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1837 }
1838 }
1839 }
1840 } else if (destination.IsSIMDStackSlot()) {
1841 if (source.IsFpuRegister()) {
1842 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1843 } else {
1844 DCHECK(source.IsSIMDStackSlot());
1845 UseScratchRegisterScope temps(GetVIXLAssembler());
1846 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1847 Register temp = temps.AcquireX();
1848 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1849 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1850 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1851 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1852 } else {
1853 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1854 __ Ldr(temp, StackOperandFrom(source));
1855 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001856 }
1857 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001858 } else { // The destination is not a register. It must be a stack slot.
1859 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1860 if (source.IsRegister() || source.IsFpuRegister()) {
1861 if (unspecified_type) {
1862 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001864 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 dst_type =
1866 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001867 }
1868 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1870 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001871 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001872 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001873 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1874 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001875 UseScratchRegisterScope temps(GetVIXLAssembler());
1876 HConstant* src_cst = source.GetConstant();
1877 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001878 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001879 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1880 ? Register(xzr)
1881 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001882 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001883 if (src_cst->IsIntConstant()) {
1884 temp = temps.AcquireW();
1885 } else if (src_cst->IsLongConstant()) {
1886 temp = temps.AcquireX();
1887 } else if (src_cst->IsFloatConstant()) {
1888 temp = temps.AcquireS();
1889 } else {
1890 DCHECK(src_cst->IsDoubleConstant());
1891 temp = temps.AcquireD();
1892 }
1893 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001894 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001895 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001896 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001897 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001898 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001899 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001900 // Use any scratch register (a core or a floating-point one)
1901 // from VIXL scratch register pools as a temporary.
1902 //
1903 // We used to only use the FP scratch register pool, but in some
1904 // rare cases the only register from this pool (D31) would
1905 // already be used (e.g. within a ParallelMove instruction, when
1906 // a move is blocked by a another move requiring a scratch FP
1907 // register, which would reserve D31). To prevent this issue, we
1908 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001909 //
1910 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001911 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001912 // use AcquireFPOrCoreCPURegisterOfSize instead of
1913 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1914 // allocates core scratch registers first.
1915 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1916 GetVIXLAssembler(),
1917 &temps,
1918 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001919 __ Ldr(temp, StackOperandFrom(source));
1920 __ Str(temp, StackOperandFrom(destination));
1921 }
1922 }
1923}
1924
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001925void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001926 CPURegister dst,
1927 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001928 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001929 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001930 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001931 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001932 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001933 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001934 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001935 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001936 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001937 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001938 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001939 case DataType::Type::kInt16:
1940 __ Ldrsh(Register(dst), src);
1941 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001942 case DataType::Type::kInt32:
1943 case DataType::Type::kReference:
1944 case DataType::Type::kInt64:
1945 case DataType::Type::kFloat32:
1946 case DataType::Type::kFloat64:
1947 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001948 __ Ldr(dst, src);
1949 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001950 case DataType::Type::kUint32:
1951 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001953 LOG(FATAL) << "Unreachable type " << type;
1954 }
1955}
1956
Calin Juravle77520bc2015-01-12 18:45:46 +00001957void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001958 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001959 const MemOperand& src,
1960 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001961 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001962 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001963 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001964 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001965
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001966 DCHECK(!src.IsPreIndex());
1967 DCHECK(!src.IsPostIndex());
1968
1969 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001970 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001971 {
1972 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1973 MemOperand base = MemOperand(temp_base);
1974 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001975 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001976 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001977 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001978 {
1979 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1980 __ ldarb(Register(dst), base);
1981 if (needs_null_check) {
1982 MaybeRecordImplicitNullCheck(instruction);
1983 }
1984 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001985 if (type == DataType::Type::kInt8) {
1986 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001987 }
1988 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001989 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001991 {
1992 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1993 __ ldarh(Register(dst), base);
1994 if (needs_null_check) {
1995 MaybeRecordImplicitNullCheck(instruction);
1996 }
1997 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001998 if (type == DataType::Type::kInt16) {
1999 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
2000 }
Artem Serov914d7a82017-02-07 14:33:49 +00002001 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kInt32:
2003 case DataType::Type::kReference:
2004 case DataType::Type::kInt64:
2005 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002006 {
2007 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2008 __ ldar(Register(dst), base);
2009 if (needs_null_check) {
2010 MaybeRecordImplicitNullCheck(instruction);
2011 }
2012 }
2013 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 case DataType::Type::kFloat32:
2015 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00002016 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002017 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002018
Artem Serov914d7a82017-02-07 14:33:49 +00002019 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2020 {
2021 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2022 __ ldar(temp, base);
2023 if (needs_null_check) {
2024 MaybeRecordImplicitNullCheck(instruction);
2025 }
2026 }
2027 __ Fmov(FPRegister(dst), temp);
2028 break;
Roland Levillain44015862016-01-22 11:47:17 +00002029 }
Aart Bik66c158e2018-01-31 12:55:04 -08002030 case DataType::Type::kUint32:
2031 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002032 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00002033 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002034 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002035 }
2036}
2037
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002038void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002039 CPURegister src,
2040 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002041 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002043 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002045 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002046 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 case DataType::Type::kUint16:
2048 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002049 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002050 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 case DataType::Type::kInt32:
2052 case DataType::Type::kReference:
2053 case DataType::Type::kInt64:
2054 case DataType::Type::kFloat32:
2055 case DataType::Type::kFloat64:
2056 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002057 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002058 break;
Aart Bik66c158e2018-01-31 12:55:04 -08002059 case DataType::Type::kUint32:
2060 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002062 LOG(FATAL) << "Unreachable type " << type;
2063 }
2064}
2065
Artem Serov914d7a82017-02-07 14:33:49 +00002066void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002068 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002069 const MemOperand& dst,
2070 bool needs_null_check) {
2071 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002072 UseScratchRegisterScope temps(GetVIXLAssembler());
2073 Register temp_base = temps.AcquireX();
2074
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002075 DCHECK(!dst.IsPreIndex());
2076 DCHECK(!dst.IsPostIndex());
2077
2078 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002079 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002080 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002081 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002082 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002083 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002085 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002087 {
2088 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2089 __ stlrb(Register(src), base);
2090 if (needs_null_check) {
2091 MaybeRecordImplicitNullCheck(instruction);
2092 }
2093 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002094 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002095 case DataType::Type::kUint16:
2096 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002097 {
2098 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2099 __ stlrh(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::kInt32:
2106 case DataType::Type::kReference:
2107 case DataType::Type::kInt64:
2108 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002109 {
2110 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2111 __ stlr(Register(src), base);
2112 if (needs_null_check) {
2113 MaybeRecordImplicitNullCheck(instruction);
2114 }
2115 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002116 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 case DataType::Type::kFloat32:
2118 case DataType::Type::kFloat64: {
2119 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002120 Register temp_src;
2121 if (src.IsZero()) {
2122 // The zero register is used to avoid synthesizing zero constants.
2123 temp_src = Register(src);
2124 } else {
2125 DCHECK(src.IsFPRegister());
2126 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2127 __ Fmov(temp_src, FPRegister(src));
2128 }
Artem Serov914d7a82017-02-07 14:33:49 +00002129 {
2130 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2131 __ stlr(temp_src, base);
2132 if (needs_null_check) {
2133 MaybeRecordImplicitNullCheck(instruction);
2134 }
2135 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002136 break;
2137 }
Aart Bik66c158e2018-01-31 12:55:04 -08002138 case DataType::Type::kUint32:
2139 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002140 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002141 LOG(FATAL) << "Unreachable type " << type;
2142 }
2143}
2144
Calin Juravle175dc732015-08-25 15:42:32 +01002145void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2146 HInstruction* instruction,
2147 uint32_t dex_pc,
2148 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002149 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002150
2151 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2152 {
2153 // Ensure the pc position is recorded immediately after the `blr` instruction.
2154 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2155 __ blr(lr);
2156 if (EntrypointRequiresStackMap(entrypoint)) {
2157 RecordPcInfo(instruction, dex_pc, slow_path);
2158 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002159 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002160}
2161
Roland Levillaindec8f632016-07-22 17:10:06 +01002162void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2163 HInstruction* instruction,
2164 SlowPathCode* slow_path) {
2165 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002166 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2167 __ Blr(lr);
2168}
2169
Alexandre Rames67555f72014-11-18 10:55:16 +00002170void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002171 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002172 UseScratchRegisterScope temps(GetVIXLAssembler());
2173 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00002174 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2175 const size_t status_byte_offset =
2176 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2177 constexpr uint32_t shifted_initialized_value =
2178 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002179
Serban Constantinescu02164b32014-11-13 14:05:07 +00002180 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002181 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00002182 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07002183 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00002184 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00002185 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002186 __ Bind(slow_path->GetExitLabel());
2187}
Alexandre Rames5319def2014-10-23 10:03:10 +01002188
Vladimir Marko175e7862018-03-27 09:03:13 +00002189void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
2190 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
2191 uint32_t path_to_root = check->GetBitstringPathToRoot();
2192 uint32_t mask = check->GetBitstringMask();
2193 DCHECK(IsPowerOfTwo(mask + 1));
2194 size_t mask_bits = WhichPowerOf2(mask + 1);
2195
2196 if (mask_bits == 16u) {
2197 // Load only the bitstring part of the status word.
2198 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2199 } else {
2200 // /* uint32_t */ temp = temp->status_
2201 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2202 // Extract the bitstring bits.
2203 __ Ubfx(temp, temp, 0, mask_bits);
2204 }
2205 // Compare the bitstring bits to `path_to_root`.
2206 __ Cmp(temp, path_to_root);
2207}
2208
Roland Levillain44015862016-01-22 11:47:17 +00002209void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002210 BarrierType type = BarrierAll;
2211
2212 switch (kind) {
2213 case MemBarrierKind::kAnyAny:
2214 case MemBarrierKind::kAnyStore: {
2215 type = BarrierAll;
2216 break;
2217 }
2218 case MemBarrierKind::kLoadAny: {
2219 type = BarrierReads;
2220 break;
2221 }
2222 case MemBarrierKind::kStoreStore: {
2223 type = BarrierWrites;
2224 break;
2225 }
2226 default:
2227 LOG(FATAL) << "Unexpected memory barrier " << kind;
2228 }
2229 __ Dmb(InnerShareable, type);
2230}
2231
Serban Constantinescu02164b32014-11-13 14:05:07 +00002232void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2233 HBasicBlock* successor) {
2234 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002235 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2236 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002237 slow_path =
2238 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002239 instruction->SetSlowPath(slow_path);
2240 codegen_->AddSlowPath(slow_path);
2241 if (successor != nullptr) {
2242 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002243 }
2244 } else {
2245 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2246 }
2247
Serban Constantinescu02164b32014-11-13 14:05:07 +00002248 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2249 Register temp = temps.AcquireW();
2250
Andreas Gampe542451c2016-07-26 09:02:02 -07002251 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002252 if (successor == nullptr) {
2253 __ Cbnz(temp, slow_path->GetEntryLabel());
2254 __ Bind(slow_path->GetReturnLabel());
2255 } else {
2256 __ Cbz(temp, codegen_->GetLabelOf(successor));
2257 __ B(slow_path->GetEntryLabel());
2258 // slow_path will return to GetLabelOf(successor).
2259 }
2260}
2261
Alexandre Rames5319def2014-10-23 10:03:10 +01002262InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2263 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002264 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002265 assembler_(codegen->GetAssembler()),
2266 codegen_(codegen) {}
2267
Alexandre Rames67555f72014-11-18 10:55:16 +00002268void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002269 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002272 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 case DataType::Type::kInt32:
2274 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002275 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002276 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002278 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002280 case DataType::Type::kFloat32:
2281 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002282 locations->SetInAt(0, Location::RequiresFpuRegister());
2283 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002285 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002286
Alexandre Rames5319def2014-10-23 10:03:10 +01002287 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002288 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002289 }
2290}
2291
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002292void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2293 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2295
2296 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002298 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002299 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2300 object_field_get_with_read_barrier
2301 ? LocationSummary::kCallOnSlowPath
2302 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002303 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002304 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002305 // We need a temporary register for the read barrier marking slow
2306 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002307 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2308 !Runtime::Current()->UseJitCompilation() &&
2309 !field_info.IsVolatile()) {
2310 // If link-time thunks for the Baker read barrier are enabled, for AOT
2311 // non-volatile loads we need a temporary only if the offset is too big.
2312 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2313 locations->AddTemp(FixedTempLocation());
2314 }
2315 } else {
2316 locations->AddTemp(Location::RequiresRegister());
2317 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002318 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002319 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002321 locations->SetOut(Location::RequiresFpuRegister());
2322 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002323 // The output overlaps for an object field get when read barriers
2324 // are enabled: we do not want the load to overwrite the object's
2325 // location, as we need it to emit the read barrier.
2326 locations->SetOut(
2327 Location::RequiresRegister(),
2328 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002329 }
2330}
2331
2332void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2333 const FieldInfo& field_info) {
2334 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002335 LocationSummary* locations = instruction->GetLocations();
2336 Location base_loc = locations->InAt(0);
2337 Location out = locations->Out();
2338 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002339 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2340 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002341 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002342
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002344 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002345 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002346 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002348 Location maybe_temp =
2349 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002350 // Note that potential implicit null checks are handled in this
2351 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2352 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2353 instruction,
2354 out,
2355 base,
2356 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002357 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002358 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002359 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002360 } else {
2361 // General case.
2362 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002363 // Note that a potential implicit null check is handled in this
2364 // CodeGeneratorARM64::LoadAcquire call.
2365 // NB: LoadAcquire will record the pc info if needed.
2366 codegen_->LoadAcquire(
2367 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002368 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002369 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2370 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002371 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002372 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002373 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002374 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002375 // If read barriers are enabled, emit read barriers other than
2376 // Baker's using a slow path (and also unpoison the loaded
2377 // reference, if heap poisoning is enabled).
2378 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2379 }
Roland Levillain4d027112015-07-01 15:41:14 +01002380 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002381}
2382
2383void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2384 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002385 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002386 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002387 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2388 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002389 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002390 locations->SetInAt(1, Location::RequiresFpuRegister());
2391 } else {
2392 locations->SetInAt(1, Location::RequiresRegister());
2393 }
2394}
2395
2396void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002397 const FieldInfo& field_info,
2398 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002399 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2400
2401 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002402 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002403 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002404 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002405 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002406
Roland Levillain4d027112015-07-01 15:41:14 +01002407 {
2408 // We use a block to end the scratch scope before the write barrier, thus
2409 // freeing the temporary registers so they can be used in `MarkGCCard`.
2410 UseScratchRegisterScope temps(GetVIXLAssembler());
2411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002413 DCHECK(value.IsW());
2414 Register temp = temps.AcquireW();
2415 __ Mov(temp, value.W());
2416 GetAssembler()->PoisonHeapReference(temp.W());
2417 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002418 }
Roland Levillain4d027112015-07-01 15:41:14 +01002419
2420 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002421 codegen_->StoreRelease(
2422 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002423 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002424 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2425 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002426 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2427 codegen_->MaybeRecordImplicitNullCheck(instruction);
2428 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002429 }
2430
2431 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002432 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002433 }
2434}
2435
Alexandre Rames67555f72014-11-18 10:55:16 +00002436void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002437 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002438
2439 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 case DataType::Type::kInt32:
2441 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002442 Register dst = OutputRegister(instr);
2443 Register lhs = InputRegisterAt(instr, 0);
2444 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002445 if (instr->IsAdd()) {
2446 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002447 } else if (instr->IsAnd()) {
2448 __ And(dst, lhs, rhs);
2449 } else if (instr->IsOr()) {
2450 __ Orr(dst, lhs, rhs);
2451 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002452 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002453 } else if (instr->IsRor()) {
2454 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002455 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002456 __ Ror(dst, lhs, shift);
2457 } else {
2458 // Ensure shift distance is in the same size register as the result. If
2459 // we are rotating a long and the shift comes in a w register originally,
2460 // we don't need to sxtw for use as an x since the shift distances are
2461 // all & reg_bits - 1.
2462 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2463 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002464 } else if (instr->IsMin() || instr->IsMax()) {
2465 __ Cmp(lhs, rhs);
2466 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002467 } else {
2468 DCHECK(instr->IsXor());
2469 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 }
2471 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002472 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 case DataType::Type::kFloat32:
2474 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002475 FPRegister dst = OutputFPRegister(instr);
2476 FPRegister lhs = InputFPRegisterAt(instr, 0);
2477 FPRegister rhs = InputFPRegisterAt(instr, 1);
2478 if (instr->IsAdd()) {
2479 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002480 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002481 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002482 } else if (instr->IsMin()) {
2483 __ Fmin(dst, lhs, rhs);
2484 } else if (instr->IsMax()) {
2485 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002486 } else {
2487 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002488 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002489 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002490 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002491 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002492 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002493 }
2494}
2495
Serban Constantinescu02164b32014-11-13 14:05:07 +00002496void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2497 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2498
Vladimir Markoca6fff82017-10-03 14:49:14 +01002499 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002501 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002502 case DataType::Type::kInt32:
2503 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002504 locations->SetInAt(0, Location::RequiresRegister());
2505 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002507 break;
2508 }
2509 default:
2510 LOG(FATAL) << "Unexpected shift type " << type;
2511 }
2512}
2513
2514void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2515 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002517 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002518 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002519 case DataType::Type::kInt32:
2520 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002521 Register dst = OutputRegister(instr);
2522 Register lhs = InputRegisterAt(instr, 0);
2523 Operand rhs = InputOperandAt(instr, 1);
2524 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002525 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002527 if (instr->IsShl()) {
2528 __ Lsl(dst, lhs, shift_value);
2529 } else if (instr->IsShr()) {
2530 __ Asr(dst, lhs, shift_value);
2531 } else {
2532 __ Lsr(dst, lhs, shift_value);
2533 }
2534 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002535 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002536
2537 if (instr->IsShl()) {
2538 __ Lsl(dst, lhs, rhs_reg);
2539 } else if (instr->IsShr()) {
2540 __ Asr(dst, lhs, rhs_reg);
2541 } else {
2542 __ Lsr(dst, lhs, rhs_reg);
2543 }
2544 }
2545 break;
2546 }
2547 default:
2548 LOG(FATAL) << "Unexpected shift operation type " << type;
2549 }
2550}
2551
Alexandre Rames5319def2014-10-23 10:03:10 +01002552void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002553 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002554}
2555
2556void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002557 HandleBinaryOp(instruction);
2558}
2559
2560void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2561 HandleBinaryOp(instruction);
2562}
2563
2564void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2565 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002566}
2567
Artem Serov7fc63502016-02-09 17:15:29 +00002568void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002569 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002570 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002571 locations->SetInAt(0, Location::RequiresRegister());
2572 // There is no immediate variant of negated bitwise instructions in AArch64.
2573 locations->SetInAt(1, Location::RequiresRegister());
2574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2575}
2576
Artem Serov7fc63502016-02-09 17:15:29 +00002577void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002578 Register dst = OutputRegister(instr);
2579 Register lhs = InputRegisterAt(instr, 0);
2580 Register rhs = InputRegisterAt(instr, 1);
2581
2582 switch (instr->GetOpKind()) {
2583 case HInstruction::kAnd:
2584 __ Bic(dst, lhs, rhs);
2585 break;
2586 case HInstruction::kOr:
2587 __ Orn(dst, lhs, rhs);
2588 break;
2589 case HInstruction::kXor:
2590 __ Eon(dst, lhs, rhs);
2591 break;
2592 default:
2593 LOG(FATAL) << "Unreachable";
2594 }
2595}
2596
Anton Kirilov74234da2017-01-13 14:42:47 +00002597void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2598 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002599 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2600 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002601 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002602 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002603 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2604 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2605 } else {
2606 locations->SetInAt(0, Location::RequiresRegister());
2607 }
2608 locations->SetInAt(1, Location::RequiresRegister());
2609 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2610}
2611
Anton Kirilov74234da2017-01-13 14:42:47 +00002612void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2613 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002615 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002617 Register out = OutputRegister(instruction);
2618 Register left;
2619 if (kind != HInstruction::kNeg) {
2620 left = InputRegisterAt(instruction, 0);
2621 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002622 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002623 // shifter operand operation, the IR generating `right_reg` (input to the type
2624 // conversion) can have a different type from the current instruction's type,
2625 // so we manually indicate the type.
2626 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002627 Operand right_operand(0);
2628
Anton Kirilov74234da2017-01-13 14:42:47 +00002629 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2630 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002631 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2632 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002633 right_operand = Operand(right_reg,
2634 helpers::ShiftFromOpKind(op_kind),
2635 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002636 }
2637
2638 // Logical binary operations do not support extension operations in the
2639 // operand. Note that VIXL would still manage if it was passed by generating
2640 // the extension as a separate instruction.
2641 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2642 DCHECK(!right_operand.IsExtendedRegister() ||
2643 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2644 kind != HInstruction::kNeg));
2645 switch (kind) {
2646 case HInstruction::kAdd:
2647 __ Add(out, left, right_operand);
2648 break;
2649 case HInstruction::kAnd:
2650 __ And(out, left, right_operand);
2651 break;
2652 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002653 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002654 __ Neg(out, right_operand);
2655 break;
2656 case HInstruction::kOr:
2657 __ Orr(out, left, right_operand);
2658 break;
2659 case HInstruction::kSub:
2660 __ Sub(out, left, right_operand);
2661 break;
2662 case HInstruction::kXor:
2663 __ Eor(out, left, right_operand);
2664 break;
2665 default:
2666 LOG(FATAL) << "Unexpected operation kind: " << kind;
2667 UNREACHABLE();
2668 }
2669}
2670
Artem Serov328429f2016-07-06 16:23:04 +01002671void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002672 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002673 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002674 locations->SetInAt(0, Location::RequiresRegister());
2675 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002677}
2678
Roland Levillain19c54192016-11-04 13:44:09 +00002679void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002680 __ Add(OutputRegister(instruction),
2681 InputRegisterAt(instruction, 0),
2682 Operand(InputOperandAt(instruction, 1)));
2683}
2684
Artem Serove1811ed2017-04-27 16:50:47 +01002685void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2686 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002687 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002688
2689 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2690
2691 locations->SetInAt(0, Location::RequiresRegister());
2692 // For byte case we don't need to shift the index variable so we can encode the data offset into
2693 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2694 // data offset constant generation out of the loop and reduce the critical path length in the
2695 // loop.
2696 locations->SetInAt(1, shift->GetValue() == 0
2697 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2698 : Location::RequiresRegister());
2699 locations->SetInAt(2, Location::ConstantLocation(shift));
2700 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2701}
2702
2703void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2704 HIntermediateAddressIndex* instruction) {
2705 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002706 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002707 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2708
2709 if (shift == 0) {
2710 __ Add(OutputRegister(instruction), index_reg, offset);
2711 } else {
2712 Register offset_reg = InputRegisterAt(instruction, 1);
2713 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2714 }
2715}
2716
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002717void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002718 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002719 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002720 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2721 if (instr->GetOpKind() == HInstruction::kSub &&
2722 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002723 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002724 // Don't allocate register for Mneg instruction.
2725 } else {
2726 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2727 Location::RequiresRegister());
2728 }
2729 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2730 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002731 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2732}
2733
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002734void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002735 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002736 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2737 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002738
2739 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2740 // This fixup should be carried out for all multiply-accumulate instructions:
2741 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002743 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2744 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002745 vixl::aarch64::Instruction* prev =
2746 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002747 if (prev->IsLoadOrStore()) {
2748 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002749 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002750 __ nop();
2751 }
2752 }
2753
2754 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002755 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002756 __ Madd(res, mul_left, mul_right, accumulator);
2757 } else {
2758 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002759 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002760 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002761 __ Mneg(res, mul_left, mul_right);
2762 } else {
2763 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2764 __ Msub(res, mul_left, mul_right, accumulator);
2765 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002766 }
2767}
2768
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002769void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002770 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002772 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002773 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2774 object_array_get_with_read_barrier
2775 ? LocationSummary::kCallOnSlowPath
2776 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002777 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002778 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002779 // We need a temporary register for the read barrier marking slow
2780 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002781 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2782 !Runtime::Current()->UseJitCompilation() &&
2783 instruction->GetIndex()->IsConstant()) {
2784 // Array loads with constant index are treated as field loads.
2785 // If link-time thunks for the Baker read barrier are enabled, for AOT
2786 // constant index loads we need a temporary only if the offset is too big.
2787 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2788 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002790 if (offset >= kReferenceLoadMinFarOffset) {
2791 locations->AddTemp(FixedTempLocation());
2792 }
2793 } else {
2794 locations->AddTemp(Location::RequiresRegister());
2795 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002796 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002797 locations->SetInAt(0, Location::RequiresRegister());
2798 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002800 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2801 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002802 // The output overlaps in the case of an object array get with
2803 // read barriers enabled: we do not want the move to overwrite the
2804 // array's location, as we need it to emit the read barrier.
2805 locations->SetOut(
2806 Location::RequiresRegister(),
2807 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002808 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002809}
2810
2811void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002813 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002814 LocationSummary* locations = instruction->GetLocations();
2815 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002816 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002817 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002818 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2819 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002820 MacroAssembler* masm = GetVIXLAssembler();
2821 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002822
Roland Levillain19c54192016-11-04 13:44:09 +00002823 // The read barrier instrumentation of object ArrayGet instructions
2824 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002825 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002826 instruction->GetArray()->IsIntermediateAddress() &&
2827 kEmitCompilerReadBarrier));
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002830 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002831 // Note that a potential implicit null check is handled in the
2832 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002833 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002834 if (index.IsConstant()) {
2835 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002836 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002837 Location maybe_temp =
2838 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2839 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2840 out,
2841 obj.W(),
2842 offset,
2843 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002844 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002845 /* use_load_acquire */ false);
2846 } else {
2847 Register temp = WRegisterFrom(locations->GetTemp(0));
2848 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002849 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002850 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002851 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002852 // General case.
2853 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002854 Register length;
2855 if (maybe_compressed_char_at) {
2856 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2857 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002858 {
2859 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2860 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2861
2862 if (instruction->GetArray()->IsIntermediateAddress()) {
2863 DCHECK_LT(count_offset, offset);
2864 int64_t adjusted_offset =
2865 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2866 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2867 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2868 } else {
2869 __ Ldr(length, HeapOperand(obj, count_offset));
2870 }
2871 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002872 }
jessicahandojo05765752016-09-09 19:01:32 -07002873 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002874 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002875 if (maybe_compressed_char_at) {
2876 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002877 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2878 "Expecting 0=compressed, 1=uncompressed");
2879 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002880 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002881 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002882 __ B(&done);
2883 __ Bind(&uncompressed_load);
2884 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002885 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002886 __ Bind(&done);
2887 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002888 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002889 source = HeapOperand(obj, offset);
2890 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002891 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002892 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002893 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002894 // We do not need to compute the intermediate address from the array: the
2895 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002896 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002897 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002898 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002899 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2900 }
2901 temp = obj;
2902 } else {
2903 __ Add(temp, obj, offset);
2904 }
jessicahandojo05765752016-09-09 19:01:32 -07002905 if (maybe_compressed_char_at) {
2906 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002907 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2908 "Expecting 0=compressed, 1=uncompressed");
2909 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002910 __ Ldrb(Register(OutputCPURegister(instruction)),
2911 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2912 __ B(&done);
2913 __ Bind(&uncompressed_load);
2914 __ Ldrh(Register(OutputCPURegister(instruction)),
2915 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2916 __ Bind(&done);
2917 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002919 }
Roland Levillain44015862016-01-22 11:47:17 +00002920 }
jessicahandojo05765752016-09-09 19:01:32 -07002921 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002922 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2923 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002924 codegen_->Load(type, OutputCPURegister(instruction), source);
2925 codegen_->MaybeRecordImplicitNullCheck(instruction);
2926 }
Roland Levillain44015862016-01-22 11:47:17 +00002927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002929 static_assert(
2930 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2931 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2932 Location obj_loc = locations->InAt(0);
2933 if (index.IsConstant()) {
2934 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2935 } else {
2936 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2937 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002938 }
Roland Levillain4d027112015-07-01 15:41:14 +01002939 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002940}
2941
Alexandre Rames5319def2014-10-23 10:03:10 +01002942void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002943 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002944 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002946}
2947
2948void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002949 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002950 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002951 {
2952 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2953 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2954 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2955 codegen_->MaybeRecordImplicitNullCheck(instruction);
2956 }
jessicahandojo05765752016-09-09 19:01:32 -07002957 // Mask out compression flag from String's array length.
2958 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002959 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002960 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002961}
2962
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002963void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002965
2966 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002967 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002968 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002969 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002970 LocationSummary::kCallOnSlowPath :
2971 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002972 locations->SetInAt(0, Location::RequiresRegister());
2973 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002974 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2975 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002977 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002978 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002979 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002980 }
2981}
2982
2983void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002984 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002985 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002986 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002987 bool needs_write_barrier =
2988 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002989
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002990 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002991 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002992 CPURegister source = value;
2993 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002994 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002995 MemOperand destination = HeapOperand(array);
2996 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002997
2998 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002999 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003000 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003001 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003002 destination = HeapOperand(array, offset);
3003 } else {
3004 UseScratchRegisterScope temps(masm);
3005 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01003006 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003007 // We do not need to compute the intermediate address from the array: the
3008 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01003009 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003010 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01003011 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003012 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
3013 }
3014 temp = array;
3015 } else {
3016 __ Add(temp, array, offset);
3017 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003018 destination = HeapOperand(temp,
3019 XRegisterFrom(index),
3020 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003021 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003022 }
Artem Serov914d7a82017-02-07 14:33:49 +00003023 {
3024 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3025 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3026 codegen_->Store(value_type, value, destination);
3027 codegen_->MaybeRecordImplicitNullCheck(instruction);
3028 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003029 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003030 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003031 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003032 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003033 {
3034 // We use a block to end the scratch scope before the write barrier, thus
3035 // freeing the temporary registers so they can be used in `MarkGCCard`.
3036 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003037 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003038 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003039 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003040 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003041 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003042 destination = HeapOperand(temp,
3043 XRegisterFrom(index),
3044 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003046 }
3047
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003048 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3049 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3050 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3051
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003052 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003053 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003054 codegen_->AddSlowPath(slow_path);
3055 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003056 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003057 __ Cbnz(Register(value), &non_zero);
3058 if (!index.IsConstant()) {
3059 __ Add(temp, array, offset);
3060 }
Artem Serov914d7a82017-02-07 14:33:49 +00003061 {
3062 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3063 // emitted.
3064 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3065 __ Str(wzr, destination);
3066 codegen_->MaybeRecordImplicitNullCheck(instruction);
3067 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003068 __ B(&done);
3069 __ Bind(&non_zero);
3070 }
3071
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003072 // Note that when Baker read barriers are enabled, the type
3073 // checks are performed without read barriers. This is fine,
3074 // even in the case where a class object is in the from-space
3075 // after the flip, as a comparison involving such a type would
3076 // not produce a false positive; it may of course produce a
3077 // false negative, in which case we would take the ArraySet
3078 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003079
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003080 Register temp2 = temps.AcquireSameSizeAs(array);
3081 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003082 {
3083 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3084 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3085 __ Ldr(temp, HeapOperand(array, class_offset));
3086 codegen_->MaybeRecordImplicitNullCheck(instruction);
3087 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003088 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003089
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003090 // /* HeapReference<Class> */ temp = temp->component_type_
3091 __ Ldr(temp, HeapOperand(temp, component_offset));
3092 // /* HeapReference<Class> */ temp2 = value->klass_
3093 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3094 // If heap poisoning is enabled, no need to unpoison `temp`
3095 // nor `temp2`, as we are comparing two poisoned references.
3096 __ Cmp(temp, temp2);
3097 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003098
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003099 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3100 vixl::aarch64::Label do_put;
3101 __ B(eq, &do_put);
3102 // If heap poisoning is enabled, the `temp` reference has
3103 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003104 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3105
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003106 // /* HeapReference<Class> */ temp = temp->super_class_
3107 __ Ldr(temp, HeapOperand(temp, super_offset));
3108 // If heap poisoning is enabled, no need to unpoison
3109 // `temp`, as we are comparing against null below.
3110 __ Cbnz(temp, slow_path->GetEntryLabel());
3111 __ Bind(&do_put);
3112 } else {
3113 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003114 }
3115 }
3116
3117 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003118 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003119 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003120 __ Mov(temp2, value.W());
3121 GetAssembler()->PoisonHeapReference(temp2);
3122 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003123 }
3124
3125 if (!index.IsConstant()) {
3126 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003127 } else {
3128 // We no longer need the `temp` here so release it as the store below may
3129 // need a scratch register (if the constant index makes the offset too large)
3130 // and the poisoned `source` could be using the other scratch register.
3131 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003132 }
Artem Serov914d7a82017-02-07 14:33:49 +00003133 {
3134 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3135 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3136 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003137
Artem Serov914d7a82017-02-07 14:33:49 +00003138 if (!may_need_runtime_call_for_type_check) {
3139 codegen_->MaybeRecordImplicitNullCheck(instruction);
3140 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003141 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003142 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003143
3144 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3145
3146 if (done.IsLinked()) {
3147 __ Bind(&done);
3148 }
3149
3150 if (slow_path != nullptr) {
3151 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003152 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003153 }
3154}
3155
Alexandre Rames67555f72014-11-18 10:55:16 +00003156void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003157 RegisterSet caller_saves = RegisterSet::Empty();
3158 InvokeRuntimeCallingConvention calling_convention;
3159 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3160 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3161 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003162 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003163 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003164}
3165
3166void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003167 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003168 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003169 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003170 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3171 __ B(slow_path->GetEntryLabel(), hs);
3172}
3173
Alexandre Rames67555f72014-11-18 10:55:16 +00003174void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3175 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003176 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003177 locations->SetInAt(0, Location::RequiresRegister());
3178 if (check->HasUses()) {
3179 locations->SetOut(Location::SameAsFirstInput());
3180 }
3181}
3182
3183void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3184 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003185 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Alexandre Rames67555f72014-11-18 10:55:16 +00003186 check->GetLoadClass(), check, check->GetDexPc(), true);
3187 codegen_->AddSlowPath(slow_path);
3188 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3189}
3190
Roland Levillain1a653882016-03-18 18:05:57 +00003191static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3192 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3193 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3194}
3195
3196void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3197 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3198 Location rhs_loc = instruction->GetLocations()->InAt(1);
3199 if (rhs_loc.IsConstant()) {
3200 // 0.0 is the only immediate that can be encoded directly in
3201 // an FCMP instruction.
3202 //
3203 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3204 // specify that in a floating-point comparison, positive zero
3205 // and negative zero are considered equal, so we can use the
3206 // literal 0.0 for both cases here.
3207 //
3208 // Note however that some methods (Float.equal, Float.compare,
3209 // Float.compareTo, Double.equal, Double.compare,
3210 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3211 // StrictMath.min) consider 0.0 to be (strictly) greater than
3212 // -0.0. So if we ever translate calls to these methods into a
3213 // HCompare instruction, we must handle the -0.0 case with
3214 // care here.
3215 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3216 __ Fcmp(lhs_reg, 0.0);
3217 } else {
3218 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3219 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003220}
3221
Serban Constantinescu02164b32014-11-13 14:05:07 +00003222void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003223 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003224 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003226 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003228 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003229 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003231 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003232 case DataType::Type::kInt32:
3233 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003234 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003235 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3237 break;
3238 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 case DataType::Type::kFloat32:
3240 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003241 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003242 locations->SetInAt(1,
3243 IsFloatingPointZeroConstant(compare->InputAt(1))
3244 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3245 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003246 locations->SetOut(Location::RequiresRegister());
3247 break;
3248 }
3249 default:
3250 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3251 }
3252}
3253
3254void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003255 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003256
3257 // 0 if: left == right
3258 // 1 if: left > right
3259 // -1 if: left < right
3260 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003261 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003262 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003264 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003265 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003266 case DataType::Type::kInt32:
3267 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003268 Register result = OutputRegister(compare);
3269 Register left = InputRegisterAt(compare, 0);
3270 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003271 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003272 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3273 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003274 break;
3275 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003276 case DataType::Type::kFloat32:
3277 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003278 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003279 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003280 __ Cset(result, ne);
3281 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003282 break;
3283 }
3284 default:
3285 LOG(FATAL) << "Unimplemented compare type " << in_type;
3286 }
3287}
3288
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003289void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003290 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003291
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003292 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003293 locations->SetInAt(0, Location::RequiresFpuRegister());
3294 locations->SetInAt(1,
3295 IsFloatingPointZeroConstant(instruction->InputAt(1))
3296 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3297 : Location::RequiresFpuRegister());
3298 } else {
3299 // Integer cases.
3300 locations->SetInAt(0, Location::RequiresRegister());
3301 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3302 }
3303
David Brazdilb3e773e2016-01-26 11:28:37 +00003304 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003306 }
3307}
3308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003309void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003310 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003311 return;
3312 }
3313
3314 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003315 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003316 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003318 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003319 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003320 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003321 } else {
3322 // Integer cases.
3323 Register lhs = InputRegisterAt(instruction, 0);
3324 Operand rhs = InputOperandAt(instruction, 1);
3325 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003326 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003327 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003328}
3329
3330#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3331 M(Equal) \
3332 M(NotEqual) \
3333 M(LessThan) \
3334 M(LessThanOrEqual) \
3335 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003336 M(GreaterThanOrEqual) \
3337 M(Below) \
3338 M(BelowOrEqual) \
3339 M(Above) \
3340 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003341#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003342void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3343void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003344FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003345#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003346#undef FOR_EACH_CONDITION_INSTRUCTION
3347
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003348void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003349 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003350 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003351 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3352
3353 Register out = OutputRegister(instruction);
3354 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003355
3356 if (abs_imm == 2) {
3357 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3358 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3359 } else {
3360 UseScratchRegisterScope temps(GetVIXLAssembler());
3361 Register temp = temps.AcquireSameSizeAs(out);
3362 __ Add(temp, dividend, abs_imm - 1);
3363 __ Cmp(dividend, 0);
3364 __ Csel(out, temp, dividend, lt);
3365 }
3366
Zheng Xuc6667102015-05-15 16:08:45 +08003367 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003368 if (imm > 0) {
3369 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003370 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003371 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003372 }
3373}
3374
3375void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3376 DCHECK(instruction->IsDiv() || instruction->IsRem());
3377
3378 LocationSummary* locations = instruction->GetLocations();
3379 Location second = locations->InAt(1);
3380 DCHECK(second.IsConstant());
3381
3382 Register out = OutputRegister(instruction);
3383 Register dividend = InputRegisterAt(instruction, 0);
3384 int64_t imm = Int64FromConstant(second.GetConstant());
3385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 DataType::Type type = instruction->GetResultType();
3387 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003388
3389 int64_t magic;
3390 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003391 CalculateMagicAndShiftForDivRem(
3392 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003393
3394 UseScratchRegisterScope temps(GetVIXLAssembler());
3395 Register temp = temps.AcquireSameSizeAs(out);
3396
3397 // temp = get_high(dividend * magic)
3398 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003399 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003400 __ Smulh(temp, dividend, temp);
3401 } else {
3402 __ Smull(temp.X(), dividend, temp);
3403 __ Lsr(temp.X(), temp.X(), 32);
3404 }
3405
3406 if (imm > 0 && magic < 0) {
3407 __ Add(temp, temp, dividend);
3408 } else if (imm < 0 && magic > 0) {
3409 __ Sub(temp, temp, dividend);
3410 }
3411
3412 if (shift != 0) {
3413 __ Asr(temp, temp, shift);
3414 }
3415
3416 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003418 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003419 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003420 // TODO: Strength reduction for msub.
3421 Register temp_imm = temps.AcquireSameSizeAs(out);
3422 __ Mov(temp_imm, imm);
3423 __ Msub(out, temp, temp_imm, dividend);
3424 }
3425}
3426
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003427void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003428 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003429
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003430 if (imm == 0) {
3431 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3432 return;
3433 }
Zheng Xuc6667102015-05-15 16:08:45 +08003434
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003435 if (IsPowerOfTwo(AbsOrMin(imm))) {
3436 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003437 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003438 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3439 DCHECK(imm < -2 || imm > 2) << imm;
3440 GenerateDivRemWithAnyConstant(instruction);
3441 }
3442}
3443
3444void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3445 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3446 << instruction->GetResultType();
3447
3448 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3449 GenerateIntDivForConstDenom(instruction);
3450 } else {
3451 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003452 Register dividend = InputRegisterAt(instruction, 0);
3453 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003454 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003455 }
3456}
3457
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003458void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3459 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003460 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003461 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 case DataType::Type::kInt32:
3463 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003464 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003465 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3467 break;
3468
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003469 case DataType::Type::kFloat32:
3470 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003471 locations->SetInAt(0, Location::RequiresFpuRegister());
3472 locations->SetInAt(1, Location::RequiresFpuRegister());
3473 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3474 break;
3475
3476 default:
3477 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3478 }
3479}
3480
3481void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003482 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003483 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003484 case DataType::Type::kInt32:
3485 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003486 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003487 break;
3488
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 case DataType::Type::kFloat32:
3490 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003491 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3492 break;
3493
3494 default:
3495 LOG(FATAL) << "Unexpected div type " << type;
3496 }
3497}
3498
Alexandre Rames67555f72014-11-18 10:55:16 +00003499void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003500 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003501 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003502}
3503
3504void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3505 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003506 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003507 codegen_->AddSlowPath(slow_path);
3508 Location value = instruction->GetLocations()->InAt(0);
3509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003511
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003513 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003514 return;
3515 }
3516
Alexandre Rames67555f72014-11-18 10:55:16 +00003517 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003518 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003519 if (divisor == 0) {
3520 __ B(slow_path->GetEntryLabel());
3521 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003522 // A division by a non-null constant is valid. We don't need to perform
3523 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003524 }
3525 } else {
3526 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3527 }
3528}
3529
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003530void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3531 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003532 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003533 locations->SetOut(Location::ConstantLocation(constant));
3534}
3535
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003536void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3537 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003538 // Will be generated at use site.
3539}
3540
Alexandre Rames5319def2014-10-23 10:03:10 +01003541void LocationsBuilderARM64::VisitExit(HExit* exit) {
3542 exit->SetLocations(nullptr);
3543}
3544
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003545void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003546}
3547
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003548void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3549 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003550 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003551 locations->SetOut(Location::ConstantLocation(constant));
3552}
3553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003554void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003555 // Will be generated at use site.
3556}
3557
David Brazdilfc6a86a2015-06-26 10:33:45 +00003558void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003559 if (successor->IsExitBlock()) {
3560 DCHECK(got->GetPrevious()->AlwaysThrows());
3561 return; // no code needed
3562 }
3563
Serban Constantinescu02164b32014-11-13 14:05:07 +00003564 HBasicBlock* block = got->GetBlock();
3565 HInstruction* previous = got->GetPrevious();
3566 HLoopInformation* info = block->GetLoopInformation();
3567
David Brazdil46e2a392015-03-16 17:31:52 +00003568 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003569 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3570 UseScratchRegisterScope temps(GetVIXLAssembler());
3571 Register temp1 = temps.AcquireX();
3572 Register temp2 = temps.AcquireX();
3573 __ Ldr(temp1, MemOperand(sp, 0));
3574 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3575 __ Add(temp2, temp2, 1);
3576 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3577 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003578 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3579 return;
3580 }
3581 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3582 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003583 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003584 }
3585 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003586 __ B(codegen_->GetLabelOf(successor));
3587 }
3588}
3589
David Brazdilfc6a86a2015-06-26 10:33:45 +00003590void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3591 got->SetLocations(nullptr);
3592}
3593
3594void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3595 HandleGoto(got, got->GetSuccessor());
3596}
3597
3598void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3599 try_boundary->SetLocations(nullptr);
3600}
3601
3602void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3603 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3604 if (!successor->IsExitBlock()) {
3605 HandleGoto(try_boundary, successor);
3606 }
3607}
3608
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003609void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003610 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003611 vixl::aarch64::Label* true_target,
3612 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003613 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003614
David Brazdil0debae72015-11-12 18:37:00 +00003615 if (true_target == nullptr && false_target == nullptr) {
3616 // Nothing to do. The code always falls through.
3617 return;
3618 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003619 // Constant condition, statically compared against "true" (integer value 1).
3620 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003621 if (true_target != nullptr) {
3622 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003623 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003624 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003625 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003626 if (false_target != nullptr) {
3627 __ B(false_target);
3628 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003629 }
David Brazdil0debae72015-11-12 18:37:00 +00003630 return;
3631 }
3632
3633 // The following code generates these patterns:
3634 // (1) true_target == nullptr && false_target != nullptr
3635 // - opposite condition true => branch to false_target
3636 // (2) true_target != nullptr && false_target == nullptr
3637 // - condition true => branch to true_target
3638 // (3) true_target != nullptr && false_target != nullptr
3639 // - condition true => branch to true_target
3640 // - branch to false_target
3641 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003642 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003643 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003644 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003645 if (true_target == nullptr) {
3646 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3647 } else {
3648 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3649 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003650 } else {
3651 // The condition instruction has not been materialized, use its inputs as
3652 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003653 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003654
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003655 DataType::Type type = condition->InputAt(0)->GetType();
3656 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003657 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003658 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003659 IfCondition opposite_condition = condition->GetOppositeCondition();
3660 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003661 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003662 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003663 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003664 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003665 // Integer cases.
3666 Register lhs = InputRegisterAt(condition, 0);
3667 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003668
3669 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003670 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003671 if (true_target == nullptr) {
3672 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3673 non_fallthrough_target = false_target;
3674 } else {
3675 arm64_cond = ARM64Condition(condition->GetCondition());
3676 non_fallthrough_target = true_target;
3677 }
3678
Aart Bik086d27e2016-01-20 17:02:00 -08003679 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003680 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003681 switch (arm64_cond) {
3682 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003683 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003684 break;
3685 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003686 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003687 break;
3688 case lt:
3689 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003690 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003691 break;
3692 case ge:
3693 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003694 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003695 break;
3696 default:
3697 // Without the `static_cast` the compiler throws an error for
3698 // `-Werror=sign-promo`.
3699 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3700 }
3701 } else {
3702 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003703 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003704 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003705 }
3706 }
David Brazdil0debae72015-11-12 18:37:00 +00003707
3708 // If neither branch falls through (case 3), the conditional branch to `true_target`
3709 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3710 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003711 __ B(false_target);
3712 }
3713}
3714
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003715void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003716 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003717 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003718 locations->SetInAt(0, Location::RequiresRegister());
3719 }
3720}
3721
3722void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003723 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3724 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003725 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3726 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3727 true_target = nullptr;
3728 }
3729 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3730 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3731 false_target = nullptr;
3732 }
David Brazdil0debae72015-11-12 18:37:00 +00003733 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003734}
3735
3736void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003737 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003738 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003739 InvokeRuntimeCallingConvention calling_convention;
3740 RegisterSet caller_saves = RegisterSet::Empty();
3741 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3742 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003743 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003744 locations->SetInAt(0, Location::RequiresRegister());
3745 }
3746}
3747
3748void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003749 SlowPathCodeARM64* slow_path =
3750 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003751 GenerateTestAndBranch(deoptimize,
3752 /* condition_input_index */ 0,
3753 slow_path->GetEntryLabel(),
3754 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003755}
3756
Mingyao Yang063fc772016-08-02 11:02:54 -07003757void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003758 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003759 LocationSummary(flag, LocationSummary::kNoCall);
3760 locations->SetOut(Location::RequiresRegister());
3761}
3762
3763void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3764 __ Ldr(OutputRegister(flag),
3765 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3766}
3767
David Brazdilc0b601b2016-02-08 14:20:45 +00003768static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3769 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003771}
3772
Alexandre Rames880f1192016-06-13 16:04:50 +01003773static inline Condition GetConditionForSelect(HCondition* condition) {
3774 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003775 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3776 : ARM64Condition(cond);
3777}
3778
David Brazdil74eb1b22015-12-14 11:44:01 +00003779void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003780 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003781 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003782 locations->SetInAt(0, Location::RequiresFpuRegister());
3783 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003784 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003785 } else {
3786 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3787 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3788 bool is_true_value_constant = cst_true_value != nullptr;
3789 bool is_false_value_constant = cst_false_value != nullptr;
3790 // Ask VIXL whether we should synthesize constants in registers.
3791 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3792 Operand true_op = is_true_value_constant ?
3793 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3794 Operand false_op = is_false_value_constant ?
3795 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3796 bool true_value_in_register = false;
3797 bool false_value_in_register = false;
3798 MacroAssembler::GetCselSynthesisInformation(
3799 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3800 true_value_in_register |= !is_true_value_constant;
3801 false_value_in_register |= !is_false_value_constant;
3802
3803 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3804 : Location::ConstantLocation(cst_true_value));
3805 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3806 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003808 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003809
David Brazdil74eb1b22015-12-14 11:44:01 +00003810 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3811 locations->SetInAt(2, Location::RequiresRegister());
3812 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003813}
3814
3815void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003816 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003817 Condition csel_cond;
3818
3819 if (IsBooleanValueOrMaterializedCondition(cond)) {
3820 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003821 // Use the condition flags set by the previous instruction.
3822 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003823 } else {
3824 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003825 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003826 }
3827 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003828 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003829 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003830 } else {
3831 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003832 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003833 }
3834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003836 __ Fcsel(OutputFPRegister(select),
3837 InputFPRegisterAt(select, 1),
3838 InputFPRegisterAt(select, 0),
3839 csel_cond);
3840 } else {
3841 __ Csel(OutputRegister(select),
3842 InputOperandAt(select, 1),
3843 InputOperandAt(select, 0),
3844 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003845 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003846}
3847
David Srbecky0cf44932015-12-09 14:09:59 +00003848void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003849 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003850}
3851
David Srbeckyd28f4a02016-03-14 17:14:24 +00003852void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3853 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003854}
3855
3856void CodeGeneratorARM64::GenerateNop() {
3857 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003858}
3859
Alexandre Rames5319def2014-10-23 10:03:10 +01003860void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003861 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003862}
3863
3864void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003865 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003866}
3867
3868void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003869 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003870}
3871
3872void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003873 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003874}
3875
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003876// Temp is used for read barrier.
3877static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3878 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003879 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003880 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3881 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3882 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3883 return 1;
3884 }
3885 return 0;
3886}
3887
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003888// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003889// interface pointer, one for loading the current interface.
3890// The other checks have one temp for loading the object's class.
3891static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3892 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3893 return 3;
3894 }
3895 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003896}
3897
Alexandre Rames67555f72014-11-18 10:55:16 +00003898void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003899 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003900 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003901 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003902 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003903 case TypeCheckKind::kExactCheck:
3904 case TypeCheckKind::kAbstractClassCheck:
3905 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003906 case TypeCheckKind::kArrayObjectCheck: {
3907 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3908 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3909 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003910 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003911 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003913 case TypeCheckKind::kUnresolvedCheck:
3914 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003915 call_kind = LocationSummary::kCallOnSlowPath;
3916 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003917 case TypeCheckKind::kBitstringCheck:
3918 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003919 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920
Vladimir Markoca6fff82017-10-03 14:49:14 +01003921 LocationSummary* locations =
3922 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003923 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003924 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003925 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003926 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003927 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3928 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3929 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3930 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3931 } else {
3932 locations->SetInAt(1, Location::RequiresRegister());
3933 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003934 // The "out" register is used as a temporary, so it overlaps with the inputs.
3935 // Note that TypeCheckSlowPathARM64 uses this register too.
3936 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003937 // Add temps if necessary for read barriers.
3938 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003939}
3940
3941void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003942 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003943 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003944 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003945 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003946 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3947 ? Register()
3948 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003949 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003950 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003951 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3952 DCHECK_LE(num_temps, 1u);
3953 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003954 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3955 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3956 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3957 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003958
Scott Wakeling97c72b72016-06-24 16:19:36 +01003959 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003960 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003961
3962 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003963 // Avoid null check if we know `obj` is not null.
3964 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003965 __ Cbz(obj, &zero);
3966 }
3967
Roland Levillain44015862016-01-22 11:47:17 +00003968 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003970 ReadBarrierOption read_barrier_option =
3971 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003972 // /* HeapReference<Class> */ out = obj->klass_
3973 GenerateReferenceLoadTwoRegisters(instruction,
3974 out_loc,
3975 obj_loc,
3976 class_offset,
3977 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003978 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 __ Cmp(out, cls);
3980 __ Cset(out, eq);
3981 if (zero.IsLinked()) {
3982 __ B(&done);
3983 }
3984 break;
3985 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003988 ReadBarrierOption read_barrier_option =
3989 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003990 // /* HeapReference<Class> */ out = obj->klass_
3991 GenerateReferenceLoadTwoRegisters(instruction,
3992 out_loc,
3993 obj_loc,
3994 class_offset,
3995 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003996 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003997 // If the class is abstract, we eagerly fetch the super class of the
3998 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003999 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004000 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004002 GenerateReferenceLoadOneRegister(instruction,
4003 out_loc,
4004 super_offset,
4005 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004006 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004007 // If `out` is null, we use it for the result, and jump to `done`.
4008 __ Cbz(out, &done);
4009 __ Cmp(out, cls);
4010 __ B(ne, &loop);
4011 __ Mov(out, 1);
4012 if (zero.IsLinked()) {
4013 __ B(&done);
4014 }
4015 break;
4016 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004017
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004018 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004019 ReadBarrierOption read_barrier_option =
4020 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004021 // /* HeapReference<Class> */ out = obj->klass_
4022 GenerateReferenceLoadTwoRegisters(instruction,
4023 out_loc,
4024 obj_loc,
4025 class_offset,
4026 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004027 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004028 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004029 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004030 __ Bind(&loop);
4031 __ Cmp(out, cls);
4032 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004033 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004034 GenerateReferenceLoadOneRegister(instruction,
4035 out_loc,
4036 super_offset,
4037 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004038 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004039 __ Cbnz(out, &loop);
4040 // If `out` is null, we use it for the result, and jump to `done`.
4041 __ B(&done);
4042 __ Bind(&success);
4043 __ Mov(out, 1);
4044 if (zero.IsLinked()) {
4045 __ B(&done);
4046 }
4047 break;
4048 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004050 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004051 ReadBarrierOption read_barrier_option =
4052 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004053 // /* HeapReference<Class> */ out = obj->klass_
4054 GenerateReferenceLoadTwoRegisters(instruction,
4055 out_loc,
4056 obj_loc,
4057 class_offset,
4058 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004059 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004060 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004061 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004062 __ Cmp(out, cls);
4063 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004064 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004065 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004066 GenerateReferenceLoadOneRegister(instruction,
4067 out_loc,
4068 component_offset,
4069 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004070 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004071 // If `out` is null, we use it for the result, and jump to `done`.
4072 __ Cbz(out, &done);
4073 __ Ldrh(out, HeapOperand(out, primitive_offset));
4074 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4075 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004076 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004077 __ Mov(out, 1);
4078 __ B(&done);
4079 break;
4080 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004081
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004082 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004083 // No read barrier since the slow path will retry upon failure.
4084 // /* HeapReference<Class> */ out = obj->klass_
4085 GenerateReferenceLoadTwoRegisters(instruction,
4086 out_loc,
4087 obj_loc,
4088 class_offset,
4089 maybe_temp_loc,
4090 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004091 __ Cmp(out, cls);
4092 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004093 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4094 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 codegen_->AddSlowPath(slow_path);
4096 __ B(ne, slow_path->GetEntryLabel());
4097 __ Mov(out, 1);
4098 if (zero.IsLinked()) {
4099 __ B(&done);
4100 }
4101 break;
4102 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004103
Calin Juravle98893e12015-10-02 21:05:03 +01004104 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105 case TypeCheckKind::kInterfaceCheck: {
4106 // Note that we indeed only call on slow path, but we always go
4107 // into the slow path for the unresolved and interface check
4108 // cases.
4109 //
4110 // We cannot directly call the InstanceofNonTrivial runtime
4111 // entry point without resorting to a type checking slow path
4112 // here (i.e. by calling InvokeRuntime directly), as it would
4113 // require to assign fixed registers for the inputs of this
4114 // HInstanceOf instruction (following the runtime calling
4115 // convention), which might be cluttered by the potential first
4116 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004117 //
4118 // TODO: Introduce a new runtime entry point taking the object
4119 // to test (instead of its class) as argument, and let it deal
4120 // with the read barrier issues. This will let us refactor this
4121 // case of the `switch` code as it was previously (with a direct
4122 // call to the runtime not using a type checking slow path).
4123 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004124 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004125 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4126 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004127 codegen_->AddSlowPath(slow_path);
4128 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 if (zero.IsLinked()) {
4130 __ B(&done);
4131 }
4132 break;
4133 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004134
4135 case TypeCheckKind::kBitstringCheck: {
4136 // /* HeapReference<Class> */ temp = obj->klass_
4137 GenerateReferenceLoadTwoRegisters(instruction,
4138 out_loc,
4139 obj_loc,
4140 class_offset,
4141 maybe_temp_loc,
4142 kWithoutReadBarrier);
4143
4144 GenerateBitstringTypeCheckCompare(instruction, out);
4145 __ Cset(out, eq);
4146 if (zero.IsLinked()) {
4147 __ B(&done);
4148 }
4149 break;
4150 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004151 }
4152
4153 if (zero.IsLinked()) {
4154 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004155 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004156 }
4157
4158 if (done.IsLinked()) {
4159 __ Bind(&done);
4160 }
4161
4162 if (slow_path != nullptr) {
4163 __ Bind(slow_path->GetExitLabel());
4164 }
4165}
4166
4167void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004168 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004169 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004170 LocationSummary* locations =
4171 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004172 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004173 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4174 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4175 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4176 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4177 } else {
4178 locations->SetInAt(1, Location::RequiresRegister());
4179 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004180 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4181 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004182}
4183
4184void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004185 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004186 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004187 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004188 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004189 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4190 ? Register()
4191 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004192 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4193 DCHECK_GE(num_temps, 1u);
4194 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004195 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004196 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4197 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004198 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004199 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4200 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4201 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4202 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4203 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4204 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4205 const uint32_t object_array_data_offset =
4206 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207
Vladimir Marko87584542017-12-12 17:47:52 +00004208 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004209 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004210 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4211 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004213
Scott Wakeling97c72b72016-06-24 16:19:36 +01004214 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004215 // Avoid null check if we know obj is not null.
4216 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004217 __ Cbz(obj, &done);
4218 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004219
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004220 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004221 case TypeCheckKind::kExactCheck:
4222 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004223 // /* HeapReference<Class> */ temp = obj->klass_
4224 GenerateReferenceLoadTwoRegisters(instruction,
4225 temp_loc,
4226 obj_loc,
4227 class_offset,
4228 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004229 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004230
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004231 __ Cmp(temp, cls);
4232 // Jump to slow path for throwing the exception or doing a
4233 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004234 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004235 break;
4236 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004237
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004238 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004239 // /* HeapReference<Class> */ temp = obj->klass_
4240 GenerateReferenceLoadTwoRegisters(instruction,
4241 temp_loc,
4242 obj_loc,
4243 class_offset,
4244 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004245 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004246
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004247 // If the class is abstract, we eagerly fetch the super class of the
4248 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004249 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004250 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004251 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004252 GenerateReferenceLoadOneRegister(instruction,
4253 temp_loc,
4254 super_offset,
4255 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004256 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004257
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004258 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4259 // exception.
4260 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4261 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004262 __ Cmp(temp, cls);
4263 __ B(ne, &loop);
4264 break;
4265 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004266
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004267 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004268 // /* HeapReference<Class> */ temp = obj->klass_
4269 GenerateReferenceLoadTwoRegisters(instruction,
4270 temp_loc,
4271 obj_loc,
4272 class_offset,
4273 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004274 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004275
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004276 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004277 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004278 __ Bind(&loop);
4279 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004280 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004282 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004283 GenerateReferenceLoadOneRegister(instruction,
4284 temp_loc,
4285 super_offset,
4286 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004287 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004288
4289 // If the class reference currently in `temp` is not null, jump
4290 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004291 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004292 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004293 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004294 break;
4295 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004297 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004298 // /* HeapReference<Class> */ temp = obj->klass_
4299 GenerateReferenceLoadTwoRegisters(instruction,
4300 temp_loc,
4301 obj_loc,
4302 class_offset,
4303 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004304 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004305
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004306 // Do an exact check.
4307 __ Cmp(temp, cls);
4308 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004309
4310 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004311 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004312 GenerateReferenceLoadOneRegister(instruction,
4313 temp_loc,
4314 component_offset,
4315 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004316 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004317
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004318 // If the component type is null, jump to the slow path to throw the exception.
4319 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4320 // Otherwise, the object is indeed an array. Further check that this component type is not a
4321 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004322 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4323 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004324 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004325 break;
4326 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004327
Calin Juravle98893e12015-10-02 21:05:03 +01004328 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004329 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004330 //
4331 // We cannot directly call the CheckCast runtime entry point
4332 // without resorting to a type checking slow path here (i.e. by
4333 // calling InvokeRuntime directly), as it would require to
4334 // assign fixed registers for the inputs of this HInstanceOf
4335 // instruction (following the runtime calling convention), which
4336 // might be cluttered by the potential first read barrier
4337 // emission at the beginning of this method.
4338 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004339 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004340 case TypeCheckKind::kInterfaceCheck: {
4341 // /* HeapReference<Class> */ temp = obj->klass_
4342 GenerateReferenceLoadTwoRegisters(instruction,
4343 temp_loc,
4344 obj_loc,
4345 class_offset,
4346 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004347 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004348
4349 // /* HeapReference<Class> */ temp = temp->iftable_
4350 GenerateReferenceLoadTwoRegisters(instruction,
4351 temp_loc,
4352 temp_loc,
4353 iftable_offset,
4354 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004355 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004356 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004357 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004358 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004359 vixl::aarch64::Label start_loop;
4360 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004361 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004362 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4363 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004364 // Go to next interface.
4365 __ Add(temp, temp, 2 * kHeapReferenceSize);
4366 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004367 // Compare the classes and continue the loop if they do not match.
4368 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4369 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004370 break;
4371 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004372
4373 case TypeCheckKind::kBitstringCheck: {
4374 // /* HeapReference<Class> */ temp = obj->klass_
4375 GenerateReferenceLoadTwoRegisters(instruction,
4376 temp_loc,
4377 obj_loc,
4378 class_offset,
4379 maybe_temp2_loc,
4380 kWithoutReadBarrier);
4381
4382 GenerateBitstringTypeCheckCompare(instruction, temp);
4383 __ B(ne, type_check_slow_path->GetEntryLabel());
4384 break;
4385 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004386 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004387 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004388
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004389 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004390}
4391
Alexandre Rames5319def2014-10-23 10:03:10 +01004392void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004393 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004394 locations->SetOut(Location::ConstantLocation(constant));
4395}
4396
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004397void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004398 // Will be generated at use site.
4399}
4400
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004401void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004402 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004403 locations->SetOut(Location::ConstantLocation(constant));
4404}
4405
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004406void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004407 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004408}
4409
Calin Juravle175dc732015-08-25 15:42:32 +01004410void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4411 // The trampoline uses the same calling convention as dex calling conventions,
4412 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4413 // the method_idx.
4414 HandleInvoke(invoke);
4415}
4416
4417void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4418 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004419 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004420}
4421
Alexandre Rames5319def2014-10-23 10:03:10 +01004422void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004423 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004424 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004425}
4426
Alexandre Rames67555f72014-11-18 10:55:16 +00004427void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4428 HandleInvoke(invoke);
4429}
4430
4431void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4432 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004433 LocationSummary* locations = invoke->GetLocations();
4434 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004435 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004436 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004437 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004438
4439 // The register ip1 is required to be used for the hidden argument in
4440 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004441 MacroAssembler* masm = GetVIXLAssembler();
4442 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004443 scratch_scope.Exclude(ip1);
4444 __ Mov(ip1, invoke->GetDexMethodIndex());
4445
Artem Serov914d7a82017-02-07 14:33:49 +00004446 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004447 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004448 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004449 {
4450 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4451 // /* HeapReference<Class> */ temp = temp->klass_
4452 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4453 codegen_->MaybeRecordImplicitNullCheck(invoke);
4454 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004455 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004456 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004457 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004458 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004459 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004460 }
Artem Serov914d7a82017-02-07 14:33:49 +00004461
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004462 // Instead of simply (possibly) unpoisoning `temp` here, we should
4463 // emit a read barrier for the previous class reference load.
4464 // However this is not required in practice, as this is an
4465 // intermediate/temporary reference and because the current
4466 // concurrent copying collector keeps the from-space memory
4467 // intact/accessible until the end of the marking phase (the
4468 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004469 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004470 __ Ldr(temp,
4471 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4472 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004473 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004474 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004475 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004476 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004477 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004478
4479 {
4480 // Ensure the pc position is recorded immediately after the `blr` instruction.
4481 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4482
4483 // lr();
4484 __ blr(lr);
4485 DCHECK(!codegen_->IsLeafMethod());
4486 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4487 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004488
4489 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004490}
4491
4492void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004493 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004494 if (intrinsic.TryDispatch(invoke)) {
4495 return;
4496 }
4497
Alexandre Rames67555f72014-11-18 10:55:16 +00004498 HandleInvoke(invoke);
4499}
4500
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004501void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004502 // Explicit clinit checks triggered by static invokes must have been pruned by
4503 // art::PrepareForRegisterAllocation.
4504 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004505
Vladimir Markoca6fff82017-10-03 14:49:14 +01004506 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004507 if (intrinsic.TryDispatch(invoke)) {
4508 return;
4509 }
4510
Alexandre Rames67555f72014-11-18 10:55:16 +00004511 HandleInvoke(invoke);
4512}
4513
Andreas Gampe878d58c2015-01-15 23:24:00 -08004514static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4515 if (invoke->GetLocations()->Intrinsified()) {
4516 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4517 intrinsic.Dispatch(invoke);
4518 return true;
4519 }
4520 return false;
4521}
4522
Vladimir Markodc151b22015-10-15 18:02:30 +01004523HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4524 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004525 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004526 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004527 return desired_dispatch_info;
4528}
4529
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004530void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4531 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004532 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004533 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4534 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004535 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4536 uint32_t offset =
4537 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004538 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004539 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004540 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004541 }
Vladimir Marko58155012015-08-19 12:49:41 +00004542 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004543 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004544 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004545 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4546 DCHECK(GetCompilerOptions().IsBootImage());
4547 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004548 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004549 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4550 // Add ADD with its PC-relative method patch.
4551 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004552 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004553 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4554 break;
4555 }
Vladimir Markob066d432018-01-03 13:14:37 +00004556 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4557 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004558 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004559 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4560 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4561 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4562 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4563 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4564 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4565 break;
4566 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004567 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004568 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004569 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4570 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004571 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004572 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004573 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004574 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004575 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004576 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004577 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004578 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4579 // Load method address from literal pool.
4580 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4581 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004582 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4583 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4584 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004585 }
4586 }
4587
4588 switch (invoke->GetCodePtrLocation()) {
4589 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004590 {
4591 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4592 ExactAssemblyScope eas(GetVIXLAssembler(),
4593 kInstructionSize,
4594 CodeBufferCheckScope::kExactSize);
4595 __ bl(&frame_entry_label_);
4596 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4597 }
Vladimir Marko58155012015-08-19 12:49:41 +00004598 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004599 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4600 // LR = callee_method->entry_point_from_quick_compiled_code_;
4601 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004602 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004603 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004604 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004605 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004606 ExactAssemblyScope eas(GetVIXLAssembler(),
4607 kInstructionSize,
4608 CodeBufferCheckScope::kExactSize);
4609 // lr()
4610 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004611 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004612 }
Vladimir Marko58155012015-08-19 12:49:41 +00004613 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004614 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004615
Andreas Gampe878d58c2015-01-15 23:24:00 -08004616 DCHECK(!IsLeafMethod());
4617}
4618
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004619void CodeGeneratorARM64::GenerateVirtualCall(
4620 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004621 // Use the calling convention instead of the location of the receiver, as
4622 // intrinsics may have put the receiver in a different register. In the intrinsics
4623 // slow path, the arguments have been moved to the right place, so here we are
4624 // guaranteed that the receiver is the first register of the calling convention.
4625 InvokeDexCallingConvention calling_convention;
4626 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004627 Register temp = XRegisterFrom(temp_in);
4628 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4629 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4630 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004631 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004632
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004633 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004634
4635 {
4636 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4637 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4638 // /* HeapReference<Class> */ temp = receiver->klass_
4639 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4640 MaybeRecordImplicitNullCheck(invoke);
4641 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004642 // Instead of simply (possibly) unpoisoning `temp` here, we should
4643 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004644 // intermediate/temporary reference and because the current
4645 // concurrent copying collector keeps the from-space memory
4646 // intact/accessible until the end of the marking phase (the
4647 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004648 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4649 // temp = temp->GetMethodAt(method_offset);
4650 __ Ldr(temp, MemOperand(temp, method_offset));
4651 // lr = temp->GetEntryPoint();
4652 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004653 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004654 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004655 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4656 // lr();
4657 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004658 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004659 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004660}
4661
Orion Hodsonac141392017-01-13 11:53:47 +00004662void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4663 HandleInvoke(invoke);
4664}
4665
4666void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4667 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004668 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004669}
4670
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004671void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4672 HandleInvoke(invoke);
4673}
4674
4675void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4676 codegen_->GenerateInvokeCustomCall(invoke);
4677 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
4678}
4679
Vladimir Marko6fd16062018-06-26 11:02:04 +01004680vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4681 uint32_t intrinsic_data,
4682 vixl::aarch64::Label* adrp_label) {
4683 return NewPcRelativePatch(
4684 /* dex_file */ nullptr, intrinsic_data, adrp_label, &boot_image_intrinsic_patches_);
4685}
4686
Vladimir Markob066d432018-01-03 13:14:37 +00004687vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4688 uint32_t boot_image_offset,
4689 vixl::aarch64::Label* adrp_label) {
4690 return NewPcRelativePatch(
4691 /* dex_file */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
4692}
4693
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004694vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004695 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004696 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004697 return NewPcRelativePatch(
4698 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004699}
4700
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004701vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4702 MethodReference target_method,
4703 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004704 return NewPcRelativePatch(
4705 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004706}
4707
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004708vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004709 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004710 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004711 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004712 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004713}
4714
Vladimir Marko1998cd02017-01-13 13:02:58 +00004715vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4716 const DexFile& dex_file,
4717 dex::TypeIndex type_index,
4718 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004719 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004720}
4721
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004722vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004723 const DexFile& dex_file,
4724 dex::StringIndex string_index,
4725 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004726 return NewPcRelativePatch(
4727 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004728}
4729
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004730vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4731 const DexFile& dex_file,
4732 dex::StringIndex string_index,
4733 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004734 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004735}
4736
Nicolas Geoffray756e7222018-08-02 17:53:46 +00004737vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4738 baker_read_barrier_patches_.emplace_back(custom_data);
4739 return &baker_read_barrier_patches_.back().label;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004740}
4741
Scott Wakeling97c72b72016-06-24 16:19:36 +01004742vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004743 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004744 uint32_t offset_or_index,
4745 vixl::aarch64::Label* adrp_label,
4746 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004747 // Add a patch entry and return the label.
4748 patches->emplace_back(dex_file, offset_or_index);
4749 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004750 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004751 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4752 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4753 return label;
4754}
4755
Scott Wakeling97c72b72016-06-24 16:19:36 +01004756vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4757 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004758 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004759}
4760
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004761vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004762 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004763 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004764 return jit_string_patches_.GetOrCreate(
4765 StringReference(&dex_file, string_index),
4766 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4767}
4768
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004769vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004770 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004771 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004772 return jit_class_patches_.GetOrCreate(
4773 TypeReference(&dex_file, type_index),
4774 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4775}
4776
Vladimir Markoaad75c62016-10-03 08:46:48 +00004777void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4778 vixl::aarch64::Register reg) {
4779 DCHECK(reg.IsX());
4780 SingleEmissionCheckScope guard(GetVIXLAssembler());
4781 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004782 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004783}
4784
4785void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4786 vixl::aarch64::Register out,
4787 vixl::aarch64::Register base) {
4788 DCHECK(out.IsX());
4789 DCHECK(base.IsX());
4790 SingleEmissionCheckScope guard(GetVIXLAssembler());
4791 __ Bind(fixup_label);
4792 __ add(out, base, Operand(/* offset placeholder */ 0));
4793}
4794
4795void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4796 vixl::aarch64::Register out,
4797 vixl::aarch64::Register base) {
4798 DCHECK(base.IsX());
4799 SingleEmissionCheckScope guard(GetVIXLAssembler());
4800 __ Bind(fixup_label);
4801 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4802}
4803
Vladimir Markoeebb8212018-06-05 14:57:24 +01004804void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004805 uint32_t boot_image_reference) {
4806 if (GetCompilerOptions().IsBootImage()) {
4807 // Add ADRP with its PC-relative type patch.
4808 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4809 EmitAdrpPlaceholder(adrp_label, reg.X());
4810 // Add ADD with its PC-relative type patch.
4811 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4812 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004813 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004814 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004815 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004816 EmitAdrpPlaceholder(adrp_label, reg.X());
4817 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004818 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004819 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4820 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004821 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004822 gc::Heap* heap = Runtime::Current()->GetHeap();
4823 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004824 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004825 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4826 }
4827}
4828
Vladimir Marko6fd16062018-06-26 11:02:04 +01004829void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4830 uint32_t boot_image_offset) {
4831 DCHECK(invoke->IsStatic());
4832 InvokeRuntimeCallingConvention calling_convention;
4833 Register argument = calling_convention.GetRegisterAt(0);
4834 if (GetCompilerOptions().IsBootImage()) {
4835 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4836 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4837 MethodReference target_method = invoke->GetTargetMethod();
4838 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4839 // Add ADRP with its PC-relative type patch.
4840 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4841 EmitAdrpPlaceholder(adrp_label, argument.X());
4842 // Add ADD with its PC-relative type patch.
4843 vixl::aarch64::Label* add_label =
4844 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4845 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4846 } else {
4847 LoadBootImageAddress(argument, boot_image_offset);
4848 }
4849 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4850 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4851}
4852
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004853template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004854inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4855 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004856 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004857 for (const PcRelativePatchInfo& info : infos) {
4858 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004859 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004860 info.pc_insn_label->GetLocation(),
4861 info.offset_or_index));
4862 }
4863}
4864
Vladimir Marko6fd16062018-06-26 11:02:04 +01004865template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4866linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4867 const DexFile* target_dex_file,
4868 uint32_t pc_insn_offset,
4869 uint32_t boot_image_offset) {
4870 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4871 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004872}
4873
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004874void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004875 DCHECK(linker_patches->empty());
4876 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004877 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004878 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004879 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004880 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004881 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004882 string_bss_entry_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01004883 boot_image_intrinsic_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004884 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004885 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004886 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004887 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004888 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004889 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004890 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004891 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004892 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004893 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4894 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004895 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01004896 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00004897 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004898 DCHECK(boot_image_type_patches_.empty());
4899 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004900 DCHECK(boot_image_intrinsic_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004901 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004902 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4903 method_bss_entry_patches_, linker_patches);
4904 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4905 type_bss_entry_patches_, linker_patches);
4906 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4907 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004908 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004909 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4910 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004911 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004912 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004913}
4914
Vladimir Markoca1e0382018-04-11 09:58:41 +00004915bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
4916 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
4917 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4918}
4919
4920void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4921 /*out*/ ArenaVector<uint8_t>* code,
4922 /*out*/ std::string* debug_name) {
4923 Arm64Assembler assembler(GetGraph()->GetAllocator());
4924 switch (patch.GetType()) {
4925 case linker::LinkerPatch::Type::kCallRelative: {
4926 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4927 // to the generic JNI and interpreter trampolines.
4928 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4929 kArm64PointerSize).Int32Value());
4930 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4931 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4932 *debug_name = "MethodCallThunk";
4933 }
4934 break;
4935 }
4936 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4937 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4938 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4939 break;
4940 }
4941 default:
4942 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4943 UNREACHABLE();
4944 }
4945
4946 // Ensure we emit the literal pool if any.
4947 assembler.FinalizeCode();
4948 code->resize(assembler.CodeSize());
4949 MemoryRegion code_region(code->data(), code->size());
4950 assembler.FinalizeInstructions(code_region);
4951}
4952
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004953vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4954 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004955 value,
4956 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4957}
4958
Scott Wakeling97c72b72016-06-24 16:19:36 +01004959vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004960 return uint64_literals_.GetOrCreate(
4961 value,
4962 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004963}
4964
Andreas Gampe878d58c2015-01-15 23:24:00 -08004965void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004966 // Explicit clinit checks triggered by static invokes must have been pruned by
4967 // art::PrepareForRegisterAllocation.
4968 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004969
Andreas Gampe878d58c2015-01-15 23:24:00 -08004970 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004971 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004972 return;
4973 }
4974
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004975 {
4976 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4977 // are no pools emitted.
4978 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4979 LocationSummary* locations = invoke->GetLocations();
4980 codegen_->GenerateStaticOrDirectCall(
4981 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4982 }
4983
4984 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004985}
4986
4987void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004988 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004989 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004990 return;
4991 }
4992
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004993 {
4994 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4995 // are no pools emitted.
4996 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4997 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4998 DCHECK(!codegen_->IsLeafMethod());
4999 }
5000
5001 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005002}
5003
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005004HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
5005 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005006 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005007 case HLoadClass::LoadKind::kInvalid:
5008 LOG(FATAL) << "UNREACHABLE";
5009 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005010 case HLoadClass::LoadKind::kReferrersClass:
5011 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005012 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005013 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005014 case HLoadClass::LoadKind::kBssEntry:
5015 DCHECK(!Runtime::Current()->UseJitCompilation());
5016 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005017 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005018 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005019 DCHECK(Runtime::Current()->UseJitCompilation());
5020 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005021 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005022 break;
5023 }
5024 return desired_class_load_kind;
5025}
5026
Alexandre Rames67555f72014-11-18 10:55:16 +00005027void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005028 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005029 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005030 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005031 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005032 cls,
5033 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005034 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005035 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005036 return;
5037 }
Vladimir Marko41559982017-01-06 14:04:23 +00005038 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005039
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005040 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5041 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005042 ? LocationSummary::kCallOnSlowPath
5043 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005044 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005045 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005046 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005047 }
5048
Vladimir Marko41559982017-01-06 14:04:23 +00005049 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005050 locations->SetInAt(0, Location::RequiresRegister());
5051 }
5052 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005053 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5054 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5055 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00005056 RegisterSet caller_saves = RegisterSet::Empty();
5057 InvokeRuntimeCallingConvention calling_convention;
5058 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5059 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005060 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5061 DataType::Type::kReference).GetCode());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005062 locations->SetCustomSlowPathCallerSaves(caller_saves);
5063 } else {
5064 // For non-Baker read barrier we have a temp-clobbering call.
5065 }
5066 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005067}
5068
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005069// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5070// move.
5071void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005072 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005073 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005074 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005075 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005076 return;
5077 }
Vladimir Marko41559982017-01-06 14:04:23 +00005078 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005079
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005080 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005081 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005082
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005083 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5084 ? kWithoutReadBarrier
5085 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005086 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005087 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005088 case HLoadClass::LoadKind::kReferrersClass: {
5089 DCHECK(!cls->CanCallRuntime());
5090 DCHECK(!cls->MustGenerateClinitCheck());
5091 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5092 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005093 codegen_->GenerateGcRootFieldLoad(cls,
5094 out_loc,
5095 current_method,
5096 ArtMethod::DeclaringClassOffset().Int32Value(),
5097 /* fixup_label */ nullptr,
5098 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005099 break;
5100 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005101 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005102 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005103 // Add ADRP with its PC-relative type patch.
5104 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005105 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005106 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005107 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005108 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005109 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005110 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005111 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005112 break;
5113 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005114 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005115 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005116 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5117 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5118 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005119 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005120 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005121 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005122 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005123 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005124 break;
5125 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005126 case HLoadClass::LoadKind::kBssEntry: {
5127 // Add ADRP with its PC-relative Class .bss entry patch.
5128 const DexFile& dex_file = cls->GetDexFile();
5129 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005130 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
5131 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5132 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005133 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005134 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005135 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005136 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005137 codegen_->GenerateGcRootFieldLoad(cls,
5138 out_loc,
5139 temp,
5140 /* offset placeholder */ 0u,
5141 ldr_label,
5142 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005143 generate_null_check = true;
5144 break;
5145 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005146 case HLoadClass::LoadKind::kJitBootImageAddress: {
5147 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
5148 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
5149 DCHECK_NE(address, 0u);
5150 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5151 break;
5152 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005153 case HLoadClass::LoadKind::kJitTableAddress: {
5154 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5155 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005156 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005157 codegen_->GenerateGcRootFieldLoad(cls,
5158 out_loc,
5159 out.X(),
5160 /* offset */ 0,
5161 /* fixup_label */ nullptr,
5162 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005163 break;
5164 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005165 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005166 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005167 LOG(FATAL) << "UNREACHABLE";
5168 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005169 }
5170
Vladimir Markoea4c1262017-02-06 19:59:33 +00005171 bool do_clinit = cls->MustGenerateClinitCheck();
5172 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005173 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005174 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00005175 cls, cls, cls->GetDexPc(), do_clinit);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005176 codegen_->AddSlowPath(slow_path);
5177 if (generate_null_check) {
5178 __ Cbz(out, slow_path->GetEntryLabel());
5179 }
5180 if (cls->MustGenerateClinitCheck()) {
5181 GenerateClassInitializationCheck(slow_path, out);
5182 } else {
5183 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005184 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005185 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005186 }
5187}
5188
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005189void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5190 InvokeRuntimeCallingConvention calling_convention;
5191 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5192 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5193}
5194
5195void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5196 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5197}
5198
Orion Hodson18259d72018-04-12 11:18:23 +01005199void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5200 InvokeRuntimeCallingConvention calling_convention;
5201 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5202 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5203}
5204
5205void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5206 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5207}
5208
David Brazdilcb1c0552015-08-04 16:22:25 +01005209static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005210 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005211}
5212
Alexandre Rames67555f72014-11-18 10:55:16 +00005213void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5214 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005215 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005216 locations->SetOut(Location::RequiresRegister());
5217}
5218
5219void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005220 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5221}
5222
5223void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005224 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005225}
5226
5227void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5228 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005229}
5230
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005231HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5232 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005233 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005234 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005235 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005236 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005237 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005238 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005239 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005240 case HLoadString::LoadKind::kJitTableAddress:
5241 DCHECK(Runtime::Current()->UseJitCompilation());
5242 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005243 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005244 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005245 }
5246 return desired_string_load_kind;
5247}
5248
Alexandre Rames67555f72014-11-18 10:55:16 +00005249void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005250 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005251 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005252 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005253 InvokeRuntimeCallingConvention calling_convention;
5254 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5255 } else {
5256 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005257 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5258 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005259 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005260 RegisterSet caller_saves = RegisterSet::Empty();
5261 InvokeRuntimeCallingConvention calling_convention;
5262 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5263 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005264 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5265 DataType::Type::kReference).GetCode());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005266 locations->SetCustomSlowPathCallerSaves(caller_saves);
5267 } 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