blob: 1380596ab25e2e4ae27f4e668f208445e4b25e05 [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
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001491 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1492 UseScratchRegisterScope temps(masm);
1493 Register temp = temps.AcquireX();
1494 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1495 __ Add(temp, temp, 1);
1496 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1497 }
1498
Vladimir Marko33bff252017-11-01 14:35:42 +00001499 bool do_overflow_check =
1500 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001501 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001502 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001503 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001504 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001505 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001506 {
1507 // Ensure that between load and RecordPcInfo there are no pools emitted.
1508 ExactAssemblyScope eas(GetVIXLAssembler(),
1509 kInstructionSize,
1510 CodeBufferCheckScope::kExactSize);
1511 __ ldr(wzr, MemOperand(temp, 0));
1512 RecordPcInfo(nullptr, 0);
1513 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001514 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001515
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001516 if (!HasEmptyFrame()) {
1517 int frame_size = GetFrameSize();
1518 // Stack layout:
1519 // sp[frame_size - 8] : lr.
1520 // ... : other preserved core registers.
1521 // ... : other preserved fp registers.
1522 // ... : reserved frame space.
1523 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001524
1525 // Save the current method if we need it. Note that we do not
1526 // do this in HCurrentMethod, as the instruction might have been removed
1527 // in the SSA graph.
1528 if (RequiresCurrentMethod()) {
1529 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001530 } else {
1531 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001532 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001533 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001534 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1535 frame_size - GetCoreSpillSize());
1536 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1537 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001538
1539 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1540 // Initialize should_deoptimize flag to 0.
1541 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1542 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1543 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001544 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001545
1546 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001547}
1548
1549void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001550 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001551 if (!HasEmptyFrame()) {
1552 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001553 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1554 frame_size - FrameEntrySpillSize());
1555 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1556 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001557 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001558 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001559 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001560 __ Ret();
1561 GetAssembler()->cfi().RestoreState();
1562 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001563}
1564
Scott Wakeling97c72b72016-06-24 16:19:36 +01001565CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001566 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001567 return CPURegList(CPURegister::kRegister, kXRegSize,
1568 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001569}
1570
Scott Wakeling97c72b72016-06-24 16:19:36 +01001571CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001572 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1573 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001574 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1575 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001576}
1577
Alexandre Rames5319def2014-10-23 10:03:10 +01001578void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1579 __ Bind(GetLabelOf(block));
1580}
1581
Calin Juravle175dc732015-08-25 15:42:32 +01001582void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1583 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001585}
1586
Calin Juravlee460d1d2015-09-29 04:52:17 +01001587void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1588 if (location.IsRegister()) {
1589 locations->AddTemp(location);
1590 } else {
1591 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1592 }
1593}
1594
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001595void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001597 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001598 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001599 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001600 if (value_can_be_null) {
1601 __ Cbz(value, &done);
1602 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001603 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001604 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001605 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001606 if (value_can_be_null) {
1607 __ Bind(&done);
1608 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001609}
1610
David Brazdil58282f42016-01-14 12:45:10 +00001611void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001612 // Blocked core registers:
1613 // lr : Runtime reserved.
1614 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001615 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001616 // ip1 : VIXL core temp.
1617 // ip0 : VIXL core temp.
1618 //
1619 // Blocked fp registers:
1620 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001621 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1622 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001623 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001624 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001625 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001626
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001627 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001628 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001629 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001630 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001631
David Brazdil58282f42016-01-14 12:45:10 +00001632 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001633 // Stubs do not save callee-save floating point registers. If the graph
1634 // is debuggable, we need to deal with these registers differently. For
1635 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001636 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1637 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001638 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001639 }
1640 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001641}
1642
Alexandre Rames3e69f162014-12-10 10:36:50 +00001643size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1644 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1645 __ Str(reg, MemOperand(sp, stack_index));
1646 return kArm64WordSize;
1647}
1648
1649size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1650 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1651 __ Ldr(reg, MemOperand(sp, stack_index));
1652 return kArm64WordSize;
1653}
1654
1655size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1656 FPRegister reg = FPRegister(reg_id, kDRegSize);
1657 __ Str(reg, MemOperand(sp, stack_index));
1658 return kArm64WordSize;
1659}
1660
1661size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1662 FPRegister reg = FPRegister(reg_id, kDRegSize);
1663 __ Ldr(reg, MemOperand(sp, stack_index));
1664 return kArm64WordSize;
1665}
1666
Alexandre Rames5319def2014-10-23 10:03:10 +01001667void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001668 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001669}
1670
1671void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001672 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001673}
1674
Alexandre Rames67555f72014-11-18 10:55:16 +00001675void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001676 if (constant->IsIntConstant()) {
1677 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1678 } else if (constant->IsLongConstant()) {
1679 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1680 } else if (constant->IsNullConstant()) {
1681 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001682 } else if (constant->IsFloatConstant()) {
1683 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1684 } else {
1685 DCHECK(constant->IsDoubleConstant());
1686 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1687 }
1688}
1689
Alexandre Rames3e69f162014-12-10 10:36:50 +00001690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001692 DCHECK(constant.IsConstant());
1693 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001695 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1697 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1698 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1699 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001700}
1701
Roland Levillain952b2352017-05-03 19:49:14 +01001702// Allocate a scratch register from the VIXL pool, querying first
1703// the floating-point register pool, and then the core register
1704// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001705// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1706// using a different allocation strategy.
1707static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1708 vixl::aarch64::UseScratchRegisterScope* temps,
1709 int size_in_bits) {
1710 return masm->GetScratchFPRegisterList()->IsEmpty()
1711 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1712 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1713}
1714
Calin Juravlee460d1d2015-09-29 04:52:17 +01001715void CodeGeneratorARM64::MoveLocation(Location destination,
1716 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001717 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001718 if (source.Equals(destination)) {
1719 return;
1720 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001721
1722 // A valid move can always be inferred from the destination and source
1723 // locations. When moving from and to a register, the argument type can be
1724 // used to generate 32bit instead of 64bit moves. In debug mode we also
1725 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001727
1728 if (destination.IsRegister() || destination.IsFpuRegister()) {
1729 if (unspecified_type) {
1730 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1731 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001732 (src_cst != nullptr && (src_cst->IsIntConstant()
1733 || src_cst->IsFloatConstant()
1734 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001735 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001736 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001737 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001738 // If the source is a double stack slot or a 64bit constant, a 64bit
1739 // type is appropriate. Else the source is a register, and since the
1740 // type has not been specified, we chose a 64bit type to force a 64bit
1741 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001743 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001744 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1746 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001747 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1749 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1750 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001751 } else if (source.IsSIMDStackSlot()) {
1752 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001753 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001754 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001755 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001756 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001757 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001758 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001759 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001760 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001761 DataType::Type source_type = DataType::Is64BitType(dst_type)
1762 ? DataType::Type::kInt64
1763 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001764 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1765 }
1766 } else {
1767 DCHECK(source.IsFpuRegister());
1768 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001769 DataType::Type source_type = DataType::Is64BitType(dst_type)
1770 ? DataType::Type::kFloat64
1771 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001772 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1773 } else {
1774 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001775 if (GetGraph()->HasSIMD()) {
1776 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1777 } else {
1778 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1779 }
1780 }
1781 }
1782 } else if (destination.IsSIMDStackSlot()) {
1783 if (source.IsFpuRegister()) {
1784 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1785 } else {
1786 DCHECK(source.IsSIMDStackSlot());
1787 UseScratchRegisterScope temps(GetVIXLAssembler());
1788 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1789 Register temp = temps.AcquireX();
1790 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1791 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1792 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1793 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1794 } else {
1795 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1796 __ Ldr(temp, StackOperandFrom(source));
1797 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001798 }
1799 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001800 } else { // The destination is not a register. It must be a stack slot.
1801 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1802 if (source.IsRegister() || source.IsFpuRegister()) {
1803 if (unspecified_type) {
1804 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001805 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001806 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001807 dst_type =
1808 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 }
1810 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001811 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1812 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001813 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001814 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001815 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1816 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001817 UseScratchRegisterScope temps(GetVIXLAssembler());
1818 HConstant* src_cst = source.GetConstant();
1819 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001820 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001821 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1822 ? Register(xzr)
1823 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001824 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001825 if (src_cst->IsIntConstant()) {
1826 temp = temps.AcquireW();
1827 } else if (src_cst->IsLongConstant()) {
1828 temp = temps.AcquireX();
1829 } else if (src_cst->IsFloatConstant()) {
1830 temp = temps.AcquireS();
1831 } else {
1832 DCHECK(src_cst->IsDoubleConstant());
1833 temp = temps.AcquireD();
1834 }
1835 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001836 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001837 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001838 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001839 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001840 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001841 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001842 // Use any scratch register (a core or a floating-point one)
1843 // from VIXL scratch register pools as a temporary.
1844 //
1845 // We used to only use the FP scratch register pool, but in some
1846 // rare cases the only register from this pool (D31) would
1847 // already be used (e.g. within a ParallelMove instruction, when
1848 // a move is blocked by a another move requiring a scratch FP
1849 // register, which would reserve D31). To prevent this issue, we
1850 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001851 //
1852 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001853 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001854 // use AcquireFPOrCoreCPURegisterOfSize instead of
1855 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1856 // allocates core scratch registers first.
1857 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1858 GetVIXLAssembler(),
1859 &temps,
1860 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001861 __ Ldr(temp, StackOperandFrom(source));
1862 __ Str(temp, StackOperandFrom(destination));
1863 }
1864 }
1865}
1866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001868 CPURegister dst,
1869 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001870 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001871 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001872 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001873 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001874 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001875 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001876 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001877 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001878 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001879 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001880 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001881 case DataType::Type::kInt16:
1882 __ Ldrsh(Register(dst), src);
1883 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001884 case DataType::Type::kInt32:
1885 case DataType::Type::kReference:
1886 case DataType::Type::kInt64:
1887 case DataType::Type::kFloat32:
1888 case DataType::Type::kFloat64:
1889 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001890 __ Ldr(dst, src);
1891 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001892 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001893 LOG(FATAL) << "Unreachable type " << type;
1894 }
1895}
1896
Calin Juravle77520bc2015-01-12 18:45:46 +00001897void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001898 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001899 const MemOperand& src,
1900 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001901 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001902 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001903 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001904 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001905
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001906 DCHECK(!src.IsPreIndex());
1907 DCHECK(!src.IsPostIndex());
1908
1909 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001910 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001911 {
1912 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1913 MemOperand base = MemOperand(temp_base);
1914 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001915 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001916 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001917 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001918 {
1919 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1920 __ ldarb(Register(dst), base);
1921 if (needs_null_check) {
1922 MaybeRecordImplicitNullCheck(instruction);
1923 }
1924 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001925 if (type == DataType::Type::kInt8) {
1926 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001927 }
1928 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001929 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001930 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001931 {
1932 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1933 __ ldarh(Register(dst), base);
1934 if (needs_null_check) {
1935 MaybeRecordImplicitNullCheck(instruction);
1936 }
1937 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001938 if (type == DataType::Type::kInt16) {
1939 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1940 }
Artem Serov914d7a82017-02-07 14:33:49 +00001941 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001942 case DataType::Type::kInt32:
1943 case DataType::Type::kReference:
1944 case DataType::Type::kInt64:
1945 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001946 {
1947 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1948 __ ldar(Register(dst), base);
1949 if (needs_null_check) {
1950 MaybeRecordImplicitNullCheck(instruction);
1951 }
1952 }
1953 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001954 case DataType::Type::kFloat32:
1955 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001956 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001958
Artem Serov914d7a82017-02-07 14:33:49 +00001959 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1960 {
1961 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1962 __ ldar(temp, base);
1963 if (needs_null_check) {
1964 MaybeRecordImplicitNullCheck(instruction);
1965 }
1966 }
1967 __ Fmov(FPRegister(dst), temp);
1968 break;
Roland Levillain44015862016-01-22 11:47:17 +00001969 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001970 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001971 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001972 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001973 }
1974}
1975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001977 CPURegister src,
1978 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001979 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001981 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001983 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001984 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001985 case DataType::Type::kUint16:
1986 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001987 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001988 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kInt32:
1990 case DataType::Type::kReference:
1991 case DataType::Type::kInt64:
1992 case DataType::Type::kFloat32:
1993 case DataType::Type::kFloat64:
1994 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001995 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001996 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001998 LOG(FATAL) << "Unreachable type " << type;
1999 }
2000}
2001
Artem Serov914d7a82017-02-07 14:33:49 +00002002void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002003 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002004 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002005 const MemOperand& dst,
2006 bool needs_null_check) {
2007 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002008 UseScratchRegisterScope temps(GetVIXLAssembler());
2009 Register temp_base = temps.AcquireX();
2010
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002011 DCHECK(!dst.IsPreIndex());
2012 DCHECK(!dst.IsPostIndex());
2013
2014 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002015 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002016 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002017 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002018 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002019 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002021 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002022 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002023 {
2024 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2025 __ stlrb(Register(src), base);
2026 if (needs_null_check) {
2027 MaybeRecordImplicitNullCheck(instruction);
2028 }
2029 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002031 case DataType::Type::kUint16:
2032 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002033 {
2034 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2035 __ stlrh(Register(src), base);
2036 if (needs_null_check) {
2037 MaybeRecordImplicitNullCheck(instruction);
2038 }
2039 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002040 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 case DataType::Type::kInt32:
2042 case DataType::Type::kReference:
2043 case DataType::Type::kInt64:
2044 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002045 {
2046 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2047 __ stlr(Register(src), base);
2048 if (needs_null_check) {
2049 MaybeRecordImplicitNullCheck(instruction);
2050 }
2051 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002052 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002053 case DataType::Type::kFloat32:
2054 case DataType::Type::kFloat64: {
2055 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002056 Register temp_src;
2057 if (src.IsZero()) {
2058 // The zero register is used to avoid synthesizing zero constants.
2059 temp_src = Register(src);
2060 } else {
2061 DCHECK(src.IsFPRegister());
2062 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2063 __ Fmov(temp_src, FPRegister(src));
2064 }
Artem Serov914d7a82017-02-07 14:33:49 +00002065 {
2066 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2067 __ stlr(temp_src, base);
2068 if (needs_null_check) {
2069 MaybeRecordImplicitNullCheck(instruction);
2070 }
2071 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002072 break;
2073 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002075 LOG(FATAL) << "Unreachable type " << type;
2076 }
2077}
2078
Calin Juravle175dc732015-08-25 15:42:32 +01002079void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2080 HInstruction* instruction,
2081 uint32_t dex_pc,
2082 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002083 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002084
2085 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2086 {
2087 // Ensure the pc position is recorded immediately after the `blr` instruction.
2088 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2089 __ blr(lr);
2090 if (EntrypointRequiresStackMap(entrypoint)) {
2091 RecordPcInfo(instruction, dex_pc, slow_path);
2092 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002093 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002094}
2095
Roland Levillaindec8f632016-07-22 17:10:06 +01002096void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2097 HInstruction* instruction,
2098 SlowPathCode* slow_path) {
2099 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002100 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2101 __ Blr(lr);
2102}
2103
Alexandre Rames67555f72014-11-18 10:55:16 +00002104void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002105 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002106 UseScratchRegisterScope temps(GetVIXLAssembler());
2107 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00002108 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2109 const size_t status_byte_offset =
2110 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2111 constexpr uint32_t shifted_initialized_value =
2112 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002113
Serban Constantinescu02164b32014-11-13 14:05:07 +00002114 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002115 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00002116 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07002117 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00002118 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00002119 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002120 __ Bind(slow_path->GetExitLabel());
2121}
Alexandre Rames5319def2014-10-23 10:03:10 +01002122
Roland Levillain44015862016-01-22 11:47:17 +00002123void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002124 BarrierType type = BarrierAll;
2125
2126 switch (kind) {
2127 case MemBarrierKind::kAnyAny:
2128 case MemBarrierKind::kAnyStore: {
2129 type = BarrierAll;
2130 break;
2131 }
2132 case MemBarrierKind::kLoadAny: {
2133 type = BarrierReads;
2134 break;
2135 }
2136 case MemBarrierKind::kStoreStore: {
2137 type = BarrierWrites;
2138 break;
2139 }
2140 default:
2141 LOG(FATAL) << "Unexpected memory barrier " << kind;
2142 }
2143 __ Dmb(InnerShareable, type);
2144}
2145
Serban Constantinescu02164b32014-11-13 14:05:07 +00002146void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2147 HBasicBlock* successor) {
2148 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002149 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2150 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002151 slow_path =
2152 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002153 instruction->SetSlowPath(slow_path);
2154 codegen_->AddSlowPath(slow_path);
2155 if (successor != nullptr) {
2156 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002157 }
2158 } else {
2159 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2160 }
2161
Serban Constantinescu02164b32014-11-13 14:05:07 +00002162 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2163 Register temp = temps.AcquireW();
2164
Andreas Gampe542451c2016-07-26 09:02:02 -07002165 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002166 if (successor == nullptr) {
2167 __ Cbnz(temp, slow_path->GetEntryLabel());
2168 __ Bind(slow_path->GetReturnLabel());
2169 } else {
2170 __ Cbz(temp, codegen_->GetLabelOf(successor));
2171 __ B(slow_path->GetEntryLabel());
2172 // slow_path will return to GetLabelOf(successor).
2173 }
2174}
2175
Alexandre Rames5319def2014-10-23 10:03:10 +01002176InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2177 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002178 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002179 assembler_(codegen->GetAssembler()),
2180 codegen_(codegen) {}
2181
Alexandre Rames67555f72014-11-18 10:55:16 +00002182void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002183 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002184 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002185 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002186 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002187 case DataType::Type::kInt32:
2188 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002189 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002190 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002192 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002194 case DataType::Type::kFloat32:
2195 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002196 locations->SetInAt(0, Location::RequiresFpuRegister());
2197 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002198 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002199 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002200
Alexandre Rames5319def2014-10-23 10:03:10 +01002201 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002202 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002203 }
2204}
2205
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002206void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2207 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002208 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2209
2210 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002211 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002212 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002213 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2214 object_field_get_with_read_barrier
2215 ? LocationSummary::kCallOnSlowPath
2216 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002217 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002218 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002219 // We need a temporary register for the read barrier marking slow
2220 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002221 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2222 !Runtime::Current()->UseJitCompilation() &&
2223 !field_info.IsVolatile()) {
2224 // If link-time thunks for the Baker read barrier are enabled, for AOT
2225 // non-volatile loads we need a temporary only if the offset is too big.
2226 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2227 locations->AddTemp(FixedTempLocation());
2228 }
2229 } else {
2230 locations->AddTemp(Location::RequiresRegister());
2231 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002232 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002233 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002235 locations->SetOut(Location::RequiresFpuRegister());
2236 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002237 // The output overlaps for an object field get when read barriers
2238 // are enabled: we do not want the load to overwrite the object's
2239 // location, as we need it to emit the read barrier.
2240 locations->SetOut(
2241 Location::RequiresRegister(),
2242 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002243 }
2244}
2245
2246void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2247 const FieldInfo& field_info) {
2248 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002249 LocationSummary* locations = instruction->GetLocations();
2250 Location base_loc = locations->InAt(0);
2251 Location out = locations->Out();
2252 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002253 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2254 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002255 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002258 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002259 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002260 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002261 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002262 Location maybe_temp =
2263 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002264 // Note that potential implicit null checks are handled in this
2265 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2266 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2267 instruction,
2268 out,
2269 base,
2270 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002271 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002272 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002273 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002274 } else {
2275 // General case.
2276 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002277 // Note that a potential implicit null check is handled in this
2278 // CodeGeneratorARM64::LoadAcquire call.
2279 // NB: LoadAcquire will record the pc info if needed.
2280 codegen_->LoadAcquire(
2281 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002282 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002283 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2284 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002285 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002286 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002287 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002288 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002289 // If read barriers are enabled, emit read barriers other than
2290 // Baker's using a slow path (and also unpoison the loaded
2291 // reference, if heap poisoning is enabled).
2292 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2293 }
Roland Levillain4d027112015-07-01 15:41:14 +01002294 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002295}
2296
2297void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2298 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002299 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002300 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002301 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2302 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002304 locations->SetInAt(1, Location::RequiresFpuRegister());
2305 } else {
2306 locations->SetInAt(1, Location::RequiresRegister());
2307 }
2308}
2309
2310void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002311 const FieldInfo& field_info,
2312 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002313 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2314
2315 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002316 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002317 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002318 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002320
Roland Levillain4d027112015-07-01 15:41:14 +01002321 {
2322 // We use a block to end the scratch scope before the write barrier, thus
2323 // freeing the temporary registers so they can be used in `MarkGCCard`.
2324 UseScratchRegisterScope temps(GetVIXLAssembler());
2325
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002327 DCHECK(value.IsW());
2328 Register temp = temps.AcquireW();
2329 __ Mov(temp, value.W());
2330 GetAssembler()->PoisonHeapReference(temp.W());
2331 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002332 }
Roland Levillain4d027112015-07-01 15:41:14 +01002333
2334 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002335 codegen_->StoreRelease(
2336 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002337 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002338 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2339 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002340 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2341 codegen_->MaybeRecordImplicitNullCheck(instruction);
2342 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002343 }
2344
2345 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002346 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002347 }
2348}
2349
Alexandre Rames67555f72014-11-18 10:55:16 +00002350void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002352
2353 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kInt32:
2355 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002356 Register dst = OutputRegister(instr);
2357 Register lhs = InputRegisterAt(instr, 0);
2358 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002359 if (instr->IsAdd()) {
2360 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002361 } else if (instr->IsAnd()) {
2362 __ And(dst, lhs, rhs);
2363 } else if (instr->IsOr()) {
2364 __ Orr(dst, lhs, rhs);
2365 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002366 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002367 } else if (instr->IsRor()) {
2368 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002369 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002370 __ Ror(dst, lhs, shift);
2371 } else {
2372 // Ensure shift distance is in the same size register as the result. If
2373 // we are rotating a long and the shift comes in a w register originally,
2374 // we don't need to sxtw for use as an x since the shift distances are
2375 // all & reg_bits - 1.
2376 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2377 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002378 } else {
2379 DCHECK(instr->IsXor());
2380 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002381 }
2382 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002383 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kFloat32:
2385 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002386 FPRegister dst = OutputFPRegister(instr);
2387 FPRegister lhs = InputFPRegisterAt(instr, 0);
2388 FPRegister rhs = InputFPRegisterAt(instr, 1);
2389 if (instr->IsAdd()) {
2390 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002391 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002392 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002393 } else {
2394 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002395 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002396 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002397 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002398 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002399 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002400 }
2401}
2402
Serban Constantinescu02164b32014-11-13 14:05:07 +00002403void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2404 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2405
Vladimir Markoca6fff82017-10-03 14:49:14 +01002406 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002408 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 case DataType::Type::kInt32:
2410 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002411 locations->SetInAt(0, Location::RequiresRegister());
2412 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002414 break;
2415 }
2416 default:
2417 LOG(FATAL) << "Unexpected shift type " << type;
2418 }
2419}
2420
2421void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2422 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002424 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002425 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 case DataType::Type::kInt32:
2427 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002428 Register dst = OutputRegister(instr);
2429 Register lhs = InputRegisterAt(instr, 0);
2430 Operand rhs = InputOperandAt(instr, 1);
2431 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002432 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002433 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002434 if (instr->IsShl()) {
2435 __ Lsl(dst, lhs, shift_value);
2436 } else if (instr->IsShr()) {
2437 __ Asr(dst, lhs, shift_value);
2438 } else {
2439 __ Lsr(dst, lhs, shift_value);
2440 }
2441 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002442 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002443
2444 if (instr->IsShl()) {
2445 __ Lsl(dst, lhs, rhs_reg);
2446 } else if (instr->IsShr()) {
2447 __ Asr(dst, lhs, rhs_reg);
2448 } else {
2449 __ Lsr(dst, lhs, rhs_reg);
2450 }
2451 }
2452 break;
2453 }
2454 default:
2455 LOG(FATAL) << "Unexpected shift operation type " << type;
2456 }
2457}
2458
Alexandre Rames5319def2014-10-23 10:03:10 +01002459void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002460 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002461}
2462
2463void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002464 HandleBinaryOp(instruction);
2465}
2466
2467void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2468 HandleBinaryOp(instruction);
2469}
2470
2471void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2472 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002473}
2474
Artem Serov7fc63502016-02-09 17:15:29 +00002475void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002477 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002478 locations->SetInAt(0, Location::RequiresRegister());
2479 // There is no immediate variant of negated bitwise instructions in AArch64.
2480 locations->SetInAt(1, Location::RequiresRegister());
2481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2482}
2483
Artem Serov7fc63502016-02-09 17:15:29 +00002484void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002485 Register dst = OutputRegister(instr);
2486 Register lhs = InputRegisterAt(instr, 0);
2487 Register rhs = InputRegisterAt(instr, 1);
2488
2489 switch (instr->GetOpKind()) {
2490 case HInstruction::kAnd:
2491 __ Bic(dst, lhs, rhs);
2492 break;
2493 case HInstruction::kOr:
2494 __ Orn(dst, lhs, rhs);
2495 break;
2496 case HInstruction::kXor:
2497 __ Eon(dst, lhs, rhs);
2498 break;
2499 default:
2500 LOG(FATAL) << "Unreachable";
2501 }
2502}
2503
Anton Kirilov74234da2017-01-13 14:42:47 +00002504void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2505 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2507 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002508 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002509 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002510 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2511 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2512 } else {
2513 locations->SetInAt(0, Location::RequiresRegister());
2514 }
2515 locations->SetInAt(1, Location::RequiresRegister());
2516 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2517}
2518
Anton Kirilov74234da2017-01-13 14:42:47 +00002519void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2520 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002522 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002523 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002524 Register out = OutputRegister(instruction);
2525 Register left;
2526 if (kind != HInstruction::kNeg) {
2527 left = InputRegisterAt(instruction, 0);
2528 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002529 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002530 // shifter operand operation, the IR generating `right_reg` (input to the type
2531 // conversion) can have a different type from the current instruction's type,
2532 // so we manually indicate the type.
2533 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002534 Operand right_operand(0);
2535
Anton Kirilov74234da2017-01-13 14:42:47 +00002536 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2537 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002538 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2539 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002540 right_operand = Operand(right_reg,
2541 helpers::ShiftFromOpKind(op_kind),
2542 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002543 }
2544
2545 // Logical binary operations do not support extension operations in the
2546 // operand. Note that VIXL would still manage if it was passed by generating
2547 // the extension as a separate instruction.
2548 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2549 DCHECK(!right_operand.IsExtendedRegister() ||
2550 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2551 kind != HInstruction::kNeg));
2552 switch (kind) {
2553 case HInstruction::kAdd:
2554 __ Add(out, left, right_operand);
2555 break;
2556 case HInstruction::kAnd:
2557 __ And(out, left, right_operand);
2558 break;
2559 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002560 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002561 __ Neg(out, right_operand);
2562 break;
2563 case HInstruction::kOr:
2564 __ Orr(out, left, right_operand);
2565 break;
2566 case HInstruction::kSub:
2567 __ Sub(out, left, right_operand);
2568 break;
2569 case HInstruction::kXor:
2570 __ Eor(out, left, right_operand);
2571 break;
2572 default:
2573 LOG(FATAL) << "Unexpected operation kind: " << kind;
2574 UNREACHABLE();
2575 }
2576}
2577
Artem Serov328429f2016-07-06 16:23:04 +01002578void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002579 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002580 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002581 locations->SetInAt(0, Location::RequiresRegister());
2582 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002584}
2585
Roland Levillain19c54192016-11-04 13:44:09 +00002586void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002587 __ Add(OutputRegister(instruction),
2588 InputRegisterAt(instruction, 0),
2589 Operand(InputOperandAt(instruction, 1)));
2590}
2591
Artem Serove1811ed2017-04-27 16:50:47 +01002592void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2593 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002594 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002595
2596 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2597
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 // For byte case we don't need to shift the index variable so we can encode the data offset into
2600 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2601 // data offset constant generation out of the loop and reduce the critical path length in the
2602 // loop.
2603 locations->SetInAt(1, shift->GetValue() == 0
2604 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2605 : Location::RequiresRegister());
2606 locations->SetInAt(2, Location::ConstantLocation(shift));
2607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2608}
2609
2610void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2611 HIntermediateAddressIndex* instruction) {
2612 Register index_reg = InputRegisterAt(instruction, 0);
2613 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2614 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2615
2616 if (shift == 0) {
2617 __ Add(OutputRegister(instruction), index_reg, offset);
2618 } else {
2619 Register offset_reg = InputRegisterAt(instruction, 1);
2620 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2621 }
2622}
2623
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002624void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002625 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002626 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002627 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2628 if (instr->GetOpKind() == HInstruction::kSub &&
2629 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002630 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002631 // Don't allocate register for Mneg instruction.
2632 } else {
2633 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2634 Location::RequiresRegister());
2635 }
2636 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2637 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2639}
2640
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002641void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002642 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002643 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2644 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002645
2646 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2647 // This fixup should be carried out for all multiply-accumulate instructions:
2648 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002650 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2651 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002652 vixl::aarch64::Instruction* prev =
2653 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002654 if (prev->IsLoadOrStore()) {
2655 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002656 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002657 __ nop();
2658 }
2659 }
2660
2661 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002662 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002663 __ Madd(res, mul_left, mul_right, accumulator);
2664 } else {
2665 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002666 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002667 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002668 __ Mneg(res, mul_left, mul_right);
2669 } else {
2670 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2671 __ Msub(res, mul_left, mul_right, accumulator);
2672 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002673 }
2674}
2675
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002676void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002677 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002678 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002679 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002680 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2681 object_array_get_with_read_barrier
2682 ? LocationSummary::kCallOnSlowPath
2683 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002684 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002685 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002686 // We need a temporary register for the read barrier marking slow
2687 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002688 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2689 !Runtime::Current()->UseJitCompilation() &&
2690 instruction->GetIndex()->IsConstant()) {
2691 // Array loads with constant index are treated as field loads.
2692 // If link-time thunks for the Baker read barrier are enabled, for AOT
2693 // constant index loads we need a temporary only if the offset is too big.
2694 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2695 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002697 if (offset >= kReferenceLoadMinFarOffset) {
2698 locations->AddTemp(FixedTempLocation());
2699 }
2700 } else {
2701 locations->AddTemp(Location::RequiresRegister());
2702 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002703 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002704 locations->SetInAt(0, Location::RequiresRegister());
2705 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002707 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2708 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002709 // The output overlaps in the case of an object array get with
2710 // read barriers enabled: we do not want the move to overwrite the
2711 // array's location, as we need it to emit the read barrier.
2712 locations->SetOut(
2713 Location::RequiresRegister(),
2714 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002715 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002716}
2717
2718void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002720 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002721 LocationSummary* locations = instruction->GetLocations();
2722 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002723 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002724 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002725 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2726 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002727 MacroAssembler* masm = GetVIXLAssembler();
2728 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002729
Roland Levillain19c54192016-11-04 13:44:09 +00002730 // The read barrier instrumentation of object ArrayGet instructions
2731 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002733 instruction->GetArray()->IsIntermediateAddress() &&
2734 kEmitCompilerReadBarrier));
2735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002737 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002738 // Note that a potential implicit null check is handled in the
2739 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002740 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002741 if (index.IsConstant()) {
2742 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002744 Location maybe_temp =
2745 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2746 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2747 out,
2748 obj.W(),
2749 offset,
2750 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002751 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002752 /* use_load_acquire */ false);
2753 } else {
2754 Register temp = WRegisterFrom(locations->GetTemp(0));
2755 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002756 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002757 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002758 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002759 // General case.
2760 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002761 Register length;
2762 if (maybe_compressed_char_at) {
2763 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2764 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002765 {
2766 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2767 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2768
2769 if (instruction->GetArray()->IsIntermediateAddress()) {
2770 DCHECK_LT(count_offset, offset);
2771 int64_t adjusted_offset =
2772 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2773 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2774 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2775 } else {
2776 __ Ldr(length, HeapOperand(obj, count_offset));
2777 }
2778 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002779 }
jessicahandojo05765752016-09-09 19:01:32 -07002780 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002781 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002782 if (maybe_compressed_char_at) {
2783 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002784 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2785 "Expecting 0=compressed, 1=uncompressed");
2786 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002787 __ Ldrb(Register(OutputCPURegister(instruction)),
2788 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2789 __ B(&done);
2790 __ Bind(&uncompressed_load);
2791 __ Ldrh(Register(OutputCPURegister(instruction)),
2792 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2793 __ Bind(&done);
2794 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002796 source = HeapOperand(obj, offset);
2797 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002798 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002799 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002800 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002801 // We do not need to compute the intermediate address from the array: the
2802 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002803 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002804 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002805 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002806 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2807 }
2808 temp = obj;
2809 } else {
2810 __ Add(temp, obj, offset);
2811 }
jessicahandojo05765752016-09-09 19:01:32 -07002812 if (maybe_compressed_char_at) {
2813 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002814 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2815 "Expecting 0=compressed, 1=uncompressed");
2816 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002817 __ Ldrb(Register(OutputCPURegister(instruction)),
2818 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2819 __ B(&done);
2820 __ Bind(&uncompressed_load);
2821 __ Ldrh(Register(OutputCPURegister(instruction)),
2822 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2823 __ Bind(&done);
2824 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002825 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002826 }
Roland Levillain44015862016-01-22 11:47:17 +00002827 }
jessicahandojo05765752016-09-09 19:01:32 -07002828 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002829 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2830 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002831 codegen_->Load(type, OutputCPURegister(instruction), source);
2832 codegen_->MaybeRecordImplicitNullCheck(instruction);
2833 }
Roland Levillain44015862016-01-22 11:47:17 +00002834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002836 static_assert(
2837 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2838 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2839 Location obj_loc = locations->InAt(0);
2840 if (index.IsConstant()) {
2841 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2842 } else {
2843 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2844 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002845 }
Roland Levillain4d027112015-07-01 15:41:14 +01002846 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002847}
2848
Alexandre Rames5319def2014-10-23 10:03:10 +01002849void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002850 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002851 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002853}
2854
2855void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002856 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002857 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002858 {
2859 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2860 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2861 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2862 codegen_->MaybeRecordImplicitNullCheck(instruction);
2863 }
jessicahandojo05765752016-09-09 19:01:32 -07002864 // Mask out compression flag from String's array length.
2865 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002866 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002867 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002868}
2869
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002870void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002871 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002872
2873 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002874 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002875 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002876 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002877 LocationSummary::kCallOnSlowPath :
2878 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002879 locations->SetInAt(0, Location::RequiresRegister());
2880 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002881 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2882 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002884 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002885 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002886 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002887 }
2888}
2889
2890void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002892 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002893 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002894 bool needs_write_barrier =
2895 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002896
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002897 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002898 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002899 CPURegister source = value;
2900 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002902 MemOperand destination = HeapOperand(array);
2903 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002904
2905 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002906 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002907 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002909 destination = HeapOperand(array, offset);
2910 } else {
2911 UseScratchRegisterScope temps(masm);
2912 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002913 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002914 // We do not need to compute the intermediate address from the array: the
2915 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002916 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002917 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002918 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002919 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2920 }
2921 temp = array;
2922 } else {
2923 __ Add(temp, array, offset);
2924 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002925 destination = HeapOperand(temp,
2926 XRegisterFrom(index),
2927 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002929 }
Artem Serov914d7a82017-02-07 14:33:49 +00002930 {
2931 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2932 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2933 codegen_->Store(value_type, value, destination);
2934 codegen_->MaybeRecordImplicitNullCheck(instruction);
2935 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002936 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002937 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002938 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002939 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002940 {
2941 // We use a block to end the scratch scope before the write barrier, thus
2942 // freeing the temporary registers so they can be used in `MarkGCCard`.
2943 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002944 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002945 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002947 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002948 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002949 destination = HeapOperand(temp,
2950 XRegisterFrom(index),
2951 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002953 }
2954
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002955 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2956 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2957 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2958
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002959 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002960 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002961 codegen_->AddSlowPath(slow_path);
2962 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002963 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002964 __ Cbnz(Register(value), &non_zero);
2965 if (!index.IsConstant()) {
2966 __ Add(temp, array, offset);
2967 }
Artem Serov914d7a82017-02-07 14:33:49 +00002968 {
2969 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2970 // emitted.
2971 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2972 __ Str(wzr, destination);
2973 codegen_->MaybeRecordImplicitNullCheck(instruction);
2974 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002975 __ B(&done);
2976 __ Bind(&non_zero);
2977 }
2978
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002979 // Note that when Baker read barriers are enabled, the type
2980 // checks are performed without read barriers. This is fine,
2981 // even in the case where a class object is in the from-space
2982 // after the flip, as a comparison involving such a type would
2983 // not produce a false positive; it may of course produce a
2984 // false negative, in which case we would take the ArraySet
2985 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002986
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002987 Register temp2 = temps.AcquireSameSizeAs(array);
2988 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002989 {
2990 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2991 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2992 __ Ldr(temp, HeapOperand(array, class_offset));
2993 codegen_->MaybeRecordImplicitNullCheck(instruction);
2994 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002995 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002996
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002997 // /* HeapReference<Class> */ temp = temp->component_type_
2998 __ Ldr(temp, HeapOperand(temp, component_offset));
2999 // /* HeapReference<Class> */ temp2 = value->klass_
3000 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3001 // If heap poisoning is enabled, no need to unpoison `temp`
3002 // nor `temp2`, as we are comparing two poisoned references.
3003 __ Cmp(temp, temp2);
3004 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003005
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003006 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3007 vixl::aarch64::Label do_put;
3008 __ B(eq, &do_put);
3009 // If heap poisoning is enabled, the `temp` reference has
3010 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003011 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3012
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003013 // /* HeapReference<Class> */ temp = temp->super_class_
3014 __ Ldr(temp, HeapOperand(temp, super_offset));
3015 // If heap poisoning is enabled, no need to unpoison
3016 // `temp`, as we are comparing against null below.
3017 __ Cbnz(temp, slow_path->GetEntryLabel());
3018 __ Bind(&do_put);
3019 } else {
3020 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003021 }
3022 }
3023
3024 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003025 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003026 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003027 __ Mov(temp2, value.W());
3028 GetAssembler()->PoisonHeapReference(temp2);
3029 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003030 }
3031
3032 if (!index.IsConstant()) {
3033 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003034 } else {
3035 // We no longer need the `temp` here so release it as the store below may
3036 // need a scratch register (if the constant index makes the offset too large)
3037 // and the poisoned `source` could be using the other scratch register.
3038 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003039 }
Artem Serov914d7a82017-02-07 14:33:49 +00003040 {
3041 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3042 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3043 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003044
Artem Serov914d7a82017-02-07 14:33:49 +00003045 if (!may_need_runtime_call_for_type_check) {
3046 codegen_->MaybeRecordImplicitNullCheck(instruction);
3047 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003048 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003049 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003050
3051 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3052
3053 if (done.IsLinked()) {
3054 __ Bind(&done);
3055 }
3056
3057 if (slow_path != nullptr) {
3058 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003059 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003060 }
3061}
3062
Alexandre Rames67555f72014-11-18 10:55:16 +00003063void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003064 RegisterSet caller_saves = RegisterSet::Empty();
3065 InvokeRuntimeCallingConvention calling_convention;
3066 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3067 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3068 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003069 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003070 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003071}
3072
3073void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003074 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003075 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003076 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003077 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3078 __ B(slow_path->GetEntryLabel(), hs);
3079}
3080
Alexandre Rames67555f72014-11-18 10:55:16 +00003081void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3082 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003083 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003084 locations->SetInAt(0, Location::RequiresRegister());
3085 if (check->HasUses()) {
3086 locations->SetOut(Location::SameAsFirstInput());
3087 }
3088}
3089
3090void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3091 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003092 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Alexandre Rames67555f72014-11-18 10:55:16 +00003093 check->GetLoadClass(), check, check->GetDexPc(), true);
3094 codegen_->AddSlowPath(slow_path);
3095 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3096}
3097
Roland Levillain1a653882016-03-18 18:05:57 +00003098static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3099 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3100 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3101}
3102
3103void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3104 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3105 Location rhs_loc = instruction->GetLocations()->InAt(1);
3106 if (rhs_loc.IsConstant()) {
3107 // 0.0 is the only immediate that can be encoded directly in
3108 // an FCMP instruction.
3109 //
3110 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3111 // specify that in a floating-point comparison, positive zero
3112 // and negative zero are considered equal, so we can use the
3113 // literal 0.0 for both cases here.
3114 //
3115 // Note however that some methods (Float.equal, Float.compare,
3116 // Float.compareTo, Double.equal, Double.compare,
3117 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3118 // StrictMath.min) consider 0.0 to be (strictly) greater than
3119 // -0.0. So if we ever translate calls to these methods into a
3120 // HCompare instruction, we must handle the -0.0 case with
3121 // care here.
3122 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3123 __ Fcmp(lhs_reg, 0.0);
3124 } else {
3125 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3126 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003127}
3128
Serban Constantinescu02164b32014-11-13 14:05:07 +00003129void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003130 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003131 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003133 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003134 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003135 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003136 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003137 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003138 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kInt32:
3140 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003141 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003142 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3144 break;
3145 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kFloat32:
3147 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003148 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003149 locations->SetInAt(1,
3150 IsFloatingPointZeroConstant(compare->InputAt(1))
3151 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3152 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003153 locations->SetOut(Location::RequiresRegister());
3154 break;
3155 }
3156 default:
3157 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3158 }
3159}
3160
3161void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003162 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003163
3164 // 0 if: left == right
3165 // 1 if: left > right
3166 // -1 if: left < right
3167 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003168 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003169 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003170 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003172 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003173 case DataType::Type::kInt32:
3174 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003175 Register result = OutputRegister(compare);
3176 Register left = InputRegisterAt(compare, 0);
3177 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003178 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003179 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3180 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003181 break;
3182 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 case DataType::Type::kFloat32:
3184 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003185 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003186 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003187 __ Cset(result, ne);
3188 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003189 break;
3190 }
3191 default:
3192 LOG(FATAL) << "Unimplemented compare type " << in_type;
3193 }
3194}
3195
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003196void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003197 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003200 locations->SetInAt(0, Location::RequiresFpuRegister());
3201 locations->SetInAt(1,
3202 IsFloatingPointZeroConstant(instruction->InputAt(1))
3203 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3204 : Location::RequiresFpuRegister());
3205 } else {
3206 // Integer cases.
3207 locations->SetInAt(0, Location::RequiresRegister());
3208 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3209 }
3210
David Brazdilb3e773e2016-01-26 11:28:37 +00003211 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003212 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003213 }
3214}
3215
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003216void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003217 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003218 return;
3219 }
3220
3221 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003222 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003223 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003226 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003227 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003228 } else {
3229 // Integer cases.
3230 Register lhs = InputRegisterAt(instruction, 0);
3231 Operand rhs = InputOperandAt(instruction, 1);
3232 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003233 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003234 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003235}
3236
3237#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3238 M(Equal) \
3239 M(NotEqual) \
3240 M(LessThan) \
3241 M(LessThanOrEqual) \
3242 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003243 M(GreaterThanOrEqual) \
3244 M(Below) \
3245 M(BelowOrEqual) \
3246 M(Above) \
3247 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003248#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003249void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3250void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003251FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003252#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003253#undef FOR_EACH_CONDITION_INSTRUCTION
3254
Zheng Xuc6667102015-05-15 16:08:45 +08003255void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3256 DCHECK(instruction->IsDiv() || instruction->IsRem());
3257
3258 LocationSummary* locations = instruction->GetLocations();
3259 Location second = locations->InAt(1);
3260 DCHECK(second.IsConstant());
3261
3262 Register out = OutputRegister(instruction);
3263 Register dividend = InputRegisterAt(instruction, 0);
3264 int64_t imm = Int64FromConstant(second.GetConstant());
3265 DCHECK(imm == 1 || imm == -1);
3266
3267 if (instruction->IsRem()) {
3268 __ Mov(out, 0);
3269 } else {
3270 if (imm == 1) {
3271 __ Mov(out, dividend);
3272 } else {
3273 __ Neg(out, dividend);
3274 }
3275 }
3276}
3277
3278void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3279 DCHECK(instruction->IsDiv() || instruction->IsRem());
3280
3281 LocationSummary* locations = instruction->GetLocations();
3282 Location second = locations->InAt(1);
3283 DCHECK(second.IsConstant());
3284
3285 Register out = OutputRegister(instruction);
3286 Register dividend = InputRegisterAt(instruction, 0);
3287 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003288 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003289 int ctz_imm = CTZ(abs_imm);
3290
3291 UseScratchRegisterScope temps(GetVIXLAssembler());
3292 Register temp = temps.AcquireSameSizeAs(out);
3293
3294 if (instruction->IsDiv()) {
3295 __ Add(temp, dividend, abs_imm - 1);
3296 __ Cmp(dividend, 0);
3297 __ Csel(out, temp, dividend, lt);
3298 if (imm > 0) {
3299 __ Asr(out, out, ctz_imm);
3300 } else {
3301 __ Neg(out, Operand(out, ASR, ctz_imm));
3302 }
3303 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003304 int bits = instruction->GetResultType() == DataType::Type::kInt32 ? 32 : 64;
Zheng Xuc6667102015-05-15 16:08:45 +08003305 __ Asr(temp, dividend, bits - 1);
3306 __ Lsr(temp, temp, bits - ctz_imm);
3307 __ Add(out, dividend, temp);
3308 __ And(out, out, abs_imm - 1);
3309 __ Sub(out, out, temp);
3310 }
3311}
3312
3313void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3314 DCHECK(instruction->IsDiv() || instruction->IsRem());
3315
3316 LocationSummary* locations = instruction->GetLocations();
3317 Location second = locations->InAt(1);
3318 DCHECK(second.IsConstant());
3319
3320 Register out = OutputRegister(instruction);
3321 Register dividend = InputRegisterAt(instruction, 0);
3322 int64_t imm = Int64FromConstant(second.GetConstant());
3323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003324 DataType::Type type = instruction->GetResultType();
3325 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003326
3327 int64_t magic;
3328 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 CalculateMagicAndShiftForDivRem(
3330 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003331
3332 UseScratchRegisterScope temps(GetVIXLAssembler());
3333 Register temp = temps.AcquireSameSizeAs(out);
3334
3335 // temp = get_high(dividend * magic)
3336 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003337 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003338 __ Smulh(temp, dividend, temp);
3339 } else {
3340 __ Smull(temp.X(), dividend, temp);
3341 __ Lsr(temp.X(), temp.X(), 32);
3342 }
3343
3344 if (imm > 0 && magic < 0) {
3345 __ Add(temp, temp, dividend);
3346 } else if (imm < 0 && magic > 0) {
3347 __ Sub(temp, temp, dividend);
3348 }
3349
3350 if (shift != 0) {
3351 __ Asr(temp, temp, shift);
3352 }
3353
3354 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003355 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003356 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003357 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003358 // TODO: Strength reduction for msub.
3359 Register temp_imm = temps.AcquireSameSizeAs(out);
3360 __ Mov(temp_imm, imm);
3361 __ Msub(out, temp, temp_imm, dividend);
3362 }
3363}
3364
3365void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3366 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003367 DataType::Type type = instruction->GetResultType();
3368 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003369
3370 LocationSummary* locations = instruction->GetLocations();
3371 Register out = OutputRegister(instruction);
3372 Location second = locations->InAt(1);
3373
3374 if (second.IsConstant()) {
3375 int64_t imm = Int64FromConstant(second.GetConstant());
3376
3377 if (imm == 0) {
3378 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3379 } else if (imm == 1 || imm == -1) {
3380 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003381 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003382 DivRemByPowerOfTwo(instruction);
3383 } else {
3384 DCHECK(imm <= -2 || imm >= 2);
3385 GenerateDivRemWithAnyConstant(instruction);
3386 }
3387 } else {
3388 Register dividend = InputRegisterAt(instruction, 0);
3389 Register divisor = InputRegisterAt(instruction, 1);
3390 if (instruction->IsDiv()) {
3391 __ Sdiv(out, dividend, divisor);
3392 } else {
3393 UseScratchRegisterScope temps(GetVIXLAssembler());
3394 Register temp = temps.AcquireSameSizeAs(out);
3395 __ Sdiv(temp, dividend, divisor);
3396 __ Msub(out, temp, divisor, dividend);
3397 }
3398 }
3399}
3400
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003401void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3402 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003403 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003404 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003405 case DataType::Type::kInt32:
3406 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003407 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003408 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3410 break;
3411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 case DataType::Type::kFloat32:
3413 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003414 locations->SetInAt(0, Location::RequiresFpuRegister());
3415 locations->SetInAt(1, Location::RequiresFpuRegister());
3416 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3417 break;
3418
3419 default:
3420 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3421 }
3422}
3423
3424void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003425 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003426 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003427 case DataType::Type::kInt32:
3428 case DataType::Type::kInt64:
Zheng Xuc6667102015-05-15 16:08:45 +08003429 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003430 break;
3431
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003432 case DataType::Type::kFloat32:
3433 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003434 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3435 break;
3436
3437 default:
3438 LOG(FATAL) << "Unexpected div type " << type;
3439 }
3440}
3441
Alexandre Rames67555f72014-11-18 10:55:16 +00003442void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003443 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003444 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003445}
3446
3447void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3448 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003449 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003450 codegen_->AddSlowPath(slow_path);
3451 Location value = instruction->GetLocations()->InAt(0);
3452
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003454
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003455 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003456 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003457 return;
3458 }
3459
Alexandre Rames67555f72014-11-18 10:55:16 +00003460 if (value.IsConstant()) {
3461 int64_t divisor = Int64ConstantFrom(value);
3462 if (divisor == 0) {
3463 __ B(slow_path->GetEntryLabel());
3464 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003465 // A division by a non-null constant is valid. We don't need to perform
3466 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003467 }
3468 } else {
3469 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3470 }
3471}
3472
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003473void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3474 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003475 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003476 locations->SetOut(Location::ConstantLocation(constant));
3477}
3478
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003479void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3480 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003481 // Will be generated at use site.
3482}
3483
Alexandre Rames5319def2014-10-23 10:03:10 +01003484void LocationsBuilderARM64::VisitExit(HExit* exit) {
3485 exit->SetLocations(nullptr);
3486}
3487
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003488void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003489}
3490
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003491void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3492 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003493 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003494 locations->SetOut(Location::ConstantLocation(constant));
3495}
3496
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003497void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003498 // Will be generated at use site.
3499}
3500
David Brazdilfc6a86a2015-06-26 10:33:45 +00003501void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003502 if (successor->IsExitBlock()) {
3503 DCHECK(got->GetPrevious()->AlwaysThrows());
3504 return; // no code needed
3505 }
3506
Serban Constantinescu02164b32014-11-13 14:05:07 +00003507 HBasicBlock* block = got->GetBlock();
3508 HInstruction* previous = got->GetPrevious();
3509 HLoopInformation* info = block->GetLoopInformation();
3510
David Brazdil46e2a392015-03-16 17:31:52 +00003511 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003512 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3513 UseScratchRegisterScope temps(GetVIXLAssembler());
3514 Register temp1 = temps.AcquireX();
3515 Register temp2 = temps.AcquireX();
3516 __ Ldr(temp1, MemOperand(sp, 0));
3517 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3518 __ Add(temp2, temp2, 1);
3519 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3520 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003521 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3522 return;
3523 }
3524 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3525 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003526 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003527 }
3528 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003529 __ B(codegen_->GetLabelOf(successor));
3530 }
3531}
3532
David Brazdilfc6a86a2015-06-26 10:33:45 +00003533void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3534 got->SetLocations(nullptr);
3535}
3536
3537void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3538 HandleGoto(got, got->GetSuccessor());
3539}
3540
3541void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3542 try_boundary->SetLocations(nullptr);
3543}
3544
3545void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3546 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3547 if (!successor->IsExitBlock()) {
3548 HandleGoto(try_boundary, successor);
3549 }
3550}
3551
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003552void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003553 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003554 vixl::aarch64::Label* true_target,
3555 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003556 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003557
David Brazdil0debae72015-11-12 18:37:00 +00003558 if (true_target == nullptr && false_target == nullptr) {
3559 // Nothing to do. The code always falls through.
3560 return;
3561 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003562 // Constant condition, statically compared against "true" (integer value 1).
3563 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003564 if (true_target != nullptr) {
3565 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003566 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003567 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003568 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003569 if (false_target != nullptr) {
3570 __ B(false_target);
3571 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003572 }
David Brazdil0debae72015-11-12 18:37:00 +00003573 return;
3574 }
3575
3576 // The following code generates these patterns:
3577 // (1) true_target == nullptr && false_target != nullptr
3578 // - opposite condition true => branch to false_target
3579 // (2) true_target != nullptr && false_target == nullptr
3580 // - condition true => branch to true_target
3581 // (3) true_target != nullptr && false_target != nullptr
3582 // - condition true => branch to true_target
3583 // - branch to false_target
3584 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003585 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003586 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003587 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003588 if (true_target == nullptr) {
3589 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3590 } else {
3591 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3592 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003593 } else {
3594 // The condition instruction has not been materialized, use its inputs as
3595 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003596 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003598 DataType::Type type = condition->InputAt(0)->GetType();
3599 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003600 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003601 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003602 IfCondition opposite_condition = condition->GetOppositeCondition();
3603 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003604 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003605 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003606 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003607 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003608 // Integer cases.
3609 Register lhs = InputRegisterAt(condition, 0);
3610 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003611
3612 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003613 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003614 if (true_target == nullptr) {
3615 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3616 non_fallthrough_target = false_target;
3617 } else {
3618 arm64_cond = ARM64Condition(condition->GetCondition());
3619 non_fallthrough_target = true_target;
3620 }
3621
Aart Bik086d27e2016-01-20 17:02:00 -08003622 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003623 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003624 switch (arm64_cond) {
3625 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003626 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003627 break;
3628 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003629 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003630 break;
3631 case lt:
3632 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003633 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003634 break;
3635 case ge:
3636 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003637 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003638 break;
3639 default:
3640 // Without the `static_cast` the compiler throws an error for
3641 // `-Werror=sign-promo`.
3642 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3643 }
3644 } else {
3645 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003646 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003647 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003648 }
3649 }
David Brazdil0debae72015-11-12 18:37:00 +00003650
3651 // If neither branch falls through (case 3), the conditional branch to `true_target`
3652 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3653 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003654 __ B(false_target);
3655 }
3656}
3657
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003658void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003659 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003660 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003661 locations->SetInAt(0, Location::RequiresRegister());
3662 }
3663}
3664
3665void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003666 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3667 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003668 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3669 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3670 true_target = nullptr;
3671 }
3672 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3673 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3674 false_target = nullptr;
3675 }
David Brazdil0debae72015-11-12 18:37:00 +00003676 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003677}
3678
3679void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003680 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003681 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003682 InvokeRuntimeCallingConvention calling_convention;
3683 RegisterSet caller_saves = RegisterSet::Empty();
3684 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3685 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003686 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003687 locations->SetInAt(0, Location::RequiresRegister());
3688 }
3689}
3690
3691void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003692 SlowPathCodeARM64* slow_path =
3693 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003694 GenerateTestAndBranch(deoptimize,
3695 /* condition_input_index */ 0,
3696 slow_path->GetEntryLabel(),
3697 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003698}
3699
Mingyao Yang063fc772016-08-02 11:02:54 -07003700void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003701 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003702 LocationSummary(flag, LocationSummary::kNoCall);
3703 locations->SetOut(Location::RequiresRegister());
3704}
3705
3706void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3707 __ Ldr(OutputRegister(flag),
3708 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3709}
3710
David Brazdilc0b601b2016-02-08 14:20:45 +00003711static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3712 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003713 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003714}
3715
Alexandre Rames880f1192016-06-13 16:04:50 +01003716static inline Condition GetConditionForSelect(HCondition* condition) {
3717 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003718 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3719 : ARM64Condition(cond);
3720}
3721
David Brazdil74eb1b22015-12-14 11:44:01 +00003722void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003723 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003724 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003725 locations->SetInAt(0, Location::RequiresFpuRegister());
3726 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003727 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003728 } else {
3729 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3730 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3731 bool is_true_value_constant = cst_true_value != nullptr;
3732 bool is_false_value_constant = cst_false_value != nullptr;
3733 // Ask VIXL whether we should synthesize constants in registers.
3734 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3735 Operand true_op = is_true_value_constant ?
3736 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3737 Operand false_op = is_false_value_constant ?
3738 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3739 bool true_value_in_register = false;
3740 bool false_value_in_register = false;
3741 MacroAssembler::GetCselSynthesisInformation(
3742 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3743 true_value_in_register |= !is_true_value_constant;
3744 false_value_in_register |= !is_false_value_constant;
3745
3746 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3747 : Location::ConstantLocation(cst_true_value));
3748 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3749 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003750 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003751 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003752
David Brazdil74eb1b22015-12-14 11:44:01 +00003753 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3754 locations->SetInAt(2, Location::RequiresRegister());
3755 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003756}
3757
3758void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003759 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003760 Condition csel_cond;
3761
3762 if (IsBooleanValueOrMaterializedCondition(cond)) {
3763 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003764 // Use the condition flags set by the previous instruction.
3765 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003766 } else {
3767 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003768 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003769 }
3770 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003771 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003772 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003773 } else {
3774 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003775 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003776 }
3777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003778 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003779 __ Fcsel(OutputFPRegister(select),
3780 InputFPRegisterAt(select, 1),
3781 InputFPRegisterAt(select, 0),
3782 csel_cond);
3783 } else {
3784 __ Csel(OutputRegister(select),
3785 InputOperandAt(select, 1),
3786 InputOperandAt(select, 0),
3787 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003788 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003789}
3790
David Srbecky0cf44932015-12-09 14:09:59 +00003791void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003792 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003793}
3794
David Srbeckyd28f4a02016-03-14 17:14:24 +00003795void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3796 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003797}
3798
3799void CodeGeneratorARM64::GenerateNop() {
3800 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003801}
3802
Alexandre Rames5319def2014-10-23 10:03:10 +01003803void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003804 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003805}
3806
3807void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003808 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003809}
3810
3811void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003812 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003813}
3814
3815void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003816 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003817}
3818
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003819// Temp is used for read barrier.
3820static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3821 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003822 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003823 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3824 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3825 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3826 return 1;
3827 }
3828 return 0;
3829}
3830
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003831// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003832// interface pointer, one for loading the current interface.
3833// The other checks have one temp for loading the object's class.
3834static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3835 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3836 return 3;
3837 }
3838 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003839}
3840
Alexandre Rames67555f72014-11-18 10:55:16 +00003841void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003842 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003843 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003844 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003845 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846 case TypeCheckKind::kExactCheck:
3847 case TypeCheckKind::kAbstractClassCheck:
3848 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003849 case TypeCheckKind::kArrayObjectCheck: {
3850 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3851 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3852 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003853 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003854 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003855 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856 case TypeCheckKind::kUnresolvedCheck:
3857 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003858 call_kind = LocationSummary::kCallOnSlowPath;
3859 break;
3860 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003861
Vladimir Markoca6fff82017-10-03 14:49:14 +01003862 LocationSummary* locations =
3863 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003864 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003865 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003866 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003867 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003868 locations->SetInAt(1, Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 // The "out" register is used as a temporary, so it overlaps with the inputs.
3870 // Note that TypeCheckSlowPathARM64 uses this register too.
3871 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003872 // Add temps if necessary for read barriers.
3873 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003874}
3875
3876void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003877 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003878 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003879 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003880 Register obj = InputRegisterAt(instruction, 0);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003881 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003882 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003883 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003884 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3885 DCHECK_LE(num_temps, 1u);
3886 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003887 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3888 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3889 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3890 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003891
Scott Wakeling97c72b72016-06-24 16:19:36 +01003892 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003893 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003894
3895 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003896 // Avoid null check if we know `obj` is not null.
3897 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003898 __ Cbz(obj, &zero);
3899 }
3900
Roland Levillain44015862016-01-22 11:47:17 +00003901 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003902 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003903 ReadBarrierOption read_barrier_option =
3904 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003905 // /* HeapReference<Class> */ out = obj->klass_
3906 GenerateReferenceLoadTwoRegisters(instruction,
3907 out_loc,
3908 obj_loc,
3909 class_offset,
3910 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003911 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 __ Cmp(out, cls);
3913 __ Cset(out, eq);
3914 if (zero.IsLinked()) {
3915 __ B(&done);
3916 }
3917 break;
3918 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003919
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003920 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003921 ReadBarrierOption read_barrier_option =
3922 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003923 // /* HeapReference<Class> */ out = obj->klass_
3924 GenerateReferenceLoadTwoRegisters(instruction,
3925 out_loc,
3926 obj_loc,
3927 class_offset,
3928 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003929 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003930 // If the class is abstract, we eagerly fetch the super class of the
3931 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003932 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003934 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003935 GenerateReferenceLoadOneRegister(instruction,
3936 out_loc,
3937 super_offset,
3938 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003939 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003940 // If `out` is null, we use it for the result, and jump to `done`.
3941 __ Cbz(out, &done);
3942 __ Cmp(out, cls);
3943 __ B(ne, &loop);
3944 __ Mov(out, 1);
3945 if (zero.IsLinked()) {
3946 __ B(&done);
3947 }
3948 break;
3949 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003950
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003952 ReadBarrierOption read_barrier_option =
3953 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003954 // /* HeapReference<Class> */ out = obj->klass_
3955 GenerateReferenceLoadTwoRegisters(instruction,
3956 out_loc,
3957 obj_loc,
3958 class_offset,
3959 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003960 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003962 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003963 __ Bind(&loop);
3964 __ Cmp(out, cls);
3965 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003966 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003967 GenerateReferenceLoadOneRegister(instruction,
3968 out_loc,
3969 super_offset,
3970 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003971 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003972 __ Cbnz(out, &loop);
3973 // If `out` is null, we use it for the result, and jump to `done`.
3974 __ B(&done);
3975 __ Bind(&success);
3976 __ Mov(out, 1);
3977 if (zero.IsLinked()) {
3978 __ B(&done);
3979 }
3980 break;
3981 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003982
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003983 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003984 ReadBarrierOption read_barrier_option =
3985 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003986 // /* HeapReference<Class> */ out = obj->klass_
3987 GenerateReferenceLoadTwoRegisters(instruction,
3988 out_loc,
3989 obj_loc,
3990 class_offset,
3991 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003992 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003993 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003994 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003995 __ Cmp(out, cls);
3996 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003998 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003999 GenerateReferenceLoadOneRegister(instruction,
4000 out_loc,
4001 component_offset,
4002 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004003 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004004 // If `out` is null, we use it for the result, and jump to `done`.
4005 __ Cbz(out, &done);
4006 __ Ldrh(out, HeapOperand(out, primitive_offset));
4007 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4008 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004009 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010 __ Mov(out, 1);
4011 __ B(&done);
4012 break;
4013 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004014
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004015 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004016 // No read barrier since the slow path will retry upon failure.
4017 // /* HeapReference<Class> */ out = obj->klass_
4018 GenerateReferenceLoadTwoRegisters(instruction,
4019 out_loc,
4020 obj_loc,
4021 class_offset,
4022 maybe_temp_loc,
4023 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004024 __ Cmp(out, cls);
4025 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004026 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4027 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004028 codegen_->AddSlowPath(slow_path);
4029 __ B(ne, slow_path->GetEntryLabel());
4030 __ Mov(out, 1);
4031 if (zero.IsLinked()) {
4032 __ B(&done);
4033 }
4034 break;
4035 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004036
Calin Juravle98893e12015-10-02 21:05:03 +01004037 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004038 case TypeCheckKind::kInterfaceCheck: {
4039 // Note that we indeed only call on slow path, but we always go
4040 // into the slow path for the unresolved and interface check
4041 // cases.
4042 //
4043 // We cannot directly call the InstanceofNonTrivial runtime
4044 // entry point without resorting to a type checking slow path
4045 // here (i.e. by calling InvokeRuntime directly), as it would
4046 // require to assign fixed registers for the inputs of this
4047 // HInstanceOf instruction (following the runtime calling
4048 // convention), which might be cluttered by the potential first
4049 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004050 //
4051 // TODO: Introduce a new runtime entry point taking the object
4052 // to test (instead of its class) as argument, and let it deal
4053 // with the read barrier issues. This will let us refactor this
4054 // case of the `switch` code as it was previously (with a direct
4055 // call to the runtime not using a type checking slow path).
4056 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004058 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4059 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004060 codegen_->AddSlowPath(slow_path);
4061 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004062 if (zero.IsLinked()) {
4063 __ B(&done);
4064 }
4065 break;
4066 }
4067 }
4068
4069 if (zero.IsLinked()) {
4070 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004071 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004072 }
4073
4074 if (done.IsLinked()) {
4075 __ Bind(&done);
4076 }
4077
4078 if (slow_path != nullptr) {
4079 __ Bind(slow_path->GetExitLabel());
4080 }
4081}
4082
4083void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004084 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004085 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004086 LocationSummary* locations =
4087 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00004089 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004090 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4091 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004092}
4093
4094void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004095 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004096 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004098 Register obj = InputRegisterAt(instruction, 0);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00004099 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004100 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4101 DCHECK_GE(num_temps, 1u);
4102 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004103 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004104 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4105 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004106 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004107 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4108 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4109 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4110 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4111 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4112 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4113 const uint32_t object_array_data_offset =
4114 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115
Vladimir Marko87584542017-12-12 17:47:52 +00004116 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004118 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4119 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004121
Scott Wakeling97c72b72016-06-24 16:19:36 +01004122 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004123 // Avoid null check if we know obj is not null.
4124 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004125 __ Cbz(obj, &done);
4126 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004127
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004128 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 case TypeCheckKind::kExactCheck:
4130 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004131 // /* HeapReference<Class> */ temp = obj->klass_
4132 GenerateReferenceLoadTwoRegisters(instruction,
4133 temp_loc,
4134 obj_loc,
4135 class_offset,
4136 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004137 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004138
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004139 __ Cmp(temp, cls);
4140 // Jump to slow path for throwing the exception or doing a
4141 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004142 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004143 break;
4144 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004145
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004146 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004147 // /* HeapReference<Class> */ temp = obj->klass_
4148 GenerateReferenceLoadTwoRegisters(instruction,
4149 temp_loc,
4150 obj_loc,
4151 class_offset,
4152 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004153 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004154
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004155 // If the class is abstract, we eagerly fetch the super class of the
4156 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004157 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004159 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004160 GenerateReferenceLoadOneRegister(instruction,
4161 temp_loc,
4162 super_offset,
4163 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004164 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004165
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004166 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4167 // exception.
4168 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4169 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004170 __ Cmp(temp, cls);
4171 __ B(ne, &loop);
4172 break;
4173 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004174
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004175 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004176 // /* HeapReference<Class> */ temp = obj->klass_
4177 GenerateReferenceLoadTwoRegisters(instruction,
4178 temp_loc,
4179 obj_loc,
4180 class_offset,
4181 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004182 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004184 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004185 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004186 __ Bind(&loop);
4187 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004188 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004189
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004190 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004191 GenerateReferenceLoadOneRegister(instruction,
4192 temp_loc,
4193 super_offset,
4194 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004195 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196
4197 // If the class reference currently in `temp` is not null, jump
4198 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004199 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004200 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004202 break;
4203 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004205 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004206 // /* HeapReference<Class> */ temp = obj->klass_
4207 GenerateReferenceLoadTwoRegisters(instruction,
4208 temp_loc,
4209 obj_loc,
4210 class_offset,
4211 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004212 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004213
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004214 // Do an exact check.
4215 __ Cmp(temp, cls);
4216 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004217
4218 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004220 GenerateReferenceLoadOneRegister(instruction,
4221 temp_loc,
4222 component_offset,
4223 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004224 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004225
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004226 // If the component type is null, jump to the slow path to throw the exception.
4227 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4228 // Otherwise, the object is indeed an array. Further check that this component type is not a
4229 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004230 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4231 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004232 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004233 break;
4234 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004235
Calin Juravle98893e12015-10-02 21:05:03 +01004236 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004237 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238 //
4239 // We cannot directly call the CheckCast runtime entry point
4240 // without resorting to a type checking slow path here (i.e. by
4241 // calling InvokeRuntime directly), as it would require to
4242 // assign fixed registers for the inputs of this HInstanceOf
4243 // instruction (following the runtime calling convention), which
4244 // might be cluttered by the potential first read barrier
4245 // emission at the beginning of this method.
4246 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004247 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004248 case TypeCheckKind::kInterfaceCheck: {
4249 // /* HeapReference<Class> */ temp = obj->klass_
4250 GenerateReferenceLoadTwoRegisters(instruction,
4251 temp_loc,
4252 obj_loc,
4253 class_offset,
4254 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004255 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004256
4257 // /* HeapReference<Class> */ temp = temp->iftable_
4258 GenerateReferenceLoadTwoRegisters(instruction,
4259 temp_loc,
4260 temp_loc,
4261 iftable_offset,
4262 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004263 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004264 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004265 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004266 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004267 vixl::aarch64::Label start_loop;
4268 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004269 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004270 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4271 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004272 // Go to next interface.
4273 __ Add(temp, temp, 2 * kHeapReferenceSize);
4274 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004275 // Compare the classes and continue the loop if they do not match.
4276 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4277 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004278 break;
4279 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004280 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004281 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004282
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004283 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004284}
4285
Alexandre Rames5319def2014-10-23 10:03:10 +01004286void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004287 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004288 locations->SetOut(Location::ConstantLocation(constant));
4289}
4290
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004291void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004292 // Will be generated at use site.
4293}
4294
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004295void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004296 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004297 locations->SetOut(Location::ConstantLocation(constant));
4298}
4299
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004300void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004301 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004302}
4303
Calin Juravle175dc732015-08-25 15:42:32 +01004304void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4305 // The trampoline uses the same calling convention as dex calling conventions,
4306 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4307 // the method_idx.
4308 HandleInvoke(invoke);
4309}
4310
4311void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4312 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004313 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004314}
4315
Alexandre Rames5319def2014-10-23 10:03:10 +01004316void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004317 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004318 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004319}
4320
Alexandre Rames67555f72014-11-18 10:55:16 +00004321void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4322 HandleInvoke(invoke);
4323}
4324
4325void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4326 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004327 LocationSummary* locations = invoke->GetLocations();
4328 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004329 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004330 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004331 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004332
4333 // The register ip1 is required to be used for the hidden argument in
4334 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004335 MacroAssembler* masm = GetVIXLAssembler();
4336 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004337 scratch_scope.Exclude(ip1);
4338 __ Mov(ip1, invoke->GetDexMethodIndex());
4339
Artem Serov914d7a82017-02-07 14:33:49 +00004340 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004341 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004342 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004343 {
4344 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4345 // /* HeapReference<Class> */ temp = temp->klass_
4346 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4347 codegen_->MaybeRecordImplicitNullCheck(invoke);
4348 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004349 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004350 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004351 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004352 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004353 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004354 }
Artem Serov914d7a82017-02-07 14:33:49 +00004355
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004356 // Instead of simply (possibly) unpoisoning `temp` here, we should
4357 // emit a read barrier for the previous class reference load.
4358 // However this is not required in practice, as this is an
4359 // intermediate/temporary reference and because the current
4360 // concurrent copying collector keeps the from-space memory
4361 // intact/accessible until the end of the marking phase (the
4362 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004363 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004364 __ Ldr(temp,
4365 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4366 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004367 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004368 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004369 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004370 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004371 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004372
4373 {
4374 // Ensure the pc position is recorded immediately after the `blr` instruction.
4375 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4376
4377 // lr();
4378 __ blr(lr);
4379 DCHECK(!codegen_->IsLeafMethod());
4380 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4381 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004382
4383 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004384}
4385
4386void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004387 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004388 if (intrinsic.TryDispatch(invoke)) {
4389 return;
4390 }
4391
Alexandre Rames67555f72014-11-18 10:55:16 +00004392 HandleInvoke(invoke);
4393}
4394
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004395void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004396 // Explicit clinit checks triggered by static invokes must have been pruned by
4397 // art::PrepareForRegisterAllocation.
4398 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004399
Vladimir Markoca6fff82017-10-03 14:49:14 +01004400 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004401 if (intrinsic.TryDispatch(invoke)) {
4402 return;
4403 }
4404
Alexandre Rames67555f72014-11-18 10:55:16 +00004405 HandleInvoke(invoke);
4406}
4407
Andreas Gampe878d58c2015-01-15 23:24:00 -08004408static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4409 if (invoke->GetLocations()->Intrinsified()) {
4410 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4411 intrinsic.Dispatch(invoke);
4412 return true;
4413 }
4414 return false;
4415}
4416
Vladimir Markodc151b22015-10-15 18:02:30 +01004417HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4418 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004419 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004420 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004421 return desired_dispatch_info;
4422}
4423
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004424void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4425 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004426 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004427 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4428 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004429 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4430 uint32_t offset =
4431 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004432 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004433 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004434 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004435 }
Vladimir Marko58155012015-08-19 12:49:41 +00004436 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004437 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004438 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004439 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4440 DCHECK(GetCompilerOptions().IsBootImage());
4441 // Add ADRP with its PC-relative method patch.
4442 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4443 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4444 // Add ADD with its PC-relative method patch.
4445 vixl::aarch64::Label* add_label =
4446 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4447 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4448 break;
4449 }
Vladimir Marko58155012015-08-19 12:49:41 +00004450 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4451 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004452 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004453 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004454 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004455 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004456 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4457 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004458 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004459 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004460 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004461 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004462 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004463 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004464 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004465 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4466 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4467 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004468 }
4469 }
4470
4471 switch (invoke->GetCodePtrLocation()) {
4472 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004473 {
4474 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4475 ExactAssemblyScope eas(GetVIXLAssembler(),
4476 kInstructionSize,
4477 CodeBufferCheckScope::kExactSize);
4478 __ bl(&frame_entry_label_);
4479 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4480 }
Vladimir Marko58155012015-08-19 12:49:41 +00004481 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004482 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4483 // LR = callee_method->entry_point_from_quick_compiled_code_;
4484 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004485 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004486 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004487 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004488 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004489 ExactAssemblyScope eas(GetVIXLAssembler(),
4490 kInstructionSize,
4491 CodeBufferCheckScope::kExactSize);
4492 // lr()
4493 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004494 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004495 }
Vladimir Marko58155012015-08-19 12:49:41 +00004496 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004497 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004498
Andreas Gampe878d58c2015-01-15 23:24:00 -08004499 DCHECK(!IsLeafMethod());
4500}
4501
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004502void CodeGeneratorARM64::GenerateVirtualCall(
4503 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004504 // Use the calling convention instead of the location of the receiver, as
4505 // intrinsics may have put the receiver in a different register. In the intrinsics
4506 // slow path, the arguments have been moved to the right place, so here we are
4507 // guaranteed that the receiver is the first register of the calling convention.
4508 InvokeDexCallingConvention calling_convention;
4509 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004510 Register temp = XRegisterFrom(temp_in);
4511 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4512 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4513 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004514 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004515
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004516 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004517
4518 {
4519 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4520 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4521 // /* HeapReference<Class> */ temp = receiver->klass_
4522 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4523 MaybeRecordImplicitNullCheck(invoke);
4524 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004525 // Instead of simply (possibly) unpoisoning `temp` here, we should
4526 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004527 // intermediate/temporary reference and because the current
4528 // concurrent copying collector keeps the from-space memory
4529 // intact/accessible until the end of the marking phase (the
4530 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004531 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4532 // temp = temp->GetMethodAt(method_offset);
4533 __ Ldr(temp, MemOperand(temp, method_offset));
4534 // lr = temp->GetEntryPoint();
4535 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004536 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004537 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004538 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4539 // lr();
4540 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004541 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004542 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004543}
4544
Orion Hodsonac141392017-01-13 11:53:47 +00004545void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4546 HandleInvoke(invoke);
4547}
4548
4549void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4550 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004551 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004552}
4553
Vladimir Marko65979462017-05-19 17:25:12 +01004554vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4555 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004556 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004557 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004558 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01004559 adrp_label,
4560 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004561}
4562
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004563vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4564 MethodReference target_method,
4565 vixl::aarch64::Label* adrp_label) {
4566 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004567 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004568 adrp_label,
4569 &method_bss_entry_patches_);
4570}
4571
Scott Wakeling97c72b72016-06-24 16:19:36 +01004572vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4573 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004574 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004575 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004576 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004577}
4578
Vladimir Marko1998cd02017-01-13 13:02:58 +00004579vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4580 const DexFile& dex_file,
4581 dex::TypeIndex type_index,
4582 vixl::aarch64::Label* adrp_label) {
4583 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4584}
4585
Vladimir Marko65979462017-05-19 17:25:12 +01004586vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4587 const DexFile& dex_file,
4588 dex::StringIndex string_index,
4589 vixl::aarch64::Label* adrp_label) {
4590 return
4591 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4592}
4593
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004594vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4595 const DexFile& dex_file,
4596 dex::StringIndex string_index,
4597 vixl::aarch64::Label* adrp_label) {
4598 return NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
4599}
4600
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004601vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4602 baker_read_barrier_patches_.emplace_back(custom_data);
4603 return &baker_read_barrier_patches_.back().label;
4604}
4605
Scott Wakeling97c72b72016-06-24 16:19:36 +01004606vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4607 const DexFile& dex_file,
4608 uint32_t offset_or_index,
4609 vixl::aarch64::Label* adrp_label,
4610 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004611 // Add a patch entry and return the label.
4612 patches->emplace_back(dex_file, offset_or_index);
4613 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004614 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004615 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4616 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4617 return label;
4618}
4619
Scott Wakeling97c72b72016-06-24 16:19:36 +01004620vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4621 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004622 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004623}
4624
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004625vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004626 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004627 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004628 return jit_string_patches_.GetOrCreate(
4629 StringReference(&dex_file, string_index),
4630 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4631}
4632
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004633vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004634 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004635 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004636 return jit_class_patches_.GetOrCreate(
4637 TypeReference(&dex_file, type_index),
4638 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4639}
4640
Vladimir Markoaad75c62016-10-03 08:46:48 +00004641void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4642 vixl::aarch64::Register reg) {
4643 DCHECK(reg.IsX());
4644 SingleEmissionCheckScope guard(GetVIXLAssembler());
4645 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004646 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004647}
4648
4649void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4650 vixl::aarch64::Register out,
4651 vixl::aarch64::Register base) {
4652 DCHECK(out.IsX());
4653 DCHECK(base.IsX());
4654 SingleEmissionCheckScope guard(GetVIXLAssembler());
4655 __ Bind(fixup_label);
4656 __ add(out, base, Operand(/* offset placeholder */ 0));
4657}
4658
4659void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4660 vixl::aarch64::Register out,
4661 vixl::aarch64::Register base) {
4662 DCHECK(base.IsX());
4663 SingleEmissionCheckScope guard(GetVIXLAssembler());
4664 __ Bind(fixup_label);
4665 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4666}
4667
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004668template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004669inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4670 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004671 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004672 for (const PcRelativePatchInfo& info : infos) {
4673 linker_patches->push_back(Factory(info.label.GetLocation(),
4674 &info.target_dex_file,
4675 info.pc_insn_label->GetLocation(),
4676 info.offset_or_index));
4677 }
4678}
4679
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004680void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004681 DCHECK(linker_patches->empty());
4682 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004683 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004684 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004685 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004686 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004687 pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004688 string_bss_entry_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004689 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004690 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004691 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004692 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4693 pc_relative_method_patches_, linker_patches);
4694 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4695 pc_relative_type_patches_, linker_patches);
4696 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4697 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004698 } else {
4699 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004700 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4701 pc_relative_type_patches_, linker_patches);
4702 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4703 pc_relative_string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004704 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004705 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4706 method_bss_entry_patches_, linker_patches);
4707 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4708 type_bss_entry_patches_, linker_patches);
4709 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4710 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004711 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004712 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4713 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004714 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004715 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004716}
4717
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004718vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4719 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004720 value,
4721 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4722}
4723
Scott Wakeling97c72b72016-06-24 16:19:36 +01004724vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004725 return uint64_literals_.GetOrCreate(
4726 value,
4727 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004728}
4729
Andreas Gampe878d58c2015-01-15 23:24:00 -08004730void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004731 // Explicit clinit checks triggered by static invokes must have been pruned by
4732 // art::PrepareForRegisterAllocation.
4733 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004734
Andreas Gampe878d58c2015-01-15 23:24:00 -08004735 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004736 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004737 return;
4738 }
4739
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004740 {
4741 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4742 // are no pools emitted.
4743 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4744 LocationSummary* locations = invoke->GetLocations();
4745 codegen_->GenerateStaticOrDirectCall(
4746 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4747 }
4748
4749 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004750}
4751
4752void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004753 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004754 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004755 return;
4756 }
4757
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004758 {
4759 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4760 // are no pools emitted.
4761 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4762 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4763 DCHECK(!codegen_->IsLeafMethod());
4764 }
4765
4766 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004767}
4768
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004769HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4770 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004771 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004772 case HLoadClass::LoadKind::kInvalid:
4773 LOG(FATAL) << "UNREACHABLE";
4774 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004775 case HLoadClass::LoadKind::kReferrersClass:
4776 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004777 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004778 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004779 case HLoadClass::LoadKind::kBssEntry:
4780 DCHECK(!Runtime::Current()->UseJitCompilation());
4781 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004782 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004783 DCHECK(Runtime::Current()->UseJitCompilation());
4784 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004785 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004786 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004787 break;
4788 }
4789 return desired_class_load_kind;
4790}
4791
Alexandre Rames67555f72014-11-18 10:55:16 +00004792void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004793 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004794 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004795 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004796 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004797 cls,
4798 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004799 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004800 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004801 return;
4802 }
Vladimir Marko41559982017-01-06 14:04:23 +00004803 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004804
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004805 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4806 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004807 ? LocationSummary::kCallOnSlowPath
4808 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004809 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004810 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004811 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004812 }
4813
Vladimir Marko41559982017-01-06 14:04:23 +00004814 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004815 locations->SetInAt(0, Location::RequiresRegister());
4816 }
4817 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004818 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4819 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4820 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00004821 RegisterSet caller_saves = RegisterSet::Empty();
4822 InvokeRuntimeCallingConvention calling_convention;
4823 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4824 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004825 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
4826 DataType::Type::kReference).GetCode());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004827 locations->SetCustomSlowPathCallerSaves(caller_saves);
4828 } else {
4829 // For non-Baker read barrier we have a temp-clobbering call.
4830 }
4831 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004832}
4833
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004834// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4835// move.
4836void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004837 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004838 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004839 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004840 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004841 return;
4842 }
Vladimir Marko41559982017-01-06 14:04:23 +00004843 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004844
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004845 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004846 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004847
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004848 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4849 ? kWithoutReadBarrier
4850 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004851 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004852 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004853 case HLoadClass::LoadKind::kReferrersClass: {
4854 DCHECK(!cls->CanCallRuntime());
4855 DCHECK(!cls->MustGenerateClinitCheck());
4856 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4857 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004858 GenerateGcRootFieldLoad(cls,
4859 out_loc,
4860 current_method,
4861 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004862 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004863 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004864 break;
4865 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004867 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004868 // Add ADRP with its PC-relative type patch.
4869 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004870 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004871 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004872 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004873 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004874 vixl::aarch64::Label* add_label =
4875 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004876 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004877 break;
4878 }
4879 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004880 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004881 uint32_t address = dchecked_integral_cast<uint32_t>(
4882 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4883 DCHECK_NE(address, 0u);
4884 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004885 break;
4886 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004887 case HLoadClass::LoadKind::kBootImageClassTable: {
4888 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4889 // Add ADRP with its PC-relative type patch.
4890 const DexFile& dex_file = cls->GetDexFile();
4891 dex::TypeIndex type_index = cls->GetTypeIndex();
4892 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
4893 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4894 // Add LDR with its PC-relative type patch.
4895 vixl::aarch64::Label* ldr_label =
4896 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
4897 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4898 // Extract the reference from the slot data, i.e. clear the hash bits.
4899 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
4900 ComputeModifiedUtf8Hash(dex_file.StringByTypeIdx(type_index)));
4901 if (masked_hash != 0) {
4902 __ Sub(out.W(), out.W(), Operand(masked_hash));
4903 }
4904 break;
4905 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004906 case HLoadClass::LoadKind::kBssEntry: {
4907 // Add ADRP with its PC-relative Class .bss entry patch.
4908 const DexFile& dex_file = cls->GetDexFile();
4909 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004910 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4911 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4912 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004913 // Add LDR with its PC-relative Class patch.
4914 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004915 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004916 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4917 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004918 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00004919 temp,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004920 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004921 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004922 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004923 generate_null_check = true;
4924 break;
4925 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004926 case HLoadClass::LoadKind::kJitTableAddress: {
4927 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4928 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004929 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004930 GenerateGcRootFieldLoad(cls,
4931 out_loc,
4932 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004933 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004934 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004935 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004936 break;
4937 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004938 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004939 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004940 LOG(FATAL) << "UNREACHABLE";
4941 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004942 }
4943
Vladimir Markoea4c1262017-02-06 19:59:33 +00004944 bool do_clinit = cls->MustGenerateClinitCheck();
4945 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004946 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004947 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00004948 cls, cls, cls->GetDexPc(), do_clinit);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004949 codegen_->AddSlowPath(slow_path);
4950 if (generate_null_check) {
4951 __ Cbz(out, slow_path->GetEntryLabel());
4952 }
4953 if (cls->MustGenerateClinitCheck()) {
4954 GenerateClassInitializationCheck(slow_path, out);
4955 } else {
4956 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004957 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004958 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004959 }
4960}
4961
David Brazdilcb1c0552015-08-04 16:22:25 +01004962static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004963 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004964}
4965
Alexandre Rames67555f72014-11-18 10:55:16 +00004966void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4967 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004968 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00004969 locations->SetOut(Location::RequiresRegister());
4970}
4971
4972void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004973 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4974}
4975
4976void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004977 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01004978}
4979
4980void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4981 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004982}
4983
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004984HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4985 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004986 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004987 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004988 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00004989 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004990 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004991 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004992 case HLoadString::LoadKind::kJitTableAddress:
4993 DCHECK(Runtime::Current()->UseJitCompilation());
4994 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004995 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004996 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004997 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004998 }
4999 return desired_string_load_kind;
5000}
5001
Alexandre Rames67555f72014-11-18 10:55:16 +00005002void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005003 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005004 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005005 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005006 InvokeRuntimeCallingConvention calling_convention;
5007 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5008 } else {
5009 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005010 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5011 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005012 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005013 RegisterSet caller_saves = RegisterSet::Empty();
5014 InvokeRuntimeCallingConvention calling_convention;
5015 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5016 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5018 DataType::Type::kReference).GetCode());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005019 locations->SetCustomSlowPathCallerSaves(caller_saves);
5020 } else {
5021 // For non-Baker read barrier we have a temp-clobbering call.
5022 }
5023 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005024 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005025}
5026
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005027// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5028// move.
5029void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005030 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005031 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005032
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005033 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005034 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005035 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005036 // Add ADRP with its PC-relative String patch.
5037 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005038 const dex::StringIndex string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005039 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005040 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005041 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005042 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005043 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005044 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005045 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005046 }
5047 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005048 uint32_t address = dchecked_integral_cast<uint32_t>(
5049 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5050 DCHECK_NE(address, 0u);
5051 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005052 return;
5053 }
5054 case HLoadString::LoadKind::kBootImageInternTable: {
5055 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5056 // Add ADRP with its PC-relative String patch.
5057 const DexFile& dex_file = load->GetDexFile();
5058 const dex::StringIndex string_index = load->GetStringIndex();
5059 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
5060 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
5061 // Add LDR with its PC-relative String patch.
5062 vixl::aarch64::Label* ldr_label =
5063 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
5064 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5065 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005066 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005067 case HLoadString::LoadKind::kBssEntry: {
5068 // Add ADRP with its PC-relative String .bss entry patch.
5069 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005070 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005071 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof3c52b42017-11-17 17:32:12 +00005072 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005073 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005074 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005075 // Add LDR with its .bss entry String patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005076 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005077 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005078 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005079 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005080 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005081 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005082 /* offset placeholder */ 0u,
5083 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005084 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005085 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005086 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005087 codegen_->AddSlowPath(slow_path);
5088 __ Cbz(out.X(), slow_path->GetEntryLabel());
5089 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005090 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005091 return;
5092 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005093 case HLoadString::LoadKind::kJitTableAddress: {
5094 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005095 load->GetStringIndex(),
5096 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005097 GenerateGcRootFieldLoad(load,
5098 out_loc,
5099 out.X(),
5100 /* offset */ 0,
5101 /* fixup_label */ nullptr,
5102 kCompilerReadBarrierOption);
5103 return;
5104 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005105 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005106 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005107 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005108
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005109 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005110 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005111 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005112 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005113 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5114 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005115 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005116}
5117
Alexandre Rames5319def2014-10-23 10:03:10 +01005118void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005119 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005120 locations->SetOut(Location::ConstantLocation(constant));
5121}
5122
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005123void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005124 // Will be generated at use site.
5125}
5126
Alexandre Rames67555f72014-11-18 10:55:16 +00005127void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005128 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5129 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005130 InvokeRuntimeCallingConvention calling_convention;
5131 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5132}
5133
5134void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005135 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005136 instruction,
5137 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005138 if (instruction->IsEnter()) {
5139 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5140 } else {
5141 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5142 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005143 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005144}
5145
Alexandre Rames42d641b2014-10-27 14:00:51 +00005146void LocationsBuilderARM64::VisitMul(HMul* mul) {
5147 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005148 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005149 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005150 case DataType::Type::kInt32:
5151 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005152 locations->SetInAt(0, Location::RequiresRegister());
5153 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005155 break;
5156
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005157 case DataType::Type::kFloat32:
5158 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005159 locations->SetInAt(0, Location::RequiresFpuRegister());
5160 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005161 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005162 break;
5163
5164 default:
5165 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5166 }
5167}
5168
5169void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5170 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005171 case DataType::Type::kInt32:
5172 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005173 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5174 break;
5175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005176 case DataType::Type::kFloat32:
5177 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005178 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005179 break;
5180
5181 default:
5182 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5183 }
5184}
5185
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005186void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5187 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005188 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005189 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005190 case DataType::Type::kInt32:
5191 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005192 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005193 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005194 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005196 case DataType::Type::kFloat32:
5197 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005198 locations->SetInAt(0, Location::RequiresFpuRegister());
5199 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005200 break;
5201
5202 default:
5203 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5204 }
5205}
5206
5207void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5208 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 case DataType::Type::kInt32:
5210 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005211 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5212 break;
5213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005214 case DataType::Type::kFloat32:
5215 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005216 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005217 break;
5218
5219 default:
5220 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5221 }
5222}
5223
5224void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005225 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5226 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005227 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005228 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005229 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5230 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005231}
5232
5233void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005234 // Note: if heap poisoning is enabled, the entry point takes cares
5235 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005236 QuickEntrypointEnum entrypoint =
5237 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5238 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005239 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005240 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005241}
5242
Alexandre Rames5319def2014-10-23 10:03:10 +01005243void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005244 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5245 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005246 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005247 if (instruction->IsStringAlloc()) {
5248 locations->AddTemp(LocationFrom(kArtMethodRegister));
5249 } else {
5250 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005251 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005252 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005253}
5254
5255void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005256 // Note: if heap poisoning is enabled, the entry point takes cares
5257 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005258 if (instruction->IsStringAlloc()) {
5259 // String is allocated through StringFactory. Call NewEmptyString entry point.
5260 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005261 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005262 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5263 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005264
5265 {
5266 // Ensure the pc position is recorded immediately after the `blr` instruction.
5267 ExactAssemblyScope eas(GetVIXLAssembler(),
5268 kInstructionSize,
5269 CodeBufferCheckScope::kExactSize);
5270 __ blr(lr);
5271 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5272 }
David Brazdil6de19382016-01-08 17:37:10 +00005273 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005274 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005275 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005276 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005277 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005278}
5279
5280void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005281 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005282 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005284}
5285
5286void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005287 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005288 case DataType::Type::kInt32:
5289 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005290 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005291 break;
5292
5293 default:
5294 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5295 }
5296}
5297
David Brazdil66d126e2015-04-03 16:02:44 +01005298void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005299 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005300 locations->SetInAt(0, Location::RequiresRegister());
5301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5302}
5303
5304void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005305 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005306}
5307
Alexandre Rames5319def2014-10-23 10:03:10 +01005308void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005309 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5310 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005311}
5312
Calin Juravle2ae48182016-03-16 14:05:09 +00005313void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5314 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005315 return;
5316 }
Artem Serov914d7a82017-02-07 14:33:49 +00005317 {
5318 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5319 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5320 Location obj = instruction->GetLocations()->InAt(0);
5321 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5322 RecordPcInfo(instruction, instruction->GetDexPc());
5323 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005324}
5325
Calin Juravle2ae48182016-03-16 14:05:09 +00005326void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005327 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005328 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005329
5330 LocationSummary* locations = instruction->GetLocations();
5331 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005332
5333 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005334}
5335
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005336void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005337 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005338}
5339
Alexandre Rames67555f72014-11-18 10:55:16 +00005340void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5341 HandleBinaryOp(instruction);
5342}
5343
5344void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5345 HandleBinaryOp(instruction);
5346}
5347
Alexandre Rames3e69f162014-12-10 10:36:50 +00005348void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5349 LOG(FATAL) << "Unreachable";
5350}
5351
5352void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005353 if (instruction->GetNext()->IsSuspendCheck() &&
5354 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5355 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5356 // The back edge will generate the suspend check.
5357 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5358 }
5359
Alexandre Rames3e69f162014-12-10 10:36:50 +00005360 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5361}
5362
Alexandre Rames5319def2014-10-23 10:03:10 +01005363void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005364 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005365 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5366 if (location.IsStackSlot()) {
5367 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5368 } else if (location.IsDoubleStackSlot()) {
5369 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5370 }
5371 locations->SetOut(location);
5372}
5373
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005374void InstructionCodeGeneratorARM64::VisitParameterValue(
5375 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005376 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005377}
5378
5379void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5380 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005381 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005382 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005383}
5384
5385void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5386 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5387 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005388}
5389
5390void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005391 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005392 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005393 locations->SetInAt(i, Location::Any());
5394 }
5395 locations->SetOut(Location::Any());
5396}
5397
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005398void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005399 LOG(FATAL) << "Unreachable";
5400}
5401
Serban Constantinescu02164b32014-11-13 14:05:07 +00005402void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005403 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005404 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005405 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005406 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005407 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005408
5409 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005410 case DataType::Type::kInt32:
5411 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005412 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005413 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5415 break;
5416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005417 case DataType::Type::kFloat32:
5418 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005419 InvokeRuntimeCallingConvention calling_convention;
5420 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5421 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5422 locations->SetOut(calling_convention.GetReturnLocation(type));
5423
5424 break;
5425 }
5426
Serban Constantinescu02164b32014-11-13 14:05:07 +00005427 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005428 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005429 }
5430}
5431
5432void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005433 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005434
Serban Constantinescu02164b32014-11-13 14:05:07 +00005435 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005436 case DataType::Type::kInt32:
5437 case DataType::Type::kInt64: {
Zheng Xuc6667102015-05-15 16:08:45 +08005438 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005439 break;
5440 }
5441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 case DataType::Type::kFloat32:
5443 case DataType::Type::kFloat64: {
5444 QuickEntrypointEnum entrypoint =
5445 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005446 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005447 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005448 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5449 } else {
5450 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5451 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005452 break;
5453 }
5454
Serban Constantinescu02164b32014-11-13 14:05:07 +00005455 default:
5456 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005457 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005458 }
5459}
5460
Igor Murashkind01745e2017-04-05 16:40:31 -07005461void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5462 constructor_fence->SetLocations(nullptr);
5463}
5464
5465void InstructionCodeGeneratorARM64::VisitConstructorFence(
5466 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5467 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5468}
5469
Calin Juravle27df7582015-04-17 19:12:31 +01005470void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5471 memory_barrier->SetLocations(nullptr);
5472}
5473
5474void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005475 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005476}
5477
Alexandre Rames5319def2014-10-23 10:03:10 +01005478void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005479 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005481 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005482}
5483
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005484void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005485 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005486}
5487
5488void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5489 instruction->SetLocations(nullptr);
5490}
5491
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005492void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005493 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005494}
5495
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005496void LocationsBuilderARM64::VisitRor(HRor* ror) {
5497 HandleBinaryOp(ror);
5498}
5499
5500void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5501 HandleBinaryOp(ror);
5502}
5503
Serban Constantinescu02164b32014-11-13 14:05:07 +00005504void LocationsBuilderARM64::VisitShl(HShl* shl) {
5505 HandleShift(shl);
5506}
5507
5508void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5509 HandleShift(shl);
5510}
5511
5512void LocationsBuilderARM64::VisitShr(HShr* shr) {
5513 HandleShift(shr);
5514}
5515
5516void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5517 HandleShift(shr);
5518}
5519
Alexandre Rames5319def2014-10-23 10:03:10 +01005520void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005521 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005522}
5523
5524void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005525 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005526}
5527
Alexandre Rames67555f72014-11-18 10:55:16 +00005528void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005529 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005530}
5531
5532void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005533 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005534}
5535
5536void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005537 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005538}
5539
Alexandre Rames67555f72014-11-18 10:55:16 +00005540void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005541 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005542}
5543
Calin Juravlee460d1d2015-09-29 04:52:17 +01005544void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5545 HUnresolvedInstanceFieldGet* instruction) {
5546 FieldAccessCallingConventionARM64 calling_convention;
5547 codegen_->CreateUnresolvedFieldLocationSummary(
5548 instruction, instruction->GetFieldType(), calling_convention);
5549}
5550
5551void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5552 HUnresolvedInstanceFieldGet* instruction) {
5553 FieldAccessCallingConventionARM64 calling_convention;
5554 codegen_->GenerateUnresolvedFieldAccess(instruction,
5555 instruction->GetFieldType(),
5556 instruction->GetFieldIndex(),
5557 instruction->GetDexPc(),
5558 calling_convention);
5559}
5560
5561void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5562 HUnresolvedInstanceFieldSet* instruction) {
5563 FieldAccessCallingConventionARM64 calling_convention;
5564 codegen_->CreateUnresolvedFieldLocationSummary(
5565 instruction, instruction->GetFieldType(), calling_convention);
5566}
5567
5568void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5569 HUnresolvedInstanceFieldSet* instruction) {
5570 FieldAccessCallingConventionARM64 calling_convention;
5571 codegen_->GenerateUnresolvedFieldAccess(instruction,
5572 instruction->GetFieldType(),
5573 instruction->GetFieldIndex(),
5574 instruction->GetDexPc(),
5575 calling_convention);
5576}
5577
5578void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5579 HUnresolvedStaticFieldGet* instruction) {
5580 FieldAccessCallingConventionARM64 calling_convention;
5581 codegen_->CreateUnresolvedFieldLocationSummary(
5582 instruction, instruction->GetFieldType(), calling_convention);
5583}
5584
5585void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5586 HUnresolvedStaticFieldGet* instruction) {
5587 FieldAccessCallingConventionARM64 calling_convention;
5588 codegen_->GenerateUnresolvedFieldAccess(instruction,
5589 instruction->GetFieldType(),
5590 instruction->GetFieldIndex(),
5591 instruction->GetDexPc(),
5592 calling_convention);
5593}
5594
5595void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5596 HUnresolvedStaticFieldSet* instruction) {
5597 FieldAccessCallingConventionARM64 calling_convention;
5598 codegen_->CreateUnresolvedFieldLocationSummary(
5599 instruction, instruction->GetFieldType(), calling_convention);
5600}
5601
5602void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5603 HUnresolvedStaticFieldSet* instruction) {
5604 FieldAccessCallingConventionARM64 calling_convention;
5605 codegen_->GenerateUnresolvedFieldAccess(instruction,
5606 instruction->GetFieldType(),
5607 instruction->GetFieldIndex(),
5608 instruction->GetDexPc(),
5609 calling_convention);
5610}
5611
Alexandre Rames5319def2014-10-23 10:03:10 +01005612void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005613 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5614 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005615 // In suspend check slow path, usually there are no caller-save registers at all.
5616 // If SIMD instructions are present, however, we force spilling all live SIMD
5617 // registers in full width (since the runtime only saves/restores lower part).
5618 locations->SetCustomSlowPathCallerSaves(
5619 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005620}
5621
5622void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005623 HBasicBlock* block = instruction->GetBlock();
5624 if (block->GetLoopInformation() != nullptr) {
5625 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5626 // The back edge will generate the suspend check.
5627 return;
5628 }
5629 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5630 // The goto will generate the suspend check.
5631 return;
5632 }
5633 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005634 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005635}
5636
Alexandre Rames67555f72014-11-18 10:55:16 +00005637void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005638 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5639 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005640 InvokeRuntimeCallingConvention calling_convention;
5641 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5642}
5643
5644void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005645 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005646 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005647}
5648
5649void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5650 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005651 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 DataType::Type input_type = conversion->GetInputType();
5653 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005654 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5655 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5657 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005658 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5659 }
5660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005662 locations->SetInAt(0, Location::RequiresFpuRegister());
5663 } else {
5664 locations->SetInAt(0, Location::RequiresRegister());
5665 }
5666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005667 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5669 } else {
5670 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5671 }
5672}
5673
5674void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005675 DataType::Type result_type = conversion->GetResultType();
5676 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005677
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005678 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5679 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005681 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5682 int result_size = DataType::Size(result_type);
5683 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005684 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005685 Register output = OutputRegister(conversion);
5686 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005687 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005688 // 'int' values are used directly as W registers, discarding the top
5689 // bits, so we don't need to sign-extend and can just perform a move.
5690 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5691 // top 32 bits of the target register. We theoretically could leave those
5692 // bits unchanged, but we would have to make sure that no code uses a
5693 // 32bit input value as a 64bit value assuming that the top 32 bits are
5694 // zero.
5695 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005696 } else if (DataType::IsUnsignedType(result_type) ||
5697 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5698 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005699 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005700 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005701 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005702 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005703 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005704 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5705 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005706 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005707 } else if (DataType::IsFloatingPointType(result_type) &&
5708 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005709 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5710 } else {
5711 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5712 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005713 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005714}
Alexandre Rames67555f72014-11-18 10:55:16 +00005715
Serban Constantinescu02164b32014-11-13 14:05:07 +00005716void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5717 HandleShift(ushr);
5718}
5719
5720void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5721 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005722}
5723
5724void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5725 HandleBinaryOp(instruction);
5726}
5727
5728void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5729 HandleBinaryOp(instruction);
5730}
5731
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005732void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005733 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005734 LOG(FATAL) << "Unreachable";
5735}
5736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005737void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005738 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005739 LOG(FATAL) << "Unreachable";
5740}
5741
Mark Mendellfe57faa2015-09-18 09:26:15 -04005742// Simple implementation of packed switch - generate cascaded compare/jumps.
5743void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5744 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005745 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005746 locations->SetInAt(0, Location::RequiresRegister());
5747}
5748
5749void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5750 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005751 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005752 Register value_reg = InputRegisterAt(switch_instr, 0);
5753 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5754
Zheng Xu3927c8b2015-11-18 17:46:25 +08005755 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005756 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005757 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5758 // make sure we don't emit it if the target may run out of range.
5759 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5760 // ranges and emit the tables only as required.
5761 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005762
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005763 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005764 // Current instruction id is an upper bound of the number of HIRs in the graph.
5765 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5766 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005767 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5768 Register temp = temps.AcquireW();
5769 __ Subs(temp, value_reg, Operand(lower_bound));
5770
Zheng Xu3927c8b2015-11-18 17:46:25 +08005771 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005772 // Jump to successors[0] if value == lower_bound.
5773 __ B(eq, codegen_->GetLabelOf(successors[0]));
5774 int32_t last_index = 0;
5775 for (; num_entries - last_index > 2; last_index += 2) {
5776 __ Subs(temp, temp, Operand(2));
5777 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5778 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5779 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5780 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5781 }
5782 if (num_entries - last_index == 2) {
5783 // The last missing case_value.
5784 __ Cmp(temp, Operand(1));
5785 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005786 }
5787
5788 // And the default for any other value.
5789 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5790 __ B(codegen_->GetLabelOf(default_block));
5791 }
5792 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005793 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005794
5795 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5796
5797 // Below instructions should use at most one blocked register. Since there are two blocked
5798 // registers, we are free to block one.
5799 Register temp_w = temps.AcquireW();
5800 Register index;
5801 // Remove the bias.
5802 if (lower_bound != 0) {
5803 index = temp_w;
5804 __ Sub(index, value_reg, Operand(lower_bound));
5805 } else {
5806 index = value_reg;
5807 }
5808
5809 // Jump to default block if index is out of the range.
5810 __ Cmp(index, Operand(num_entries));
5811 __ B(hs, codegen_->GetLabelOf(default_block));
5812
5813 // In current VIXL implementation, it won't require any blocked registers to encode the
5814 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5815 // register pressure.
5816 Register table_base = temps.AcquireX();
5817 // Load jump offset from the table.
5818 __ Adr(table_base, jump_table->GetTableStartLabel());
5819 Register jump_offset = temp_w;
5820 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5821
5822 // Jump to target block by branching to table_base(pc related) + offset.
5823 Register target_address = table_base;
5824 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5825 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005826 }
5827}
5828
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005829void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5830 HInstruction* instruction,
5831 Location out,
5832 uint32_t offset,
5833 Location maybe_temp,
5834 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005836 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005837 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005838 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005839 if (kUseBakerReadBarrier) {
5840 // Load with fast path based Baker's read barrier.
5841 // /* HeapReference<Object> */ out = *(out + offset)
5842 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5843 out,
5844 out_reg,
5845 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005846 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005847 /* needs_null_check */ false,
5848 /* use_load_acquire */ false);
5849 } else {
5850 // Load with slow path based read barrier.
5851 // Save the value of `out` into `maybe_temp` before overwriting it
5852 // in the following move operation, as we will need it for the
5853 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005854 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005855 __ Mov(temp_reg, out_reg);
5856 // /* HeapReference<Object> */ out = *(out + offset)
5857 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5858 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5859 }
5860 } else {
5861 // Plain load with no read barrier.
5862 // /* HeapReference<Object> */ out = *(out + offset)
5863 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5864 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5865 }
5866}
5867
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005868void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5869 HInstruction* instruction,
5870 Location out,
5871 Location obj,
5872 uint32_t offset,
5873 Location maybe_temp,
5874 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005875 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005876 Register out_reg = RegisterFrom(out, type);
5877 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005878 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005879 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005880 if (kUseBakerReadBarrier) {
5881 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005882 // /* HeapReference<Object> */ out = *(obj + offset)
5883 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5884 out,
5885 obj_reg,
5886 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005887 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005888 /* needs_null_check */ false,
5889 /* use_load_acquire */ false);
5890 } else {
5891 // Load with slow path based read barrier.
5892 // /* HeapReference<Object> */ out = *(obj + offset)
5893 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5894 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5895 }
5896 } else {
5897 // Plain load with no read barrier.
5898 // /* HeapReference<Object> */ out = *(obj + offset)
5899 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5900 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5901 }
5902}
5903
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005904void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5905 HInstruction* instruction,
5906 Location root,
5907 Register obj,
5908 uint32_t offset,
5909 vixl::aarch64::Label* fixup_label,
5910 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005911 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005912 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005913 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005914 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005915 if (kUseBakerReadBarrier) {
5916 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005917 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005918 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5919 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01005920 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5921 // the Marking Register) to decide whether we need to enter
5922 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005923 //
5924 // We use link-time generated thunks for the slow path. That thunk
5925 // checks the reference and jumps to the entrypoint if needed.
5926 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005927 // lr = &return_address;
5928 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005929 // if (mr) { // Thread::Current()->GetIsGcMarking()
5930 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005931 // }
5932 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005933
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005934 UseScratchRegisterScope temps(GetVIXLAssembler());
5935 DCHECK(temps.IsAvailable(ip0));
5936 DCHECK(temps.IsAvailable(ip1));
5937 temps.Exclude(ip0, ip1);
5938 uint32_t custom_data =
5939 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5940 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005941
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005942 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5943 vixl::aarch64::Label return_address;
5944 __ adr(lr, &return_address);
5945 if (fixup_label != nullptr) {
5946 __ Bind(fixup_label);
5947 }
5948 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5949 "GC root LDR must be 2 instruction (8B) before the return address label.");
5950 __ ldr(root_reg, MemOperand(obj.X(), offset));
5951 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01005952 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005953 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01005955 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5956 // the Marking Register) to decide whether we need to enter
5957 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005958 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005959 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005960 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005961 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01005962 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5963 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005964 // }
Roland Levillain44015862016-01-22 11:47:17 +00005965
Roland Levillain97c46462017-05-11 14:04:03 +01005966 // Slow path marking the GC root `root`. The entrypoint will
5967 // be loaded by the slow path code.
5968 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005969 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005970 codegen_->AddSlowPath(slow_path);
5971
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005972 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5973 if (fixup_label == nullptr) {
5974 __ Ldr(root_reg, MemOperand(obj, offset));
5975 } else {
5976 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
5977 }
5978 static_assert(
5979 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5980 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5981 "have different sizes.");
5982 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5983 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5984 "have different sizes.");
5985
Roland Levillain97c46462017-05-11 14:04:03 +01005986 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005987 __ Bind(slow_path->GetExitLabel());
5988 }
Roland Levillain44015862016-01-22 11:47:17 +00005989 } else {
5990 // GC root loaded through a slow path for read barriers other
5991 // than Baker's.
5992 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005993 if (fixup_label == nullptr) {
5994 __ Add(root_reg.X(), obj.X(), offset);
5995 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005996 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005997 }
Roland Levillain44015862016-01-22 11:47:17 +00005998 // /* mirror::Object* */ root = root->Read()
5999 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6000 }
6001 } else {
6002 // Plain GC root load with no read barrier.
6003 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006004 if (fixup_label == nullptr) {
6005 __ Ldr(root_reg, MemOperand(obj, offset));
6006 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006007 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006008 }
Roland Levillain44015862016-01-22 11:47:17 +00006009 // Note that GC roots are not affected by heap poisoning, thus we
6010 // do not have to unpoison `root_reg` here.
6011 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006012 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006013}
6014
6015void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6016 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006017 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006018 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006019 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006020 bool needs_null_check,
6021 bool use_load_acquire) {
6022 DCHECK(kEmitCompilerReadBarrier);
6023 DCHECK(kUseBakerReadBarrier);
6024
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006025 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6026 !use_load_acquire &&
6027 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006028 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6029 // Marking Register) to decide whether we need to enter the slow
6030 // path to mark the reference. Then, in the slow path, check the
6031 // gray bit in the lock word of the reference's holder (`obj`) to
6032 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006033 //
6034 // We use link-time generated thunks for the slow path. That thunk checks
6035 // the holder and jumps to the entrypoint if needed. If the holder is not
6036 // gray, it creates a fake dependency and returns to the LDR instruction.
6037 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006038 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006039 // if (mr) { // Thread::Current()->GetIsGcMarking()
6040 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006041 // }
6042 // not_gray_return_address:
6043 // // Original reference load. If the offset is too large to fit
6044 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006045 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006046 // gray_return_address:
6047
6048 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6049 Register base = obj;
6050 if (offset >= kReferenceLoadMinFarOffset) {
6051 DCHECK(maybe_temp.IsRegister());
6052 base = WRegisterFrom(maybe_temp);
6053 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6054 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6055 offset &= (kReferenceLoadMinFarOffset - 1u);
6056 }
6057 UseScratchRegisterScope temps(GetVIXLAssembler());
6058 DCHECK(temps.IsAvailable(ip0));
6059 DCHECK(temps.IsAvailable(ip1));
6060 temps.Exclude(ip0, ip1);
6061 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6062 base.GetCode(),
6063 obj.GetCode());
6064 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6065
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006066 {
6067 EmissionCheckScope guard(GetVIXLAssembler(),
6068 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6069 vixl::aarch64::Label return_address;
6070 __ adr(lr, &return_address);
6071 __ Bind(cbnz_label);
6072 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6073 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6074 "Field LDR must be 1 instruction (4B) before the return address label; "
6075 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006077 __ ldr(ref_reg, MemOperand(base.X(), offset));
6078 if (needs_null_check) {
6079 MaybeRecordImplicitNullCheck(instruction);
6080 }
6081 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6082 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006083 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006084 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006085 return;
6086 }
6087
Roland Levillain44015862016-01-22 11:47:17 +00006088 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006089 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006090 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006091 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006092 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6093 ref,
6094 obj,
6095 offset,
6096 no_index,
6097 no_scale_factor,
6098 temp,
6099 needs_null_check,
6100 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006101}
6102
6103void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6104 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006105 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006106 uint32_t data_offset,
6107 Location index,
6108 Register temp,
6109 bool needs_null_check) {
6110 DCHECK(kEmitCompilerReadBarrier);
6111 DCHECK(kUseBakerReadBarrier);
6112
Vladimir Marko66d691d2017-04-07 17:53:39 +01006113 static_assert(
6114 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6115 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006116 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006117
6118 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6119 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006120 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6121 // Marking Register) to decide whether we need to enter the slow
6122 // path to mark the reference. Then, in the slow path, check the
6123 // gray bit in the lock word of the reference's holder (`obj`) to
6124 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006125 //
6126 // We use link-time generated thunks for the slow path. That thunk checks
6127 // the holder and jumps to the entrypoint if needed. If the holder is not
6128 // gray, it creates a fake dependency and returns to the LDR instruction.
6129 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006130 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006131 // if (mr) { // Thread::Current()->GetIsGcMarking()
6132 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006133 // }
6134 // not_gray_return_address:
6135 // // Original reference load. If the offset is too large to fit
6136 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006137 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006138 // gray_return_address:
6139
6140 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006141 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6142 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006143
6144 UseScratchRegisterScope temps(GetVIXLAssembler());
6145 DCHECK(temps.IsAvailable(ip0));
6146 DCHECK(temps.IsAvailable(ip1));
6147 temps.Exclude(ip0, ip1);
6148 uint32_t custom_data =
6149 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6150 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6151
Vladimir Marko66d691d2017-04-07 17:53:39 +01006152 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006153 {
6154 EmissionCheckScope guard(GetVIXLAssembler(),
6155 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6156 vixl::aarch64::Label return_address;
6157 __ adr(lr, &return_address);
6158 __ Bind(cbnz_label);
6159 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6160 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6161 "Array LDR must be 1 instruction (4B) before the return address label; "
6162 " 2 instructions (8B) for heap poisoning.");
6163 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6164 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6165 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6166 __ Bind(&return_address);
6167 }
6168 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006169 return;
6170 }
6171
Roland Levillain44015862016-01-22 11:47:17 +00006172 // Array cells are never volatile variables, therefore array loads
6173 // never use Load-Acquire instructions on ARM64.
6174 const bool use_load_acquire = false;
6175
6176 // /* HeapReference<Object> */ ref =
6177 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006178 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6179 ref,
6180 obj,
6181 data_offset,
6182 index,
6183 scale_factor,
6184 temp,
6185 needs_null_check,
6186 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006187}
6188
6189void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6190 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006191 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006192 uint32_t offset,
6193 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006194 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006195 Register temp,
6196 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006197 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006198 DCHECK(kEmitCompilerReadBarrier);
6199 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006200 // If we are emitting an array load, we should not be using a
6201 // Load Acquire instruction. In other words:
6202 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6203 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006204
Roland Levillain97c46462017-05-11 14:04:03 +01006205 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6206 // Marking Register) to decide whether we need to enter the slow
6207 // path to mark the reference. Then, in the slow path, check the
6208 // gray bit in the lock word of the reference's holder (`obj`) to
6209 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006210 //
Roland Levillain97c46462017-05-11 14:04:03 +01006211 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006212 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006213 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6214 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6215 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6216 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6217 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006218 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6219 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006220 // }
6221 // } else {
6222 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006223 // }
Roland Levillain44015862016-01-22 11:47:17 +00006224
Roland Levillainba650a42017-03-06 13:52:32 +00006225 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006226 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006227 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006228 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006229 instruction,
6230 ref,
6231 obj,
6232 offset,
6233 index,
6234 scale_factor,
6235 needs_null_check,
6236 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006237 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006238 AddSlowPath(slow_path);
6239
Roland Levillain97c46462017-05-11 14:04:03 +01006240 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006241 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006242 GenerateRawReferenceLoad(
6243 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006244 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006245 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006246}
6247
Roland Levillainff487002017-03-07 16:50:01 +00006248void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6249 Location ref,
6250 Register obj,
6251 Location field_offset,
6252 Register temp,
6253 bool needs_null_check,
6254 bool use_load_acquire) {
6255 DCHECK(kEmitCompilerReadBarrier);
6256 DCHECK(kUseBakerReadBarrier);
6257 // If we are emitting an array load, we should not be using a
6258 // Load Acquire instruction. In other words:
6259 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6260 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6261
Roland Levillain97c46462017-05-11 14:04:03 +01006262 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6263 // Marking Register) to decide whether we need to enter the slow
6264 // path to update the reference field within `obj`. Then, in the
6265 // slow path, check the gray bit in the lock word of the reference's
6266 // holder (`obj`) to decide whether to mark `ref` and update the
6267 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006268 //
Roland Levillain97c46462017-05-11 14:04:03 +01006269 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006270 // // Slow path.
6271 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6272 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6273 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6274 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6275 // if (is_gray) {
6276 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006277 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6278 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006279 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6280 // }
6281 // }
6282
6283 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006284 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006285 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006286 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006287 instruction,
6288 ref,
6289 obj,
6290 /* offset */ 0u,
6291 /* index */ field_offset,
6292 /* scale_factor */ 0u /* "times 1" */,
6293 needs_null_check,
6294 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006295 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006296 AddSlowPath(slow_path);
6297
Roland Levillain97c46462017-05-11 14:04:03 +01006298 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006299 // Fast path: the GC is not marking: nothing to do (the field is
6300 // up-to-date, and we don't need to load the reference).
6301 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006302 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006303}
6304
Roland Levillainba650a42017-03-06 13:52:32 +00006305void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6306 Location ref,
6307 Register obj,
6308 uint32_t offset,
6309 Location index,
6310 size_t scale_factor,
6311 bool needs_null_check,
6312 bool use_load_acquire) {
6313 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006315 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006316
Roland Levillainba650a42017-03-06 13:52:32 +00006317 // If needed, vixl::EmissionCheckScope guards are used to ensure
6318 // that no pools are emitted between the load (macro) instruction
6319 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006320
Roland Levillain44015862016-01-22 11:47:17 +00006321 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006322 // Load types involving an "index": ArrayGet,
6323 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6324 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006325 if (use_load_acquire) {
6326 // UnsafeGetObjectVolatile intrinsic case.
6327 // Register `index` is not an index in an object array, but an
6328 // offset to an object reference field within object `obj`.
6329 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6330 DCHECK(instruction->GetLocations()->Intrinsified());
6331 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6332 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006333 DCHECK_EQ(offset, 0u);
6334 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006335 DCHECK_EQ(needs_null_check, false);
6336 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006337 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6338 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006339 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006340 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6341 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006342 if (index.IsConstant()) {
6343 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006344 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006345 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006346 if (needs_null_check) {
6347 MaybeRecordImplicitNullCheck(instruction);
6348 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006349 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006350 UseScratchRegisterScope temps(GetVIXLAssembler());
6351 Register temp = temps.AcquireW();
6352 __ Add(temp, obj, offset);
6353 {
6354 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6355 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6356 if (needs_null_check) {
6357 MaybeRecordImplicitNullCheck(instruction);
6358 }
6359 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006360 }
Roland Levillain44015862016-01-22 11:47:17 +00006361 }
Roland Levillain44015862016-01-22 11:47:17 +00006362 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006363 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006364 MemOperand field = HeapOperand(obj, offset);
6365 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006366 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6367 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006368 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006369 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006370 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006371 if (needs_null_check) {
6372 MaybeRecordImplicitNullCheck(instruction);
6373 }
Roland Levillain44015862016-01-22 11:47:17 +00006374 }
6375 }
6376
6377 // Object* ref = ref_addr->AsMirrorPtr()
6378 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006379}
6380
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006381void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6382 // The following condition is a compile-time one, so it does not have a run-time cost.
6383 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6384 // The following condition is a run-time one; it is executed after the
6385 // previous compile-time test, to avoid penalizing non-debug builds.
6386 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6387 UseScratchRegisterScope temps(GetVIXLAssembler());
6388 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6389 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6390 }
6391 }
6392}
6393
Roland Levillain44015862016-01-22 11:47:17 +00006394void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6395 Location out,
6396 Location ref,
6397 Location obj,
6398 uint32_t offset,
6399 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006400 DCHECK(kEmitCompilerReadBarrier);
6401
Roland Levillain44015862016-01-22 11:47:17 +00006402 // Insert a slow path based read barrier *after* the reference load.
6403 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006404 // If heap poisoning is enabled, the unpoisoning of the loaded
6405 // reference will be carried out by the runtime within the slow
6406 // path.
6407 //
6408 // Note that `ref` currently does not get unpoisoned (when heap
6409 // poisoning is enabled), which is alright as the `ref` argument is
6410 // not used by the artReadBarrierSlow entry point.
6411 //
6412 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006413 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006414 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6415 AddSlowPath(slow_path);
6416
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006417 __ B(slow_path->GetEntryLabel());
6418 __ Bind(slow_path->GetExitLabel());
6419}
6420
Roland Levillain44015862016-01-22 11:47:17 +00006421void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6422 Location out,
6423 Location ref,
6424 Location obj,
6425 uint32_t offset,
6426 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006427 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006428 // Baker's read barriers shall be handled by the fast path
6429 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6430 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006431 // If heap poisoning is enabled, unpoisoning will be taken care of
6432 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006433 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006434 } else if (kPoisonHeapReferences) {
6435 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6436 }
6437}
6438
Roland Levillain44015862016-01-22 11:47:17 +00006439void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6440 Location out,
6441 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006442 DCHECK(kEmitCompilerReadBarrier);
6443
Roland Levillain44015862016-01-22 11:47:17 +00006444 // Insert a slow path based read barrier *after* the GC root load.
6445 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006446 // Note that GC roots are not affected by heap poisoning, so we do
6447 // not need to do anything special for this here.
6448 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006449 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006450 AddSlowPath(slow_path);
6451
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006452 __ B(slow_path->GetEntryLabel());
6453 __ Bind(slow_path->GetExitLabel());
6454}
6455
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006456void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6457 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006458 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006459 locations->SetInAt(0, Location::RequiresRegister());
6460 locations->SetOut(Location::RequiresRegister());
6461}
6462
6463void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6464 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006465 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006466 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006467 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006468 __ Ldr(XRegisterFrom(locations->Out()),
6469 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006470 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006471 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006472 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006473 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6474 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006475 __ Ldr(XRegisterFrom(locations->Out()),
6476 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006477 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006478}
6479
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006480static void PatchJitRootUse(uint8_t* code,
6481 const uint8_t* roots_data,
6482 vixl::aarch64::Literal<uint32_t>* literal,
6483 uint64_t index_in_table) {
6484 uint32_t literal_offset = literal->GetOffset();
6485 uintptr_t address =
6486 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6487 uint8_t* data = code + literal_offset;
6488 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6489}
6490
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006491void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6492 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006493 const StringReference& string_reference = entry.first;
6494 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006495 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006496 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006497 }
6498 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006499 const TypeReference& type_reference = entry.first;
6500 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006501 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006502 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006503 }
6504}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006505
Alexandre Rames67555f72014-11-18 10:55:16 +00006506#undef __
6507#undef QUICK_ENTRY_POINT
6508
Alexandre Rames5319def2014-10-23 10:03:10 +01006509} // namespace arm64
6510} // namespace art