blob: b47a5cf3c4775dddf1e23053c42e6a42e870ffc8 [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"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080031#include "intrinsics.h"
32#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000033#include "linker/arm64/relative_patcher_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;
67using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010071using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::LocationFrom;
75using helpers::OperandFromMemOperand;
76using helpers::OutputCPURegister;
77using helpers::OutputFPRegister;
78using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010079using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080080using helpers::RegisterFrom;
81using helpers::StackOperandFrom;
82using helpers::VIXLRegCodeFromART;
83using helpers::WRegisterFrom;
84using helpers::XRegisterFrom;
85
Vladimir Markof3e0ee22015-12-17 15:23:13 +000086// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080087// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
88// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000089static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010090
Vladimir Markof4f2daa2017-03-20 18:26:59 +000091// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
92// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
93// For the Baker read barrier implementation using link-generated thunks we need to split
94// the offset explicitly.
95constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
96
97// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010098constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010099constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +0100100constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000101
102// Some instructions have special requirements for a temporary, for example
103// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
104// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
105// loads with large offsets need a fixed register to limit the number of link-time
106// thunks we generate. For these and similar cases, we want to reserve a specific
107// register that's neither callee-save nor an argument register. We choose x15.
108inline Location FixedTempLocation() {
109 return Location::RegisterLocation(x15.GetCode());
110}
111
Alexandre Rames5319def2014-10-23 10:03:10 +0100112inline Condition ARM64Condition(IfCondition cond) {
113 switch (cond) {
114 case kCondEQ: return eq;
115 case kCondNE: return ne;
116 case kCondLT: return lt;
117 case kCondLE: return le;
118 case kCondGT: return gt;
119 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700120 case kCondB: return lo;
121 case kCondBE: return ls;
122 case kCondA: return hi;
123 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100124 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000125 LOG(FATAL) << "Unreachable";
126 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100127}
128
Vladimir Markod6e069b2016-01-18 11:11:01 +0000129inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
130 // The ARM64 condition codes can express all the necessary branches, see the
131 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
132 // There is no dex instruction or HIR that would need the missing conditions
133 // "equal or unordered" or "not equal".
134 switch (cond) {
135 case kCondEQ: return eq;
136 case kCondNE: return ne /* unordered */;
137 case kCondLT: return gt_bias ? cc : lt /* unordered */;
138 case kCondLE: return gt_bias ? ls : le /* unordered */;
139 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
140 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
141 default:
142 LOG(FATAL) << "UNREACHABLE";
143 UNREACHABLE();
144 }
145}
146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000148 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
149 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
150 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000152 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000156 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100157 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100158 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000159 } else {
160 return LocationFrom(w0);
161 }
162}
163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100164Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000165 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100166}
167
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100168// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
169#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700170#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100171
Zheng Xuda403092015-04-24 17:35:39 +0800172// Calculate memory accessing operand for save/restore live registers.
173static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800175 int64_t spill_offset,
176 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
178 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
179 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800180 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800182 codegen->GetNumberOfFloatingPointRegisters()));
183
Vladimir Marko804b03f2016-09-14 16:26:36 +0100184 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100185 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
186 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800187
188 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
189 UseScratchRegisterScope temps(masm);
190
191 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
193 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800194 int64_t reg_size = kXRegSizeInBytes;
195 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
196 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100197 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800198 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
199 // If the offset does not fit in the instruction's immediate field, use an alternate register
200 // to compute the base address(float point registers spill base address).
201 Register new_base = temps.AcquireSameSizeAs(base);
202 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
203 base = new_base;
204 spill_offset = -core_spill_size;
205 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
206 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
207 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
208 }
209
210 if (is_save) {
211 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
212 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 } else {
214 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
215 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
216 }
217}
218
219void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800220 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100221 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
222 for (uint32_t i : LowToHighBits(core_spills)) {
223 // If the register holds an object, update the stack mask.
224 if (locations->RegisterContainsObject(i)) {
225 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100227 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
228 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
229 saved_core_stack_offsets_[i] = stack_offset;
230 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800231 }
232
Vladimir Marko804b03f2016-09-14 16:26:36 +0100233 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
234 for (uint32_t i : LowToHighBits(fp_spills)) {
235 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
236 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
237 saved_fpu_stack_offsets_[i] = stack_offset;
238 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800239 }
240
Vladimir Marko804b03f2016-09-14 16:26:36 +0100241 SaveRestoreLiveRegistersHelper(codegen,
242 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800243 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
244}
245
246void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100247 SaveRestoreLiveRegistersHelper(codegen,
248 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800249 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
250}
251
Alexandre Rames5319def2014-10-23 10:03:10 +0100252class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
253 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000254 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100257 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000258 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100259
Alexandre Rames5319def2014-10-23 10:03:10 +0100260 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000261 if (instruction_->CanThrowIntoCatchBlock()) {
262 // Live registers will be restored in the catch block if caught.
263 SaveLiveRegisters(codegen, instruction_->GetLocations());
264 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000265 // We're moving two locations to locations that could overlap, so we need a parallel
266 // move resolver.
267 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100268 codegen->EmitParallelMoves(locations->InAt(0),
269 LocationFrom(calling_convention.GetRegisterAt(0)),
270 DataType::Type::kInt32,
271 locations->InAt(1),
272 LocationFrom(calling_convention.GetRegisterAt(1)),
273 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000274 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
275 ? kQuickThrowStringBounds
276 : kQuickThrowArrayBounds;
277 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100278 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800279 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
285
Alexandre Rames5319def2014-10-23 10:03:10 +0100286 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100287 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
288};
289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
291 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000292 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000293
294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
295 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
296 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000297 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 }
300
Alexandre Rames8158f282015-08-07 10:26:17 +0100301 bool IsFatal() const OVERRIDE { return true; }
302
Alexandre Rames9931f312015-06-19 14:47:01 +0100303 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
304
Alexandre Rames67555f72014-11-18 10:55:16 +0000305 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000306 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
307};
308
309class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
310 public:
311 LoadClassSlowPathARM64(HLoadClass* cls,
312 HInstruction* at,
313 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000314 bool do_clinit)
Vladimir Markoea4c1262017-02-06 19:59:33 +0000315 : SlowPathCodeARM64(at),
316 cls_(cls),
317 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000318 do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
320 }
321
322 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000323 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000324 Location out = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +0000325 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
326
327 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000328 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000329
Vladimir Markof3c52b42017-11-17 17:32:12 +0000330 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000331 dex::TypeIndex type_index = cls_->GetTypeIndex();
332 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000333 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
334 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000335 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800336 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100337 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800338 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100339 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800340 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
342 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 if (out.IsValid()) {
344 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100345 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000346 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000347 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000348 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349 __ B(GetExitLabel());
350 }
351
Alexandre Rames9931f312015-06-19 14:47:01 +0100352 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
353
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 private:
355 // The class this slow path will load.
356 HLoadClass* const cls_;
357
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 // The dex PC of `at_`.
359 const uint32_t dex_pc_;
360
361 // Whether to initialize the class.
362 const bool do_clinit_;
363
364 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
365};
366
Vladimir Markoaad75c62016-10-03 08:46:48 +0000367class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000369 explicit LoadStringSlowPathARM64(HLoadString* instruction)
370 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 LocationSummary* locations = instruction_->GetLocations();
374 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
375 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
376
377 __ Bind(GetEntryLabel());
378 SaveLiveRegisters(codegen, locations);
379
Vladimir Markof3c52b42017-11-17 17:32:12 +0000380 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000381 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
382 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000383 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
384 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100385 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000386 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
387
388 RestoreLiveRegisters(codegen, locations);
389
Vladimir Markoaad75c62016-10-03 08:46:48 +0000390 __ B(GetExitLabel());
391 }
392
393 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
394
395 private:
396 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
397};
398
Alexandre Rames5319def2014-10-23 10:03:10 +0100399class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
400 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000401 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100402
Alexandre Rames67555f72014-11-18 10:55:16 +0000403 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
404 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100405 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000406 if (instruction_->CanThrowIntoCatchBlock()) {
407 // Live registers will be restored in the catch block if caught.
408 SaveLiveRegisters(codegen, instruction_->GetLocations());
409 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000410 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
411 instruction_,
412 instruction_->GetDexPc(),
413 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800414 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100415 }
416
Alexandre Rames8158f282015-08-07 10:26:17 +0100417 bool IsFatal() const OVERRIDE { return true; }
418
Alexandre Rames9931f312015-06-19 14:47:01 +0100419 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
420
Alexandre Rames5319def2014-10-23 10:03:10 +0100421 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
423};
424
425class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
426 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100427 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000428 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100429
Alexandre Rames67555f72014-11-18 10:55:16 +0000430 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100431 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000432 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100433 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100434 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000435 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800436 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100437 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000438 if (successor_ == nullptr) {
439 __ B(GetReturnLabel());
440 } else {
441 __ B(arm64_codegen->GetLabelOf(successor_));
442 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100443 }
444
Scott Wakeling97c72b72016-06-24 16:19:36 +0100445 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100446 DCHECK(successor_ == nullptr);
447 return &return_label_;
448 }
449
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100450 HBasicBlock* GetSuccessor() const {
451 return successor_;
452 }
453
Alexandre Rames9931f312015-06-19 14:47:01 +0100454 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
455
Alexandre Rames5319def2014-10-23 10:03:10 +0100456 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100457 // If not null, the block to branch to after the suspend check.
458 HBasicBlock* const successor_;
459
460 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100461 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100462
463 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
464};
465
Alexandre Rames67555f72014-11-18 10:55:16 +0000466class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
467 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000468 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000469 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000470
471 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800473
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474 DCHECK(instruction_->IsCheckCast()
475 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
476 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100477 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478
Alexandre Rames67555f72014-11-18 10:55:16 +0000479 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000480
Vladimir Marko87584542017-12-12 17:47:52 +0000481 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000482 SaveLiveRegisters(codegen, locations);
483 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000484
485 // We're moving two locations to locations that could overlap, so we need a parallel
486 // move resolver.
487 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800488 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800489 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100490 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800491 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800492 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100493 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000494 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000495 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800496 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100497 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000498 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
499 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
500 } else {
501 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800502 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
503 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000504 }
505
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 if (!is_fatal_) {
507 RestoreLiveRegisters(codegen, locations);
508 __ B(GetExitLabel());
509 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000510 }
511
Alexandre Rames9931f312015-06-19 14:47:01 +0100512 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100513 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100514
Alexandre Rames67555f72014-11-18 10:55:16 +0000515 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000516 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000517
Alexandre Rames67555f72014-11-18 10:55:16 +0000518 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
519};
520
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700521class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
522 public:
Aart Bik42249c32016-01-07 15:33:50 -0800523 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000524 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700525
526 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800527 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100529 LocationSummary* locations = instruction_->GetLocations();
530 SaveLiveRegisters(codegen, locations);
531 InvokeRuntimeCallingConvention calling_convention;
532 __ Mov(calling_convention.GetRegisterAt(0),
533 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000534 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100535 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700536 }
537
Alexandre Rames9931f312015-06-19 14:47:01 +0100538 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
539
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700540 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700541 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
542};
543
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100544class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
545 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000546 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100547
548 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
549 LocationSummary* locations = instruction_->GetLocations();
550 __ Bind(GetEntryLabel());
551 SaveLiveRegisters(codegen, locations);
552
553 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100554 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100555 parallel_move.AddMove(
556 locations->InAt(0),
557 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100558 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100559 nullptr);
560 parallel_move.AddMove(
561 locations->InAt(1),
562 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100563 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564 nullptr);
565 parallel_move.AddMove(
566 locations->InAt(2),
567 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100568 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100569 nullptr);
570 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
571
572 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000573 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100574 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
575 RestoreLiveRegisters(codegen, locations);
576 __ B(GetExitLabel());
577 }
578
579 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
580
581 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100582 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
583};
584
Zheng Xu3927c8b2015-11-18 17:46:25 +0800585void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
586 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000587 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800588
589 // We are about to use the assembler to place literals directly. Make sure we have enough
590 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000591 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
592 num_entries * sizeof(int32_t),
593 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800594
595 __ Bind(&table_start_);
596 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
597 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100598 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800599 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100600 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800601 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
602 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
603 Literal<int32_t> literal(jump_offset);
604 __ place(&literal);
605 }
606}
607
Roland Levillain54f869e2017-03-06 13:54:11 +0000608// Abstract base class for read barrier slow paths marking a reference
609// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000610//
Roland Levillain54f869e2017-03-06 13:54:11 +0000611// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100612// barrier marking runtime entry point to be invoked or an empty
613// location; in the latter case, the read barrier marking runtime
614// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000615class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
616 protected:
617 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
618 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000619 DCHECK(kEmitCompilerReadBarrier);
620 }
621
Roland Levillain54f869e2017-03-06 13:54:11 +0000622 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000623
Roland Levillain54f869e2017-03-06 13:54:11 +0000624 // Generate assembly code calling the read barrier marking runtime
625 // entry point (ReadBarrierMarkRegX).
626 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000627 // No need to save live registers; it's taken care of by the
628 // entrypoint. Also, there is no need to update the stack mask,
629 // as this runtime call will not trigger a garbage collection.
630 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
631 DCHECK_NE(ref_.reg(), LR);
632 DCHECK_NE(ref_.reg(), WSP);
633 DCHECK_NE(ref_.reg(), WZR);
634 // IP0 is used internally by the ReadBarrierMarkRegX entry point
635 // as a temporary, it cannot be the entry point's input/output.
636 DCHECK_NE(ref_.reg(), IP0);
637 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
638 // "Compact" slow path, saving two moves.
639 //
640 // Instead of using the standard runtime calling convention (input
641 // and output in W0):
642 //
643 // W0 <- ref
644 // W0 <- ReadBarrierMark(W0)
645 // ref <- W0
646 //
647 // we just use rX (the register containing `ref`) as input and output
648 // of a dedicated entrypoint:
649 //
650 // rX <- ReadBarrierMarkRegX(rX)
651 //
652 if (entrypoint_.IsValid()) {
653 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
654 __ Blr(XRegisterFrom(entrypoint_));
655 } else {
656 // Entrypoint is not already loaded, load from the thread.
657 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100658 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000659 // This runtime call does not require a stack map.
660 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
661 }
662 }
663
664 // The location (register) of the marked object reference.
665 const Location ref_;
666
667 // The location of the entrypoint if it is already loaded.
668 const Location entrypoint_;
669
Roland Levillain54f869e2017-03-06 13:54:11 +0000670 private:
671 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
672};
673
Alexandre Rames5319def2014-10-23 10:03:10 +0100674// Slow path marking an object reference `ref` during a read
675// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000676// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100677//
678// This means that after the execution of this slow path, `ref` will
679// always be up-to-date, but `obj.field` may not; i.e., after the
680// flip, `ref` will be a to-space reference, but `obj.field` will
681// probably still be a from-space reference (unless it gets updated by
682// another thread, or if another thread installed another object
683// reference (different from `ref`) in `obj.field`).
684//
Roland Levillain97c46462017-05-11 14:04:03 +0100685// Argument `entrypoint` must be a register location holding the read
686// barrier marking runtime entry point to be invoked or an empty
687// location; in the latter case, the read barrier marking runtime
688// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000689class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100690 public:
691 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
692 Location ref,
693 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000694 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100695 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100696 }
697
698 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
699
700 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000701 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100702 DCHECK(locations->CanCall());
703 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000704 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000705 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
706 << "Unexpected instruction in read barrier marking slow path: "
707 << instruction_->DebugName();
708
709 __ Bind(GetEntryLabel());
710 GenerateReadBarrierMarkRuntimeCall(codegen);
711 __ B(GetExitLabel());
712 }
713
714 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000715 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
716};
717
Roland Levillain54f869e2017-03-06 13:54:11 +0000718// Slow path loading `obj`'s lock word, loading a reference from
719// object `*(obj + offset + (index << scale_factor))` into `ref`, and
720// marking `ref` if `obj` is gray according to the lock word (Baker
721// read barrier). The field `obj.field` in the object `obj` holding
722// this reference does not get updated by this slow path after marking
723// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
724// below for that).
725//
726// This means that after the execution of this slow path, `ref` will
727// always be up-to-date, but `obj.field` may not; i.e., after the
728// flip, `ref` will be a to-space reference, but `obj.field` will
729// probably still be a from-space reference (unless it gets updated by
730// another thread, or if another thread installed another object
731// reference (different from `ref`) in `obj.field`).
732//
733// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100734// barrier marking runtime entry point to be invoked or an empty
735// location; in the latter case, the read barrier marking runtime
736// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000737class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
738 public:
739 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
740 Location ref,
741 Register obj,
742 uint32_t offset,
743 Location index,
744 size_t scale_factor,
745 bool needs_null_check,
746 bool use_load_acquire,
747 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100748 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000749 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
750 obj_(obj),
751 offset_(offset),
752 index_(index),
753 scale_factor_(scale_factor),
754 needs_null_check_(needs_null_check),
755 use_load_acquire_(use_load_acquire),
756 temp_(temp) {
757 DCHECK(kEmitCompilerReadBarrier);
758 DCHECK(kUseBakerReadBarrier);
759 }
760
761 const char* GetDescription() const OVERRIDE {
762 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
763 }
764
765 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
766 LocationSummary* locations = instruction_->GetLocations();
767 DCHECK(locations->CanCall());
768 DCHECK(ref_.IsRegister()) << ref_;
769 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
770 DCHECK(obj_.IsW());
771 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 DCHECK(instruction_->IsInstanceFieldGet() ||
773 instruction_->IsStaticFieldGet() ||
774 instruction_->IsArrayGet() ||
775 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100776 instruction_->IsInstanceOf() ||
777 instruction_->IsCheckCast() ||
778 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
779 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
780 << "Unexpected instruction in read barrier marking slow path: "
781 << instruction_->DebugName();
782 // The read barrier instrumentation of object ArrayGet
783 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000784 // instruction.
785 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000786 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
787
Roland Levillain54f869e2017-03-06 13:54:11 +0000788 // Temporary register `temp_`, used to store the lock word, must
789 // not be IP0 nor IP1, as we may use them to emit the reference
790 // load (in the call to GenerateRawReferenceLoad below), and we
791 // need the lock word to still be in `temp_` after the reference
792 // load.
793 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
794 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
795
Alexandre Rames5319def2014-10-23 10:03:10 +0100796 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000797
798 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
799 // inserted after the original load. However, in fast path based
800 // Baker's read barriers, we need to perform the load of
801 // mirror::Object::monitor_ *before* the original reference load.
802 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000803 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100804 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000805 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
806 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
807 // HeapReference<mirror::Object> ref = *src; // Original reference load.
808 // bool is_gray = (rb_state == ReadBarrier::GrayState());
809 // if (is_gray) {
810 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
811 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000812 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000813 // Note: the original implementation in ReadBarrier::Barrier is
814 // slightly more complex as it performs additional checks that we do
815 // not do here for performance reasons.
816
817 // /* int32_t */ monitor = obj->monitor_
818 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
819 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
820 if (needs_null_check_) {
821 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100822 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000823 // /* LockWord */ lock_word = LockWord(monitor)
824 static_assert(sizeof(LockWord) == sizeof(int32_t),
825 "art::LockWord and int32_t have different sizes.");
826
827 // Introduce a dependency on the lock_word including rb_state,
828 // to prevent load-load reordering, and without using
829 // a memory barrier (which would be more expensive).
830 // `obj` is unchanged by this operation, but its value now depends
831 // on `temp`.
832 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
833
834 // The actual reference load.
835 // A possible implicit null check has already been handled above.
836 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
837 arm64_codegen->GenerateRawReferenceLoad(instruction_,
838 ref_,
839 obj_,
840 offset_,
841 index_,
842 scale_factor_,
843 /* needs_null_check */ false,
844 use_load_acquire_);
845
846 // Mark the object `ref` when `obj` is gray.
847 //
848 // if (rb_state == ReadBarrier::GrayState())
849 // ref = ReadBarrier::Mark(ref);
850 //
851 // Given the numeric representation, it's enough to check the low bit of the rb_state.
852 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
853 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
854 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
855 GenerateReadBarrierMarkRuntimeCall(codegen);
856
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000857 __ B(GetExitLabel());
858 }
859
860 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 // The register containing the object holding the marked object reference field.
862 Register obj_;
863 // The offset, index and scale factor to access the reference in `obj_`.
864 uint32_t offset_;
865 Location index_;
866 size_t scale_factor_;
867 // Is a null check required?
868 bool needs_null_check_;
869 // Should this reference load use Load-Acquire semantics?
870 bool use_load_acquire_;
871 // A temporary register used to hold the lock word of `obj_`.
872 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000873
Roland Levillain54f869e2017-03-06 13:54:11 +0000874 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000875};
876
Roland Levillain54f869e2017-03-06 13:54:11 +0000877// Slow path loading `obj`'s lock word, loading a reference from
878// object `*(obj + offset + (index << scale_factor))` into `ref`, and
879// marking `ref` if `obj` is gray according to the lock word (Baker
880// read barrier). If needed, this slow path also atomically updates
881// the field `obj.field` in the object `obj` holding this reference
882// after marking (contrary to
883// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
884// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100885//
886// This means that after the execution of this slow path, both `ref`
887// and `obj.field` will be up-to-date; i.e., after the flip, both will
888// hold the same to-space reference (unless another thread installed
889// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000890//
Roland Levillain54f869e2017-03-06 13:54:11 +0000891// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100892// barrier marking runtime entry point to be invoked or an empty
893// location; in the latter case, the read barrier marking runtime
894// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000895class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
896 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100897 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100898 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
899 HInstruction* instruction,
900 Location ref,
901 Register obj,
902 uint32_t offset,
903 Location index,
904 size_t scale_factor,
905 bool needs_null_check,
906 bool use_load_acquire,
907 Register temp,
908 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000909 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100910 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000911 offset_(offset),
912 index_(index),
913 scale_factor_(scale_factor),
914 needs_null_check_(needs_null_check),
915 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000916 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100917 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000918 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100919 }
920
921 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000922 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100923 }
924
925 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
926 LocationSummary* locations = instruction_->GetLocations();
927 Register ref_reg = WRegisterFrom(ref_);
928 DCHECK(locations->CanCall());
929 DCHECK(ref_.IsRegister()) << ref_;
930 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000931 DCHECK(obj_.IsW());
932 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
933
934 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100935 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
936 << "Unexpected instruction in read barrier marking and field updating slow path: "
937 << instruction_->DebugName();
938 DCHECK(instruction_->GetLocations()->Intrinsified());
939 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000940 DCHECK_EQ(offset_, 0u);
941 DCHECK_EQ(scale_factor_, 0u);
942 DCHECK_EQ(use_load_acquire_, false);
943 // The location of the offset of the marked reference field within `obj_`.
944 Location field_offset = index_;
945 DCHECK(field_offset.IsRegister()) << field_offset;
946
947 // Temporary register `temp_`, used to store the lock word, must
948 // not be IP0 nor IP1, as we may use them to emit the reference
949 // load (in the call to GenerateRawReferenceLoad below), and we
950 // need the lock word to still be in `temp_` after the reference
951 // load.
952 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
953 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100954
955 __ Bind(GetEntryLabel());
956
Roland Levillainff487002017-03-07 16:50:01 +0000957 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
958 //
959 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
960 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
961 // HeapReference<mirror::Object> ref = *src; // Original reference load.
962 // bool is_gray = (rb_state == ReadBarrier::GrayState());
963 // if (is_gray) {
964 // old_ref = ref;
965 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
966 // compareAndSwapObject(obj, field_offset, old_ref, ref);
967 // }
968
Roland Levillain54f869e2017-03-06 13:54:11 +0000969 // /* int32_t */ monitor = obj->monitor_
970 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
971 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
972 if (needs_null_check_) {
973 codegen->MaybeRecordImplicitNullCheck(instruction_);
974 }
975 // /* LockWord */ lock_word = LockWord(monitor)
976 static_assert(sizeof(LockWord) == sizeof(int32_t),
977 "art::LockWord and int32_t have different sizes.");
978
979 // Introduce a dependency on the lock_word including rb_state,
980 // to prevent load-load reordering, and without using
981 // a memory barrier (which would be more expensive).
982 // `obj` is unchanged by this operation, but its value now depends
983 // on `temp`.
984 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
985
986 // The actual reference load.
987 // A possible implicit null check has already been handled above.
988 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
989 arm64_codegen->GenerateRawReferenceLoad(instruction_,
990 ref_,
991 obj_,
992 offset_,
993 index_,
994 scale_factor_,
995 /* needs_null_check */ false,
996 use_load_acquire_);
997
998 // Mark the object `ref` when `obj` is gray.
999 //
1000 // if (rb_state == ReadBarrier::GrayState())
1001 // ref = ReadBarrier::Mark(ref);
1002 //
1003 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1004 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1005 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1006 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1007
1008 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001009 // Note that we cannot use IP to save the old reference, as IP is
1010 // used internally by the ReadBarrierMarkRegX entry point, and we
1011 // need the old reference after the call to that entry point.
1012 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1013 __ Mov(temp_.W(), ref_reg);
1014
Roland Levillain54f869e2017-03-06 13:54:11 +00001015 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001016
1017 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001018 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001019 //
1020 // Note that this field could also hold a different object, if
1021 // another thread had concurrently changed it. In that case, the
1022 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1023 // (CAS) operation below would abort the CAS, leaving the field
1024 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001025 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001026 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001027
1028 // Update the the holder's field atomically. This may fail if
1029 // mutator updates before us, but it's OK. This is achieved
1030 // using a strong compare-and-set (CAS) operation with relaxed
1031 // memory synchronization ordering, where the expected value is
1032 // the old reference and the desired value is the new reference.
1033
1034 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1035 UseScratchRegisterScope temps(masm);
1036
1037 // Convenience aliases.
1038 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001039 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001040 Register expected = temp_.W();
1041 Register value = ref_reg;
1042 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1043 Register tmp_value = temps.AcquireW(); // Value in memory.
1044
1045 __ Add(tmp_ptr, base.X(), Operand(offset));
1046
1047 if (kPoisonHeapReferences) {
1048 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1049 if (value.Is(expected)) {
1050 // Do not poison `value`, as it is the same register as
1051 // `expected`, which has just been poisoned.
1052 } else {
1053 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1054 }
1055 }
1056
1057 // do {
1058 // tmp_value = [tmp_ptr] - expected;
1059 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1060
Roland Levillain24a4d112016-10-26 13:10:46 +01001061 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001062 __ Bind(&loop_head);
1063 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1064 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001065 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001066 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1067 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001068 __ B(&exit_loop);
1069 __ Bind(&comparison_failed);
1070 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001071 __ Bind(&exit_loop);
1072
1073 if (kPoisonHeapReferences) {
1074 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1075 if (value.Is(expected)) {
1076 // Do not unpoison `value`, as it is the same register as
1077 // `expected`, which has just been unpoisoned.
1078 } else {
1079 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1080 }
1081 }
1082
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001083 __ B(GetExitLabel());
1084 }
1085
1086 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001087 // The register containing the object holding the marked object reference field.
1088 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001089 // The offset, index and scale factor to access the reference in `obj_`.
1090 uint32_t offset_;
1091 Location index_;
1092 size_t scale_factor_;
1093 // Is a null check required?
1094 bool needs_null_check_;
1095 // Should this reference load use Load-Acquire semantics?
1096 bool use_load_acquire_;
1097 // A temporary register used to hold the lock word of `obj_`; and
1098 // also to hold the original reference value, when the reference is
1099 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001100 const Register temp_;
1101
Roland Levillain54f869e2017-03-06 13:54:11 +00001102 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001103};
1104
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001105// Slow path generating a read barrier for a heap reference.
1106class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1107 public:
1108 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1109 Location out,
1110 Location ref,
1111 Location obj,
1112 uint32_t offset,
1113 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001114 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001115 out_(out),
1116 ref_(ref),
1117 obj_(obj),
1118 offset_(offset),
1119 index_(index) {
1120 DCHECK(kEmitCompilerReadBarrier);
1121 // If `obj` is equal to `out` or `ref`, it means the initial object
1122 // has been overwritten by (or after) the heap object reference load
1123 // to be instrumented, e.g.:
1124 //
1125 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001126 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001127 //
1128 // In that case, we have lost the information about the original
1129 // object, and the emitted read barrier cannot work properly.
1130 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1131 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1132 }
1133
1134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1135 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1136 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001137 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001138 DCHECK(locations->CanCall());
1139 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001140 DCHECK(instruction_->IsInstanceFieldGet() ||
1141 instruction_->IsStaticFieldGet() ||
1142 instruction_->IsArrayGet() ||
1143 instruction_->IsInstanceOf() ||
1144 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001145 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001146 << "Unexpected instruction in read barrier for heap reference slow path: "
1147 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001148 // The read barrier instrumentation of object ArrayGet
1149 // instructions does not support the HIntermediateAddress
1150 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001151 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001152 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001153
1154 __ Bind(GetEntryLabel());
1155
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001156 SaveLiveRegisters(codegen, locations);
1157
1158 // We may have to change the index's value, but as `index_` is a
1159 // constant member (like other "inputs" of this slow path),
1160 // introduce a copy of it, `index`.
1161 Location index = index_;
1162 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001163 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001164 if (instruction_->IsArrayGet()) {
1165 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001166 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001167 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1168 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1169 // We are about to change the value of `index_reg` (see the
1170 // calls to vixl::MacroAssembler::Lsl and
1171 // vixl::MacroAssembler::Mov below), but it has
1172 // not been saved by the previous call to
1173 // art::SlowPathCode::SaveLiveRegisters, as it is a
1174 // callee-save register --
1175 // art::SlowPathCode::SaveLiveRegisters does not consider
1176 // callee-save registers, as it has been designed with the
1177 // assumption that callee-save registers are supposed to be
1178 // handled by the called function. So, as a callee-save
1179 // register, `index_reg` _would_ eventually be saved onto
1180 // the stack, but it would be too late: we would have
1181 // changed its value earlier. Therefore, we manually save
1182 // it here into another freely available register,
1183 // `free_reg`, chosen of course among the caller-save
1184 // registers (as a callee-save `free_reg` register would
1185 // exhibit the same problem).
1186 //
1187 // Note we could have requested a temporary register from
1188 // the register allocator instead; but we prefer not to, as
1189 // this is a slow path, and we know we can find a
1190 // caller-save register that is available.
1191 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1192 __ Mov(free_reg.W(), index_reg);
1193 index_reg = free_reg;
1194 index = LocationFrom(index_reg);
1195 } else {
1196 // The initial register stored in `index_` has already been
1197 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1198 // (as it is not a callee-save register), so we can freely
1199 // use it.
1200 }
1201 // Shifting the index value contained in `index_reg` by the scale
1202 // factor (2) cannot overflow in practice, as the runtime is
1203 // unable to allocate object arrays with a size larger than
1204 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001206 static_assert(
1207 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1208 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1209 __ Add(index_reg, index_reg, Operand(offset_));
1210 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001211 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1212 // intrinsics, `index_` is not shifted by a scale factor of 2
1213 // (as in the case of ArrayGet), as it is actually an offset
1214 // to an object field within an object.
1215 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001216 DCHECK(instruction_->GetLocations()->Intrinsified());
1217 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1218 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1219 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001220 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001221 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001222 }
1223 }
1224
1225 // We're moving two or three locations to locations that could
1226 // overlap, so we need a parallel move resolver.
1227 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001228 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001229 parallel_move.AddMove(ref_,
1230 LocationFrom(calling_convention.GetRegisterAt(0)),
1231 type,
1232 nullptr);
1233 parallel_move.AddMove(obj_,
1234 LocationFrom(calling_convention.GetRegisterAt(1)),
1235 type,
1236 nullptr);
1237 if (index.IsValid()) {
1238 parallel_move.AddMove(index,
1239 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001240 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001241 nullptr);
1242 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1243 } else {
1244 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1245 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1246 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001247 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001248 instruction_,
1249 instruction_->GetDexPc(),
1250 this);
1251 CheckEntrypointTypes<
1252 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1253 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1254
1255 RestoreLiveRegisters(codegen, locations);
1256
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001257 __ B(GetExitLabel());
1258 }
1259
1260 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1261
1262 private:
1263 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001264 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1265 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001266 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1267 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1268 return Register(VIXLRegCodeFromART(i), kXRegSize);
1269 }
1270 }
1271 // We shall never fail to find a free caller-save register, as
1272 // there are more than two core caller-save registers on ARM64
1273 // (meaning it is possible to find one which is different from
1274 // `ref` and `obj`).
1275 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1276 LOG(FATAL) << "Could not find a free register";
1277 UNREACHABLE();
1278 }
1279
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001280 const Location out_;
1281 const Location ref_;
1282 const Location obj_;
1283 const uint32_t offset_;
1284 // An additional location containing an index to an array.
1285 // Only used for HArrayGet and the UnsafeGetObject &
1286 // UnsafeGetObjectVolatile intrinsics.
1287 const Location index_;
1288
1289 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1290};
1291
1292// Slow path generating a read barrier for a GC root.
1293class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1294 public:
1295 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001296 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001297 DCHECK(kEmitCompilerReadBarrier);
1298 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001299
1300 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1301 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001302 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001303 DCHECK(locations->CanCall());
1304 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001305 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1306 << "Unexpected instruction in read barrier for GC root slow path: "
1307 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001308
1309 __ Bind(GetEntryLabel());
1310 SaveLiveRegisters(codegen, locations);
1311
1312 InvokeRuntimeCallingConvention calling_convention;
1313 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1314 // The argument of the ReadBarrierForRootSlow is not a managed
1315 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1316 // thus we need a 64-bit move here, and we cannot use
1317 //
1318 // arm64_codegen->MoveLocation(
1319 // LocationFrom(calling_convention.GetRegisterAt(0)),
1320 // root_,
1321 // type);
1322 //
1323 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001324 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001325 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001326 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001327 instruction_,
1328 instruction_->GetDexPc(),
1329 this);
1330 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1331 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1332
1333 RestoreLiveRegisters(codegen, locations);
1334 __ B(GetExitLabel());
1335 }
1336
1337 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1338
1339 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001340 const Location out_;
1341 const Location root_;
1342
1343 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1344};
1345
Alexandre Rames5319def2014-10-23 10:03:10 +01001346#undef __
1347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001348Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001349 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001350 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001351 LOG(FATAL) << "Unreachable type " << type;
1352 }
1353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001354 if (DataType::IsFloatingPointType(type) &&
Alexandre Rames5319def2014-10-23 10:03:10 +01001355 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001356 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001357 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1359 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1360 } else {
1361 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001362 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1363 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 }
1365
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001366 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001367 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001368 return next_location;
1369}
1370
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001371Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001372 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001373}
1374
Serban Constantinescu579885a2015-02-22 20:51:33 +00001375CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1376 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001377 const CompilerOptions& compiler_options,
1378 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001379 : CodeGenerator(graph,
1380 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001381 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001382 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001383 callee_saved_core_registers.GetList(),
1384 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001385 compiler_options,
1386 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001387 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1388 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001389 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001390 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001391 move_resolver_(graph->GetAllocator(), this),
1392 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001393 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001394 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001395 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001396 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001397 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1398 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1399 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1400 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1401 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1402 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1403 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1404 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001405 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001406 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001407 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001408 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001409 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001410 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001411}
Alexandre Rames5319def2014-10-23 10:03:10 +01001412
Alexandre Rames67555f72014-11-18 10:55:16 +00001413#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001414
Zheng Xu3927c8b2015-11-18 17:46:25 +08001415void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001416 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001417 jump_table->EmitTable(this);
1418 }
1419}
1420
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001421void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001422 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001423 // Ensure we emit the literal pool.
1424 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001425
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001426 CodeGenerator::Finalize(allocator);
1427}
1428
Zheng Xuad4450e2015-04-17 18:48:56 +08001429void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1430 // Note: There are 6 kinds of moves:
1431 // 1. constant -> GPR/FPR (non-cycle)
1432 // 2. constant -> stack (non-cycle)
1433 // 3. GPR/FPR -> GPR/FPR
1434 // 4. GPR/FPR -> stack
1435 // 5. stack -> GPR/FPR
1436 // 6. stack -> stack (non-cycle)
1437 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1438 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1439 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1440 // dependency.
1441 vixl_temps_.Open(GetVIXLAssembler());
1442}
1443
1444void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1445 vixl_temps_.Close();
1446}
1447
1448Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001449 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1450 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1451 || kind == Location::kSIMDStackSlot);
1452 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1453 ? Location::kFpuRegister
1454 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001455 Location scratch = GetScratchLocation(kind);
1456 if (!scratch.Equals(Location::NoLocation())) {
1457 return scratch;
1458 }
1459 // Allocate from VIXL temp registers.
1460 if (kind == Location::kRegister) {
1461 scratch = LocationFrom(vixl_temps_.AcquireX());
1462 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001463 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001464 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1465 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1466 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001467 }
1468 AddScratchLocation(scratch);
1469 return scratch;
1470}
1471
1472void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1473 if (loc.IsRegister()) {
1474 vixl_temps_.Release(XRegisterFrom(loc));
1475 } else {
1476 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001477 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001478 }
1479 RemoveScratchLocation(loc);
1480}
1481
Alexandre Rames3e69f162014-12-10 10:36:50 +00001482void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001483 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001484 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001485}
1486
Alexandre Rames5319def2014-10-23 10:03:10 +01001487void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001488 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001489 __ Bind(&frame_entry_label_);
1490
Vladimir Marko33bff252017-11-01 14:35:42 +00001491 bool do_overflow_check =
1492 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001493 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001494 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001495 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001496 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001497 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001498 {
1499 // Ensure that between load and RecordPcInfo there are no pools emitted.
1500 ExactAssemblyScope eas(GetVIXLAssembler(),
1501 kInstructionSize,
1502 CodeBufferCheckScope::kExactSize);
1503 __ ldr(wzr, MemOperand(temp, 0));
1504 RecordPcInfo(nullptr, 0);
1505 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001506 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001507
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001508 if (!HasEmptyFrame()) {
1509 int frame_size = GetFrameSize();
1510 // Stack layout:
1511 // sp[frame_size - 8] : lr.
1512 // ... : other preserved core registers.
1513 // ... : other preserved fp registers.
1514 // ... : reserved frame space.
1515 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001516
1517 // Save the current method if we need it. Note that we do not
1518 // do this in HCurrentMethod, as the instruction might have been removed
1519 // in the SSA graph.
1520 if (RequiresCurrentMethod()) {
1521 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001522 } else {
1523 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001524 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001525 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001526 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1527 frame_size - GetCoreSpillSize());
1528 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1529 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001530
1531 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1532 // Initialize should_deoptimize flag to 0.
1533 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1534 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1535 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001536 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001537
1538 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001539}
1540
1541void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001542 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001543 if (!HasEmptyFrame()) {
1544 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001545 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1546 frame_size - FrameEntrySpillSize());
1547 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1548 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001549 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001550 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001551 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001552 __ Ret();
1553 GetAssembler()->cfi().RestoreState();
1554 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001555}
1556
Scott Wakeling97c72b72016-06-24 16:19:36 +01001557CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001558 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001559 return CPURegList(CPURegister::kRegister, kXRegSize,
1560 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001561}
1562
Scott Wakeling97c72b72016-06-24 16:19:36 +01001563CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001564 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1565 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001566 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1567 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001568}
1569
Alexandre Rames5319def2014-10-23 10:03:10 +01001570void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1571 __ Bind(GetLabelOf(block));
1572}
1573
Calin Juravle175dc732015-08-25 15:42:32 +01001574void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1575 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001576 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001577}
1578
Calin Juravlee460d1d2015-09-29 04:52:17 +01001579void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1580 if (location.IsRegister()) {
1581 locations->AddTemp(location);
1582 } else {
1583 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1584 }
1585}
1586
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001587void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001588 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001589 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001590 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001591 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001592 if (value_can_be_null) {
1593 __ Cbz(value, &done);
1594 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001595 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001596 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001597 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001598 if (value_can_be_null) {
1599 __ Bind(&done);
1600 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001601}
1602
David Brazdil58282f42016-01-14 12:45:10 +00001603void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001604 // Blocked core registers:
1605 // lr : Runtime reserved.
1606 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001607 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001608 // ip1 : VIXL core temp.
1609 // ip0 : VIXL core temp.
1610 //
1611 // Blocked fp registers:
1612 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001613 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1614 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001615 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001616 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001617 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001618
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001619 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001620 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001621 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001622 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001623
David Brazdil58282f42016-01-14 12:45:10 +00001624 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001625 // Stubs do not save callee-save floating point registers. If the graph
1626 // is debuggable, we need to deal with these registers differently. For
1627 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001628 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1629 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001630 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001631 }
1632 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001633}
1634
Alexandre Rames3e69f162014-12-10 10:36:50 +00001635size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1636 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1637 __ Str(reg, MemOperand(sp, stack_index));
1638 return kArm64WordSize;
1639}
1640
1641size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1642 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1643 __ Ldr(reg, MemOperand(sp, stack_index));
1644 return kArm64WordSize;
1645}
1646
1647size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1648 FPRegister reg = FPRegister(reg_id, kDRegSize);
1649 __ Str(reg, MemOperand(sp, stack_index));
1650 return kArm64WordSize;
1651}
1652
1653size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1654 FPRegister reg = FPRegister(reg_id, kDRegSize);
1655 __ Ldr(reg, MemOperand(sp, stack_index));
1656 return kArm64WordSize;
1657}
1658
Alexandre Rames5319def2014-10-23 10:03:10 +01001659void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001660 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001661}
1662
1663void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001664 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001665}
1666
Alexandre Rames67555f72014-11-18 10:55:16 +00001667void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001668 if (constant->IsIntConstant()) {
1669 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1670 } else if (constant->IsLongConstant()) {
1671 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1672 } else if (constant->IsNullConstant()) {
1673 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001674 } else if (constant->IsFloatConstant()) {
1675 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1676 } else {
1677 DCHECK(constant->IsDoubleConstant());
1678 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1679 }
1680}
1681
Alexandre Rames3e69f162014-12-10 10:36:50 +00001682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001684 DCHECK(constant.IsConstant());
1685 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001687 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1689 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1690 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1691 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001692}
1693
Roland Levillain952b2352017-05-03 19:49:14 +01001694// Allocate a scratch register from the VIXL pool, querying first
1695// the floating-point register pool, and then the core register
1696// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001697// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1698// using a different allocation strategy.
1699static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1700 vixl::aarch64::UseScratchRegisterScope* temps,
1701 int size_in_bits) {
1702 return masm->GetScratchFPRegisterList()->IsEmpty()
1703 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1704 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1705}
1706
Calin Juravlee460d1d2015-09-29 04:52:17 +01001707void CodeGeneratorARM64::MoveLocation(Location destination,
1708 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001709 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001710 if (source.Equals(destination)) {
1711 return;
1712 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001713
1714 // A valid move can always be inferred from the destination and source
1715 // locations. When moving from and to a register, the argument type can be
1716 // used to generate 32bit instead of 64bit moves. In debug mode we also
1717 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001719
1720 if (destination.IsRegister() || destination.IsFpuRegister()) {
1721 if (unspecified_type) {
1722 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1723 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001724 (src_cst != nullptr && (src_cst->IsIntConstant()
1725 || src_cst->IsFloatConstant()
1726 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001727 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001728 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001729 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001730 // If the source is a double stack slot or a 64bit constant, a 64bit
1731 // type is appropriate. Else the source is a register, and since the
1732 // type has not been specified, we chose a 64bit type to force a 64bit
1733 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001734 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001735 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001736 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001737 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1738 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001739 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001740 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1741 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1742 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001743 } else if (source.IsSIMDStackSlot()) {
1744 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001745 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001746 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001747 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001748 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001749 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001750 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001751 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001752 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001753 DataType::Type source_type = DataType::Is64BitType(dst_type)
1754 ? DataType::Type::kInt64
1755 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001756 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1757 }
1758 } else {
1759 DCHECK(source.IsFpuRegister());
1760 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001761 DataType::Type source_type = DataType::Is64BitType(dst_type)
1762 ? DataType::Type::kFloat64
1763 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001764 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1765 } else {
1766 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001767 if (GetGraph()->HasSIMD()) {
1768 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1769 } else {
1770 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1771 }
1772 }
1773 }
1774 } else if (destination.IsSIMDStackSlot()) {
1775 if (source.IsFpuRegister()) {
1776 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1777 } else {
1778 DCHECK(source.IsSIMDStackSlot());
1779 UseScratchRegisterScope temps(GetVIXLAssembler());
1780 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1781 Register temp = temps.AcquireX();
1782 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1783 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1784 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1785 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1786 } else {
1787 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1788 __ Ldr(temp, StackOperandFrom(source));
1789 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001790 }
1791 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001792 } else { // The destination is not a register. It must be a stack slot.
1793 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1794 if (source.IsRegister() || source.IsFpuRegister()) {
1795 if (unspecified_type) {
1796 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001797 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001798 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001799 dst_type =
1800 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801 }
1802 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001803 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1804 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001805 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001806 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001807 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1808 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 UseScratchRegisterScope temps(GetVIXLAssembler());
1810 HConstant* src_cst = source.GetConstant();
1811 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001812 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001813 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1814 ? Register(xzr)
1815 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001816 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001817 if (src_cst->IsIntConstant()) {
1818 temp = temps.AcquireW();
1819 } else if (src_cst->IsLongConstant()) {
1820 temp = temps.AcquireX();
1821 } else if (src_cst->IsFloatConstant()) {
1822 temp = temps.AcquireS();
1823 } else {
1824 DCHECK(src_cst->IsDoubleConstant());
1825 temp = temps.AcquireD();
1826 }
1827 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001828 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001829 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001830 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001831 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001832 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001833 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001834 // Use any scratch register (a core or a floating-point one)
1835 // from VIXL scratch register pools as a temporary.
1836 //
1837 // We used to only use the FP scratch register pool, but in some
1838 // rare cases the only register from this pool (D31) would
1839 // already be used (e.g. within a ParallelMove instruction, when
1840 // a move is blocked by a another move requiring a scratch FP
1841 // register, which would reserve D31). To prevent this issue, we
1842 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001843 //
1844 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001845 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001846 // use AcquireFPOrCoreCPURegisterOfSize instead of
1847 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1848 // allocates core scratch registers first.
1849 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1850 GetVIXLAssembler(),
1851 &temps,
1852 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001853 __ Ldr(temp, StackOperandFrom(source));
1854 __ Str(temp, StackOperandFrom(destination));
1855 }
1856 }
1857}
1858
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001859void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001860 CPURegister dst,
1861 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001862 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001864 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001865 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001866 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001868 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001869 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001871 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001872 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001873 case DataType::Type::kInt16:
1874 __ Ldrsh(Register(dst), src);
1875 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 case DataType::Type::kInt32:
1877 case DataType::Type::kReference:
1878 case DataType::Type::kInt64:
1879 case DataType::Type::kFloat32:
1880 case DataType::Type::kFloat64:
1881 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001882 __ Ldr(dst, src);
1883 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001884 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001885 LOG(FATAL) << "Unreachable type " << type;
1886 }
1887}
1888
Calin Juravle77520bc2015-01-12 18:45:46 +00001889void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001890 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001891 const MemOperand& src,
1892 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001893 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001894 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001895 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001896 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001897
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001898 DCHECK(!src.IsPreIndex());
1899 DCHECK(!src.IsPostIndex());
1900
1901 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001902 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001903 {
1904 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1905 MemOperand base = MemOperand(temp_base);
1906 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001907 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001908 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001909 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001910 {
1911 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1912 __ ldarb(Register(dst), base);
1913 if (needs_null_check) {
1914 MaybeRecordImplicitNullCheck(instruction);
1915 }
1916 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001917 if (type == DataType::Type::kInt8) {
1918 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001919 }
1920 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001921 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001922 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001923 {
1924 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1925 __ ldarh(Register(dst), base);
1926 if (needs_null_check) {
1927 MaybeRecordImplicitNullCheck(instruction);
1928 }
1929 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001930 if (type == DataType::Type::kInt16) {
1931 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1932 }
Artem Serov914d7a82017-02-07 14:33:49 +00001933 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 case DataType::Type::kInt32:
1935 case DataType::Type::kReference:
1936 case DataType::Type::kInt64:
1937 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001938 {
1939 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1940 __ ldar(Register(dst), base);
1941 if (needs_null_check) {
1942 MaybeRecordImplicitNullCheck(instruction);
1943 }
1944 }
1945 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001946 case DataType::Type::kFloat32:
1947 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001948 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001949 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001950
Artem Serov914d7a82017-02-07 14:33:49 +00001951 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1952 {
1953 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1954 __ ldar(temp, base);
1955 if (needs_null_check) {
1956 MaybeRecordImplicitNullCheck(instruction);
1957 }
1958 }
1959 __ Fmov(FPRegister(dst), temp);
1960 break;
Roland Levillain44015862016-01-22 11:47:17 +00001961 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001962 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001963 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001964 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001965 }
1966}
1967
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001968void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001969 CPURegister src,
1970 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001971 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001972 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001973 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001974 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001975 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001976 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001977 case DataType::Type::kUint16:
1978 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001979 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001980 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001981 case DataType::Type::kInt32:
1982 case DataType::Type::kReference:
1983 case DataType::Type::kInt64:
1984 case DataType::Type::kFloat32:
1985 case DataType::Type::kFloat64:
1986 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001987 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001988 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001990 LOG(FATAL) << "Unreachable type " << type;
1991 }
1992}
1993
Artem Serov914d7a82017-02-07 14:33:49 +00001994void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001995 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001996 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001997 const MemOperand& dst,
1998 bool needs_null_check) {
1999 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002000 UseScratchRegisterScope temps(GetVIXLAssembler());
2001 Register temp_base = temps.AcquireX();
2002
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002003 DCHECK(!dst.IsPreIndex());
2004 DCHECK(!dst.IsPostIndex());
2005
2006 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002007 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002008 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002009 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002010 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002011 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002012 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002013 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002015 {
2016 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2017 __ stlrb(Register(src), base);
2018 if (needs_null_check) {
2019 MaybeRecordImplicitNullCheck(instruction);
2020 }
2021 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002022 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 case DataType::Type::kUint16:
2024 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002025 {
2026 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2027 __ stlrh(Register(src), base);
2028 if (needs_null_check) {
2029 MaybeRecordImplicitNullCheck(instruction);
2030 }
2031 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002032 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 case DataType::Type::kInt32:
2034 case DataType::Type::kReference:
2035 case DataType::Type::kInt64:
2036 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002037 {
2038 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2039 __ stlr(Register(src), base);
2040 if (needs_null_check) {
2041 MaybeRecordImplicitNullCheck(instruction);
2042 }
2043 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002044 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 case DataType::Type::kFloat32:
2046 case DataType::Type::kFloat64: {
2047 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002048 Register temp_src;
2049 if (src.IsZero()) {
2050 // The zero register is used to avoid synthesizing zero constants.
2051 temp_src = Register(src);
2052 } else {
2053 DCHECK(src.IsFPRegister());
2054 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2055 __ Fmov(temp_src, FPRegister(src));
2056 }
Artem Serov914d7a82017-02-07 14:33:49 +00002057 {
2058 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2059 __ stlr(temp_src, base);
2060 if (needs_null_check) {
2061 MaybeRecordImplicitNullCheck(instruction);
2062 }
2063 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002064 break;
2065 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002067 LOG(FATAL) << "Unreachable type " << type;
2068 }
2069}
2070
Calin Juravle175dc732015-08-25 15:42:32 +01002071void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2072 HInstruction* instruction,
2073 uint32_t dex_pc,
2074 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002075 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002076
2077 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2078 {
2079 // Ensure the pc position is recorded immediately after the `blr` instruction.
2080 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2081 __ blr(lr);
2082 if (EntrypointRequiresStackMap(entrypoint)) {
2083 RecordPcInfo(instruction, dex_pc, slow_path);
2084 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002085 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002086}
2087
Roland Levillaindec8f632016-07-22 17:10:06 +01002088void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2089 HInstruction* instruction,
2090 SlowPathCode* slow_path) {
2091 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002092 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2093 __ Blr(lr);
2094}
2095
Alexandre Rames67555f72014-11-18 10:55:16 +00002096void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002097 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002098 UseScratchRegisterScope temps(GetVIXLAssembler());
2099 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00002100 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2101 const size_t status_byte_offset =
2102 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2103 constexpr uint32_t shifted_initialized_value =
2104 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002105
Serban Constantinescu02164b32014-11-13 14:05:07 +00002106 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002107 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00002108 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07002109 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00002110 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00002111 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002112 __ Bind(slow_path->GetExitLabel());
2113}
Alexandre Rames5319def2014-10-23 10:03:10 +01002114
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00002115void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
2116 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
2117 uint32_t path_to_root = check->GetBitstringPathToRoot();
2118 uint32_t mask = check->GetBitstringMask();
2119 DCHECK(IsPowerOfTwo(mask + 1));
2120 size_t mask_bits = WhichPowerOf2(mask + 1);
2121
2122 if (mask_bits == 16u) {
2123 // Load only the bitstring part of the status word.
2124 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2125 } else {
2126 // /* uint32_t */ temp = temp->status_
2127 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2128 // Extract the bitstring bits.
2129 __ Ubfx(temp, temp, 0, mask_bits);
2130 }
2131 // Compare the bitstring bits to `path_to_root`.
2132 __ Cmp(temp, path_to_root);
2133}
2134
Roland Levillain44015862016-01-22 11:47:17 +00002135void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002136 BarrierType type = BarrierAll;
2137
2138 switch (kind) {
2139 case MemBarrierKind::kAnyAny:
2140 case MemBarrierKind::kAnyStore: {
2141 type = BarrierAll;
2142 break;
2143 }
2144 case MemBarrierKind::kLoadAny: {
2145 type = BarrierReads;
2146 break;
2147 }
2148 case MemBarrierKind::kStoreStore: {
2149 type = BarrierWrites;
2150 break;
2151 }
2152 default:
2153 LOG(FATAL) << "Unexpected memory barrier " << kind;
2154 }
2155 __ Dmb(InnerShareable, type);
2156}
2157
Serban Constantinescu02164b32014-11-13 14:05:07 +00002158void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2159 HBasicBlock* successor) {
2160 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002161 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2162 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002163 slow_path =
2164 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002165 instruction->SetSlowPath(slow_path);
2166 codegen_->AddSlowPath(slow_path);
2167 if (successor != nullptr) {
2168 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002169 }
2170 } else {
2171 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2172 }
2173
Serban Constantinescu02164b32014-11-13 14:05:07 +00002174 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2175 Register temp = temps.AcquireW();
2176
Andreas Gampe542451c2016-07-26 09:02:02 -07002177 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002178 if (successor == nullptr) {
2179 __ Cbnz(temp, slow_path->GetEntryLabel());
2180 __ Bind(slow_path->GetReturnLabel());
2181 } else {
2182 __ Cbz(temp, codegen_->GetLabelOf(successor));
2183 __ B(slow_path->GetEntryLabel());
2184 // slow_path will return to GetLabelOf(successor).
2185 }
2186}
2187
Alexandre Rames5319def2014-10-23 10:03:10 +01002188InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2189 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002190 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002191 assembler_(codegen->GetAssembler()),
2192 codegen_(codegen) {}
2193
Alexandre Rames67555f72014-11-18 10:55:16 +00002194void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002195 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002196 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002197 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002198 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002199 case DataType::Type::kInt32:
2200 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002201 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002202 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002204 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002205
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002206 case DataType::Type::kFloat32:
2207 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002208 locations->SetInAt(0, Location::RequiresFpuRegister());
2209 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002210 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002211 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002212
Alexandre Rames5319def2014-10-23 10:03:10 +01002213 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002214 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002215 }
2216}
2217
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002218void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2219 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002220 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2221
2222 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002224 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002225 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2226 object_field_get_with_read_barrier
2227 ? LocationSummary::kCallOnSlowPath
2228 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002229 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002230 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002231 // We need a temporary register for the read barrier marking slow
2232 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002233 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2234 !Runtime::Current()->UseJitCompilation() &&
2235 !field_info.IsVolatile()) {
2236 // If link-time thunks for the Baker read barrier are enabled, for AOT
2237 // non-volatile loads we need a temporary only if the offset is too big.
2238 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2239 locations->AddTemp(FixedTempLocation());
2240 }
2241 } else {
2242 locations->AddTemp(Location::RequiresRegister());
2243 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002244 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002245 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002247 locations->SetOut(Location::RequiresFpuRegister());
2248 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002249 // The output overlaps for an object field get when read barriers
2250 // are enabled: we do not want the load to overwrite the object's
2251 // location, as we need it to emit the read barrier.
2252 locations->SetOut(
2253 Location::RequiresRegister(),
2254 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002255 }
2256}
2257
2258void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2259 const FieldInfo& field_info) {
2260 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002261 LocationSummary* locations = instruction->GetLocations();
2262 Location base_loc = locations->InAt(0);
2263 Location out = locations->Out();
2264 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002265 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2266 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002267 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002269 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002270 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002271 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002272 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002274 Location maybe_temp =
2275 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002276 // Note that potential implicit null checks are handled in this
2277 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2278 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2279 instruction,
2280 out,
2281 base,
2282 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002283 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002284 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002285 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002286 } else {
2287 // General case.
2288 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002289 // Note that a potential implicit null check is handled in this
2290 // CodeGeneratorARM64::LoadAcquire call.
2291 // NB: LoadAcquire will record the pc info if needed.
2292 codegen_->LoadAcquire(
2293 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002294 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002295 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2296 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002297 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002299 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002300 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002301 // If read barriers are enabled, emit read barriers other than
2302 // Baker's using a slow path (and also unpoison the loaded
2303 // reference, if heap poisoning is enabled).
2304 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2305 }
Roland Levillain4d027112015-07-01 15:41:14 +01002306 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002307}
2308
2309void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2310 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002311 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002312 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002313 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2314 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002316 locations->SetInAt(1, Location::RequiresFpuRegister());
2317 } else {
2318 locations->SetInAt(1, Location::RequiresRegister());
2319 }
2320}
2321
2322void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002323 const FieldInfo& field_info,
2324 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002325 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2326
2327 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002328 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002329 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002330 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002332
Roland Levillain4d027112015-07-01 15:41:14 +01002333 {
2334 // We use a block to end the scratch scope before the write barrier, thus
2335 // freeing the temporary registers so they can be used in `MarkGCCard`.
2336 UseScratchRegisterScope temps(GetVIXLAssembler());
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002339 DCHECK(value.IsW());
2340 Register temp = temps.AcquireW();
2341 __ Mov(temp, value.W());
2342 GetAssembler()->PoisonHeapReference(temp.W());
2343 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002344 }
Roland Levillain4d027112015-07-01 15:41:14 +01002345
2346 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002347 codegen_->StoreRelease(
2348 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002349 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002350 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2351 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002352 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2353 codegen_->MaybeRecordImplicitNullCheck(instruction);
2354 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002355 }
2356
2357 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002358 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002359 }
2360}
2361
Alexandre Rames67555f72014-11-18 10:55:16 +00002362void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002364
2365 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 case DataType::Type::kInt32:
2367 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002368 Register dst = OutputRegister(instr);
2369 Register lhs = InputRegisterAt(instr, 0);
2370 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002371 if (instr->IsAdd()) {
2372 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002373 } else if (instr->IsAnd()) {
2374 __ And(dst, lhs, rhs);
2375 } else if (instr->IsOr()) {
2376 __ Orr(dst, lhs, rhs);
2377 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002378 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002379 } else if (instr->IsRor()) {
2380 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002381 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002382 __ Ror(dst, lhs, shift);
2383 } else {
2384 // Ensure shift distance is in the same size register as the result. If
2385 // we are rotating a long and the shift comes in a w register originally,
2386 // we don't need to sxtw for use as an x since the shift distances are
2387 // all & reg_bits - 1.
2388 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2389 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002390 } else {
2391 DCHECK(instr->IsXor());
2392 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002393 }
2394 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002395 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002396 case DataType::Type::kFloat32:
2397 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002398 FPRegister dst = OutputFPRegister(instr);
2399 FPRegister lhs = InputFPRegisterAt(instr, 0);
2400 FPRegister rhs = InputFPRegisterAt(instr, 1);
2401 if (instr->IsAdd()) {
2402 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002403 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002404 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002405 } else {
2406 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002407 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002408 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002409 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002410 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002411 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002412 }
2413}
2414
Serban Constantinescu02164b32014-11-13 14:05:07 +00002415void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2416 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2417
Vladimir Markoca6fff82017-10-03 14:49:14 +01002418 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002419 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002420 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 case DataType::Type::kInt32:
2422 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002423 locations->SetInAt(0, Location::RequiresRegister());
2424 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002426 break;
2427 }
2428 default:
2429 LOG(FATAL) << "Unexpected shift type " << type;
2430 }
2431}
2432
2433void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2434 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002437 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 case DataType::Type::kInt32:
2439 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002440 Register dst = OutputRegister(instr);
2441 Register lhs = InputRegisterAt(instr, 0);
2442 Operand rhs = InputOperandAt(instr, 1);
2443 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002444 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002445 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002446 if (instr->IsShl()) {
2447 __ Lsl(dst, lhs, shift_value);
2448 } else if (instr->IsShr()) {
2449 __ Asr(dst, lhs, shift_value);
2450 } else {
2451 __ Lsr(dst, lhs, shift_value);
2452 }
2453 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002454 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002455
2456 if (instr->IsShl()) {
2457 __ Lsl(dst, lhs, rhs_reg);
2458 } else if (instr->IsShr()) {
2459 __ Asr(dst, lhs, rhs_reg);
2460 } else {
2461 __ Lsr(dst, lhs, rhs_reg);
2462 }
2463 }
2464 break;
2465 }
2466 default:
2467 LOG(FATAL) << "Unexpected shift operation type " << type;
2468 }
2469}
2470
Alexandre Rames5319def2014-10-23 10:03:10 +01002471void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002472 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002473}
2474
2475void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002476 HandleBinaryOp(instruction);
2477}
2478
2479void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2480 HandleBinaryOp(instruction);
2481}
2482
2483void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2484 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002485}
2486
Artem Serov7fc63502016-02-09 17:15:29 +00002487void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002489 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002490 locations->SetInAt(0, Location::RequiresRegister());
2491 // There is no immediate variant of negated bitwise instructions in AArch64.
2492 locations->SetInAt(1, Location::RequiresRegister());
2493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2494}
2495
Artem Serov7fc63502016-02-09 17:15:29 +00002496void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002497 Register dst = OutputRegister(instr);
2498 Register lhs = InputRegisterAt(instr, 0);
2499 Register rhs = InputRegisterAt(instr, 1);
2500
2501 switch (instr->GetOpKind()) {
2502 case HInstruction::kAnd:
2503 __ Bic(dst, lhs, rhs);
2504 break;
2505 case HInstruction::kOr:
2506 __ Orn(dst, lhs, rhs);
2507 break;
2508 case HInstruction::kXor:
2509 __ Eon(dst, lhs, rhs);
2510 break;
2511 default:
2512 LOG(FATAL) << "Unreachable";
2513 }
2514}
2515
Anton Kirilov74234da2017-01-13 14:42:47 +00002516void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2517 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2519 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002520 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002521 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002522 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2523 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2524 } else {
2525 locations->SetInAt(0, Location::RequiresRegister());
2526 }
2527 locations->SetInAt(1, Location::RequiresRegister());
2528 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2529}
2530
Anton Kirilov74234da2017-01-13 14:42:47 +00002531void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2532 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002534 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002536 Register out = OutputRegister(instruction);
2537 Register left;
2538 if (kind != HInstruction::kNeg) {
2539 left = InputRegisterAt(instruction, 0);
2540 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002541 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002542 // shifter operand operation, the IR generating `right_reg` (input to the type
2543 // conversion) can have a different type from the current instruction's type,
2544 // so we manually indicate the type.
2545 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002546 Operand right_operand(0);
2547
Anton Kirilov74234da2017-01-13 14:42:47 +00002548 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2549 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002550 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2551 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002552 right_operand = Operand(right_reg,
2553 helpers::ShiftFromOpKind(op_kind),
2554 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002555 }
2556
2557 // Logical binary operations do not support extension operations in the
2558 // operand. Note that VIXL would still manage if it was passed by generating
2559 // the extension as a separate instruction.
2560 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2561 DCHECK(!right_operand.IsExtendedRegister() ||
2562 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2563 kind != HInstruction::kNeg));
2564 switch (kind) {
2565 case HInstruction::kAdd:
2566 __ Add(out, left, right_operand);
2567 break;
2568 case HInstruction::kAnd:
2569 __ And(out, left, right_operand);
2570 break;
2571 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002572 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002573 __ Neg(out, right_operand);
2574 break;
2575 case HInstruction::kOr:
2576 __ Orr(out, left, right_operand);
2577 break;
2578 case HInstruction::kSub:
2579 __ Sub(out, left, right_operand);
2580 break;
2581 case HInstruction::kXor:
2582 __ Eor(out, left, right_operand);
2583 break;
2584 default:
2585 LOG(FATAL) << "Unexpected operation kind: " << kind;
2586 UNREACHABLE();
2587 }
2588}
2589
Artem Serov328429f2016-07-06 16:23:04 +01002590void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002591 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002592 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002593 locations->SetInAt(0, Location::RequiresRegister());
2594 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002596}
2597
Roland Levillain19c54192016-11-04 13:44:09 +00002598void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002599 __ Add(OutputRegister(instruction),
2600 InputRegisterAt(instruction, 0),
2601 Operand(InputOperandAt(instruction, 1)));
2602}
2603
Artem Serove1811ed2017-04-27 16:50:47 +01002604void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2605 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002606 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002607
2608 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2609
2610 locations->SetInAt(0, Location::RequiresRegister());
2611 // For byte case we don't need to shift the index variable so we can encode the data offset into
2612 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2613 // data offset constant generation out of the loop and reduce the critical path length in the
2614 // loop.
2615 locations->SetInAt(1, shift->GetValue() == 0
2616 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2617 : Location::RequiresRegister());
2618 locations->SetInAt(2, Location::ConstantLocation(shift));
2619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2620}
2621
2622void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2623 HIntermediateAddressIndex* instruction) {
2624 Register index_reg = InputRegisterAt(instruction, 0);
2625 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2626 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2627
2628 if (shift == 0) {
2629 __ Add(OutputRegister(instruction), index_reg, offset);
2630 } else {
2631 Register offset_reg = InputRegisterAt(instruction, 1);
2632 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2633 }
2634}
2635
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002636void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002637 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002638 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002639 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2640 if (instr->GetOpKind() == HInstruction::kSub &&
2641 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002642 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002643 // Don't allocate register for Mneg instruction.
2644 } else {
2645 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2646 Location::RequiresRegister());
2647 }
2648 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2649 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2651}
2652
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002653void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002654 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002655 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2656 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002657
2658 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2659 // This fixup should be carried out for all multiply-accumulate instructions:
2660 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002662 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2663 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002664 vixl::aarch64::Instruction* prev =
2665 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002666 if (prev->IsLoadOrStore()) {
2667 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002668 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002669 __ nop();
2670 }
2671 }
2672
2673 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002674 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002675 __ Madd(res, mul_left, mul_right, accumulator);
2676 } else {
2677 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002678 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002679 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002680 __ Mneg(res, mul_left, mul_right);
2681 } else {
2682 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2683 __ Msub(res, mul_left, mul_right, accumulator);
2684 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002685 }
2686}
2687
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002688void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002689 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002690 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002691 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002692 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2693 object_array_get_with_read_barrier
2694 ? LocationSummary::kCallOnSlowPath
2695 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002696 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002697 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002698 // We need a temporary register for the read barrier marking slow
2699 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002700 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2701 !Runtime::Current()->UseJitCompilation() &&
2702 instruction->GetIndex()->IsConstant()) {
2703 // Array loads with constant index are treated as field loads.
2704 // If link-time thunks for the Baker read barrier are enabled, for AOT
2705 // constant index loads we need a temporary only if the offset is too big.
2706 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2707 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002709 if (offset >= kReferenceLoadMinFarOffset) {
2710 locations->AddTemp(FixedTempLocation());
2711 }
2712 } else {
2713 locations->AddTemp(Location::RequiresRegister());
2714 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002715 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002716 locations->SetInAt(0, Location::RequiresRegister());
2717 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002719 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2720 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002721 // The output overlaps in the case of an object array get with
2722 // read barriers enabled: we do not want the move to overwrite the
2723 // array's location, as we need it to emit the read barrier.
2724 locations->SetOut(
2725 Location::RequiresRegister(),
2726 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002727 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002728}
2729
2730void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002732 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002733 LocationSummary* locations = instruction->GetLocations();
2734 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002735 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002736 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002737 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2738 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002739 MacroAssembler* masm = GetVIXLAssembler();
2740 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002741
Roland Levillain19c54192016-11-04 13:44:09 +00002742 // The read barrier instrumentation of object ArrayGet instructions
2743 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002745 instruction->GetArray()->IsIntermediateAddress() &&
2746 kEmitCompilerReadBarrier));
2747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002748 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002749 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002750 // Note that a potential implicit null check is handled in the
2751 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002752 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002753 if (index.IsConstant()) {
2754 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002756 Location maybe_temp =
2757 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2758 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2759 out,
2760 obj.W(),
2761 offset,
2762 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002763 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002764 /* use_load_acquire */ false);
2765 } else {
2766 Register temp = WRegisterFrom(locations->GetTemp(0));
2767 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002768 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002769 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002770 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002771 // General case.
2772 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002773 Register length;
2774 if (maybe_compressed_char_at) {
2775 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2776 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002777 {
2778 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2779 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2780
2781 if (instruction->GetArray()->IsIntermediateAddress()) {
2782 DCHECK_LT(count_offset, offset);
2783 int64_t adjusted_offset =
2784 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2785 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2786 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2787 } else {
2788 __ Ldr(length, HeapOperand(obj, count_offset));
2789 }
2790 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002791 }
jessicahandojo05765752016-09-09 19:01:32 -07002792 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002793 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002794 if (maybe_compressed_char_at) {
2795 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002796 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2797 "Expecting 0=compressed, 1=uncompressed");
2798 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002799 __ Ldrb(Register(OutputCPURegister(instruction)),
2800 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2801 __ B(&done);
2802 __ Bind(&uncompressed_load);
2803 __ Ldrh(Register(OutputCPURegister(instruction)),
2804 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2805 __ Bind(&done);
2806 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002808 source = HeapOperand(obj, offset);
2809 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002810 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002811 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002812 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002813 // We do not need to compute the intermediate address from the array: the
2814 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002815 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002816 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002817 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002818 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2819 }
2820 temp = obj;
2821 } else {
2822 __ Add(temp, obj, offset);
2823 }
jessicahandojo05765752016-09-09 19:01:32 -07002824 if (maybe_compressed_char_at) {
2825 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002826 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2827 "Expecting 0=compressed, 1=uncompressed");
2828 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002829 __ Ldrb(Register(OutputCPURegister(instruction)),
2830 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2831 __ B(&done);
2832 __ Bind(&uncompressed_load);
2833 __ Ldrh(Register(OutputCPURegister(instruction)),
2834 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2835 __ Bind(&done);
2836 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002838 }
Roland Levillain44015862016-01-22 11:47:17 +00002839 }
jessicahandojo05765752016-09-09 19:01:32 -07002840 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002841 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2842 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002843 codegen_->Load(type, OutputCPURegister(instruction), source);
2844 codegen_->MaybeRecordImplicitNullCheck(instruction);
2845 }
Roland Levillain44015862016-01-22 11:47:17 +00002846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002848 static_assert(
2849 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2850 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2851 Location obj_loc = locations->InAt(0);
2852 if (index.IsConstant()) {
2853 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2854 } else {
2855 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2856 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002857 }
Roland Levillain4d027112015-07-01 15:41:14 +01002858 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002859}
2860
Alexandre Rames5319def2014-10-23 10:03:10 +01002861void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002862 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002863 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002865}
2866
2867void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002868 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002869 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002870 {
2871 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2872 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2873 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2874 codegen_->MaybeRecordImplicitNullCheck(instruction);
2875 }
jessicahandojo05765752016-09-09 19:01:32 -07002876 // Mask out compression flag from String's array length.
2877 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002878 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002879 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002880}
2881
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002882void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002884
2885 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002886 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002887 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002888 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002889 LocationSummary::kCallOnSlowPath :
2890 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002891 locations->SetInAt(0, Location::RequiresRegister());
2892 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002893 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2894 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002896 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002897 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002898 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002899 }
2900}
2901
2902void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002904 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002905 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002906 bool needs_write_barrier =
2907 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002908
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002909 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002910 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002911 CPURegister source = value;
2912 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002914 MemOperand destination = HeapOperand(array);
2915 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002916
2917 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002918 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002919 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002921 destination = HeapOperand(array, offset);
2922 } else {
2923 UseScratchRegisterScope temps(masm);
2924 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002925 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002926 // We do not need to compute the intermediate address from the array: the
2927 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002928 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002929 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002930 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002931 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2932 }
2933 temp = array;
2934 } else {
2935 __ Add(temp, array, offset);
2936 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002937 destination = HeapOperand(temp,
2938 XRegisterFrom(index),
2939 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002941 }
Artem Serov914d7a82017-02-07 14:33:49 +00002942 {
2943 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2944 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2945 codegen_->Store(value_type, value, destination);
2946 codegen_->MaybeRecordImplicitNullCheck(instruction);
2947 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002948 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002949 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002950 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002951 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002952 {
2953 // We use a block to end the scratch scope before the write barrier, thus
2954 // freeing the temporary registers so they can be used in `MarkGCCard`.
2955 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002956 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002957 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002958 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002959 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002960 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002961 destination = HeapOperand(temp,
2962 XRegisterFrom(index),
2963 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002965 }
2966
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2968 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2969 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2970
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002971 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002972 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002973 codegen_->AddSlowPath(slow_path);
2974 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002975 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002976 __ Cbnz(Register(value), &non_zero);
2977 if (!index.IsConstant()) {
2978 __ Add(temp, array, offset);
2979 }
Artem Serov914d7a82017-02-07 14:33:49 +00002980 {
2981 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2982 // emitted.
2983 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2984 __ Str(wzr, destination);
2985 codegen_->MaybeRecordImplicitNullCheck(instruction);
2986 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002987 __ B(&done);
2988 __ Bind(&non_zero);
2989 }
2990
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002991 // Note that when Baker read barriers are enabled, the type
2992 // checks are performed without read barriers. This is fine,
2993 // even in the case where a class object is in the from-space
2994 // after the flip, as a comparison involving such a type would
2995 // not produce a false positive; it may of course produce a
2996 // false negative, in which case we would take the ArraySet
2997 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002998
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002999 Register temp2 = temps.AcquireSameSizeAs(array);
3000 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003001 {
3002 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3003 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3004 __ Ldr(temp, HeapOperand(array, class_offset));
3005 codegen_->MaybeRecordImplicitNullCheck(instruction);
3006 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003007 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003008
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003009 // /* HeapReference<Class> */ temp = temp->component_type_
3010 __ Ldr(temp, HeapOperand(temp, component_offset));
3011 // /* HeapReference<Class> */ temp2 = value->klass_
3012 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3013 // If heap poisoning is enabled, no need to unpoison `temp`
3014 // nor `temp2`, as we are comparing two poisoned references.
3015 __ Cmp(temp, temp2);
3016 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003017
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003018 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3019 vixl::aarch64::Label do_put;
3020 __ B(eq, &do_put);
3021 // If heap poisoning is enabled, the `temp` reference has
3022 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003023 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3024
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003025 // /* HeapReference<Class> */ temp = temp->super_class_
3026 __ Ldr(temp, HeapOperand(temp, super_offset));
3027 // If heap poisoning is enabled, no need to unpoison
3028 // `temp`, as we are comparing against null below.
3029 __ Cbnz(temp, slow_path->GetEntryLabel());
3030 __ Bind(&do_put);
3031 } else {
3032 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003033 }
3034 }
3035
3036 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003037 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003038 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003039 __ Mov(temp2, value.W());
3040 GetAssembler()->PoisonHeapReference(temp2);
3041 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003042 }
3043
3044 if (!index.IsConstant()) {
3045 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003046 } else {
3047 // We no longer need the `temp` here so release it as the store below may
3048 // need a scratch register (if the constant index makes the offset too large)
3049 // and the poisoned `source` could be using the other scratch register.
3050 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003051 }
Artem Serov914d7a82017-02-07 14:33:49 +00003052 {
3053 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3054 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3055 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003056
Artem Serov914d7a82017-02-07 14:33:49 +00003057 if (!may_need_runtime_call_for_type_check) {
3058 codegen_->MaybeRecordImplicitNullCheck(instruction);
3059 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003060 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003061 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003062
3063 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3064
3065 if (done.IsLinked()) {
3066 __ Bind(&done);
3067 }
3068
3069 if (slow_path != nullptr) {
3070 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003071 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003072 }
3073}
3074
Alexandre Rames67555f72014-11-18 10:55:16 +00003075void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003076 RegisterSet caller_saves = RegisterSet::Empty();
3077 InvokeRuntimeCallingConvention calling_convention;
3078 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3079 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3080 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003081 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003082 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003083}
3084
3085void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003086 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003087 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003088 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003089 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3090 __ B(slow_path->GetEntryLabel(), hs);
3091}
3092
Alexandre Rames67555f72014-11-18 10:55:16 +00003093void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3094 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003095 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003096 locations->SetInAt(0, Location::RequiresRegister());
3097 if (check->HasUses()) {
3098 locations->SetOut(Location::SameAsFirstInput());
3099 }
3100}
3101
3102void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3103 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003104 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Alexandre Rames67555f72014-11-18 10:55:16 +00003105 check->GetLoadClass(), check, check->GetDexPc(), true);
3106 codegen_->AddSlowPath(slow_path);
3107 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3108}
3109
Roland Levillain1a653882016-03-18 18:05:57 +00003110static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3111 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3112 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3113}
3114
3115void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3116 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3117 Location rhs_loc = instruction->GetLocations()->InAt(1);
3118 if (rhs_loc.IsConstant()) {
3119 // 0.0 is the only immediate that can be encoded directly in
3120 // an FCMP instruction.
3121 //
3122 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3123 // specify that in a floating-point comparison, positive zero
3124 // and negative zero are considered equal, so we can use the
3125 // literal 0.0 for both cases here.
3126 //
3127 // Note however that some methods (Float.equal, Float.compare,
3128 // Float.compareTo, Double.equal, Double.compare,
3129 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3130 // StrictMath.min) consider 0.0 to be (strictly) greater than
3131 // -0.0. So if we ever translate calls to these methods into a
3132 // HCompare instruction, we must handle the -0.0 case with
3133 // care here.
3134 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3135 __ Fcmp(lhs_reg, 0.0);
3136 } else {
3137 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3138 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003139}
3140
Serban Constantinescu02164b32014-11-13 14:05:07 +00003141void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003143 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003144 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003145 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003147 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003150 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003151 case DataType::Type::kInt32:
3152 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003153 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003154 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003155 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3156 break;
3157 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003158 case DataType::Type::kFloat32:
3159 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003160 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003161 locations->SetInAt(1,
3162 IsFloatingPointZeroConstant(compare->InputAt(1))
3163 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3164 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003165 locations->SetOut(Location::RequiresRegister());
3166 break;
3167 }
3168 default:
3169 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3170 }
3171}
3172
3173void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003175
3176 // 0 if: left == right
3177 // 1 if: left > right
3178 // -1 if: left < right
3179 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003181 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003182 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003184 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003185 case DataType::Type::kInt32:
3186 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003187 Register result = OutputRegister(compare);
3188 Register left = InputRegisterAt(compare, 0);
3189 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003190 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003191 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3192 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003193 break;
3194 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kFloat32:
3196 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003197 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003198 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003199 __ Cset(result, ne);
3200 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003201 break;
3202 }
3203 default:
3204 LOG(FATAL) << "Unimplemented compare type " << in_type;
3205 }
3206}
3207
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003208void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003209 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003211 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003212 locations->SetInAt(0, Location::RequiresFpuRegister());
3213 locations->SetInAt(1,
3214 IsFloatingPointZeroConstant(instruction->InputAt(1))
3215 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3216 : Location::RequiresFpuRegister());
3217 } else {
3218 // Integer cases.
3219 locations->SetInAt(0, Location::RequiresRegister());
3220 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3221 }
3222
David Brazdilb3e773e2016-01-26 11:28:37 +00003223 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003225 }
3226}
3227
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003228void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003229 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003230 return;
3231 }
3232
3233 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003234 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003235 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003236
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003237 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003238 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003239 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003240 } else {
3241 // Integer cases.
3242 Register lhs = InputRegisterAt(instruction, 0);
3243 Operand rhs = InputOperandAt(instruction, 1);
3244 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003245 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003246 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003247}
3248
3249#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3250 M(Equal) \
3251 M(NotEqual) \
3252 M(LessThan) \
3253 M(LessThanOrEqual) \
3254 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003255 M(GreaterThanOrEqual) \
3256 M(Below) \
3257 M(BelowOrEqual) \
3258 M(Above) \
3259 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003260#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003261void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3262void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003263FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003264#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003265#undef FOR_EACH_CONDITION_INSTRUCTION
3266
Zheng Xuc6667102015-05-15 16:08:45 +08003267void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3268 DCHECK(instruction->IsDiv() || instruction->IsRem());
3269
3270 LocationSummary* locations = instruction->GetLocations();
3271 Location second = locations->InAt(1);
3272 DCHECK(second.IsConstant());
3273
3274 Register out = OutputRegister(instruction);
3275 Register dividend = InputRegisterAt(instruction, 0);
3276 int64_t imm = Int64FromConstant(second.GetConstant());
3277 DCHECK(imm == 1 || imm == -1);
3278
3279 if (instruction->IsRem()) {
3280 __ Mov(out, 0);
3281 } else {
3282 if (imm == 1) {
3283 __ Mov(out, dividend);
3284 } else {
3285 __ Neg(out, dividend);
3286 }
3287 }
3288}
3289
3290void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3291 DCHECK(instruction->IsDiv() || instruction->IsRem());
3292
3293 LocationSummary* locations = instruction->GetLocations();
3294 Location second = locations->InAt(1);
3295 DCHECK(second.IsConstant());
3296
3297 Register out = OutputRegister(instruction);
3298 Register dividend = InputRegisterAt(instruction, 0);
3299 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003300 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003301 int ctz_imm = CTZ(abs_imm);
3302
3303 UseScratchRegisterScope temps(GetVIXLAssembler());
3304 Register temp = temps.AcquireSameSizeAs(out);
3305
3306 if (instruction->IsDiv()) {
3307 __ Add(temp, dividend, abs_imm - 1);
3308 __ Cmp(dividend, 0);
3309 __ Csel(out, temp, dividend, lt);
3310 if (imm > 0) {
3311 __ Asr(out, out, ctz_imm);
3312 } else {
3313 __ Neg(out, Operand(out, ASR, ctz_imm));
3314 }
3315 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 int bits = instruction->GetResultType() == DataType::Type::kInt32 ? 32 : 64;
Zheng Xuc6667102015-05-15 16:08:45 +08003317 __ Asr(temp, dividend, bits - 1);
3318 __ Lsr(temp, temp, bits - ctz_imm);
3319 __ Add(out, dividend, temp);
3320 __ And(out, out, abs_imm - 1);
3321 __ Sub(out, out, temp);
3322 }
3323}
3324
3325void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3326 DCHECK(instruction->IsDiv() || instruction->IsRem());
3327
3328 LocationSummary* locations = instruction->GetLocations();
3329 Location second = locations->InAt(1);
3330 DCHECK(second.IsConstant());
3331
3332 Register out = OutputRegister(instruction);
3333 Register dividend = InputRegisterAt(instruction, 0);
3334 int64_t imm = Int64FromConstant(second.GetConstant());
3335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003336 DataType::Type type = instruction->GetResultType();
3337 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003338
3339 int64_t magic;
3340 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003341 CalculateMagicAndShiftForDivRem(
3342 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003343
3344 UseScratchRegisterScope temps(GetVIXLAssembler());
3345 Register temp = temps.AcquireSameSizeAs(out);
3346
3347 // temp = get_high(dividend * magic)
3348 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003349 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003350 __ Smulh(temp, dividend, temp);
3351 } else {
3352 __ Smull(temp.X(), dividend, temp);
3353 __ Lsr(temp.X(), temp.X(), 32);
3354 }
3355
3356 if (imm > 0 && magic < 0) {
3357 __ Add(temp, temp, dividend);
3358 } else if (imm < 0 && magic > 0) {
3359 __ Sub(temp, temp, dividend);
3360 }
3361
3362 if (shift != 0) {
3363 __ Asr(temp, temp, shift);
3364 }
3365
3366 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003367 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003368 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003369 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003370 // TODO: Strength reduction for msub.
3371 Register temp_imm = temps.AcquireSameSizeAs(out);
3372 __ Mov(temp_imm, imm);
3373 __ Msub(out, temp, temp_imm, dividend);
3374 }
3375}
3376
3377void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3378 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003379 DataType::Type type = instruction->GetResultType();
3380 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003381
3382 LocationSummary* locations = instruction->GetLocations();
3383 Register out = OutputRegister(instruction);
3384 Location second = locations->InAt(1);
3385
3386 if (second.IsConstant()) {
3387 int64_t imm = Int64FromConstant(second.GetConstant());
3388
3389 if (imm == 0) {
3390 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3391 } else if (imm == 1 || imm == -1) {
3392 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003393 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003394 DivRemByPowerOfTwo(instruction);
3395 } else {
3396 DCHECK(imm <= -2 || imm >= 2);
3397 GenerateDivRemWithAnyConstant(instruction);
3398 }
3399 } else {
3400 Register dividend = InputRegisterAt(instruction, 0);
3401 Register divisor = InputRegisterAt(instruction, 1);
3402 if (instruction->IsDiv()) {
3403 __ Sdiv(out, dividend, divisor);
3404 } else {
3405 UseScratchRegisterScope temps(GetVIXLAssembler());
3406 Register temp = temps.AcquireSameSizeAs(out);
3407 __ Sdiv(temp, dividend, divisor);
3408 __ Msub(out, temp, divisor, dividend);
3409 }
3410 }
3411}
3412
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003413void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3414 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003415 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003416 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kInt32:
3418 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003419 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003420 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3422 break;
3423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003424 case DataType::Type::kFloat32:
3425 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003426 locations->SetInAt(0, Location::RequiresFpuRegister());
3427 locations->SetInAt(1, Location::RequiresFpuRegister());
3428 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3429 break;
3430
3431 default:
3432 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3433 }
3434}
3435
3436void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003437 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003438 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003439 case DataType::Type::kInt32:
3440 case DataType::Type::kInt64:
Zheng Xuc6667102015-05-15 16:08:45 +08003441 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003442 break;
3443
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003444 case DataType::Type::kFloat32:
3445 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003446 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3447 break;
3448
3449 default:
3450 LOG(FATAL) << "Unexpected div type " << type;
3451 }
3452}
3453
Alexandre Rames67555f72014-11-18 10:55:16 +00003454void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003455 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003456 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003457}
3458
3459void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3460 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003461 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003462 codegen_->AddSlowPath(slow_path);
3463 Location value = instruction->GetLocations()->InAt(0);
3464
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003465 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003467 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003468 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003469 return;
3470 }
3471
Alexandre Rames67555f72014-11-18 10:55:16 +00003472 if (value.IsConstant()) {
3473 int64_t divisor = Int64ConstantFrom(value);
3474 if (divisor == 0) {
3475 __ B(slow_path->GetEntryLabel());
3476 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003477 // A division by a non-null constant is valid. We don't need to perform
3478 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003479 }
3480 } else {
3481 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3482 }
3483}
3484
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003485void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3486 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003487 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003488 locations->SetOut(Location::ConstantLocation(constant));
3489}
3490
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003491void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3492 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003493 // Will be generated at use site.
3494}
3495
Alexandre Rames5319def2014-10-23 10:03:10 +01003496void LocationsBuilderARM64::VisitExit(HExit* exit) {
3497 exit->SetLocations(nullptr);
3498}
3499
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003500void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003501}
3502
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003503void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3504 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003505 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003506 locations->SetOut(Location::ConstantLocation(constant));
3507}
3508
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003509void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003510 // Will be generated at use site.
3511}
3512
David Brazdilfc6a86a2015-06-26 10:33:45 +00003513void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003514 if (successor->IsExitBlock()) {
3515 DCHECK(got->GetPrevious()->AlwaysThrows());
3516 return; // no code needed
3517 }
3518
Serban Constantinescu02164b32014-11-13 14:05:07 +00003519 HBasicBlock* block = got->GetBlock();
3520 HInstruction* previous = got->GetPrevious();
3521 HLoopInformation* info = block->GetLoopInformation();
3522
David Brazdil46e2a392015-03-16 17:31:52 +00003523 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003524 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3525 return;
3526 }
3527 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3528 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003529 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003530 }
3531 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003532 __ B(codegen_->GetLabelOf(successor));
3533 }
3534}
3535
David Brazdilfc6a86a2015-06-26 10:33:45 +00003536void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3537 got->SetLocations(nullptr);
3538}
3539
3540void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3541 HandleGoto(got, got->GetSuccessor());
3542}
3543
3544void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3545 try_boundary->SetLocations(nullptr);
3546}
3547
3548void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3549 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3550 if (!successor->IsExitBlock()) {
3551 HandleGoto(try_boundary, successor);
3552 }
3553}
3554
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003555void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003556 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003557 vixl::aarch64::Label* true_target,
3558 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003559 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003560
David Brazdil0debae72015-11-12 18:37:00 +00003561 if (true_target == nullptr && false_target == nullptr) {
3562 // Nothing to do. The code always falls through.
3563 return;
3564 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003565 // Constant condition, statically compared against "true" (integer value 1).
3566 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003567 if (true_target != nullptr) {
3568 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003569 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003570 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003571 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003572 if (false_target != nullptr) {
3573 __ B(false_target);
3574 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003575 }
David Brazdil0debae72015-11-12 18:37:00 +00003576 return;
3577 }
3578
3579 // The following code generates these patterns:
3580 // (1) true_target == nullptr && false_target != nullptr
3581 // - opposite condition true => branch to false_target
3582 // (2) true_target != nullptr && false_target == nullptr
3583 // - condition true => branch to true_target
3584 // (3) true_target != nullptr && false_target != nullptr
3585 // - condition true => branch to true_target
3586 // - branch to false_target
3587 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003588 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003589 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003590 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003591 if (true_target == nullptr) {
3592 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3593 } else {
3594 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3595 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003596 } else {
3597 // The condition instruction has not been materialized, use its inputs as
3598 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003599 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003601 DataType::Type type = condition->InputAt(0)->GetType();
3602 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003603 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003604 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003605 IfCondition opposite_condition = condition->GetOppositeCondition();
3606 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003607 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003608 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003609 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003610 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003611 // Integer cases.
3612 Register lhs = InputRegisterAt(condition, 0);
3613 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003614
3615 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003616 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003617 if (true_target == nullptr) {
3618 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3619 non_fallthrough_target = false_target;
3620 } else {
3621 arm64_cond = ARM64Condition(condition->GetCondition());
3622 non_fallthrough_target = true_target;
3623 }
3624
Aart Bik086d27e2016-01-20 17:02:00 -08003625 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003626 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003627 switch (arm64_cond) {
3628 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003629 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003630 break;
3631 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003632 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003633 break;
3634 case lt:
3635 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003636 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003637 break;
3638 case ge:
3639 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003640 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003641 break;
3642 default:
3643 // Without the `static_cast` the compiler throws an error for
3644 // `-Werror=sign-promo`.
3645 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3646 }
3647 } else {
3648 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003649 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003650 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003651 }
3652 }
David Brazdil0debae72015-11-12 18:37:00 +00003653
3654 // If neither branch falls through (case 3), the conditional branch to `true_target`
3655 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3656 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003657 __ B(false_target);
3658 }
3659}
3660
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003661void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003662 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003663 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003664 locations->SetInAt(0, Location::RequiresRegister());
3665 }
3666}
3667
3668void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003669 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3670 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003671 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3672 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3673 true_target = nullptr;
3674 }
3675 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3676 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3677 false_target = nullptr;
3678 }
David Brazdil0debae72015-11-12 18:37:00 +00003679 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003680}
3681
3682void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003683 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003684 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003685 InvokeRuntimeCallingConvention calling_convention;
3686 RegisterSet caller_saves = RegisterSet::Empty();
3687 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3688 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003689 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003690 locations->SetInAt(0, Location::RequiresRegister());
3691 }
3692}
3693
3694void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003695 SlowPathCodeARM64* slow_path =
3696 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003697 GenerateTestAndBranch(deoptimize,
3698 /* condition_input_index */ 0,
3699 slow_path->GetEntryLabel(),
3700 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003701}
3702
Mingyao Yang063fc772016-08-02 11:02:54 -07003703void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003704 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003705 LocationSummary(flag, LocationSummary::kNoCall);
3706 locations->SetOut(Location::RequiresRegister());
3707}
3708
3709void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3710 __ Ldr(OutputRegister(flag),
3711 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3712}
3713
David Brazdilc0b601b2016-02-08 14:20:45 +00003714static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3715 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003716 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003717}
3718
Alexandre Rames880f1192016-06-13 16:04:50 +01003719static inline Condition GetConditionForSelect(HCondition* condition) {
3720 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003721 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3722 : ARM64Condition(cond);
3723}
3724
David Brazdil74eb1b22015-12-14 11:44:01 +00003725void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003726 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003728 locations->SetInAt(0, Location::RequiresFpuRegister());
3729 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003730 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003731 } else {
3732 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3733 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3734 bool is_true_value_constant = cst_true_value != nullptr;
3735 bool is_false_value_constant = cst_false_value != nullptr;
3736 // Ask VIXL whether we should synthesize constants in registers.
3737 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3738 Operand true_op = is_true_value_constant ?
3739 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3740 Operand false_op = is_false_value_constant ?
3741 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3742 bool true_value_in_register = false;
3743 bool false_value_in_register = false;
3744 MacroAssembler::GetCselSynthesisInformation(
3745 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3746 true_value_in_register |= !is_true_value_constant;
3747 false_value_in_register |= !is_false_value_constant;
3748
3749 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3750 : Location::ConstantLocation(cst_true_value));
3751 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3752 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003754 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003755
David Brazdil74eb1b22015-12-14 11:44:01 +00003756 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3757 locations->SetInAt(2, Location::RequiresRegister());
3758 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003759}
3760
3761void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003762 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003763 Condition csel_cond;
3764
3765 if (IsBooleanValueOrMaterializedCondition(cond)) {
3766 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003767 // Use the condition flags set by the previous instruction.
3768 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003769 } else {
3770 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003771 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003772 }
3773 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003774 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003775 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003776 } else {
3777 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003778 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003779 }
3780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003781 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003782 __ Fcsel(OutputFPRegister(select),
3783 InputFPRegisterAt(select, 1),
3784 InputFPRegisterAt(select, 0),
3785 csel_cond);
3786 } else {
3787 __ Csel(OutputRegister(select),
3788 InputOperandAt(select, 1),
3789 InputOperandAt(select, 0),
3790 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003791 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003792}
3793
David Srbecky0cf44932015-12-09 14:09:59 +00003794void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003795 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003796}
3797
David Srbeckyd28f4a02016-03-14 17:14:24 +00003798void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3799 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003800}
3801
3802void CodeGeneratorARM64::GenerateNop() {
3803 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003804}
3805
Alexandre Rames5319def2014-10-23 10:03:10 +01003806void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003807 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003808}
3809
3810void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003811 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003812}
3813
3814void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003815 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003816}
3817
3818void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003819 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003820}
3821
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003822// Temp is used for read barrier.
3823static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3824 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003825 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003826 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3827 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3828 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3829 return 1;
3830 }
3831 return 0;
3832}
3833
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003834// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003835// interface pointer, one for loading the current interface.
3836// The other checks have one temp for loading the object's class.
3837static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3838 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3839 return 3;
3840 }
3841 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003842}
3843
Alexandre Rames67555f72014-11-18 10:55:16 +00003844void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003845 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003846 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003847 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003848 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003849 case TypeCheckKind::kExactCheck:
3850 case TypeCheckKind::kAbstractClassCheck:
3851 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003852 case TypeCheckKind::kArrayObjectCheck: {
3853 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3854 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3855 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003856 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003857 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003858 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003859 case TypeCheckKind::kUnresolvedCheck:
3860 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003861 call_kind = LocationSummary::kCallOnSlowPath;
3862 break;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003863 case TypeCheckKind::kBitstringCheck:
3864 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003865 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003866
Vladimir Markoca6fff82017-10-03 14:49:14 +01003867 LocationSummary* locations =
3868 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003869 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003870 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003871 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003872 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003873 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3874 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3875 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3876 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3877 } else {
3878 locations->SetInAt(1, Location::RequiresRegister());
3879 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003880 // The "out" register is used as a temporary, so it overlaps with the inputs.
3881 // Note that TypeCheckSlowPathARM64 uses this register too.
3882 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003883 // Add temps if necessary for read barriers.
3884 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003885}
3886
3887void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003888 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003889 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003890 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003891 Register obj = InputRegisterAt(instruction, 0);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003892 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3893 ? Register()
3894 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003895 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003896 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003897 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3898 DCHECK_LE(num_temps, 1u);
3899 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003900 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3901 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3902 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3903 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003904
Scott Wakeling97c72b72016-06-24 16:19:36 +01003905 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003906 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003907
3908 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003909 // Avoid null check if we know `obj` is not null.
3910 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003911 __ Cbz(obj, &zero);
3912 }
3913
Roland Levillain44015862016-01-22 11:47:17 +00003914 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003915 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003916 ReadBarrierOption read_barrier_option =
3917 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003918 // /* HeapReference<Class> */ out = obj->klass_
3919 GenerateReferenceLoadTwoRegisters(instruction,
3920 out_loc,
3921 obj_loc,
3922 class_offset,
3923 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003924 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003925 __ Cmp(out, cls);
3926 __ Cset(out, eq);
3927 if (zero.IsLinked()) {
3928 __ B(&done);
3929 }
3930 break;
3931 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003934 ReadBarrierOption read_barrier_option =
3935 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003936 // /* HeapReference<Class> */ out = obj->klass_
3937 GenerateReferenceLoadTwoRegisters(instruction,
3938 out_loc,
3939 obj_loc,
3940 class_offset,
3941 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003942 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 // If the class is abstract, we eagerly fetch the super class of the
3944 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003945 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003946 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003947 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003948 GenerateReferenceLoadOneRegister(instruction,
3949 out_loc,
3950 super_offset,
3951 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003952 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003953 // If `out` is null, we use it for the result, and jump to `done`.
3954 __ Cbz(out, &done);
3955 __ Cmp(out, cls);
3956 __ B(ne, &loop);
3957 __ Mov(out, 1);
3958 if (zero.IsLinked()) {
3959 __ B(&done);
3960 }
3961 break;
3962 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003963
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003964 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003965 ReadBarrierOption read_barrier_option =
3966 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003967 // /* HeapReference<Class> */ out = obj->klass_
3968 GenerateReferenceLoadTwoRegisters(instruction,
3969 out_loc,
3970 obj_loc,
3971 class_offset,
3972 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003973 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003974 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003975 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003976 __ Bind(&loop);
3977 __ Cmp(out, cls);
3978 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003979 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003980 GenerateReferenceLoadOneRegister(instruction,
3981 out_loc,
3982 super_offset,
3983 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003984 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003985 __ Cbnz(out, &loop);
3986 // If `out` is null, we use it for the result, and jump to `done`.
3987 __ B(&done);
3988 __ Bind(&success);
3989 __ Mov(out, 1);
3990 if (zero.IsLinked()) {
3991 __ B(&done);
3992 }
3993 break;
3994 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003995
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003997 ReadBarrierOption read_barrier_option =
3998 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003999 // /* HeapReference<Class> */ out = obj->klass_
4000 GenerateReferenceLoadTwoRegisters(instruction,
4001 out_loc,
4002 obj_loc,
4003 class_offset,
4004 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004005 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004006 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004007 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004008 __ Cmp(out, cls);
4009 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004010 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004011 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004012 GenerateReferenceLoadOneRegister(instruction,
4013 out_loc,
4014 component_offset,
4015 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004016 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004017 // If `out` is null, we use it for the result, and jump to `done`.
4018 __ Cbz(out, &done);
4019 __ Ldrh(out, HeapOperand(out, primitive_offset));
4020 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4021 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004022 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004023 __ Mov(out, 1);
4024 __ B(&done);
4025 break;
4026 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004027
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004028 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004029 // No read barrier since the slow path will retry upon failure.
4030 // /* HeapReference<Class> */ out = obj->klass_
4031 GenerateReferenceLoadTwoRegisters(instruction,
4032 out_loc,
4033 obj_loc,
4034 class_offset,
4035 maybe_temp_loc,
4036 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 __ Cmp(out, cls);
4038 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004039 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4040 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004041 codegen_->AddSlowPath(slow_path);
4042 __ B(ne, slow_path->GetEntryLabel());
4043 __ Mov(out, 1);
4044 if (zero.IsLinked()) {
4045 __ B(&done);
4046 }
4047 break;
4048 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049
Calin Juravle98893e12015-10-02 21:05:03 +01004050 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 case TypeCheckKind::kInterfaceCheck: {
4052 // Note that we indeed only call on slow path, but we always go
4053 // into the slow path for the unresolved and interface check
4054 // cases.
4055 //
4056 // We cannot directly call the InstanceofNonTrivial runtime
4057 // entry point without resorting to a type checking slow path
4058 // here (i.e. by calling InvokeRuntime directly), as it would
4059 // require to assign fixed registers for the inputs of this
4060 // HInstanceOf instruction (following the runtime calling
4061 // convention), which might be cluttered by the potential first
4062 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004063 //
4064 // TODO: Introduce a new runtime entry point taking the object
4065 // to test (instead of its class) as argument, and let it deal
4066 // with the read barrier issues. This will let us refactor this
4067 // case of the `switch` code as it was previously (with a direct
4068 // call to the runtime not using a type checking slow path).
4069 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004071 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4072 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073 codegen_->AddSlowPath(slow_path);
4074 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004075 if (zero.IsLinked()) {
4076 __ B(&done);
4077 }
4078 break;
4079 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00004080
4081 case TypeCheckKind::kBitstringCheck: {
4082 // /* HeapReference<Class> */ temp = obj->klass_
4083 GenerateReferenceLoadTwoRegisters(instruction,
4084 out_loc,
4085 obj_loc,
4086 class_offset,
4087 maybe_temp_loc,
4088 kWithoutReadBarrier);
4089
4090 GenerateBitstringTypeCheckCompare(instruction, out);
4091 __ Cset(out, eq);
4092 if (zero.IsLinked()) {
4093 __ B(&done);
4094 }
4095 break;
4096 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004097 }
4098
4099 if (zero.IsLinked()) {
4100 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004101 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004102 }
4103
4104 if (done.IsLinked()) {
4105 __ Bind(&done);
4106 }
4107
4108 if (slow_path != nullptr) {
4109 __ Bind(slow_path->GetExitLabel());
4110 }
4111}
4112
4113void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004114 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004115 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004116 LocationSummary* locations =
4117 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00004119 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4120 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4121 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4122 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4123 } else {
4124 locations->SetInAt(1, Location::RequiresRegister());
4125 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004126 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4127 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004128}
4129
4130void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004131 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004132 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004133 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004134 Register obj = InputRegisterAt(instruction, 0);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00004135 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4136 ? Register()
4137 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004138 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4139 DCHECK_GE(num_temps, 1u);
4140 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004141 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004142 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4143 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004144 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004145 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4146 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4147 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4148 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4149 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4150 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4151 const uint32_t object_array_data_offset =
4152 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004153
Vladimir Marko87584542017-12-12 17:47:52 +00004154 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004155 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004156 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4157 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004158 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004159
Scott Wakeling97c72b72016-06-24 16:19:36 +01004160 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004161 // Avoid null check if we know obj is not null.
4162 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004163 __ Cbz(obj, &done);
4164 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004165
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004166 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004167 case TypeCheckKind::kExactCheck:
4168 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004169 // /* HeapReference<Class> */ temp = obj->klass_
4170 GenerateReferenceLoadTwoRegisters(instruction,
4171 temp_loc,
4172 obj_loc,
4173 class_offset,
4174 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004175 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004176
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004177 __ Cmp(temp, cls);
4178 // Jump to slow path for throwing the exception or doing a
4179 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004180 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004181 break;
4182 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004184 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004185 // /* HeapReference<Class> */ temp = obj->klass_
4186 GenerateReferenceLoadTwoRegisters(instruction,
4187 temp_loc,
4188 obj_loc,
4189 class_offset,
4190 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004191 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004192
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004193 // If the class is abstract, we eagerly fetch the super class of the
4194 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004195 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004196 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004197 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004198 GenerateReferenceLoadOneRegister(instruction,
4199 temp_loc,
4200 super_offset,
4201 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004202 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004203
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004204 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4205 // exception.
4206 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4207 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004208 __ Cmp(temp, cls);
4209 __ B(ne, &loop);
4210 break;
4211 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004213 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004214 // /* HeapReference<Class> */ temp = obj->klass_
4215 GenerateReferenceLoadTwoRegisters(instruction,
4216 temp_loc,
4217 obj_loc,
4218 class_offset,
4219 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004220 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004223 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004224 __ Bind(&loop);
4225 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004226 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004228 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004229 GenerateReferenceLoadOneRegister(instruction,
4230 temp_loc,
4231 super_offset,
4232 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004233 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004234
4235 // If the class reference currently in `temp` is not null, jump
4236 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004237 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004239 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004240 break;
4241 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004243 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004244 // /* HeapReference<Class> */ temp = obj->klass_
4245 GenerateReferenceLoadTwoRegisters(instruction,
4246 temp_loc,
4247 obj_loc,
4248 class_offset,
4249 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004250 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004251
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004252 // Do an exact check.
4253 __ Cmp(temp, cls);
4254 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004255
4256 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004257 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004258 GenerateReferenceLoadOneRegister(instruction,
4259 temp_loc,
4260 component_offset,
4261 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004262 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004263
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004264 // If the component type is null, jump to the slow path to throw the exception.
4265 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4266 // Otherwise, the object is indeed an array. Further check that this component type is not a
4267 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004268 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4269 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004270 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004271 break;
4272 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004273
Calin Juravle98893e12015-10-02 21:05:03 +01004274 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004275 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004276 //
4277 // We cannot directly call the CheckCast runtime entry point
4278 // without resorting to a type checking slow path here (i.e. by
4279 // calling InvokeRuntime directly), as it would require to
4280 // assign fixed registers for the inputs of this HInstanceOf
4281 // instruction (following the runtime calling convention), which
4282 // might be cluttered by the potential first read barrier
4283 // emission at the beginning of this method.
4284 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004285 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004286 case TypeCheckKind::kInterfaceCheck: {
4287 // /* HeapReference<Class> */ temp = obj->klass_
4288 GenerateReferenceLoadTwoRegisters(instruction,
4289 temp_loc,
4290 obj_loc,
4291 class_offset,
4292 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004293 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294
4295 // /* HeapReference<Class> */ temp = temp->iftable_
4296 GenerateReferenceLoadTwoRegisters(instruction,
4297 temp_loc,
4298 temp_loc,
4299 iftable_offset,
4300 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004301 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004302 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004303 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004304 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004305 vixl::aarch64::Label start_loop;
4306 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004307 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004308 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4309 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004310 // Go to next interface.
4311 __ Add(temp, temp, 2 * kHeapReferenceSize);
4312 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004313 // Compare the classes and continue the loop if they do not match.
4314 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4315 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004316 break;
4317 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00004318
4319 case TypeCheckKind::kBitstringCheck: {
4320 // /* HeapReference<Class> */ temp = obj->klass_
4321 GenerateReferenceLoadTwoRegisters(instruction,
4322 temp_loc,
4323 obj_loc,
4324 class_offset,
4325 maybe_temp2_loc,
4326 kWithoutReadBarrier);
4327
4328 GenerateBitstringTypeCheckCompare(instruction, temp);
4329 __ B(ne, type_check_slow_path->GetEntryLabel());
4330 break;
4331 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004332 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004333 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004334
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004335 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004336}
4337
Alexandre Rames5319def2014-10-23 10:03:10 +01004338void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004339 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004340 locations->SetOut(Location::ConstantLocation(constant));
4341}
4342
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004343void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004344 // Will be generated at use site.
4345}
4346
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004347void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004348 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004349 locations->SetOut(Location::ConstantLocation(constant));
4350}
4351
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004352void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004353 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004354}
4355
Calin Juravle175dc732015-08-25 15:42:32 +01004356void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4357 // The trampoline uses the same calling convention as dex calling conventions,
4358 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4359 // the method_idx.
4360 HandleInvoke(invoke);
4361}
4362
4363void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4364 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004365 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004366}
4367
Alexandre Rames5319def2014-10-23 10:03:10 +01004368void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004369 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004370 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004371}
4372
Alexandre Rames67555f72014-11-18 10:55:16 +00004373void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4374 HandleInvoke(invoke);
4375}
4376
4377void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4378 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004379 LocationSummary* locations = invoke->GetLocations();
4380 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004381 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004382 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004383 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004384
4385 // The register ip1 is required to be used for the hidden argument in
4386 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004387 MacroAssembler* masm = GetVIXLAssembler();
4388 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004389 scratch_scope.Exclude(ip1);
4390 __ Mov(ip1, invoke->GetDexMethodIndex());
4391
Artem Serov914d7a82017-02-07 14:33:49 +00004392 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004393 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004394 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004395 {
4396 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4397 // /* HeapReference<Class> */ temp = temp->klass_
4398 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4399 codegen_->MaybeRecordImplicitNullCheck(invoke);
4400 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004401 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004402 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004403 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004404 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004405 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004406 }
Artem Serov914d7a82017-02-07 14:33:49 +00004407
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004408 // Instead of simply (possibly) unpoisoning `temp` here, we should
4409 // emit a read barrier for the previous class reference load.
4410 // However this is not required in practice, as this is an
4411 // intermediate/temporary reference and because the current
4412 // concurrent copying collector keeps the from-space memory
4413 // intact/accessible until the end of the marking phase (the
4414 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004415 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004416 __ Ldr(temp,
4417 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4418 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004419 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004420 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004421 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004422 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004423 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004424
4425 {
4426 // Ensure the pc position is recorded immediately after the `blr` instruction.
4427 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4428
4429 // lr();
4430 __ blr(lr);
4431 DCHECK(!codegen_->IsLeafMethod());
4432 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4433 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004434
4435 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004436}
4437
4438void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004439 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004440 if (intrinsic.TryDispatch(invoke)) {
4441 return;
4442 }
4443
Alexandre Rames67555f72014-11-18 10:55:16 +00004444 HandleInvoke(invoke);
4445}
4446
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004447void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004448 // Explicit clinit checks triggered by static invokes must have been pruned by
4449 // art::PrepareForRegisterAllocation.
4450 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004451
Vladimir Markoca6fff82017-10-03 14:49:14 +01004452 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004453 if (intrinsic.TryDispatch(invoke)) {
4454 return;
4455 }
4456
Alexandre Rames67555f72014-11-18 10:55:16 +00004457 HandleInvoke(invoke);
4458}
4459
Andreas Gampe878d58c2015-01-15 23:24:00 -08004460static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4461 if (invoke->GetLocations()->Intrinsified()) {
4462 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4463 intrinsic.Dispatch(invoke);
4464 return true;
4465 }
4466 return false;
4467}
4468
Vladimir Markodc151b22015-10-15 18:02:30 +01004469HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4470 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004471 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004472 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004473 return desired_dispatch_info;
4474}
4475
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004476void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4477 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004478 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004479 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4480 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004481 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4482 uint32_t offset =
4483 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004484 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004485 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004486 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004487 }
Vladimir Marko58155012015-08-19 12:49:41 +00004488 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004489 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004490 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004491 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4492 DCHECK(GetCompilerOptions().IsBootImage());
4493 // Add ADRP with its PC-relative method patch.
4494 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4495 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4496 // Add ADD with its PC-relative method patch.
4497 vixl::aarch64::Label* add_label =
4498 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4499 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4500 break;
4501 }
Vladimir Marko58155012015-08-19 12:49:41 +00004502 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4503 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004504 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004505 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004506 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004507 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004508 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4509 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004510 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004511 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004512 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004513 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004514 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004515 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004516 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004517 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4518 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4519 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004520 }
4521 }
4522
4523 switch (invoke->GetCodePtrLocation()) {
4524 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004525 {
4526 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4527 ExactAssemblyScope eas(GetVIXLAssembler(),
4528 kInstructionSize,
4529 CodeBufferCheckScope::kExactSize);
4530 __ bl(&frame_entry_label_);
4531 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4532 }
Vladimir Marko58155012015-08-19 12:49:41 +00004533 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004534 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4535 // LR = callee_method->entry_point_from_quick_compiled_code_;
4536 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004537 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004538 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004539 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004540 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004541 ExactAssemblyScope eas(GetVIXLAssembler(),
4542 kInstructionSize,
4543 CodeBufferCheckScope::kExactSize);
4544 // lr()
4545 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004546 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004547 }
Vladimir Marko58155012015-08-19 12:49:41 +00004548 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004549 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004550
Andreas Gampe878d58c2015-01-15 23:24:00 -08004551 DCHECK(!IsLeafMethod());
4552}
4553
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004554void CodeGeneratorARM64::GenerateVirtualCall(
4555 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004556 // Use the calling convention instead of the location of the receiver, as
4557 // intrinsics may have put the receiver in a different register. In the intrinsics
4558 // slow path, the arguments have been moved to the right place, so here we are
4559 // guaranteed that the receiver is the first register of the calling convention.
4560 InvokeDexCallingConvention calling_convention;
4561 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004562 Register temp = XRegisterFrom(temp_in);
4563 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4564 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4565 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004566 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004567
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004568 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004569
4570 {
4571 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4572 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4573 // /* HeapReference<Class> */ temp = receiver->klass_
4574 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4575 MaybeRecordImplicitNullCheck(invoke);
4576 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004577 // Instead of simply (possibly) unpoisoning `temp` here, we should
4578 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004579 // intermediate/temporary reference and because the current
4580 // concurrent copying collector keeps the from-space memory
4581 // intact/accessible until the end of the marking phase (the
4582 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004583 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4584 // temp = temp->GetMethodAt(method_offset);
4585 __ Ldr(temp, MemOperand(temp, method_offset));
4586 // lr = temp->GetEntryPoint();
4587 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004588 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004589 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004590 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4591 // lr();
4592 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004593 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004594 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004595}
4596
Orion Hodsonac141392017-01-13 11:53:47 +00004597void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4598 HandleInvoke(invoke);
4599}
4600
4601void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4602 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004603 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004604}
4605
Vladimir Marko65979462017-05-19 17:25:12 +01004606vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4607 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004608 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004609 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004610 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01004611 adrp_label,
4612 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004613}
4614
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004615vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4616 MethodReference target_method,
4617 vixl::aarch64::Label* adrp_label) {
4618 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004619 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004620 adrp_label,
4621 &method_bss_entry_patches_);
4622}
4623
Scott Wakeling97c72b72016-06-24 16:19:36 +01004624vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4625 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004626 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004627 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004628 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004629}
4630
Vladimir Marko1998cd02017-01-13 13:02:58 +00004631vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4632 const DexFile& dex_file,
4633 dex::TypeIndex type_index,
4634 vixl::aarch64::Label* adrp_label) {
4635 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4636}
4637
Vladimir Marko65979462017-05-19 17:25:12 +01004638vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4639 const DexFile& dex_file,
4640 dex::StringIndex string_index,
4641 vixl::aarch64::Label* adrp_label) {
4642 return
4643 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4644}
4645
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004646vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4647 const DexFile& dex_file,
4648 dex::StringIndex string_index,
4649 vixl::aarch64::Label* adrp_label) {
4650 return NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
4651}
4652
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004653vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4654 baker_read_barrier_patches_.emplace_back(custom_data);
4655 return &baker_read_barrier_patches_.back().label;
4656}
4657
Scott Wakeling97c72b72016-06-24 16:19:36 +01004658vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4659 const DexFile& dex_file,
4660 uint32_t offset_or_index,
4661 vixl::aarch64::Label* adrp_label,
4662 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004663 // Add a patch entry and return the label.
4664 patches->emplace_back(dex_file, offset_or_index);
4665 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004666 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004667 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4668 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4669 return label;
4670}
4671
Scott Wakeling97c72b72016-06-24 16:19:36 +01004672vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4673 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004674 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004675}
4676
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004677vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004678 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004679 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004680 return jit_string_patches_.GetOrCreate(
4681 StringReference(&dex_file, string_index),
4682 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4683}
4684
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004685vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004686 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004687 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004688 return jit_class_patches_.GetOrCreate(
4689 TypeReference(&dex_file, type_index),
4690 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4691}
4692
Vladimir Markoaad75c62016-10-03 08:46:48 +00004693void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4694 vixl::aarch64::Register reg) {
4695 DCHECK(reg.IsX());
4696 SingleEmissionCheckScope guard(GetVIXLAssembler());
4697 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004698 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004699}
4700
4701void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4702 vixl::aarch64::Register out,
4703 vixl::aarch64::Register base) {
4704 DCHECK(out.IsX());
4705 DCHECK(base.IsX());
4706 SingleEmissionCheckScope guard(GetVIXLAssembler());
4707 __ Bind(fixup_label);
4708 __ add(out, base, Operand(/* offset placeholder */ 0));
4709}
4710
4711void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4712 vixl::aarch64::Register out,
4713 vixl::aarch64::Register base) {
4714 DCHECK(base.IsX());
4715 SingleEmissionCheckScope guard(GetVIXLAssembler());
4716 __ Bind(fixup_label);
4717 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4718}
4719
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004720template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004721inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4722 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004723 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004724 for (const PcRelativePatchInfo& info : infos) {
4725 linker_patches->push_back(Factory(info.label.GetLocation(),
4726 &info.target_dex_file,
4727 info.pc_insn_label->GetLocation(),
4728 info.offset_or_index));
4729 }
4730}
4731
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004732void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004733 DCHECK(linker_patches->empty());
4734 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004735 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004736 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004737 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004738 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004739 pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004740 string_bss_entry_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004741 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004742 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004743 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004744 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4745 pc_relative_method_patches_, linker_patches);
4746 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4747 pc_relative_type_patches_, linker_patches);
4748 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4749 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004750 } else {
4751 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004752 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4753 pc_relative_type_patches_, linker_patches);
4754 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4755 pc_relative_string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004756 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004757 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4758 method_bss_entry_patches_, linker_patches);
4759 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4760 type_bss_entry_patches_, linker_patches);
4761 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4762 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004763 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004764 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4765 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004766 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004767 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004768}
4769
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004770vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4771 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004772 value,
4773 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4774}
4775
Scott Wakeling97c72b72016-06-24 16:19:36 +01004776vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004777 return uint64_literals_.GetOrCreate(
4778 value,
4779 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004780}
4781
Andreas Gampe878d58c2015-01-15 23:24:00 -08004782void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004783 // Explicit clinit checks triggered by static invokes must have been pruned by
4784 // art::PrepareForRegisterAllocation.
4785 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004786
Andreas Gampe878d58c2015-01-15 23:24:00 -08004787 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004788 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004789 return;
4790 }
4791
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004792 {
4793 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4794 // are no pools emitted.
4795 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4796 LocationSummary* locations = invoke->GetLocations();
4797 codegen_->GenerateStaticOrDirectCall(
4798 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4799 }
4800
4801 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004802}
4803
4804void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004805 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004806 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004807 return;
4808 }
4809
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004810 {
4811 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4812 // are no pools emitted.
4813 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4814 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4815 DCHECK(!codegen_->IsLeafMethod());
4816 }
4817
4818 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004819}
4820
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004821HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4822 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004823 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004824 case HLoadClass::LoadKind::kInvalid:
4825 LOG(FATAL) << "UNREACHABLE";
4826 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004827 case HLoadClass::LoadKind::kReferrersClass:
4828 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004829 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004830 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004831 case HLoadClass::LoadKind::kBssEntry:
4832 DCHECK(!Runtime::Current()->UseJitCompilation());
4833 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004834 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004835 DCHECK(Runtime::Current()->UseJitCompilation());
4836 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004837 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004838 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004839 break;
4840 }
4841 return desired_class_load_kind;
4842}
4843
Alexandre Rames67555f72014-11-18 10:55:16 +00004844void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004845 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004846 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004847 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004848 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004849 cls,
4850 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004851 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004852 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004853 return;
4854 }
Vladimir Marko41559982017-01-06 14:04:23 +00004855 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004856
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004857 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4858 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004859 ? LocationSummary::kCallOnSlowPath
4860 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004861 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004862 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004863 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004864 }
4865
Vladimir Marko41559982017-01-06 14:04:23 +00004866 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004867 locations->SetInAt(0, Location::RequiresRegister());
4868 }
4869 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004870 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4871 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4872 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00004873 RegisterSet caller_saves = RegisterSet::Empty();
4874 InvokeRuntimeCallingConvention calling_convention;
4875 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4876 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004877 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
4878 DataType::Type::kReference).GetCode());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004879 locations->SetCustomSlowPathCallerSaves(caller_saves);
4880 } else {
4881 // For non-Baker read barrier we have a temp-clobbering call.
4882 }
4883 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004884}
4885
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004886// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4887// move.
4888void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004889 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004890 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004891 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004892 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004893 return;
4894 }
Vladimir Marko41559982017-01-06 14:04:23 +00004895 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004896
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004897 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004898 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004899
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004900 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4901 ? kWithoutReadBarrier
4902 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004903 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004904 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004905 case HLoadClass::LoadKind::kReferrersClass: {
4906 DCHECK(!cls->CanCallRuntime());
4907 DCHECK(!cls->MustGenerateClinitCheck());
4908 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4909 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004910 GenerateGcRootFieldLoad(cls,
4911 out_loc,
4912 current_method,
4913 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004914 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004915 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004916 break;
4917 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004918 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004919 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004920 // Add ADRP with its PC-relative type patch.
4921 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004922 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004923 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004924 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004925 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004926 vixl::aarch64::Label* add_label =
4927 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004928 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004929 break;
4930 }
4931 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004932 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004933 uint32_t address = dchecked_integral_cast<uint32_t>(
4934 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4935 DCHECK_NE(address, 0u);
4936 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004937 break;
4938 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004939 case HLoadClass::LoadKind::kBootImageClassTable: {
4940 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4941 // Add ADRP with its PC-relative type patch.
4942 const DexFile& dex_file = cls->GetDexFile();
4943 dex::TypeIndex type_index = cls->GetTypeIndex();
4944 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
4945 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4946 // Add LDR with its PC-relative type patch.
4947 vixl::aarch64::Label* ldr_label =
4948 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
4949 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4950 // Extract the reference from the slot data, i.e. clear the hash bits.
4951 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
4952 ComputeModifiedUtf8Hash(dex_file.StringByTypeIdx(type_index)));
4953 if (masked_hash != 0) {
4954 __ Sub(out.W(), out.W(), Operand(masked_hash));
4955 }
4956 break;
4957 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004958 case HLoadClass::LoadKind::kBssEntry: {
4959 // Add ADRP with its PC-relative Class .bss entry patch.
4960 const DexFile& dex_file = cls->GetDexFile();
4961 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004962 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4963 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4964 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004965 // Add LDR with its PC-relative Class patch.
4966 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004967 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004968 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4969 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004970 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00004971 temp,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004972 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004973 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004974 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004975 generate_null_check = true;
4976 break;
4977 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004978 case HLoadClass::LoadKind::kJitTableAddress: {
4979 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4980 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004981 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004982 GenerateGcRootFieldLoad(cls,
4983 out_loc,
4984 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004985 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004986 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004987 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004988 break;
4989 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004990 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004991 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004992 LOG(FATAL) << "UNREACHABLE";
4993 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004994 }
4995
Vladimir Markoea4c1262017-02-06 19:59:33 +00004996 bool do_clinit = cls->MustGenerateClinitCheck();
4997 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004998 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004999 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00005000 cls, cls, cls->GetDexPc(), do_clinit);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005001 codegen_->AddSlowPath(slow_path);
5002 if (generate_null_check) {
5003 __ Cbz(out, slow_path->GetEntryLabel());
5004 }
5005 if (cls->MustGenerateClinitCheck()) {
5006 GenerateClassInitializationCheck(slow_path, out);
5007 } else {
5008 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005009 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005010 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005011 }
5012}
5013
David Brazdilcb1c0552015-08-04 16:22:25 +01005014static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005015 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005016}
5017
Alexandre Rames67555f72014-11-18 10:55:16 +00005018void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5019 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005020 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005021 locations->SetOut(Location::RequiresRegister());
5022}
5023
5024void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005025 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5026}
5027
5028void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005029 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005030}
5031
5032void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5033 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005034}
5035
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005036HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5037 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005038 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005039 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005040 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005041 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005042 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005043 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005044 case HLoadString::LoadKind::kJitTableAddress:
5045 DCHECK(Runtime::Current()->UseJitCompilation());
5046 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005047 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005048 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005049 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005050 }
5051 return desired_string_load_kind;
5052}
5053
Alexandre Rames67555f72014-11-18 10:55:16 +00005054void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005055 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005056 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005057 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005058 InvokeRuntimeCallingConvention calling_convention;
5059 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5060 } else {
5061 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005062 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5063 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005064 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005065 RegisterSet caller_saves = RegisterSet::Empty();
5066 InvokeRuntimeCallingConvention calling_convention;
5067 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5068 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005069 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5070 DataType::Type::kReference).GetCode());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005071 locations->SetCustomSlowPathCallerSaves(caller_saves);
5072 } else {
5073 // For non-Baker read barrier we have a temp-clobbering call.
5074 }
5075 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005077}
5078
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005079// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5080// move.
5081void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005082 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005083 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005084
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005085 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005086 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005087 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 // Add ADRP with its PC-relative String patch.
5089 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005090 const dex::StringIndex string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005091 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005092 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005093 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005094 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005095 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005096 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005097 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005098 }
5099 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005100 uint32_t address = dchecked_integral_cast<uint32_t>(
5101 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5102 DCHECK_NE(address, 0u);
5103 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005104 return;
5105 }
5106 case HLoadString::LoadKind::kBootImageInternTable: {
5107 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5108 // Add ADRP with its PC-relative String patch.
5109 const DexFile& dex_file = load->GetDexFile();
5110 const dex::StringIndex string_index = load->GetStringIndex();
5111 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
5112 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
5113 // Add LDR with its PC-relative String patch.
5114 vixl::aarch64::Label* ldr_label =
5115 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
5116 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5117 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005118 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005119 case HLoadString::LoadKind::kBssEntry: {
5120 // Add ADRP with its PC-relative String .bss entry patch.
5121 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005122 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005123 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof3c52b42017-11-17 17:32:12 +00005124 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005125 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005126 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005127 // Add LDR with its .bss entry String patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005128 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005129 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005130 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005131 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005132 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005133 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005134 /* offset placeholder */ 0u,
5135 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005136 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005137 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005138 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005139 codegen_->AddSlowPath(slow_path);
5140 __ Cbz(out.X(), slow_path->GetEntryLabel());
5141 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005142 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005143 return;
5144 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005145 case HLoadString::LoadKind::kJitTableAddress: {
5146 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005147 load->GetStringIndex(),
5148 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005149 GenerateGcRootFieldLoad(load,
5150 out_loc,
5151 out.X(),
5152 /* offset */ 0,
5153 /* fixup_label */ nullptr,
5154 kCompilerReadBarrierOption);
5155 return;
5156 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005157 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005158 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005159 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005160
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005161 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005162 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005163 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005164 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005165 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5166 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005167 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005168}
5169
Alexandre Rames5319def2014-10-23 10:03:10 +01005170void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005171 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005172 locations->SetOut(Location::ConstantLocation(constant));
5173}
5174
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005175void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005176 // Will be generated at use site.
5177}
5178
Alexandre Rames67555f72014-11-18 10:55:16 +00005179void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005180 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5181 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005182 InvokeRuntimeCallingConvention calling_convention;
5183 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5184}
5185
5186void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005187 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005188 instruction,
5189 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005190 if (instruction->IsEnter()) {
5191 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5192 } else {
5193 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5194 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005195 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005196}
5197
Alexandre Rames42d641b2014-10-27 14:00:51 +00005198void LocationsBuilderARM64::VisitMul(HMul* mul) {
5199 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005200 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005201 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005202 case DataType::Type::kInt32:
5203 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005204 locations->SetInAt(0, Location::RequiresRegister());
5205 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005206 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005207 break;
5208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 case DataType::Type::kFloat32:
5210 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005211 locations->SetInAt(0, Location::RequiresFpuRegister());
5212 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005213 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005214 break;
5215
5216 default:
5217 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5218 }
5219}
5220
5221void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5222 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005223 case DataType::Type::kInt32:
5224 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005225 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5226 break;
5227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005228 case DataType::Type::kFloat32:
5229 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005230 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005231 break;
5232
5233 default:
5234 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5235 }
5236}
5237
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005238void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5239 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005240 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005241 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kInt32:
5243 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005244 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005246 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005247
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 case DataType::Type::kFloat32:
5249 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005250 locations->SetInAt(0, Location::RequiresFpuRegister());
5251 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005252 break;
5253
5254 default:
5255 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5256 }
5257}
5258
5259void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5260 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005261 case DataType::Type::kInt32:
5262 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005263 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5264 break;
5265
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 case DataType::Type::kFloat32:
5267 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005268 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005269 break;
5270
5271 default:
5272 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5273 }
5274}
5275
5276void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005277 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5278 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005279 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005280 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005281 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5282 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005283}
5284
5285void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005286 // Note: if heap poisoning is enabled, the entry point takes cares
5287 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005288 QuickEntrypointEnum entrypoint =
5289 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5290 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005291 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005292 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005293}
5294
Alexandre Rames5319def2014-10-23 10:03:10 +01005295void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005296 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5297 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005298 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005299 if (instruction->IsStringAlloc()) {
5300 locations->AddTemp(LocationFrom(kArtMethodRegister));
5301 } else {
5302 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005303 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005304 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005305}
5306
5307void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005308 // Note: if heap poisoning is enabled, the entry point takes cares
5309 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005310 if (instruction->IsStringAlloc()) {
5311 // String is allocated through StringFactory. Call NewEmptyString entry point.
5312 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005313 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005314 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5315 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005316
5317 {
5318 // Ensure the pc position is recorded immediately after the `blr` instruction.
5319 ExactAssemblyScope eas(GetVIXLAssembler(),
5320 kInstructionSize,
5321 CodeBufferCheckScope::kExactSize);
5322 __ blr(lr);
5323 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5324 }
David Brazdil6de19382016-01-08 17:37:10 +00005325 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005326 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005327 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005328 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005329 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005330}
5331
5332void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005333 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005334 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005335 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005336}
5337
5338void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005339 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005340 case DataType::Type::kInt32:
5341 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005342 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005343 break;
5344
5345 default:
5346 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5347 }
5348}
5349
David Brazdil66d126e2015-04-03 16:02:44 +01005350void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005351 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005352 locations->SetInAt(0, Location::RequiresRegister());
5353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5354}
5355
5356void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005357 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005358}
5359
Alexandre Rames5319def2014-10-23 10:03:10 +01005360void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005361 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5362 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005363}
5364
Calin Juravle2ae48182016-03-16 14:05:09 +00005365void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5366 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005367 return;
5368 }
Artem Serov914d7a82017-02-07 14:33:49 +00005369 {
5370 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5371 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5372 Location obj = instruction->GetLocations()->InAt(0);
5373 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5374 RecordPcInfo(instruction, instruction->GetDexPc());
5375 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005376}
5377
Calin Juravle2ae48182016-03-16 14:05:09 +00005378void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005379 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005380 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005381
5382 LocationSummary* locations = instruction->GetLocations();
5383 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005384
5385 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005386}
5387
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005388void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005389 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005390}
5391
Alexandre Rames67555f72014-11-18 10:55:16 +00005392void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5393 HandleBinaryOp(instruction);
5394}
5395
5396void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5397 HandleBinaryOp(instruction);
5398}
5399
Alexandre Rames3e69f162014-12-10 10:36:50 +00005400void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5401 LOG(FATAL) << "Unreachable";
5402}
5403
5404void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005405 if (instruction->GetNext()->IsSuspendCheck() &&
5406 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5407 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5408 // The back edge will generate the suspend check.
5409 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5410 }
5411
Alexandre Rames3e69f162014-12-10 10:36:50 +00005412 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5413}
5414
Alexandre Rames5319def2014-10-23 10:03:10 +01005415void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005416 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005417 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5418 if (location.IsStackSlot()) {
5419 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5420 } else if (location.IsDoubleStackSlot()) {
5421 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5422 }
5423 locations->SetOut(location);
5424}
5425
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005426void InstructionCodeGeneratorARM64::VisitParameterValue(
5427 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005428 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005429}
5430
5431void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5432 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005433 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005434 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005435}
5436
5437void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5438 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5439 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005440}
5441
5442void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005443 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005444 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005445 locations->SetInAt(i, Location::Any());
5446 }
5447 locations->SetOut(Location::Any());
5448}
5449
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005450void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005451 LOG(FATAL) << "Unreachable";
5452}
5453
Serban Constantinescu02164b32014-11-13 14:05:07 +00005454void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005455 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005456 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005457 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005458 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005459 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005460
5461 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005462 case DataType::Type::kInt32:
5463 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005464 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005465 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5467 break;
5468
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005469 case DataType::Type::kFloat32:
5470 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005471 InvokeRuntimeCallingConvention calling_convention;
5472 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5473 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5474 locations->SetOut(calling_convention.GetReturnLocation(type));
5475
5476 break;
5477 }
5478
Serban Constantinescu02164b32014-11-13 14:05:07 +00005479 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005480 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005481 }
5482}
5483
5484void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005485 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005486
Serban Constantinescu02164b32014-11-13 14:05:07 +00005487 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005488 case DataType::Type::kInt32:
5489 case DataType::Type::kInt64: {
Zheng Xuc6667102015-05-15 16:08:45 +08005490 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005491 break;
5492 }
5493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005494 case DataType::Type::kFloat32:
5495 case DataType::Type::kFloat64: {
5496 QuickEntrypointEnum entrypoint =
5497 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005498 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005499 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005500 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5501 } else {
5502 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5503 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005504 break;
5505 }
5506
Serban Constantinescu02164b32014-11-13 14:05:07 +00005507 default:
5508 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005509 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005510 }
5511}
5512
Igor Murashkind01745e2017-04-05 16:40:31 -07005513void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5514 constructor_fence->SetLocations(nullptr);
5515}
5516
5517void InstructionCodeGeneratorARM64::VisitConstructorFence(
5518 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5519 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5520}
5521
Calin Juravle27df7582015-04-17 19:12:31 +01005522void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5523 memory_barrier->SetLocations(nullptr);
5524}
5525
5526void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005527 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005528}
5529
Alexandre Rames5319def2014-10-23 10:03:10 +01005530void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005531 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005533 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005534}
5535
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005536void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005537 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005538}
5539
5540void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5541 instruction->SetLocations(nullptr);
5542}
5543
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005544void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005545 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005546}
5547
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005548void LocationsBuilderARM64::VisitRor(HRor* ror) {
5549 HandleBinaryOp(ror);
5550}
5551
5552void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5553 HandleBinaryOp(ror);
5554}
5555
Serban Constantinescu02164b32014-11-13 14:05:07 +00005556void LocationsBuilderARM64::VisitShl(HShl* shl) {
5557 HandleShift(shl);
5558}
5559
5560void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5561 HandleShift(shl);
5562}
5563
5564void LocationsBuilderARM64::VisitShr(HShr* shr) {
5565 HandleShift(shr);
5566}
5567
5568void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5569 HandleShift(shr);
5570}
5571
Alexandre Rames5319def2014-10-23 10:03:10 +01005572void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005573 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005574}
5575
5576void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005577 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005578}
5579
Alexandre Rames67555f72014-11-18 10:55:16 +00005580void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005581 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005582}
5583
5584void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005585 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005586}
5587
5588void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005589 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005590}
5591
Alexandre Rames67555f72014-11-18 10:55:16 +00005592void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005593 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005594}
5595
Calin Juravlee460d1d2015-09-29 04:52:17 +01005596void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5597 HUnresolvedInstanceFieldGet* instruction) {
5598 FieldAccessCallingConventionARM64 calling_convention;
5599 codegen_->CreateUnresolvedFieldLocationSummary(
5600 instruction, instruction->GetFieldType(), calling_convention);
5601}
5602
5603void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5604 HUnresolvedInstanceFieldGet* instruction) {
5605 FieldAccessCallingConventionARM64 calling_convention;
5606 codegen_->GenerateUnresolvedFieldAccess(instruction,
5607 instruction->GetFieldType(),
5608 instruction->GetFieldIndex(),
5609 instruction->GetDexPc(),
5610 calling_convention);
5611}
5612
5613void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5614 HUnresolvedInstanceFieldSet* instruction) {
5615 FieldAccessCallingConventionARM64 calling_convention;
5616 codegen_->CreateUnresolvedFieldLocationSummary(
5617 instruction, instruction->GetFieldType(), calling_convention);
5618}
5619
5620void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5621 HUnresolvedInstanceFieldSet* instruction) {
5622 FieldAccessCallingConventionARM64 calling_convention;
5623 codegen_->GenerateUnresolvedFieldAccess(instruction,
5624 instruction->GetFieldType(),
5625 instruction->GetFieldIndex(),
5626 instruction->GetDexPc(),
5627 calling_convention);
5628}
5629
5630void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5631 HUnresolvedStaticFieldGet* instruction) {
5632 FieldAccessCallingConventionARM64 calling_convention;
5633 codegen_->CreateUnresolvedFieldLocationSummary(
5634 instruction, instruction->GetFieldType(), calling_convention);
5635}
5636
5637void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5638 HUnresolvedStaticFieldGet* instruction) {
5639 FieldAccessCallingConventionARM64 calling_convention;
5640 codegen_->GenerateUnresolvedFieldAccess(instruction,
5641 instruction->GetFieldType(),
5642 instruction->GetFieldIndex(),
5643 instruction->GetDexPc(),
5644 calling_convention);
5645}
5646
5647void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5648 HUnresolvedStaticFieldSet* instruction) {
5649 FieldAccessCallingConventionARM64 calling_convention;
5650 codegen_->CreateUnresolvedFieldLocationSummary(
5651 instruction, instruction->GetFieldType(), calling_convention);
5652}
5653
5654void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5655 HUnresolvedStaticFieldSet* instruction) {
5656 FieldAccessCallingConventionARM64 calling_convention;
5657 codegen_->GenerateUnresolvedFieldAccess(instruction,
5658 instruction->GetFieldType(),
5659 instruction->GetFieldIndex(),
5660 instruction->GetDexPc(),
5661 calling_convention);
5662}
5663
Alexandre Rames5319def2014-10-23 10:03:10 +01005664void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005665 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5666 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005667 // In suspend check slow path, usually there are no caller-save registers at all.
5668 // If SIMD instructions are present, however, we force spilling all live SIMD
5669 // registers in full width (since the runtime only saves/restores lower part).
5670 locations->SetCustomSlowPathCallerSaves(
5671 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005672}
5673
5674void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005675 HBasicBlock* block = instruction->GetBlock();
5676 if (block->GetLoopInformation() != nullptr) {
5677 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5678 // The back edge will generate the suspend check.
5679 return;
5680 }
5681 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5682 // The goto will generate the suspend check.
5683 return;
5684 }
5685 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005686 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005687}
5688
Alexandre Rames67555f72014-11-18 10:55:16 +00005689void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005690 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5691 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005692 InvokeRuntimeCallingConvention calling_convention;
5693 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5694}
5695
5696void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005697 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005698 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005699}
5700
5701void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5702 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005703 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005704 DataType::Type input_type = conversion->GetInputType();
5705 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005706 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5707 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5709 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005710 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5711 }
5712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005713 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005714 locations->SetInAt(0, Location::RequiresFpuRegister());
5715 } else {
5716 locations->SetInAt(0, Location::RequiresRegister());
5717 }
5718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005719 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005720 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5721 } else {
5722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5723 }
5724}
5725
5726void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005727 DataType::Type result_type = conversion->GetResultType();
5728 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005729
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005730 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5731 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005733 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5734 int result_size = DataType::Size(result_type);
5735 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005736 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005737 Register output = OutputRegister(conversion);
5738 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005739 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005740 // 'int' values are used directly as W registers, discarding the top
5741 // bits, so we don't need to sign-extend and can just perform a move.
5742 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5743 // top 32 bits of the target register. We theoretically could leave those
5744 // bits unchanged, but we would have to make sure that no code uses a
5745 // 32bit input value as a 64bit value assuming that the top 32 bits are
5746 // zero.
5747 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005748 } else if (DataType::IsUnsignedType(result_type) ||
5749 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5750 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005751 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005752 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005753 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005754 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005755 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5757 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005758 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005759 } else if (DataType::IsFloatingPointType(result_type) &&
5760 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005761 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5762 } else {
5763 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5764 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005765 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005766}
Alexandre Rames67555f72014-11-18 10:55:16 +00005767
Serban Constantinescu02164b32014-11-13 14:05:07 +00005768void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5769 HandleShift(ushr);
5770}
5771
5772void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5773 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005774}
5775
5776void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5777 HandleBinaryOp(instruction);
5778}
5779
5780void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5781 HandleBinaryOp(instruction);
5782}
5783
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005784void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005785 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005786 LOG(FATAL) << "Unreachable";
5787}
5788
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005789void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005790 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005791 LOG(FATAL) << "Unreachable";
5792}
5793
Mark Mendellfe57faa2015-09-18 09:26:15 -04005794// Simple implementation of packed switch - generate cascaded compare/jumps.
5795void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5796 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005797 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005798 locations->SetInAt(0, Location::RequiresRegister());
5799}
5800
5801void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5802 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005803 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005804 Register value_reg = InputRegisterAt(switch_instr, 0);
5805 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5806
Zheng Xu3927c8b2015-11-18 17:46:25 +08005807 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005808 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005809 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5810 // make sure we don't emit it if the target may run out of range.
5811 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5812 // ranges and emit the tables only as required.
5813 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005814
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005815 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005816 // Current instruction id is an upper bound of the number of HIRs in the graph.
5817 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5818 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005819 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5820 Register temp = temps.AcquireW();
5821 __ Subs(temp, value_reg, Operand(lower_bound));
5822
Zheng Xu3927c8b2015-11-18 17:46:25 +08005823 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005824 // Jump to successors[0] if value == lower_bound.
5825 __ B(eq, codegen_->GetLabelOf(successors[0]));
5826 int32_t last_index = 0;
5827 for (; num_entries - last_index > 2; last_index += 2) {
5828 __ Subs(temp, temp, Operand(2));
5829 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5830 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5831 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5832 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5833 }
5834 if (num_entries - last_index == 2) {
5835 // The last missing case_value.
5836 __ Cmp(temp, Operand(1));
5837 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005838 }
5839
5840 // And the default for any other value.
5841 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5842 __ B(codegen_->GetLabelOf(default_block));
5843 }
5844 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005845 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005846
5847 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5848
5849 // Below instructions should use at most one blocked register. Since there are two blocked
5850 // registers, we are free to block one.
5851 Register temp_w = temps.AcquireW();
5852 Register index;
5853 // Remove the bias.
5854 if (lower_bound != 0) {
5855 index = temp_w;
5856 __ Sub(index, value_reg, Operand(lower_bound));
5857 } else {
5858 index = value_reg;
5859 }
5860
5861 // Jump to default block if index is out of the range.
5862 __ Cmp(index, Operand(num_entries));
5863 __ B(hs, codegen_->GetLabelOf(default_block));
5864
5865 // In current VIXL implementation, it won't require any blocked registers to encode the
5866 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5867 // register pressure.
5868 Register table_base = temps.AcquireX();
5869 // Load jump offset from the table.
5870 __ Adr(table_base, jump_table->GetTableStartLabel());
5871 Register jump_offset = temp_w;
5872 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5873
5874 // Jump to target block by branching to table_base(pc related) + offset.
5875 Register target_address = table_base;
5876 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5877 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005878 }
5879}
5880
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005881void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5882 HInstruction* instruction,
5883 Location out,
5884 uint32_t offset,
5885 Location maybe_temp,
5886 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005887 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005888 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005889 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005890 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005891 if (kUseBakerReadBarrier) {
5892 // Load with fast path based Baker's read barrier.
5893 // /* HeapReference<Object> */ out = *(out + offset)
5894 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5895 out,
5896 out_reg,
5897 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005898 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005899 /* needs_null_check */ false,
5900 /* use_load_acquire */ false);
5901 } else {
5902 // Load with slow path based read barrier.
5903 // Save the value of `out` into `maybe_temp` before overwriting it
5904 // in the following move operation, as we will need it for the
5905 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005906 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005907 __ Mov(temp_reg, out_reg);
5908 // /* HeapReference<Object> */ out = *(out + offset)
5909 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5910 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5911 }
5912 } else {
5913 // Plain load with no read barrier.
5914 // /* HeapReference<Object> */ out = *(out + offset)
5915 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5916 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5917 }
5918}
5919
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005920void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5921 HInstruction* instruction,
5922 Location out,
5923 Location obj,
5924 uint32_t offset,
5925 Location maybe_temp,
5926 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005927 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005928 Register out_reg = RegisterFrom(out, type);
5929 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005930 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005931 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005932 if (kUseBakerReadBarrier) {
5933 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005934 // /* HeapReference<Object> */ out = *(obj + offset)
5935 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5936 out,
5937 obj_reg,
5938 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005939 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005940 /* needs_null_check */ false,
5941 /* use_load_acquire */ false);
5942 } else {
5943 // Load with slow path based read barrier.
5944 // /* HeapReference<Object> */ out = *(obj + offset)
5945 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5946 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5947 }
5948 } else {
5949 // Plain load with no read barrier.
5950 // /* HeapReference<Object> */ out = *(obj + offset)
5951 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5952 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5953 }
5954}
5955
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005956void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5957 HInstruction* instruction,
5958 Location root,
5959 Register obj,
5960 uint32_t offset,
5961 vixl::aarch64::Label* fixup_label,
5962 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005963 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005964 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005965 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005966 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005967 if (kUseBakerReadBarrier) {
5968 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005969 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005970 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5971 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01005972 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5973 // the Marking Register) to decide whether we need to enter
5974 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005975 //
5976 // We use link-time generated thunks for the slow path. That thunk
5977 // checks the reference and jumps to the entrypoint if needed.
5978 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005979 // lr = &return_address;
5980 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005981 // if (mr) { // Thread::Current()->GetIsGcMarking()
5982 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005983 // }
5984 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005985
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005986 UseScratchRegisterScope temps(GetVIXLAssembler());
5987 DCHECK(temps.IsAvailable(ip0));
5988 DCHECK(temps.IsAvailable(ip1));
5989 temps.Exclude(ip0, ip1);
5990 uint32_t custom_data =
5991 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5992 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005993
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005994 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5995 vixl::aarch64::Label return_address;
5996 __ adr(lr, &return_address);
5997 if (fixup_label != nullptr) {
5998 __ Bind(fixup_label);
5999 }
6000 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6001 "GC root LDR must be 2 instruction (8B) before the return address label.");
6002 __ ldr(root_reg, MemOperand(obj.X(), offset));
6003 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006004 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006005 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006006 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006007 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6008 // the Marking Register) to decide whether we need to enter
6009 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006010 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006011 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006012 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006013 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006014 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6015 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006016 // }
Roland Levillain44015862016-01-22 11:47:17 +00006017
Roland Levillain97c46462017-05-11 14:04:03 +01006018 // Slow path marking the GC root `root`. The entrypoint will
6019 // be loaded by the slow path code.
6020 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006021 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006022 codegen_->AddSlowPath(slow_path);
6023
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006024 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6025 if (fixup_label == nullptr) {
6026 __ Ldr(root_reg, MemOperand(obj, offset));
6027 } else {
6028 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6029 }
6030 static_assert(
6031 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6032 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6033 "have different sizes.");
6034 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6035 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6036 "have different sizes.");
6037
Roland Levillain97c46462017-05-11 14:04:03 +01006038 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006039 __ Bind(slow_path->GetExitLabel());
6040 }
Roland Levillain44015862016-01-22 11:47:17 +00006041 } else {
6042 // GC root loaded through a slow path for read barriers other
6043 // than Baker's.
6044 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006045 if (fixup_label == nullptr) {
6046 __ Add(root_reg.X(), obj.X(), offset);
6047 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006048 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006049 }
Roland Levillain44015862016-01-22 11:47:17 +00006050 // /* mirror::Object* */ root = root->Read()
6051 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6052 }
6053 } else {
6054 // Plain GC root load with no read barrier.
6055 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006056 if (fixup_label == nullptr) {
6057 __ Ldr(root_reg, MemOperand(obj, offset));
6058 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006059 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006060 }
Roland Levillain44015862016-01-22 11:47:17 +00006061 // Note that GC roots are not affected by heap poisoning, thus we
6062 // do not have to unpoison `root_reg` here.
6063 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006064 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006065}
6066
6067void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6068 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006069 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006070 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006071 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006072 bool needs_null_check,
6073 bool use_load_acquire) {
6074 DCHECK(kEmitCompilerReadBarrier);
6075 DCHECK(kUseBakerReadBarrier);
6076
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006077 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6078 !use_load_acquire &&
6079 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006080 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6081 // Marking Register) to decide whether we need to enter the slow
6082 // path to mark the reference. Then, in the slow path, check the
6083 // gray bit in the lock word of the reference's holder (`obj`) to
6084 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006085 //
6086 // We use link-time generated thunks for the slow path. That thunk checks
6087 // the holder and jumps to the entrypoint if needed. If the holder is not
6088 // gray, it creates a fake dependency and returns to the LDR instruction.
6089 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006090 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006091 // if (mr) { // Thread::Current()->GetIsGcMarking()
6092 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006093 // }
6094 // not_gray_return_address:
6095 // // Original reference load. If the offset is too large to fit
6096 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006097 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006098 // gray_return_address:
6099
6100 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6101 Register base = obj;
6102 if (offset >= kReferenceLoadMinFarOffset) {
6103 DCHECK(maybe_temp.IsRegister());
6104 base = WRegisterFrom(maybe_temp);
6105 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6106 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6107 offset &= (kReferenceLoadMinFarOffset - 1u);
6108 }
6109 UseScratchRegisterScope temps(GetVIXLAssembler());
6110 DCHECK(temps.IsAvailable(ip0));
6111 DCHECK(temps.IsAvailable(ip1));
6112 temps.Exclude(ip0, ip1);
6113 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6114 base.GetCode(),
6115 obj.GetCode());
6116 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6117
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006118 {
6119 EmissionCheckScope guard(GetVIXLAssembler(),
6120 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6121 vixl::aarch64::Label return_address;
6122 __ adr(lr, &return_address);
6123 __ Bind(cbnz_label);
6124 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6125 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6126 "Field LDR must be 1 instruction (4B) before the return address label; "
6127 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006128 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006129 __ ldr(ref_reg, MemOperand(base.X(), offset));
6130 if (needs_null_check) {
6131 MaybeRecordImplicitNullCheck(instruction);
6132 }
6133 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6134 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006135 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006136 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006137 return;
6138 }
6139
Roland Levillain44015862016-01-22 11:47:17 +00006140 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006141 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006142 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006143 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006144 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6145 ref,
6146 obj,
6147 offset,
6148 no_index,
6149 no_scale_factor,
6150 temp,
6151 needs_null_check,
6152 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006153}
6154
6155void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6156 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006157 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006158 uint32_t data_offset,
6159 Location index,
6160 Register temp,
6161 bool needs_null_check) {
6162 DCHECK(kEmitCompilerReadBarrier);
6163 DCHECK(kUseBakerReadBarrier);
6164
Vladimir Marko66d691d2017-04-07 17:53:39 +01006165 static_assert(
6166 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6167 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006168 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006169
6170 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6171 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006172 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6173 // Marking Register) to decide whether we need to enter the slow
6174 // path to mark the reference. Then, in the slow path, check the
6175 // gray bit in the lock word of the reference's holder (`obj`) to
6176 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006177 //
6178 // We use link-time generated thunks for the slow path. That thunk checks
6179 // the holder and jumps to the entrypoint if needed. If the holder is not
6180 // gray, it creates a fake dependency and returns to the LDR instruction.
6181 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006182 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006183 // if (mr) { // Thread::Current()->GetIsGcMarking()
6184 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006185 // }
6186 // not_gray_return_address:
6187 // // Original reference load. If the offset is too large to fit
6188 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006189 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006190 // gray_return_address:
6191
6192 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006193 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6194 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006195
6196 UseScratchRegisterScope temps(GetVIXLAssembler());
6197 DCHECK(temps.IsAvailable(ip0));
6198 DCHECK(temps.IsAvailable(ip1));
6199 temps.Exclude(ip0, ip1);
6200 uint32_t custom_data =
6201 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6202 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6203
Vladimir Marko66d691d2017-04-07 17:53:39 +01006204 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006205 {
6206 EmissionCheckScope guard(GetVIXLAssembler(),
6207 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6208 vixl::aarch64::Label return_address;
6209 __ adr(lr, &return_address);
6210 __ Bind(cbnz_label);
6211 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6212 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6213 "Array LDR must be 1 instruction (4B) before the return address label; "
6214 " 2 instructions (8B) for heap poisoning.");
6215 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6216 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6217 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6218 __ Bind(&return_address);
6219 }
6220 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006221 return;
6222 }
6223
Roland Levillain44015862016-01-22 11:47:17 +00006224 // Array cells are never volatile variables, therefore array loads
6225 // never use Load-Acquire instructions on ARM64.
6226 const bool use_load_acquire = false;
6227
6228 // /* HeapReference<Object> */ ref =
6229 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006230 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6231 ref,
6232 obj,
6233 data_offset,
6234 index,
6235 scale_factor,
6236 temp,
6237 needs_null_check,
6238 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006239}
6240
6241void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6242 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006243 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006244 uint32_t offset,
6245 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006246 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006247 Register temp,
6248 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006249 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006250 DCHECK(kEmitCompilerReadBarrier);
6251 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006252 // If we are emitting an array load, we should not be using a
6253 // Load Acquire instruction. In other words:
6254 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6255 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006256
Roland Levillain97c46462017-05-11 14:04:03 +01006257 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6258 // Marking Register) to decide whether we need to enter the slow
6259 // path to mark the reference. Then, in the slow path, check the
6260 // gray bit in the lock word of the reference's holder (`obj`) to
6261 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006262 //
Roland Levillain97c46462017-05-11 14:04:03 +01006263 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006264 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006265 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6266 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6267 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6268 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6269 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006270 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6271 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006272 // }
6273 // } else {
6274 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006275 // }
Roland Levillain44015862016-01-22 11:47:17 +00006276
Roland Levillainba650a42017-03-06 13:52:32 +00006277 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006278 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006279 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006280 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006281 instruction,
6282 ref,
6283 obj,
6284 offset,
6285 index,
6286 scale_factor,
6287 needs_null_check,
6288 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006289 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006290 AddSlowPath(slow_path);
6291
Roland Levillain97c46462017-05-11 14:04:03 +01006292 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006293 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006294 GenerateRawReferenceLoad(
6295 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006296 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006297 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006298}
6299
Roland Levillainff487002017-03-07 16:50:01 +00006300void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6301 Location ref,
6302 Register obj,
6303 Location field_offset,
6304 Register temp,
6305 bool needs_null_check,
6306 bool use_load_acquire) {
6307 DCHECK(kEmitCompilerReadBarrier);
6308 DCHECK(kUseBakerReadBarrier);
6309 // If we are emitting an array load, we should not be using a
6310 // Load Acquire instruction. In other words:
6311 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6312 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6313
Roland Levillain97c46462017-05-11 14:04:03 +01006314 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6315 // Marking Register) to decide whether we need to enter the slow
6316 // path to update the reference field within `obj`. Then, in the
6317 // slow path, check the gray bit in the lock word of the reference's
6318 // holder (`obj`) to decide whether to mark `ref` and update the
6319 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006320 //
Roland Levillain97c46462017-05-11 14:04:03 +01006321 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006322 // // Slow path.
6323 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6324 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6325 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6326 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6327 // if (is_gray) {
6328 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006329 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6330 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006331 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6332 // }
6333 // }
6334
6335 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006336 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006337 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006338 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006339 instruction,
6340 ref,
6341 obj,
6342 /* offset */ 0u,
6343 /* index */ field_offset,
6344 /* scale_factor */ 0u /* "times 1" */,
6345 needs_null_check,
6346 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006347 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006348 AddSlowPath(slow_path);
6349
Roland Levillain97c46462017-05-11 14:04:03 +01006350 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006351 // Fast path: the GC is not marking: nothing to do (the field is
6352 // up-to-date, and we don't need to load the reference).
6353 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006354 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006355}
6356
Roland Levillainba650a42017-03-06 13:52:32 +00006357void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6358 Location ref,
6359 Register obj,
6360 uint32_t offset,
6361 Location index,
6362 size_t scale_factor,
6363 bool needs_null_check,
6364 bool use_load_acquire) {
6365 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006367 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006368
Roland Levillainba650a42017-03-06 13:52:32 +00006369 // If needed, vixl::EmissionCheckScope guards are used to ensure
6370 // that no pools are emitted between the load (macro) instruction
6371 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006372
Roland Levillain44015862016-01-22 11:47:17 +00006373 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006374 // Load types involving an "index": ArrayGet,
6375 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6376 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006377 if (use_load_acquire) {
6378 // UnsafeGetObjectVolatile intrinsic case.
6379 // Register `index` is not an index in an object array, but an
6380 // offset to an object reference field within object `obj`.
6381 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6382 DCHECK(instruction->GetLocations()->Intrinsified());
6383 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6384 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006385 DCHECK_EQ(offset, 0u);
6386 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006387 DCHECK_EQ(needs_null_check, false);
6388 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006389 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6390 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006391 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006392 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6393 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006394 if (index.IsConstant()) {
6395 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006396 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006397 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006398 if (needs_null_check) {
6399 MaybeRecordImplicitNullCheck(instruction);
6400 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006401 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006402 UseScratchRegisterScope temps(GetVIXLAssembler());
6403 Register temp = temps.AcquireW();
6404 __ Add(temp, obj, offset);
6405 {
6406 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6407 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6408 if (needs_null_check) {
6409 MaybeRecordImplicitNullCheck(instruction);
6410 }
6411 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006412 }
Roland Levillain44015862016-01-22 11:47:17 +00006413 }
Roland Levillain44015862016-01-22 11:47:17 +00006414 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006415 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006416 MemOperand field = HeapOperand(obj, offset);
6417 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006418 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6419 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006420 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006421 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006422 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006423 if (needs_null_check) {
6424 MaybeRecordImplicitNullCheck(instruction);
6425 }
Roland Levillain44015862016-01-22 11:47:17 +00006426 }
6427 }
6428
6429 // Object* ref = ref_addr->AsMirrorPtr()
6430 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006431}
6432
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006433void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6434 // The following condition is a compile-time one, so it does not have a run-time cost.
6435 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6436 // The following condition is a run-time one; it is executed after the
6437 // previous compile-time test, to avoid penalizing non-debug builds.
6438 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6439 UseScratchRegisterScope temps(GetVIXLAssembler());
6440 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6441 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6442 }
6443 }
6444}
6445
Roland Levillain44015862016-01-22 11:47:17 +00006446void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6447 Location out,
6448 Location ref,
6449 Location obj,
6450 uint32_t offset,
6451 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006452 DCHECK(kEmitCompilerReadBarrier);
6453
Roland Levillain44015862016-01-22 11:47:17 +00006454 // Insert a slow path based read barrier *after* the reference load.
6455 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006456 // If heap poisoning is enabled, the unpoisoning of the loaded
6457 // reference will be carried out by the runtime within the slow
6458 // path.
6459 //
6460 // Note that `ref` currently does not get unpoisoned (when heap
6461 // poisoning is enabled), which is alright as the `ref` argument is
6462 // not used by the artReadBarrierSlow entry point.
6463 //
6464 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006465 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006466 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6467 AddSlowPath(slow_path);
6468
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006469 __ B(slow_path->GetEntryLabel());
6470 __ Bind(slow_path->GetExitLabel());
6471}
6472
Roland Levillain44015862016-01-22 11:47:17 +00006473void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6474 Location out,
6475 Location ref,
6476 Location obj,
6477 uint32_t offset,
6478 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006479 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006480 // Baker's read barriers shall be handled by the fast path
6481 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6482 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006483 // If heap poisoning is enabled, unpoisoning will be taken care of
6484 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006485 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006486 } else if (kPoisonHeapReferences) {
6487 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6488 }
6489}
6490
Roland Levillain44015862016-01-22 11:47:17 +00006491void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6492 Location out,
6493 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006494 DCHECK(kEmitCompilerReadBarrier);
6495
Roland Levillain44015862016-01-22 11:47:17 +00006496 // Insert a slow path based read barrier *after* the GC root load.
6497 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006498 // Note that GC roots are not affected by heap poisoning, so we do
6499 // not need to do anything special for this here.
6500 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006501 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006502 AddSlowPath(slow_path);
6503
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006504 __ B(slow_path->GetEntryLabel());
6505 __ Bind(slow_path->GetExitLabel());
6506}
6507
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006508void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6509 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006510 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006511 locations->SetInAt(0, Location::RequiresRegister());
6512 locations->SetOut(Location::RequiresRegister());
6513}
6514
6515void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6516 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006517 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006518 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006519 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006520 __ Ldr(XRegisterFrom(locations->Out()),
6521 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006522 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006523 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006524 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006525 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6526 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006527 __ Ldr(XRegisterFrom(locations->Out()),
6528 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006529 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006530}
6531
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006532static void PatchJitRootUse(uint8_t* code,
6533 const uint8_t* roots_data,
6534 vixl::aarch64::Literal<uint32_t>* literal,
6535 uint64_t index_in_table) {
6536 uint32_t literal_offset = literal->GetOffset();
6537 uintptr_t address =
6538 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6539 uint8_t* data = code + literal_offset;
6540 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6541}
6542
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006543void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6544 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006545 const StringReference& string_reference = entry.first;
6546 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006547 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006548 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006549 }
6550 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006551 const TypeReference& type_reference = entry.first;
6552 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006553 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006554 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006555 }
6556}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006557
Alexandre Rames67555f72014-11-18 10:55:16 +00006558#undef __
6559#undef QUICK_ENTRY_POINT
6560
Alexandre Rames5319def2014-10-23 10:03:10 +01006561} // namespace arm64
6562} // namespace art