blob: 8aa790db3479f5a6f5bb284b55e093c6e551e3c5 [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 Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010035#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000037#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010038#include "thread.h"
39#include "utils/arm64/assembler_arm64.h"
40#include "utils/assembler.h"
41#include "utils/stack_checks.h"
42
Scott Wakeling97c72b72016-06-24 16:19:36 +010043using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000044using vixl::ExactAssemblyScope;
45using vixl::CodeBufferCheckScope;
46using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010047
48#ifdef __
49#error "ARM64 Codegen VIXL macro-assembler macro already defined."
50#endif
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace art {
53
Roland Levillain22ccc3a2015-11-24 13:10:05 +000054template<class MirrorType>
55class GcRoot;
56
Alexandre Rames5319def2014-10-23 10:03:10 +010057namespace arm64 {
58
Alexandre Ramesbe919d92016-08-23 18:33:36 +010059using helpers::ARM64EncodableConstantOrRegister;
60using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::CPURegisterFrom;
62using helpers::DRegisterFrom;
63using helpers::FPRegisterFrom;
64using helpers::HeapOperand;
65using helpers::HeapOperandFrom;
66using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010072using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073using helpers::LocationFrom;
74using helpers::OperandFromMemOperand;
75using helpers::OutputCPURegister;
76using helpers::OutputFPRegister;
77using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010078using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080079using helpers::RegisterFrom;
Aart Bik1f8d51b2018-02-15 10:42:37 -080080using helpers::SRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080081using 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)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001398 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001399 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001400 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001401 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001402 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001403 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);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001427
1428 // Verify Baker read barrier linker patches.
1429 if (kIsDebugBuild) {
1430 ArrayRef<const uint8_t> code = allocator->GetMemory();
1431 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1432 DCHECK(info.label.IsBound());
1433 uint32_t literal_offset = info.label.GetLocation();
1434 DCHECK_ALIGNED(literal_offset, 4u);
1435
1436 auto GetInsn = [&code](uint32_t offset) {
1437 DCHECK_ALIGNED(offset, 4u);
1438 return
1439 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1440 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1441 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1442 (static_cast<uint32_t>(code[offset + 3]) << 24);
1443 };
1444
1445 const uint32_t encoded_data = info.custom_data;
1446 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1447 // Check that the next instruction matches the expected LDR.
1448 switch (kind) {
1449 case BakerReadBarrierKind::kField: {
1450 DCHECK_GE(code.size() - literal_offset, 8u);
1451 uint32_t next_insn = GetInsn(literal_offset + 4u);
1452 // LDR (immediate) with correct base_reg.
1453 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1454 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1455 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1456 break;
1457 }
1458 case BakerReadBarrierKind::kArray: {
1459 DCHECK_GE(code.size() - literal_offset, 8u);
1460 uint32_t next_insn = GetInsn(literal_offset + 4u);
1461 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1462 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1463 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1464 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1465 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1466 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1467 break;
1468 }
1469 case BakerReadBarrierKind::kGcRoot: {
1470 DCHECK_GE(literal_offset, 4u);
1471 uint32_t prev_insn = GetInsn(literal_offset - 4u);
1472 // LDR (immediate) with correct root_reg.
1473 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1474 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg);
1475 break;
1476 }
1477 default:
1478 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1479 UNREACHABLE();
1480 }
1481 }
1482 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001483}
1484
Zheng Xuad4450e2015-04-17 18:48:56 +08001485void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1486 // Note: There are 6 kinds of moves:
1487 // 1. constant -> GPR/FPR (non-cycle)
1488 // 2. constant -> stack (non-cycle)
1489 // 3. GPR/FPR -> GPR/FPR
1490 // 4. GPR/FPR -> stack
1491 // 5. stack -> GPR/FPR
1492 // 6. stack -> stack (non-cycle)
1493 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1494 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1495 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1496 // dependency.
1497 vixl_temps_.Open(GetVIXLAssembler());
1498}
1499
1500void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1501 vixl_temps_.Close();
1502}
1503
1504Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001505 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1506 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1507 || kind == Location::kSIMDStackSlot);
1508 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1509 ? Location::kFpuRegister
1510 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001511 Location scratch = GetScratchLocation(kind);
1512 if (!scratch.Equals(Location::NoLocation())) {
1513 return scratch;
1514 }
1515 // Allocate from VIXL temp registers.
1516 if (kind == Location::kRegister) {
1517 scratch = LocationFrom(vixl_temps_.AcquireX());
1518 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001519 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001520 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1521 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1522 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001523 }
1524 AddScratchLocation(scratch);
1525 return scratch;
1526}
1527
1528void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1529 if (loc.IsRegister()) {
1530 vixl_temps_.Release(XRegisterFrom(loc));
1531 } else {
1532 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001533 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001534 }
1535 RemoveScratchLocation(loc);
1536}
1537
Alexandre Rames3e69f162014-12-10 10:36:50 +00001538void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001539 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001540 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001541}
1542
Alexandre Rames5319def2014-10-23 10:03:10 +01001543void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001544 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001545 __ Bind(&frame_entry_label_);
1546
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001547 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1548 UseScratchRegisterScope temps(masm);
1549 Register temp = temps.AcquireX();
1550 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1551 __ Add(temp, temp, 1);
1552 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1553 }
1554
Vladimir Marko33bff252017-11-01 14:35:42 +00001555 bool do_overflow_check =
1556 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001557 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001558 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001559 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001560 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001561 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001562 {
1563 // Ensure that between load and RecordPcInfo there are no pools emitted.
1564 ExactAssemblyScope eas(GetVIXLAssembler(),
1565 kInstructionSize,
1566 CodeBufferCheckScope::kExactSize);
1567 __ ldr(wzr, MemOperand(temp, 0));
1568 RecordPcInfo(nullptr, 0);
1569 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001570 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001571
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001572 if (!HasEmptyFrame()) {
1573 int frame_size = GetFrameSize();
1574 // Stack layout:
1575 // sp[frame_size - 8] : lr.
1576 // ... : other preserved core registers.
1577 // ... : other preserved fp registers.
1578 // ... : reserved frame space.
1579 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001580
1581 // Save the current method if we need it. Note that we do not
1582 // do this in HCurrentMethod, as the instruction might have been removed
1583 // in the SSA graph.
1584 if (RequiresCurrentMethod()) {
1585 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001586 } else {
1587 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001588 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001589 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001590 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1591 frame_size - GetCoreSpillSize());
1592 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1593 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001594
1595 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1596 // Initialize should_deoptimize flag to 0.
1597 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1598 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1599 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001600 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001601
1602 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001603}
1604
1605void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001606 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001607 if (!HasEmptyFrame()) {
1608 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001609 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1610 frame_size - FrameEntrySpillSize());
1611 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1612 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001613 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001614 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001615 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001616 __ Ret();
1617 GetAssembler()->cfi().RestoreState();
1618 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001619}
1620
Scott Wakeling97c72b72016-06-24 16:19:36 +01001621CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001622 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001623 return CPURegList(CPURegister::kRegister, kXRegSize,
1624 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001625}
1626
Scott Wakeling97c72b72016-06-24 16:19:36 +01001627CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001628 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1629 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001630 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1631 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001632}
1633
Alexandre Rames5319def2014-10-23 10:03:10 +01001634void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1635 __ Bind(GetLabelOf(block));
1636}
1637
Calin Juravle175dc732015-08-25 15:42:32 +01001638void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1639 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001640 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001641}
1642
Calin Juravlee460d1d2015-09-29 04:52:17 +01001643void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1644 if (location.IsRegister()) {
1645 locations->AddTemp(location);
1646 } else {
1647 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1648 }
1649}
1650
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001651void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001652 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001653 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001654 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001655 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001656 if (value_can_be_null) {
1657 __ Cbz(value, &done);
1658 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001659 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001660 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001661 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001662 if (value_can_be_null) {
1663 __ Bind(&done);
1664 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001665}
1666
David Brazdil58282f42016-01-14 12:45:10 +00001667void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001668 // Blocked core registers:
1669 // lr : Runtime reserved.
1670 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001671 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001672 // ip1 : VIXL core temp.
1673 // ip0 : VIXL core temp.
1674 //
1675 // Blocked fp registers:
1676 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001677 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1678 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001679 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001680 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001681 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001682
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001683 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001684 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001685 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001686 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001687
David Brazdil58282f42016-01-14 12:45:10 +00001688 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001689 // Stubs do not save callee-save floating point registers. If the graph
1690 // is debuggable, we need to deal with these registers differently. For
1691 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001692 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1693 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001694 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001695 }
1696 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001697}
1698
Alexandre Rames3e69f162014-12-10 10:36:50 +00001699size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1700 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1701 __ Str(reg, MemOperand(sp, stack_index));
1702 return kArm64WordSize;
1703}
1704
1705size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1706 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1707 __ Ldr(reg, MemOperand(sp, stack_index));
1708 return kArm64WordSize;
1709}
1710
1711size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1712 FPRegister reg = FPRegister(reg_id, kDRegSize);
1713 __ Str(reg, MemOperand(sp, stack_index));
1714 return kArm64WordSize;
1715}
1716
1717size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1718 FPRegister reg = FPRegister(reg_id, kDRegSize);
1719 __ Ldr(reg, MemOperand(sp, stack_index));
1720 return kArm64WordSize;
1721}
1722
Alexandre Rames5319def2014-10-23 10:03:10 +01001723void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001724 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001725}
1726
1727void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001728 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001729}
1730
Alexandre Rames67555f72014-11-18 10:55:16 +00001731void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001732 if (constant->IsIntConstant()) {
1733 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1734 } else if (constant->IsLongConstant()) {
1735 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1736 } else if (constant->IsNullConstant()) {
1737 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001738 } else if (constant->IsFloatConstant()) {
1739 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1740 } else {
1741 DCHECK(constant->IsDoubleConstant());
1742 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1743 }
1744}
1745
Alexandre Rames3e69f162014-12-10 10:36:50 +00001746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748 DCHECK(constant.IsConstant());
1749 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001750 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001751 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001752 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1753 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1754 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1755 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001756}
1757
Roland Levillain952b2352017-05-03 19:49:14 +01001758// Allocate a scratch register from the VIXL pool, querying first
1759// the floating-point register pool, and then the core register
1760// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001761// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1762// using a different allocation strategy.
1763static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1764 vixl::aarch64::UseScratchRegisterScope* temps,
1765 int size_in_bits) {
1766 return masm->GetScratchFPRegisterList()->IsEmpty()
1767 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1768 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1769}
1770
Calin Juravlee460d1d2015-09-29 04:52:17 +01001771void CodeGeneratorARM64::MoveLocation(Location destination,
1772 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001774 if (source.Equals(destination)) {
1775 return;
1776 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001777
1778 // A valid move can always be inferred from the destination and source
1779 // locations. When moving from and to a register, the argument type can be
1780 // used to generate 32bit instead of 64bit moves. In debug mode we also
1781 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001782 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001783
1784 if (destination.IsRegister() || destination.IsFpuRegister()) {
1785 if (unspecified_type) {
1786 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1787 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001788 (src_cst != nullptr && (src_cst->IsIntConstant()
1789 || src_cst->IsFloatConstant()
1790 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001791 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001792 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001793 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001794 // If the source is a double stack slot or a 64bit constant, a 64bit
1795 // type is appropriate. Else the source is a register, and since the
1796 // type has not been specified, we chose a 64bit type to force a 64bit
1797 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001798 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001799 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001800 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001801 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1802 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001803 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001804 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1805 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1806 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001807 } else if (source.IsSIMDStackSlot()) {
1808 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001810 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001811 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001812 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001813 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001814 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001815 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001816 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 DataType::Type source_type = DataType::Is64BitType(dst_type)
1818 ? DataType::Type::kInt64
1819 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001820 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1821 }
1822 } else {
1823 DCHECK(source.IsFpuRegister());
1824 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 DataType::Type source_type = DataType::Is64BitType(dst_type)
1826 ? DataType::Type::kFloat64
1827 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001828 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1829 } else {
1830 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001831 if (GetGraph()->HasSIMD()) {
1832 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1833 } else {
1834 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1835 }
1836 }
1837 }
1838 } else if (destination.IsSIMDStackSlot()) {
1839 if (source.IsFpuRegister()) {
1840 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1841 } else {
1842 DCHECK(source.IsSIMDStackSlot());
1843 UseScratchRegisterScope temps(GetVIXLAssembler());
1844 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1845 Register temp = temps.AcquireX();
1846 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1847 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1848 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1849 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1850 } else {
1851 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1852 __ Ldr(temp, StackOperandFrom(source));
1853 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001854 }
1855 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001856 } else { // The destination is not a register. It must be a stack slot.
1857 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1858 if (source.IsRegister() || source.IsFpuRegister()) {
1859 if (unspecified_type) {
1860 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001862 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 dst_type =
1864 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001865 }
1866 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1868 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001869 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001870 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001871 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1872 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001873 UseScratchRegisterScope temps(GetVIXLAssembler());
1874 HConstant* src_cst = source.GetConstant();
1875 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001876 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001877 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1878 ? Register(xzr)
1879 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001880 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001881 if (src_cst->IsIntConstant()) {
1882 temp = temps.AcquireW();
1883 } else if (src_cst->IsLongConstant()) {
1884 temp = temps.AcquireX();
1885 } else if (src_cst->IsFloatConstant()) {
1886 temp = temps.AcquireS();
1887 } else {
1888 DCHECK(src_cst->IsDoubleConstant());
1889 temp = temps.AcquireD();
1890 }
1891 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001892 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001893 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001894 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001895 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001896 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001897 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001898 // Use any scratch register (a core or a floating-point one)
1899 // from VIXL scratch register pools as a temporary.
1900 //
1901 // We used to only use the FP scratch register pool, but in some
1902 // rare cases the only register from this pool (D31) would
1903 // already be used (e.g. within a ParallelMove instruction, when
1904 // a move is blocked by a another move requiring a scratch FP
1905 // register, which would reserve D31). To prevent this issue, we
1906 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001907 //
1908 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001909 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001910 // use AcquireFPOrCoreCPURegisterOfSize instead of
1911 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1912 // allocates core scratch registers first.
1913 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1914 GetVIXLAssembler(),
1915 &temps,
1916 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001917 __ Ldr(temp, StackOperandFrom(source));
1918 __ Str(temp, StackOperandFrom(destination));
1919 }
1920 }
1921}
1922
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001923void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001924 CPURegister dst,
1925 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001926 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001928 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001929 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001930 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001932 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001933 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001935 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001936 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001937 case DataType::Type::kInt16:
1938 __ Ldrsh(Register(dst), src);
1939 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001940 case DataType::Type::kInt32:
1941 case DataType::Type::kReference:
1942 case DataType::Type::kInt64:
1943 case DataType::Type::kFloat32:
1944 case DataType::Type::kFloat64:
1945 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001946 __ Ldr(dst, src);
1947 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001948 case DataType::Type::kUint32:
1949 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001950 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001951 LOG(FATAL) << "Unreachable type " << type;
1952 }
1953}
1954
Calin Juravle77520bc2015-01-12 18:45:46 +00001955void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001956 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001957 const MemOperand& src,
1958 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001959 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001960 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001961 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001962 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001963
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001964 DCHECK(!src.IsPreIndex());
1965 DCHECK(!src.IsPostIndex());
1966
1967 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001968 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001969 {
1970 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1971 MemOperand base = MemOperand(temp_base);
1972 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001974 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001975 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001976 {
1977 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1978 __ ldarb(Register(dst), base);
1979 if (needs_null_check) {
1980 MaybeRecordImplicitNullCheck(instruction);
1981 }
1982 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001983 if (type == DataType::Type::kInt8) {
1984 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001985 }
1986 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001987 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001989 {
1990 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1991 __ ldarh(Register(dst), base);
1992 if (needs_null_check) {
1993 MaybeRecordImplicitNullCheck(instruction);
1994 }
1995 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001996 if (type == DataType::Type::kInt16) {
1997 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1998 }
Artem Serov914d7a82017-02-07 14:33:49 +00001999 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 case DataType::Type::kInt32:
2001 case DataType::Type::kReference:
2002 case DataType::Type::kInt64:
2003 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002004 {
2005 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2006 __ ldar(Register(dst), base);
2007 if (needs_null_check) {
2008 MaybeRecordImplicitNullCheck(instruction);
2009 }
2010 }
2011 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002012 case DataType::Type::kFloat32:
2013 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00002014 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002015 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002016
Artem Serov914d7a82017-02-07 14:33:49 +00002017 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2018 {
2019 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2020 __ ldar(temp, base);
2021 if (needs_null_check) {
2022 MaybeRecordImplicitNullCheck(instruction);
2023 }
2024 }
2025 __ Fmov(FPRegister(dst), temp);
2026 break;
Roland Levillain44015862016-01-22 11:47:17 +00002027 }
Aart Bik66c158e2018-01-31 12:55:04 -08002028 case DataType::Type::kUint32:
2029 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00002031 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002032 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002033 }
2034}
2035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002036void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002037 CPURegister src,
2038 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002039 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002041 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002043 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002044 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 case DataType::Type::kUint16:
2046 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002047 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002048 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 case DataType::Type::kInt32:
2050 case DataType::Type::kReference:
2051 case DataType::Type::kInt64:
2052 case DataType::Type::kFloat32:
2053 case DataType::Type::kFloat64:
2054 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002055 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002056 break;
Aart Bik66c158e2018-01-31 12:55:04 -08002057 case DataType::Type::kUint32:
2058 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002060 LOG(FATAL) << "Unreachable type " << type;
2061 }
2062}
2063
Artem Serov914d7a82017-02-07 14:33:49 +00002064void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002066 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002067 const MemOperand& dst,
2068 bool needs_null_check) {
2069 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002070 UseScratchRegisterScope temps(GetVIXLAssembler());
2071 Register temp_base = temps.AcquireX();
2072
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002073 DCHECK(!dst.IsPreIndex());
2074 DCHECK(!dst.IsPostIndex());
2075
2076 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002077 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002078 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002079 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002080 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002081 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002083 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002085 {
2086 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2087 __ stlrb(Register(src), base);
2088 if (needs_null_check) {
2089 MaybeRecordImplicitNullCheck(instruction);
2090 }
2091 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002092 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 case DataType::Type::kUint16:
2094 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002095 {
2096 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2097 __ stlrh(Register(src), base);
2098 if (needs_null_check) {
2099 MaybeRecordImplicitNullCheck(instruction);
2100 }
2101 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002102 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002103 case DataType::Type::kInt32:
2104 case DataType::Type::kReference:
2105 case DataType::Type::kInt64:
2106 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002107 {
2108 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2109 __ stlr(Register(src), base);
2110 if (needs_null_check) {
2111 MaybeRecordImplicitNullCheck(instruction);
2112 }
2113 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002114 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 case DataType::Type::kFloat32:
2116 case DataType::Type::kFloat64: {
2117 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002118 Register temp_src;
2119 if (src.IsZero()) {
2120 // The zero register is used to avoid synthesizing zero constants.
2121 temp_src = Register(src);
2122 } else {
2123 DCHECK(src.IsFPRegister());
2124 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2125 __ Fmov(temp_src, FPRegister(src));
2126 }
Artem Serov914d7a82017-02-07 14:33:49 +00002127 {
2128 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2129 __ stlr(temp_src, base);
2130 if (needs_null_check) {
2131 MaybeRecordImplicitNullCheck(instruction);
2132 }
2133 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002134 break;
2135 }
Aart Bik66c158e2018-01-31 12:55:04 -08002136 case DataType::Type::kUint32:
2137 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002139 LOG(FATAL) << "Unreachable type " << type;
2140 }
2141}
2142
Calin Juravle175dc732015-08-25 15:42:32 +01002143void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2144 HInstruction* instruction,
2145 uint32_t dex_pc,
2146 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002147 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002148
2149 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2150 {
2151 // Ensure the pc position is recorded immediately after the `blr` instruction.
2152 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2153 __ blr(lr);
2154 if (EntrypointRequiresStackMap(entrypoint)) {
2155 RecordPcInfo(instruction, dex_pc, slow_path);
2156 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002157 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002158}
2159
Roland Levillaindec8f632016-07-22 17:10:06 +01002160void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2161 HInstruction* instruction,
2162 SlowPathCode* slow_path) {
2163 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002164 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2165 __ Blr(lr);
2166}
2167
Alexandre Rames67555f72014-11-18 10:55:16 +00002168void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002169 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002170 UseScratchRegisterScope temps(GetVIXLAssembler());
2171 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00002172 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2173 const size_t status_byte_offset =
2174 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2175 constexpr uint32_t shifted_initialized_value =
2176 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002177
Serban Constantinescu02164b32014-11-13 14:05:07 +00002178 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002179 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00002180 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07002181 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00002182 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00002183 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002184 __ Bind(slow_path->GetExitLabel());
2185}
Alexandre Rames5319def2014-10-23 10:03:10 +01002186
Vladimir Marko175e7862018-03-27 09:03:13 +00002187void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
2188 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
2189 uint32_t path_to_root = check->GetBitstringPathToRoot();
2190 uint32_t mask = check->GetBitstringMask();
2191 DCHECK(IsPowerOfTwo(mask + 1));
2192 size_t mask_bits = WhichPowerOf2(mask + 1);
2193
2194 if (mask_bits == 16u) {
2195 // Load only the bitstring part of the status word.
2196 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2197 } else {
2198 // /* uint32_t */ temp = temp->status_
2199 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2200 // Extract the bitstring bits.
2201 __ Ubfx(temp, temp, 0, mask_bits);
2202 }
2203 // Compare the bitstring bits to `path_to_root`.
2204 __ Cmp(temp, path_to_root);
2205}
2206
Roland Levillain44015862016-01-22 11:47:17 +00002207void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002208 BarrierType type = BarrierAll;
2209
2210 switch (kind) {
2211 case MemBarrierKind::kAnyAny:
2212 case MemBarrierKind::kAnyStore: {
2213 type = BarrierAll;
2214 break;
2215 }
2216 case MemBarrierKind::kLoadAny: {
2217 type = BarrierReads;
2218 break;
2219 }
2220 case MemBarrierKind::kStoreStore: {
2221 type = BarrierWrites;
2222 break;
2223 }
2224 default:
2225 LOG(FATAL) << "Unexpected memory barrier " << kind;
2226 }
2227 __ Dmb(InnerShareable, type);
2228}
2229
Serban Constantinescu02164b32014-11-13 14:05:07 +00002230void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2231 HBasicBlock* successor) {
2232 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002233 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2234 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002235 slow_path =
2236 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002237 instruction->SetSlowPath(slow_path);
2238 codegen_->AddSlowPath(slow_path);
2239 if (successor != nullptr) {
2240 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002241 }
2242 } else {
2243 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2244 }
2245
Serban Constantinescu02164b32014-11-13 14:05:07 +00002246 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2247 Register temp = temps.AcquireW();
2248
Andreas Gampe542451c2016-07-26 09:02:02 -07002249 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002250 if (successor == nullptr) {
2251 __ Cbnz(temp, slow_path->GetEntryLabel());
2252 __ Bind(slow_path->GetReturnLabel());
2253 } else {
2254 __ Cbz(temp, codegen_->GetLabelOf(successor));
2255 __ B(slow_path->GetEntryLabel());
2256 // slow_path will return to GetLabelOf(successor).
2257 }
2258}
2259
Alexandre Rames5319def2014-10-23 10:03:10 +01002260InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2261 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002262 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002263 assembler_(codegen->GetAssembler()),
2264 codegen_(codegen) {}
2265
Alexandre Rames67555f72014-11-18 10:55:16 +00002266void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002267 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002268 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002269 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002270 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 case DataType::Type::kInt32:
2272 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002274 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002276 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002277
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 case DataType::Type::kFloat32:
2279 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002280 locations->SetInAt(0, Location::RequiresFpuRegister());
2281 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002282 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002283 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002284
Alexandre Rames5319def2014-10-23 10:03:10 +01002285 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002286 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002287 }
2288}
2289
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002290void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2291 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002292 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2293
2294 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002296 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002297 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2298 object_field_get_with_read_barrier
2299 ? LocationSummary::kCallOnSlowPath
2300 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002301 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002302 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002303 // We need a temporary register for the read barrier marking slow
2304 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002305 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2306 !Runtime::Current()->UseJitCompilation() &&
2307 !field_info.IsVolatile()) {
2308 // If link-time thunks for the Baker read barrier are enabled, for AOT
2309 // non-volatile loads we need a temporary only if the offset is too big.
2310 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2311 locations->AddTemp(FixedTempLocation());
2312 }
2313 } else {
2314 locations->AddTemp(Location::RequiresRegister());
2315 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002316 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002317 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002319 locations->SetOut(Location::RequiresFpuRegister());
2320 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002321 // The output overlaps for an object field get when read barriers
2322 // are enabled: we do not want the load to overwrite the object's
2323 // location, as we need it to emit the read barrier.
2324 locations->SetOut(
2325 Location::RequiresRegister(),
2326 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002327 }
2328}
2329
2330void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2331 const FieldInfo& field_info) {
2332 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002333 LocationSummary* locations = instruction->GetLocations();
2334 Location base_loc = locations->InAt(0);
2335 Location out = locations->Out();
2336 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002337 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2338 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002339 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002340
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002342 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002343 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002344 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002346 Location maybe_temp =
2347 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002348 // Note that potential implicit null checks are handled in this
2349 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2350 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2351 instruction,
2352 out,
2353 base,
2354 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002355 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002356 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002357 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002358 } else {
2359 // General case.
2360 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002361 // Note that a potential implicit null check is handled in this
2362 // CodeGeneratorARM64::LoadAcquire call.
2363 // NB: LoadAcquire will record the pc info if needed.
2364 codegen_->LoadAcquire(
2365 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002366 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002367 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2368 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002369 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002370 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002371 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002372 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002373 // If read barriers are enabled, emit read barriers other than
2374 // Baker's using a slow path (and also unpoison the loaded
2375 // reference, if heap poisoning is enabled).
2376 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2377 }
Roland Levillain4d027112015-07-01 15:41:14 +01002378 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002379}
2380
2381void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2382 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002383 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002384 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002385 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2386 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002388 locations->SetInAt(1, Location::RequiresFpuRegister());
2389 } else {
2390 locations->SetInAt(1, Location::RequiresRegister());
2391 }
2392}
2393
2394void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002395 const FieldInfo& field_info,
2396 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002397 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2398
2399 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002400 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002401 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002402 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002404
Roland Levillain4d027112015-07-01 15:41:14 +01002405 {
2406 // We use a block to end the scratch scope before the write barrier, thus
2407 // freeing the temporary registers so they can be used in `MarkGCCard`.
2408 UseScratchRegisterScope temps(GetVIXLAssembler());
2409
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002411 DCHECK(value.IsW());
2412 Register temp = temps.AcquireW();
2413 __ Mov(temp, value.W());
2414 GetAssembler()->PoisonHeapReference(temp.W());
2415 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002416 }
Roland Levillain4d027112015-07-01 15:41:14 +01002417
2418 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002419 codegen_->StoreRelease(
2420 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002421 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002422 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2423 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002424 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2425 codegen_->MaybeRecordImplicitNullCheck(instruction);
2426 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002427 }
2428
2429 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002430 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002431 }
2432}
2433
Alexandre Rames67555f72014-11-18 10:55:16 +00002434void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002435 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002436
2437 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 case DataType::Type::kInt32:
2439 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002440 Register dst = OutputRegister(instr);
2441 Register lhs = InputRegisterAt(instr, 0);
2442 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002443 if (instr->IsAdd()) {
2444 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002445 } else if (instr->IsAnd()) {
2446 __ And(dst, lhs, rhs);
2447 } else if (instr->IsOr()) {
2448 __ Orr(dst, lhs, rhs);
2449 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002450 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002451 } else if (instr->IsRor()) {
2452 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002453 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002454 __ Ror(dst, lhs, shift);
2455 } else {
2456 // Ensure shift distance is in the same size register as the result. If
2457 // we are rotating a long and the shift comes in a w register originally,
2458 // we don't need to sxtw for use as an x since the shift distances are
2459 // all & reg_bits - 1.
2460 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2461 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002462 } else if (instr->IsMin() || instr->IsMax()) {
2463 __ Cmp(lhs, rhs);
2464 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002465 } else {
2466 DCHECK(instr->IsXor());
2467 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002468 }
2469 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002470 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 case DataType::Type::kFloat32:
2472 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002473 FPRegister dst = OutputFPRegister(instr);
2474 FPRegister lhs = InputFPRegisterAt(instr, 0);
2475 FPRegister rhs = InputFPRegisterAt(instr, 1);
2476 if (instr->IsAdd()) {
2477 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002478 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002479 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002480 } else if (instr->IsMin()) {
2481 __ Fmin(dst, lhs, rhs);
2482 } else if (instr->IsMax()) {
2483 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002484 } else {
2485 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002486 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002487 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002488 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002489 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002490 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002491 }
2492}
2493
Serban Constantinescu02164b32014-11-13 14:05:07 +00002494void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2495 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2496
Vladimir Markoca6fff82017-10-03 14:49:14 +01002497 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002499 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 case DataType::Type::kInt32:
2501 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002502 locations->SetInAt(0, Location::RequiresRegister());
2503 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002504 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002505 break;
2506 }
2507 default:
2508 LOG(FATAL) << "Unexpected shift type " << type;
2509 }
2510}
2511
2512void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2513 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002516 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002517 case DataType::Type::kInt32:
2518 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002519 Register dst = OutputRegister(instr);
2520 Register lhs = InputRegisterAt(instr, 0);
2521 Operand rhs = InputOperandAt(instr, 1);
2522 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002523 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002524 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002525 if (instr->IsShl()) {
2526 __ Lsl(dst, lhs, shift_value);
2527 } else if (instr->IsShr()) {
2528 __ Asr(dst, lhs, shift_value);
2529 } else {
2530 __ Lsr(dst, lhs, shift_value);
2531 }
2532 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002533 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002534
2535 if (instr->IsShl()) {
2536 __ Lsl(dst, lhs, rhs_reg);
2537 } else if (instr->IsShr()) {
2538 __ Asr(dst, lhs, rhs_reg);
2539 } else {
2540 __ Lsr(dst, lhs, rhs_reg);
2541 }
2542 }
2543 break;
2544 }
2545 default:
2546 LOG(FATAL) << "Unexpected shift operation type " << type;
2547 }
2548}
2549
Alexandre Rames5319def2014-10-23 10:03:10 +01002550void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002551 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002552}
2553
2554void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002555 HandleBinaryOp(instruction);
2556}
2557
2558void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2559 HandleBinaryOp(instruction);
2560}
2561
2562void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2563 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002564}
2565
Artem Serov7fc63502016-02-09 17:15:29 +00002566void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002568 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002569 locations->SetInAt(0, Location::RequiresRegister());
2570 // There is no immediate variant of negated bitwise instructions in AArch64.
2571 locations->SetInAt(1, Location::RequiresRegister());
2572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2573}
2574
Artem Serov7fc63502016-02-09 17:15:29 +00002575void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002576 Register dst = OutputRegister(instr);
2577 Register lhs = InputRegisterAt(instr, 0);
2578 Register rhs = InputRegisterAt(instr, 1);
2579
2580 switch (instr->GetOpKind()) {
2581 case HInstruction::kAnd:
2582 __ Bic(dst, lhs, rhs);
2583 break;
2584 case HInstruction::kOr:
2585 __ Orn(dst, lhs, rhs);
2586 break;
2587 case HInstruction::kXor:
2588 __ Eon(dst, lhs, rhs);
2589 break;
2590 default:
2591 LOG(FATAL) << "Unreachable";
2592 }
2593}
2594
Anton Kirilov74234da2017-01-13 14:42:47 +00002595void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2596 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2598 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002599 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002600 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002601 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2602 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2603 } else {
2604 locations->SetInAt(0, Location::RequiresRegister());
2605 }
2606 locations->SetInAt(1, Location::RequiresRegister());
2607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2608}
2609
Anton Kirilov74234da2017-01-13 14:42:47 +00002610void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2611 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002613 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002615 Register out = OutputRegister(instruction);
2616 Register left;
2617 if (kind != HInstruction::kNeg) {
2618 left = InputRegisterAt(instruction, 0);
2619 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002620 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002621 // shifter operand operation, the IR generating `right_reg` (input to the type
2622 // conversion) can have a different type from the current instruction's type,
2623 // so we manually indicate the type.
2624 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002625 Operand right_operand(0);
2626
Anton Kirilov74234da2017-01-13 14:42:47 +00002627 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2628 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002629 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2630 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002631 right_operand = Operand(right_reg,
2632 helpers::ShiftFromOpKind(op_kind),
2633 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002634 }
2635
2636 // Logical binary operations do not support extension operations in the
2637 // operand. Note that VIXL would still manage if it was passed by generating
2638 // the extension as a separate instruction.
2639 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2640 DCHECK(!right_operand.IsExtendedRegister() ||
2641 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2642 kind != HInstruction::kNeg));
2643 switch (kind) {
2644 case HInstruction::kAdd:
2645 __ Add(out, left, right_operand);
2646 break;
2647 case HInstruction::kAnd:
2648 __ And(out, left, right_operand);
2649 break;
2650 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002651 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002652 __ Neg(out, right_operand);
2653 break;
2654 case HInstruction::kOr:
2655 __ Orr(out, left, right_operand);
2656 break;
2657 case HInstruction::kSub:
2658 __ Sub(out, left, right_operand);
2659 break;
2660 case HInstruction::kXor:
2661 __ Eor(out, left, right_operand);
2662 break;
2663 default:
2664 LOG(FATAL) << "Unexpected operation kind: " << kind;
2665 UNREACHABLE();
2666 }
2667}
2668
Artem Serov328429f2016-07-06 16:23:04 +01002669void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002670 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002671 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002672 locations->SetInAt(0, Location::RequiresRegister());
2673 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002675}
2676
Roland Levillain19c54192016-11-04 13:44:09 +00002677void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002678 __ Add(OutputRegister(instruction),
2679 InputRegisterAt(instruction, 0),
2680 Operand(InputOperandAt(instruction, 1)));
2681}
2682
Artem Serove1811ed2017-04-27 16:50:47 +01002683void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2684 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002685 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002686
2687 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2688
2689 locations->SetInAt(0, Location::RequiresRegister());
2690 // For byte case we don't need to shift the index variable so we can encode the data offset into
2691 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2692 // data offset constant generation out of the loop and reduce the critical path length in the
2693 // loop.
2694 locations->SetInAt(1, shift->GetValue() == 0
2695 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2696 : Location::RequiresRegister());
2697 locations->SetInAt(2, Location::ConstantLocation(shift));
2698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2699}
2700
2701void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2702 HIntermediateAddressIndex* instruction) {
2703 Register index_reg = InputRegisterAt(instruction, 0);
2704 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2705 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2706
2707 if (shift == 0) {
2708 __ Add(OutputRegister(instruction), index_reg, offset);
2709 } else {
2710 Register offset_reg = InputRegisterAt(instruction, 1);
2711 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2712 }
2713}
2714
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002715void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002716 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002717 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002718 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2719 if (instr->GetOpKind() == HInstruction::kSub &&
2720 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002721 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002722 // Don't allocate register for Mneg instruction.
2723 } else {
2724 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2725 Location::RequiresRegister());
2726 }
2727 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2728 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002729 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2730}
2731
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002732void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002733 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002734 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2735 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002736
2737 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2738 // This fixup should be carried out for all multiply-accumulate instructions:
2739 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002741 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2742 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002743 vixl::aarch64::Instruction* prev =
2744 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002745 if (prev->IsLoadOrStore()) {
2746 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002747 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002748 __ nop();
2749 }
2750 }
2751
2752 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002753 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002754 __ Madd(res, mul_left, mul_right, accumulator);
2755 } else {
2756 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002757 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002758 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002759 __ Mneg(res, mul_left, mul_right);
2760 } else {
2761 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2762 __ Msub(res, mul_left, mul_right, accumulator);
2763 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002764 }
2765}
2766
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002767void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002768 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002770 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002771 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2772 object_array_get_with_read_barrier
2773 ? LocationSummary::kCallOnSlowPath
2774 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002775 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002776 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002777 // We need a temporary register for the read barrier marking slow
2778 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002779 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2780 !Runtime::Current()->UseJitCompilation() &&
2781 instruction->GetIndex()->IsConstant()) {
2782 // Array loads with constant index are treated as field loads.
2783 // If link-time thunks for the Baker read barrier are enabled, for AOT
2784 // constant index loads we need a temporary only if the offset is too big.
2785 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2786 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002788 if (offset >= kReferenceLoadMinFarOffset) {
2789 locations->AddTemp(FixedTempLocation());
2790 }
2791 } else {
2792 locations->AddTemp(Location::RequiresRegister());
2793 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002794 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002795 locations->SetInAt(0, Location::RequiresRegister());
2796 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002797 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002798 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2799 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002800 // The output overlaps in the case of an object array get with
2801 // read barriers enabled: we do not want the move to overwrite the
2802 // array's location, as we need it to emit the read barrier.
2803 locations->SetOut(
2804 Location::RequiresRegister(),
2805 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002806 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002807}
2808
2809void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002811 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002812 LocationSummary* locations = instruction->GetLocations();
2813 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002814 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002815 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002816 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2817 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002818 MacroAssembler* masm = GetVIXLAssembler();
2819 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002820
Roland Levillain19c54192016-11-04 13:44:09 +00002821 // The read barrier instrumentation of object ArrayGet instructions
2822 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002824 instruction->GetArray()->IsIntermediateAddress() &&
2825 kEmitCompilerReadBarrier));
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002828 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002829 // Note that a potential implicit null check is handled in the
2830 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002831 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002832 if (index.IsConstant()) {
2833 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002834 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002835 Location maybe_temp =
2836 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2837 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2838 out,
2839 obj.W(),
2840 offset,
2841 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002842 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002843 /* use_load_acquire */ false);
2844 } else {
2845 Register temp = WRegisterFrom(locations->GetTemp(0));
2846 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002847 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002848 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002849 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002850 // General case.
2851 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002852 Register length;
2853 if (maybe_compressed_char_at) {
2854 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2855 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002856 {
2857 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2858 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2859
2860 if (instruction->GetArray()->IsIntermediateAddress()) {
2861 DCHECK_LT(count_offset, offset);
2862 int64_t adjusted_offset =
2863 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2864 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2865 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2866 } else {
2867 __ Ldr(length, HeapOperand(obj, count_offset));
2868 }
2869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002870 }
jessicahandojo05765752016-09-09 19:01:32 -07002871 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002872 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002873 if (maybe_compressed_char_at) {
2874 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002875 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2876 "Expecting 0=compressed, 1=uncompressed");
2877 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002878 __ Ldrb(Register(OutputCPURegister(instruction)),
2879 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2880 __ B(&done);
2881 __ Bind(&uncompressed_load);
2882 __ Ldrh(Register(OutputCPURegister(instruction)),
2883 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2884 __ Bind(&done);
2885 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002886 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002887 source = HeapOperand(obj, offset);
2888 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002889 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002890 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002891 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002892 // We do not need to compute the intermediate address from the array: the
2893 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002894 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002895 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002896 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002897 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2898 }
2899 temp = obj;
2900 } else {
2901 __ Add(temp, obj, offset);
2902 }
jessicahandojo05765752016-09-09 19:01:32 -07002903 if (maybe_compressed_char_at) {
2904 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002905 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2906 "Expecting 0=compressed, 1=uncompressed");
2907 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002908 __ Ldrb(Register(OutputCPURegister(instruction)),
2909 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2910 __ B(&done);
2911 __ Bind(&uncompressed_load);
2912 __ Ldrh(Register(OutputCPURegister(instruction)),
2913 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2914 __ Bind(&done);
2915 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002917 }
Roland Levillain44015862016-01-22 11:47:17 +00002918 }
jessicahandojo05765752016-09-09 19:01:32 -07002919 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002920 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2921 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002922 codegen_->Load(type, OutputCPURegister(instruction), source);
2923 codegen_->MaybeRecordImplicitNullCheck(instruction);
2924 }
Roland Levillain44015862016-01-22 11:47:17 +00002925
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002927 static_assert(
2928 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2929 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2930 Location obj_loc = locations->InAt(0);
2931 if (index.IsConstant()) {
2932 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2933 } else {
2934 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2935 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002936 }
Roland Levillain4d027112015-07-01 15:41:14 +01002937 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002938}
2939
Alexandre Rames5319def2014-10-23 10:03:10 +01002940void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002941 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002942 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002944}
2945
2946void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002947 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002948 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002949 {
2950 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2951 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2952 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2953 codegen_->MaybeRecordImplicitNullCheck(instruction);
2954 }
jessicahandojo05765752016-09-09 19:01:32 -07002955 // Mask out compression flag from String's array length.
2956 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002957 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002958 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002959}
2960
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002961void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002963
2964 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002965 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002966 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002967 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002968 LocationSummary::kCallOnSlowPath :
2969 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002970 locations->SetInAt(0, Location::RequiresRegister());
2971 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002972 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2973 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002975 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002976 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002977 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002978 }
2979}
2980
2981void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002983 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002984 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002985 bool needs_write_barrier =
2986 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002987
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002988 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002989 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002990 CPURegister source = value;
2991 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002993 MemOperand destination = HeapOperand(array);
2994 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002995
2996 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002997 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002998 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002999 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003000 destination = HeapOperand(array, offset);
3001 } else {
3002 UseScratchRegisterScope temps(masm);
3003 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01003004 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003005 // We do not need to compute the intermediate address from the array: the
3006 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01003007 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003008 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01003009 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003010 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
3011 }
3012 temp = array;
3013 } else {
3014 __ Add(temp, array, offset);
3015 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003016 destination = HeapOperand(temp,
3017 XRegisterFrom(index),
3018 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003020 }
Artem Serov914d7a82017-02-07 14:33:49 +00003021 {
3022 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3023 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3024 codegen_->Store(value_type, value, destination);
3025 codegen_->MaybeRecordImplicitNullCheck(instruction);
3026 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003027 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003028 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003029 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003030 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003031 {
3032 // We use a block to end the scratch scope before the write barrier, thus
3033 // freeing the temporary registers so they can be used in `MarkGCCard`.
3034 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003035 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003036 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003037 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003038 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003039 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003040 destination = HeapOperand(temp,
3041 XRegisterFrom(index),
3042 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003043 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003044 }
3045
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003046 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3047 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3048 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3049
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003050 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003051 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003052 codegen_->AddSlowPath(slow_path);
3053 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003054 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003055 __ Cbnz(Register(value), &non_zero);
3056 if (!index.IsConstant()) {
3057 __ Add(temp, array, offset);
3058 }
Artem Serov914d7a82017-02-07 14:33:49 +00003059 {
3060 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3061 // emitted.
3062 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3063 __ Str(wzr, destination);
3064 codegen_->MaybeRecordImplicitNullCheck(instruction);
3065 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003066 __ B(&done);
3067 __ Bind(&non_zero);
3068 }
3069
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003070 // Note that when Baker read barriers are enabled, the type
3071 // checks are performed without read barriers. This is fine,
3072 // even in the case where a class object is in the from-space
3073 // after the flip, as a comparison involving such a type would
3074 // not produce a false positive; it may of course produce a
3075 // false negative, in which case we would take the ArraySet
3076 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003077
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003078 Register temp2 = temps.AcquireSameSizeAs(array);
3079 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003080 {
3081 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3082 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3083 __ Ldr(temp, HeapOperand(array, class_offset));
3084 codegen_->MaybeRecordImplicitNullCheck(instruction);
3085 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003086 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003087
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003088 // /* HeapReference<Class> */ temp = temp->component_type_
3089 __ Ldr(temp, HeapOperand(temp, component_offset));
3090 // /* HeapReference<Class> */ temp2 = value->klass_
3091 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3092 // If heap poisoning is enabled, no need to unpoison `temp`
3093 // nor `temp2`, as we are comparing two poisoned references.
3094 __ Cmp(temp, temp2);
3095 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003096
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003097 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3098 vixl::aarch64::Label do_put;
3099 __ B(eq, &do_put);
3100 // If heap poisoning is enabled, the `temp` reference has
3101 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003102 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3103
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003104 // /* HeapReference<Class> */ temp = temp->super_class_
3105 __ Ldr(temp, HeapOperand(temp, super_offset));
3106 // If heap poisoning is enabled, no need to unpoison
3107 // `temp`, as we are comparing against null below.
3108 __ Cbnz(temp, slow_path->GetEntryLabel());
3109 __ Bind(&do_put);
3110 } else {
3111 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003112 }
3113 }
3114
3115 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003116 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003117 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003118 __ Mov(temp2, value.W());
3119 GetAssembler()->PoisonHeapReference(temp2);
3120 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003121 }
3122
3123 if (!index.IsConstant()) {
3124 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003125 } else {
3126 // We no longer need the `temp` here so release it as the store below may
3127 // need a scratch register (if the constant index makes the offset too large)
3128 // and the poisoned `source` could be using the other scratch register.
3129 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003130 }
Artem Serov914d7a82017-02-07 14:33:49 +00003131 {
3132 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3133 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3134 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003135
Artem Serov914d7a82017-02-07 14:33:49 +00003136 if (!may_need_runtime_call_for_type_check) {
3137 codegen_->MaybeRecordImplicitNullCheck(instruction);
3138 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003139 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003140 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003141
3142 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3143
3144 if (done.IsLinked()) {
3145 __ Bind(&done);
3146 }
3147
3148 if (slow_path != nullptr) {
3149 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003150 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003151 }
3152}
3153
Alexandre Rames67555f72014-11-18 10:55:16 +00003154void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003155 RegisterSet caller_saves = RegisterSet::Empty();
3156 InvokeRuntimeCallingConvention calling_convention;
3157 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3158 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3159 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003160 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003161 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003162}
3163
3164void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003165 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003166 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003167 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003168 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3169 __ B(slow_path->GetEntryLabel(), hs);
3170}
3171
Alexandre Rames67555f72014-11-18 10:55:16 +00003172void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3173 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003174 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003175 locations->SetInAt(0, Location::RequiresRegister());
3176 if (check->HasUses()) {
3177 locations->SetOut(Location::SameAsFirstInput());
3178 }
3179}
3180
3181void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3182 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003183 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Alexandre Rames67555f72014-11-18 10:55:16 +00003184 check->GetLoadClass(), check, check->GetDexPc(), true);
3185 codegen_->AddSlowPath(slow_path);
3186 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3187}
3188
Roland Levillain1a653882016-03-18 18:05:57 +00003189static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3190 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3191 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3192}
3193
3194void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3195 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3196 Location rhs_loc = instruction->GetLocations()->InAt(1);
3197 if (rhs_loc.IsConstant()) {
3198 // 0.0 is the only immediate that can be encoded directly in
3199 // an FCMP instruction.
3200 //
3201 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3202 // specify that in a floating-point comparison, positive zero
3203 // and negative zero are considered equal, so we can use the
3204 // literal 0.0 for both cases here.
3205 //
3206 // Note however that some methods (Float.equal, Float.compare,
3207 // Float.compareTo, Double.equal, Double.compare,
3208 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3209 // StrictMath.min) consider 0.0 to be (strictly) greater than
3210 // -0.0. So if we ever translate calls to these methods into a
3211 // HCompare instruction, we must handle the -0.0 case with
3212 // care here.
3213 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3214 __ Fcmp(lhs_reg, 0.0);
3215 } else {
3216 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3217 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003218}
3219
Serban Constantinescu02164b32014-11-13 14:05:07 +00003220void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003221 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003222 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003223 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003224 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003226 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003229 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 case DataType::Type::kInt32:
3231 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003232 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003233 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3235 break;
3236 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003237 case DataType::Type::kFloat32:
3238 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003239 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003240 locations->SetInAt(1,
3241 IsFloatingPointZeroConstant(compare->InputAt(1))
3242 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3243 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003244 locations->SetOut(Location::RequiresRegister());
3245 break;
3246 }
3247 default:
3248 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3249 }
3250}
3251
3252void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003254
3255 // 0 if: left == right
3256 // 1 if: left > right
3257 // -1 if: left < right
3258 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003259 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003260 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003261 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003262 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003263 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003264 case DataType::Type::kInt32:
3265 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003266 Register result = OutputRegister(compare);
3267 Register left = InputRegisterAt(compare, 0);
3268 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003269 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003270 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3271 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003272 break;
3273 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003274 case DataType::Type::kFloat32:
3275 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003276 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003277 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003278 __ Cset(result, ne);
3279 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003280 break;
3281 }
3282 default:
3283 LOG(FATAL) << "Unimplemented compare type " << in_type;
3284 }
3285}
3286
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003287void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003288 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003291 locations->SetInAt(0, Location::RequiresFpuRegister());
3292 locations->SetInAt(1,
3293 IsFloatingPointZeroConstant(instruction->InputAt(1))
3294 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3295 : Location::RequiresFpuRegister());
3296 } else {
3297 // Integer cases.
3298 locations->SetInAt(0, Location::RequiresRegister());
3299 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3300 }
3301
David Brazdilb3e773e2016-01-26 11:28:37 +00003302 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003304 }
3305}
3306
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003307void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003308 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003309 return;
3310 }
3311
3312 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003313 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003314 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003317 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003318 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003319 } else {
3320 // Integer cases.
3321 Register lhs = InputRegisterAt(instruction, 0);
3322 Operand rhs = InputOperandAt(instruction, 1);
3323 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003324 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003325 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003326}
3327
3328#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3329 M(Equal) \
3330 M(NotEqual) \
3331 M(LessThan) \
3332 M(LessThanOrEqual) \
3333 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003334 M(GreaterThanOrEqual) \
3335 M(Below) \
3336 M(BelowOrEqual) \
3337 M(Above) \
3338 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003339#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003340void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3341void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003342FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003343#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003344#undef FOR_EACH_CONDITION_INSTRUCTION
3345
Zheng Xuc6667102015-05-15 16:08:45 +08003346void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3347 DCHECK(instruction->IsDiv() || instruction->IsRem());
3348
3349 LocationSummary* locations = instruction->GetLocations();
3350 Location second = locations->InAt(1);
3351 DCHECK(second.IsConstant());
3352
3353 Register out = OutputRegister(instruction);
3354 Register dividend = InputRegisterAt(instruction, 0);
3355 int64_t imm = Int64FromConstant(second.GetConstant());
3356 DCHECK(imm == 1 || imm == -1);
3357
3358 if (instruction->IsRem()) {
3359 __ Mov(out, 0);
3360 } else {
3361 if (imm == 1) {
3362 __ Mov(out, dividend);
3363 } else {
3364 __ Neg(out, dividend);
3365 }
3366 }
3367}
3368
3369void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3370 DCHECK(instruction->IsDiv() || instruction->IsRem());
3371
3372 LocationSummary* locations = instruction->GetLocations();
3373 Location second = locations->InAt(1);
3374 DCHECK(second.IsConstant());
3375
3376 Register out = OutputRegister(instruction);
3377 Register dividend = InputRegisterAt(instruction, 0);
3378 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003379 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003380 int ctz_imm = CTZ(abs_imm);
3381
3382 UseScratchRegisterScope temps(GetVIXLAssembler());
3383 Register temp = temps.AcquireSameSizeAs(out);
3384
3385 if (instruction->IsDiv()) {
3386 __ Add(temp, dividend, abs_imm - 1);
3387 __ Cmp(dividend, 0);
3388 __ Csel(out, temp, dividend, lt);
3389 if (imm > 0) {
3390 __ Asr(out, out, ctz_imm);
3391 } else {
3392 __ Neg(out, Operand(out, ASR, ctz_imm));
3393 }
3394 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003395 int bits = instruction->GetResultType() == DataType::Type::kInt32 ? 32 : 64;
Zheng Xuc6667102015-05-15 16:08:45 +08003396 __ Asr(temp, dividend, bits - 1);
3397 __ Lsr(temp, temp, bits - ctz_imm);
3398 __ Add(out, dividend, temp);
3399 __ And(out, out, abs_imm - 1);
3400 __ Sub(out, out, temp);
3401 }
3402}
3403
3404void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3405 DCHECK(instruction->IsDiv() || instruction->IsRem());
3406
3407 LocationSummary* locations = instruction->GetLocations();
3408 Location second = locations->InAt(1);
3409 DCHECK(second.IsConstant());
3410
3411 Register out = OutputRegister(instruction);
3412 Register dividend = InputRegisterAt(instruction, 0);
3413 int64_t imm = Int64FromConstant(second.GetConstant());
3414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003415 DataType::Type type = instruction->GetResultType();
3416 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003417
3418 int64_t magic;
3419 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003420 CalculateMagicAndShiftForDivRem(
3421 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003422
3423 UseScratchRegisterScope temps(GetVIXLAssembler());
3424 Register temp = temps.AcquireSameSizeAs(out);
3425
3426 // temp = get_high(dividend * magic)
3427 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003428 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003429 __ Smulh(temp, dividend, temp);
3430 } else {
3431 __ Smull(temp.X(), dividend, temp);
3432 __ Lsr(temp.X(), temp.X(), 32);
3433 }
3434
3435 if (imm > 0 && magic < 0) {
3436 __ Add(temp, temp, dividend);
3437 } else if (imm < 0 && magic > 0) {
3438 __ Sub(temp, temp, dividend);
3439 }
3440
3441 if (shift != 0) {
3442 __ Asr(temp, temp, shift);
3443 }
3444
3445 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003447 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003448 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003449 // TODO: Strength reduction for msub.
3450 Register temp_imm = temps.AcquireSameSizeAs(out);
3451 __ Mov(temp_imm, imm);
3452 __ Msub(out, temp, temp_imm, dividend);
3453 }
3454}
3455
3456void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3457 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003458 DataType::Type type = instruction->GetResultType();
3459 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003460
3461 LocationSummary* locations = instruction->GetLocations();
3462 Register out = OutputRegister(instruction);
3463 Location second = locations->InAt(1);
3464
3465 if (second.IsConstant()) {
3466 int64_t imm = Int64FromConstant(second.GetConstant());
3467
3468 if (imm == 0) {
3469 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3470 } else if (imm == 1 || imm == -1) {
3471 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003472 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003473 DivRemByPowerOfTwo(instruction);
3474 } else {
3475 DCHECK(imm <= -2 || imm >= 2);
3476 GenerateDivRemWithAnyConstant(instruction);
3477 }
3478 } else {
3479 Register dividend = InputRegisterAt(instruction, 0);
3480 Register divisor = InputRegisterAt(instruction, 1);
3481 if (instruction->IsDiv()) {
3482 __ Sdiv(out, dividend, divisor);
3483 } else {
3484 UseScratchRegisterScope temps(GetVIXLAssembler());
3485 Register temp = temps.AcquireSameSizeAs(out);
3486 __ Sdiv(temp, dividend, divisor);
3487 __ Msub(out, temp, divisor, dividend);
3488 }
3489 }
3490}
3491
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003492void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3493 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003494 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003495 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003496 case DataType::Type::kInt32:
3497 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003498 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003499 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3501 break;
3502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003503 case DataType::Type::kFloat32:
3504 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003505 locations->SetInAt(0, Location::RequiresFpuRegister());
3506 locations->SetInAt(1, Location::RequiresFpuRegister());
3507 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3508 break;
3509
3510 default:
3511 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3512 }
3513}
3514
3515void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003517 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003518 case DataType::Type::kInt32:
3519 case DataType::Type::kInt64:
Zheng Xuc6667102015-05-15 16:08:45 +08003520 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003521 break;
3522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 case DataType::Type::kFloat32:
3524 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003525 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3526 break;
3527
3528 default:
3529 LOG(FATAL) << "Unexpected div type " << type;
3530 }
3531}
3532
Alexandre Rames67555f72014-11-18 10:55:16 +00003533void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003534 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003535 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003536}
3537
3538void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3539 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003540 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003541 codegen_->AddSlowPath(slow_path);
3542 Location value = instruction->GetLocations()->InAt(0);
3543
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003544 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003546 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003547 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003548 return;
3549 }
3550
Alexandre Rames67555f72014-11-18 10:55:16 +00003551 if (value.IsConstant()) {
3552 int64_t divisor = Int64ConstantFrom(value);
3553 if (divisor == 0) {
3554 __ B(slow_path->GetEntryLabel());
3555 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003556 // A division by a non-null constant is valid. We don't need to perform
3557 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003558 }
3559 } else {
3560 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3561 }
3562}
3563
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003564void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3565 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003566 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003567 locations->SetOut(Location::ConstantLocation(constant));
3568}
3569
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003570void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3571 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003572 // Will be generated at use site.
3573}
3574
Alexandre Rames5319def2014-10-23 10:03:10 +01003575void LocationsBuilderARM64::VisitExit(HExit* exit) {
3576 exit->SetLocations(nullptr);
3577}
3578
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003579void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003580}
3581
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003582void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3583 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003584 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003585 locations->SetOut(Location::ConstantLocation(constant));
3586}
3587
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003588void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003589 // Will be generated at use site.
3590}
3591
David Brazdilfc6a86a2015-06-26 10:33:45 +00003592void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003593 if (successor->IsExitBlock()) {
3594 DCHECK(got->GetPrevious()->AlwaysThrows());
3595 return; // no code needed
3596 }
3597
Serban Constantinescu02164b32014-11-13 14:05:07 +00003598 HBasicBlock* block = got->GetBlock();
3599 HInstruction* previous = got->GetPrevious();
3600 HLoopInformation* info = block->GetLoopInformation();
3601
David Brazdil46e2a392015-03-16 17:31:52 +00003602 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003603 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3604 UseScratchRegisterScope temps(GetVIXLAssembler());
3605 Register temp1 = temps.AcquireX();
3606 Register temp2 = temps.AcquireX();
3607 __ Ldr(temp1, MemOperand(sp, 0));
3608 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3609 __ Add(temp2, temp2, 1);
3610 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3611 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003612 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3613 return;
3614 }
3615 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3616 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003617 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003618 }
3619 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003620 __ B(codegen_->GetLabelOf(successor));
3621 }
3622}
3623
David Brazdilfc6a86a2015-06-26 10:33:45 +00003624void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3625 got->SetLocations(nullptr);
3626}
3627
3628void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3629 HandleGoto(got, got->GetSuccessor());
3630}
3631
3632void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3633 try_boundary->SetLocations(nullptr);
3634}
3635
3636void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3637 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3638 if (!successor->IsExitBlock()) {
3639 HandleGoto(try_boundary, successor);
3640 }
3641}
3642
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003643void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003644 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003645 vixl::aarch64::Label* true_target,
3646 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003647 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003648
David Brazdil0debae72015-11-12 18:37:00 +00003649 if (true_target == nullptr && false_target == nullptr) {
3650 // Nothing to do. The code always falls through.
3651 return;
3652 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003653 // Constant condition, statically compared against "true" (integer value 1).
3654 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003655 if (true_target != nullptr) {
3656 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003657 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003658 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003659 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003660 if (false_target != nullptr) {
3661 __ B(false_target);
3662 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003663 }
David Brazdil0debae72015-11-12 18:37:00 +00003664 return;
3665 }
3666
3667 // The following code generates these patterns:
3668 // (1) true_target == nullptr && false_target != nullptr
3669 // - opposite condition true => branch to false_target
3670 // (2) true_target != nullptr && false_target == nullptr
3671 // - condition true => branch to true_target
3672 // (3) true_target != nullptr && false_target != nullptr
3673 // - condition true => branch to true_target
3674 // - branch to false_target
3675 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003676 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003677 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003678 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003679 if (true_target == nullptr) {
3680 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3681 } else {
3682 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3683 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003684 } else {
3685 // The condition instruction has not been materialized, use its inputs as
3686 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003687 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003689 DataType::Type type = condition->InputAt(0)->GetType();
3690 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003691 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003692 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003693 IfCondition opposite_condition = condition->GetOppositeCondition();
3694 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003695 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003696 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003697 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003698 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003699 // Integer cases.
3700 Register lhs = InputRegisterAt(condition, 0);
3701 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003702
3703 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003704 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003705 if (true_target == nullptr) {
3706 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3707 non_fallthrough_target = false_target;
3708 } else {
3709 arm64_cond = ARM64Condition(condition->GetCondition());
3710 non_fallthrough_target = true_target;
3711 }
3712
Aart Bik086d27e2016-01-20 17:02:00 -08003713 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003714 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003715 switch (arm64_cond) {
3716 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003717 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003718 break;
3719 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003720 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003721 break;
3722 case lt:
3723 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003724 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003725 break;
3726 case ge:
3727 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003728 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003729 break;
3730 default:
3731 // Without the `static_cast` the compiler throws an error for
3732 // `-Werror=sign-promo`.
3733 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3734 }
3735 } else {
3736 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003737 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003738 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003739 }
3740 }
David Brazdil0debae72015-11-12 18:37:00 +00003741
3742 // If neither branch falls through (case 3), the conditional branch to `true_target`
3743 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3744 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003745 __ B(false_target);
3746 }
3747}
3748
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003749void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003750 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003751 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003752 locations->SetInAt(0, Location::RequiresRegister());
3753 }
3754}
3755
3756void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003757 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3758 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003759 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3760 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3761 true_target = nullptr;
3762 }
3763 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3764 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3765 false_target = nullptr;
3766 }
David Brazdil0debae72015-11-12 18:37:00 +00003767 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003768}
3769
3770void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003771 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003772 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003773 InvokeRuntimeCallingConvention calling_convention;
3774 RegisterSet caller_saves = RegisterSet::Empty();
3775 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3776 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003777 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003778 locations->SetInAt(0, Location::RequiresRegister());
3779 }
3780}
3781
3782void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003783 SlowPathCodeARM64* slow_path =
3784 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003785 GenerateTestAndBranch(deoptimize,
3786 /* condition_input_index */ 0,
3787 slow_path->GetEntryLabel(),
3788 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003789}
3790
Mingyao Yang063fc772016-08-02 11:02:54 -07003791void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003792 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003793 LocationSummary(flag, LocationSummary::kNoCall);
3794 locations->SetOut(Location::RequiresRegister());
3795}
3796
3797void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3798 __ Ldr(OutputRegister(flag),
3799 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3800}
3801
David Brazdilc0b601b2016-02-08 14:20:45 +00003802static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3803 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003805}
3806
Alexandre Rames880f1192016-06-13 16:04:50 +01003807static inline Condition GetConditionForSelect(HCondition* condition) {
3808 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003809 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3810 : ARM64Condition(cond);
3811}
3812
David Brazdil74eb1b22015-12-14 11:44:01 +00003813void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003814 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003816 locations->SetInAt(0, Location::RequiresFpuRegister());
3817 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003818 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003819 } else {
3820 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3821 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3822 bool is_true_value_constant = cst_true_value != nullptr;
3823 bool is_false_value_constant = cst_false_value != nullptr;
3824 // Ask VIXL whether we should synthesize constants in registers.
3825 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3826 Operand true_op = is_true_value_constant ?
3827 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3828 Operand false_op = is_false_value_constant ?
3829 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3830 bool true_value_in_register = false;
3831 bool false_value_in_register = false;
3832 MacroAssembler::GetCselSynthesisInformation(
3833 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3834 true_value_in_register |= !is_true_value_constant;
3835 false_value_in_register |= !is_false_value_constant;
3836
3837 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3838 : Location::ConstantLocation(cst_true_value));
3839 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3840 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003841 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003842 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003843
David Brazdil74eb1b22015-12-14 11:44:01 +00003844 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3845 locations->SetInAt(2, Location::RequiresRegister());
3846 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003847}
3848
3849void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003850 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003851 Condition csel_cond;
3852
3853 if (IsBooleanValueOrMaterializedCondition(cond)) {
3854 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003855 // Use the condition flags set by the previous instruction.
3856 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003857 } else {
3858 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003859 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003860 }
3861 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003862 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003863 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003864 } else {
3865 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003866 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003867 }
3868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003870 __ Fcsel(OutputFPRegister(select),
3871 InputFPRegisterAt(select, 1),
3872 InputFPRegisterAt(select, 0),
3873 csel_cond);
3874 } else {
3875 __ Csel(OutputRegister(select),
3876 InputOperandAt(select, 1),
3877 InputOperandAt(select, 0),
3878 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003879 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003880}
3881
David Srbecky0cf44932015-12-09 14:09:59 +00003882void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003883 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003884}
3885
David Srbeckyd28f4a02016-03-14 17:14:24 +00003886void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3887 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003888}
3889
3890void CodeGeneratorARM64::GenerateNop() {
3891 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003892}
3893
Alexandre Rames5319def2014-10-23 10:03:10 +01003894void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003895 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003896}
3897
3898void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003899 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003900}
3901
3902void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003903 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003904}
3905
3906void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003907 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003908}
3909
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003910// Temp is used for read barrier.
3911static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3912 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003913 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003914 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3915 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3916 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3917 return 1;
3918 }
3919 return 0;
3920}
3921
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003922// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003923// interface pointer, one for loading the current interface.
3924// The other checks have one temp for loading the object's class.
3925static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3926 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3927 return 3;
3928 }
3929 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003930}
3931
Alexandre Rames67555f72014-11-18 10:55:16 +00003932void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003934 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003935 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003936 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003937 case TypeCheckKind::kExactCheck:
3938 case TypeCheckKind::kAbstractClassCheck:
3939 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003940 case TypeCheckKind::kArrayObjectCheck: {
3941 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3942 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3943 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003944 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003945 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003946 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003947 case TypeCheckKind::kUnresolvedCheck:
3948 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003949 call_kind = LocationSummary::kCallOnSlowPath;
3950 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003951 case TypeCheckKind::kBitstringCheck:
3952 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003953 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003954
Vladimir Markoca6fff82017-10-03 14:49:14 +01003955 LocationSummary* locations =
3956 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003957 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003958 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003959 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003960 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003961 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3962 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3963 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3964 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3965 } else {
3966 locations->SetInAt(1, Location::RequiresRegister());
3967 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003968 // The "out" register is used as a temporary, so it overlaps with the inputs.
3969 // Note that TypeCheckSlowPathARM64 uses this register too.
3970 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003971 // Add temps if necessary for read barriers.
3972 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003973}
3974
3975void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003976 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003977 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003978 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003980 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3981 ? Register()
3982 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003983 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003984 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003985 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3986 DCHECK_LE(num_temps, 1u);
3987 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003988 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3989 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3990 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3991 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003992
Scott Wakeling97c72b72016-06-24 16:19:36 +01003993 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003994 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003995
3996 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003997 // Avoid null check if we know `obj` is not null.
3998 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003999 __ Cbz(obj, &zero);
4000 }
4001
Roland Levillain44015862016-01-22 11:47:17 +00004002 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004003 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004004 ReadBarrierOption read_barrier_option =
4005 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004006 // /* HeapReference<Class> */ out = obj->klass_
4007 GenerateReferenceLoadTwoRegisters(instruction,
4008 out_loc,
4009 obj_loc,
4010 class_offset,
4011 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004012 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004013 __ Cmp(out, cls);
4014 __ Cset(out, eq);
4015 if (zero.IsLinked()) {
4016 __ B(&done);
4017 }
4018 break;
4019 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004020
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004021 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004022 ReadBarrierOption read_barrier_option =
4023 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004024 // /* HeapReference<Class> */ out = obj->klass_
4025 GenerateReferenceLoadTwoRegisters(instruction,
4026 out_loc,
4027 obj_loc,
4028 class_offset,
4029 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004030 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 // If the class is abstract, we eagerly fetch the super class of the
4032 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004033 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004034 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004035 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004036 GenerateReferenceLoadOneRegister(instruction,
4037 out_loc,
4038 super_offset,
4039 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004040 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004041 // If `out` is null, we use it for the result, and jump to `done`.
4042 __ Cbz(out, &done);
4043 __ Cmp(out, cls);
4044 __ B(ne, &loop);
4045 __ Mov(out, 1);
4046 if (zero.IsLinked()) {
4047 __ B(&done);
4048 }
4049 break;
4050 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004052 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004053 ReadBarrierOption read_barrier_option =
4054 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004055 // /* HeapReference<Class> */ out = obj->klass_
4056 GenerateReferenceLoadTwoRegisters(instruction,
4057 out_loc,
4058 obj_loc,
4059 class_offset,
4060 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004061 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004062 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004063 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004064 __ Bind(&loop);
4065 __ Cmp(out, cls);
4066 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004067 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004068 GenerateReferenceLoadOneRegister(instruction,
4069 out_loc,
4070 super_offset,
4071 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004072 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004073 __ Cbnz(out, &loop);
4074 // If `out` is null, we use it for the result, and jump to `done`.
4075 __ B(&done);
4076 __ Bind(&success);
4077 __ Mov(out, 1);
4078 if (zero.IsLinked()) {
4079 __ B(&done);
4080 }
4081 break;
4082 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004083
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004084 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004085 ReadBarrierOption read_barrier_option =
4086 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004087 // /* HeapReference<Class> */ out = obj->klass_
4088 GenerateReferenceLoadTwoRegisters(instruction,
4089 out_loc,
4090 obj_loc,
4091 class_offset,
4092 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004093 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004094 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004095 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004096 __ Cmp(out, cls);
4097 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004098 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004099 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004100 GenerateReferenceLoadOneRegister(instruction,
4101 out_loc,
4102 component_offset,
4103 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004104 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004105 // If `out` is null, we use it for the result, and jump to `done`.
4106 __ Cbz(out, &done);
4107 __ Ldrh(out, HeapOperand(out, primitive_offset));
4108 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4109 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004110 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004111 __ Mov(out, 1);
4112 __ B(&done);
4113 break;
4114 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004115
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004116 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004117 // No read barrier since the slow path will retry upon failure.
4118 // /* HeapReference<Class> */ out = obj->klass_
4119 GenerateReferenceLoadTwoRegisters(instruction,
4120 out_loc,
4121 obj_loc,
4122 class_offset,
4123 maybe_temp_loc,
4124 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004125 __ Cmp(out, cls);
4126 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004127 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4128 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 codegen_->AddSlowPath(slow_path);
4130 __ B(ne, slow_path->GetEntryLabel());
4131 __ Mov(out, 1);
4132 if (zero.IsLinked()) {
4133 __ B(&done);
4134 }
4135 break;
4136 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004137
Calin Juravle98893e12015-10-02 21:05:03 +01004138 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004139 case TypeCheckKind::kInterfaceCheck: {
4140 // Note that we indeed only call on slow path, but we always go
4141 // into the slow path for the unresolved and interface check
4142 // cases.
4143 //
4144 // We cannot directly call the InstanceofNonTrivial runtime
4145 // entry point without resorting to a type checking slow path
4146 // here (i.e. by calling InvokeRuntime directly), as it would
4147 // require to assign fixed registers for the inputs of this
4148 // HInstanceOf instruction (following the runtime calling
4149 // convention), which might be cluttered by the potential first
4150 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004151 //
4152 // TODO: Introduce a new runtime entry point taking the object
4153 // to test (instead of its class) as argument, and let it deal
4154 // with the read barrier issues. This will let us refactor this
4155 // case of the `switch` code as it was previously (with a direct
4156 // call to the runtime not using a type checking slow path).
4157 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004158 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004159 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4160 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004161 codegen_->AddSlowPath(slow_path);
4162 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004163 if (zero.IsLinked()) {
4164 __ B(&done);
4165 }
4166 break;
4167 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004168
4169 case TypeCheckKind::kBitstringCheck: {
4170 // /* HeapReference<Class> */ temp = obj->klass_
4171 GenerateReferenceLoadTwoRegisters(instruction,
4172 out_loc,
4173 obj_loc,
4174 class_offset,
4175 maybe_temp_loc,
4176 kWithoutReadBarrier);
4177
4178 GenerateBitstringTypeCheckCompare(instruction, out);
4179 __ Cset(out, eq);
4180 if (zero.IsLinked()) {
4181 __ B(&done);
4182 }
4183 break;
4184 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004185 }
4186
4187 if (zero.IsLinked()) {
4188 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004189 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004190 }
4191
4192 if (done.IsLinked()) {
4193 __ Bind(&done);
4194 }
4195
4196 if (slow_path != nullptr) {
4197 __ Bind(slow_path->GetExitLabel());
4198 }
4199}
4200
4201void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004202 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004203 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004204 LocationSummary* locations =
4205 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004206 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004207 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4208 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4209 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4210 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4211 } else {
4212 locations->SetInAt(1, Location::RequiresRegister());
4213 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004214 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4215 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004216}
4217
4218void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004219 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004220 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004221 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004223 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4224 ? Register()
4225 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004226 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4227 DCHECK_GE(num_temps, 1u);
4228 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004229 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004230 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4231 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004232 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004233 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4234 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4235 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4236 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4237 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4238 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4239 const uint32_t object_array_data_offset =
4240 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004241
Vladimir Marko87584542017-12-12 17:47:52 +00004242 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004243 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004244 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4245 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004247
Scott Wakeling97c72b72016-06-24 16:19:36 +01004248 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004249 // Avoid null check if we know obj is not null.
4250 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004251 __ Cbz(obj, &done);
4252 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004253
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004254 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004255 case TypeCheckKind::kExactCheck:
4256 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004257 // /* HeapReference<Class> */ temp = obj->klass_
4258 GenerateReferenceLoadTwoRegisters(instruction,
4259 temp_loc,
4260 obj_loc,
4261 class_offset,
4262 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004263 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004265 __ Cmp(temp, cls);
4266 // Jump to slow path for throwing the exception or doing a
4267 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004268 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004269 break;
4270 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004271
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004272 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004273 // /* HeapReference<Class> */ temp = obj->klass_
4274 GenerateReferenceLoadTwoRegisters(instruction,
4275 temp_loc,
4276 obj_loc,
4277 class_offset,
4278 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004279 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004280
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004281 // If the class is abstract, we eagerly fetch the super class of the
4282 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004283 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004284 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004285 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004286 GenerateReferenceLoadOneRegister(instruction,
4287 temp_loc,
4288 super_offset,
4289 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004290 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004291
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004292 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4293 // exception.
4294 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4295 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004296 __ Cmp(temp, cls);
4297 __ B(ne, &loop);
4298 break;
4299 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004300
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004301 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004302 // /* HeapReference<Class> */ temp = obj->klass_
4303 GenerateReferenceLoadTwoRegisters(instruction,
4304 temp_loc,
4305 obj_loc,
4306 class_offset,
4307 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004308 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004309
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004310 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004311 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004312 __ Bind(&loop);
4313 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004314 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004315
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004316 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004317 GenerateReferenceLoadOneRegister(instruction,
4318 temp_loc,
4319 super_offset,
4320 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004321 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004322
4323 // If the class reference currently in `temp` is not null, jump
4324 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004325 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004326 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004327 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004328 break;
4329 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004330
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004331 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004332 // /* HeapReference<Class> */ temp = obj->klass_
4333 GenerateReferenceLoadTwoRegisters(instruction,
4334 temp_loc,
4335 obj_loc,
4336 class_offset,
4337 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004338 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004339
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004340 // Do an exact check.
4341 __ Cmp(temp, cls);
4342 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004343
4344 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004345 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004346 GenerateReferenceLoadOneRegister(instruction,
4347 temp_loc,
4348 component_offset,
4349 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004350 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004351
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004352 // If the component type is null, jump to the slow path to throw the exception.
4353 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4354 // Otherwise, the object is indeed an array. Further check that this component type is not a
4355 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004356 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4357 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004358 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004359 break;
4360 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004361
Calin Juravle98893e12015-10-02 21:05:03 +01004362 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004363 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004364 //
4365 // We cannot directly call the CheckCast runtime entry point
4366 // without resorting to a type checking slow path here (i.e. by
4367 // calling InvokeRuntime directly), as it would require to
4368 // assign fixed registers for the inputs of this HInstanceOf
4369 // instruction (following the runtime calling convention), which
4370 // might be cluttered by the potential first read barrier
4371 // emission at the beginning of this method.
4372 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004373 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004374 case TypeCheckKind::kInterfaceCheck: {
4375 // /* HeapReference<Class> */ temp = obj->klass_
4376 GenerateReferenceLoadTwoRegisters(instruction,
4377 temp_loc,
4378 obj_loc,
4379 class_offset,
4380 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004381 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004382
4383 // /* HeapReference<Class> */ temp = temp->iftable_
4384 GenerateReferenceLoadTwoRegisters(instruction,
4385 temp_loc,
4386 temp_loc,
4387 iftable_offset,
4388 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004389 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004390 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004391 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004392 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004393 vixl::aarch64::Label start_loop;
4394 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004395 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004396 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4397 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004398 // Go to next interface.
4399 __ Add(temp, temp, 2 * kHeapReferenceSize);
4400 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004401 // Compare the classes and continue the loop if they do not match.
4402 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4403 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004404 break;
4405 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004406
4407 case TypeCheckKind::kBitstringCheck: {
4408 // /* HeapReference<Class> */ temp = obj->klass_
4409 GenerateReferenceLoadTwoRegisters(instruction,
4410 temp_loc,
4411 obj_loc,
4412 class_offset,
4413 maybe_temp2_loc,
4414 kWithoutReadBarrier);
4415
4416 GenerateBitstringTypeCheckCompare(instruction, temp);
4417 __ B(ne, type_check_slow_path->GetEntryLabel());
4418 break;
4419 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004420 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004421 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004422
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004423 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004424}
4425
Alexandre Rames5319def2014-10-23 10:03:10 +01004426void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004427 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004428 locations->SetOut(Location::ConstantLocation(constant));
4429}
4430
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004431void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004432 // Will be generated at use site.
4433}
4434
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004435void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004436 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004437 locations->SetOut(Location::ConstantLocation(constant));
4438}
4439
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004440void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004441 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004442}
4443
Calin Juravle175dc732015-08-25 15:42:32 +01004444void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4445 // The trampoline uses the same calling convention as dex calling conventions,
4446 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4447 // the method_idx.
4448 HandleInvoke(invoke);
4449}
4450
4451void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4452 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004453 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004454}
4455
Alexandre Rames5319def2014-10-23 10:03:10 +01004456void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004457 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004458 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004459}
4460
Alexandre Rames67555f72014-11-18 10:55:16 +00004461void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4462 HandleInvoke(invoke);
4463}
4464
4465void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4466 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004467 LocationSummary* locations = invoke->GetLocations();
4468 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004469 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004470 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004471 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004472
4473 // The register ip1 is required to be used for the hidden argument in
4474 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004475 MacroAssembler* masm = GetVIXLAssembler();
4476 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004477 scratch_scope.Exclude(ip1);
4478 __ Mov(ip1, invoke->GetDexMethodIndex());
4479
Artem Serov914d7a82017-02-07 14:33:49 +00004480 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004481 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004482 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004483 {
4484 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4485 // /* HeapReference<Class> */ temp = temp->klass_
4486 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4487 codegen_->MaybeRecordImplicitNullCheck(invoke);
4488 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004489 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004490 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004491 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004492 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004493 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004494 }
Artem Serov914d7a82017-02-07 14:33:49 +00004495
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004496 // Instead of simply (possibly) unpoisoning `temp` here, we should
4497 // emit a read barrier for the previous class reference load.
4498 // However this is not required in practice, as this is an
4499 // intermediate/temporary reference and because the current
4500 // concurrent copying collector keeps the from-space memory
4501 // intact/accessible until the end of the marking phase (the
4502 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004503 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004504 __ Ldr(temp,
4505 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4506 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004507 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004508 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004509 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004510 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004511 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004512
4513 {
4514 // Ensure the pc position is recorded immediately after the `blr` instruction.
4515 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4516
4517 // lr();
4518 __ blr(lr);
4519 DCHECK(!codegen_->IsLeafMethod());
4520 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4521 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004522
4523 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004524}
4525
4526void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004527 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004528 if (intrinsic.TryDispatch(invoke)) {
4529 return;
4530 }
4531
Alexandre Rames67555f72014-11-18 10:55:16 +00004532 HandleInvoke(invoke);
4533}
4534
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004535void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004536 // Explicit clinit checks triggered by static invokes must have been pruned by
4537 // art::PrepareForRegisterAllocation.
4538 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004539
Vladimir Markoca6fff82017-10-03 14:49:14 +01004540 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004541 if (intrinsic.TryDispatch(invoke)) {
4542 return;
4543 }
4544
Alexandre Rames67555f72014-11-18 10:55:16 +00004545 HandleInvoke(invoke);
4546}
4547
Andreas Gampe878d58c2015-01-15 23:24:00 -08004548static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4549 if (invoke->GetLocations()->Intrinsified()) {
4550 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4551 intrinsic.Dispatch(invoke);
4552 return true;
4553 }
4554 return false;
4555}
4556
Vladimir Markodc151b22015-10-15 18:02:30 +01004557HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4558 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004559 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004560 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004561 return desired_dispatch_info;
4562}
4563
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004564void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4565 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004566 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004567 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4568 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004569 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4570 uint32_t offset =
4571 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004572 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004573 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004574 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004575 }
Vladimir Marko58155012015-08-19 12:49:41 +00004576 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004577 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004578 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004579 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4580 DCHECK(GetCompilerOptions().IsBootImage());
4581 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004582 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004583 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4584 // Add ADD with its PC-relative method patch.
4585 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004586 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004587 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4588 break;
4589 }
Vladimir Marko58155012015-08-19 12:49:41 +00004590 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4591 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004592 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004593 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004594 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4595 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004596 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004597 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4598 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4599 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4600 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4601 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4602 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4603 break;
4604 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004605 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004606 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004607 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4608 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004609 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004610 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004611 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004612 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004613 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004614 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004615 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004616 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4617 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4618 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004619 }
4620 }
4621
4622 switch (invoke->GetCodePtrLocation()) {
4623 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004624 {
4625 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4626 ExactAssemblyScope eas(GetVIXLAssembler(),
4627 kInstructionSize,
4628 CodeBufferCheckScope::kExactSize);
4629 __ bl(&frame_entry_label_);
4630 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4631 }
Vladimir Marko58155012015-08-19 12:49:41 +00004632 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004633 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4634 // LR = callee_method->entry_point_from_quick_compiled_code_;
4635 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004636 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004637 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004638 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004639 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004640 ExactAssemblyScope eas(GetVIXLAssembler(),
4641 kInstructionSize,
4642 CodeBufferCheckScope::kExactSize);
4643 // lr()
4644 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004645 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004646 }
Vladimir Marko58155012015-08-19 12:49:41 +00004647 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004648 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004649
Andreas Gampe878d58c2015-01-15 23:24:00 -08004650 DCHECK(!IsLeafMethod());
4651}
4652
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004653void CodeGeneratorARM64::GenerateVirtualCall(
4654 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004655 // Use the calling convention instead of the location of the receiver, as
4656 // intrinsics may have put the receiver in a different register. In the intrinsics
4657 // slow path, the arguments have been moved to the right place, so here we are
4658 // guaranteed that the receiver is the first register of the calling convention.
4659 InvokeDexCallingConvention calling_convention;
4660 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004661 Register temp = XRegisterFrom(temp_in);
4662 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4663 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4664 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004665 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004666
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004667 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004668
4669 {
4670 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4671 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4672 // /* HeapReference<Class> */ temp = receiver->klass_
4673 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4674 MaybeRecordImplicitNullCheck(invoke);
4675 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004676 // Instead of simply (possibly) unpoisoning `temp` here, we should
4677 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004678 // intermediate/temporary reference and because the current
4679 // concurrent copying collector keeps the from-space memory
4680 // intact/accessible until the end of the marking phase (the
4681 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004682 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4683 // temp = temp->GetMethodAt(method_offset);
4684 __ Ldr(temp, MemOperand(temp, method_offset));
4685 // lr = temp->GetEntryPoint();
4686 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004687 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004688 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004689 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4690 // lr();
4691 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004692 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004693 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004694}
4695
Orion Hodsonac141392017-01-13 11:53:47 +00004696void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4697 HandleInvoke(invoke);
4698}
4699
4700void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4701 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004702 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004703}
4704
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004705void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4706 HandleInvoke(invoke);
4707}
4708
4709void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4710 codegen_->GenerateInvokeCustomCall(invoke);
4711 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
4712}
4713
Vladimir Markob066d432018-01-03 13:14:37 +00004714vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4715 uint32_t boot_image_offset,
4716 vixl::aarch64::Label* adrp_label) {
4717 return NewPcRelativePatch(
4718 /* dex_file */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
4719}
4720
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004721vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004722 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004723 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004724 return NewPcRelativePatch(
4725 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004726}
4727
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004728vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4729 MethodReference target_method,
4730 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004731 return NewPcRelativePatch(
4732 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004733}
4734
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004735vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004736 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004737 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004738 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004739 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004740}
4741
Vladimir Marko1998cd02017-01-13 13:02:58 +00004742vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4743 const DexFile& dex_file,
4744 dex::TypeIndex type_index,
4745 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004746 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004747}
4748
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004749vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004750 const DexFile& dex_file,
4751 dex::StringIndex string_index,
4752 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004753 return NewPcRelativePatch(
4754 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004755}
4756
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004757vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4758 const DexFile& dex_file,
4759 dex::StringIndex string_index,
4760 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004761 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004762}
4763
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004764vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4765 baker_read_barrier_patches_.emplace_back(custom_data);
4766 return &baker_read_barrier_patches_.back().label;
4767}
4768
Scott Wakeling97c72b72016-06-24 16:19:36 +01004769vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004770 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004771 uint32_t offset_or_index,
4772 vixl::aarch64::Label* adrp_label,
4773 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004774 // Add a patch entry and return the label.
4775 patches->emplace_back(dex_file, offset_or_index);
4776 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004777 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004778 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4779 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4780 return label;
4781}
4782
Scott Wakeling97c72b72016-06-24 16:19:36 +01004783vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4784 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004785 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004786}
4787
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004788vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004789 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004790 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004791 return jit_string_patches_.GetOrCreate(
4792 StringReference(&dex_file, string_index),
4793 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4794}
4795
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004796vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004797 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004798 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004799 return jit_class_patches_.GetOrCreate(
4800 TypeReference(&dex_file, type_index),
4801 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4802}
4803
Vladimir Markoaad75c62016-10-03 08:46:48 +00004804void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4805 vixl::aarch64::Register reg) {
4806 DCHECK(reg.IsX());
4807 SingleEmissionCheckScope guard(GetVIXLAssembler());
4808 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004809 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004810}
4811
4812void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4813 vixl::aarch64::Register out,
4814 vixl::aarch64::Register base) {
4815 DCHECK(out.IsX());
4816 DCHECK(base.IsX());
4817 SingleEmissionCheckScope guard(GetVIXLAssembler());
4818 __ Bind(fixup_label);
4819 __ add(out, base, Operand(/* offset placeholder */ 0));
4820}
4821
4822void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4823 vixl::aarch64::Register out,
4824 vixl::aarch64::Register base) {
4825 DCHECK(base.IsX());
4826 SingleEmissionCheckScope guard(GetVIXLAssembler());
4827 __ Bind(fixup_label);
4828 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4829}
4830
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004831template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004832inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4833 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004834 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004835 for (const PcRelativePatchInfo& info : infos) {
4836 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004837 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004838 info.pc_insn_label->GetLocation(),
4839 info.offset_or_index));
4840 }
4841}
4842
Vladimir Markob066d432018-01-03 13:14:37 +00004843linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
4844 const DexFile* target_dex_file,
4845 uint32_t pc_insn_offset,
4846 uint32_t boot_image_offset) {
4847 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
4848 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
4849}
4850
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004851void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004852 DCHECK(linker_patches->empty());
4853 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004854 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004855 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004856 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004857 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004858 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004859 string_bss_entry_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004860 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004861 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004862 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004863 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004864 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004865 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004866 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004867 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004868 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004869 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00004870 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
4871 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004872 DCHECK(boot_image_type_patches_.empty());
4873 DCHECK(boot_image_string_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004874 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004875 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4876 method_bss_entry_patches_, linker_patches);
4877 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4878 type_bss_entry_patches_, linker_patches);
4879 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4880 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004881 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004882 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4883 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004884 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004885 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004886}
4887
Vladimir Markoca1e0382018-04-11 09:58:41 +00004888bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
4889 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
4890 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4891}
4892
4893void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4894 /*out*/ ArenaVector<uint8_t>* code,
4895 /*out*/ std::string* debug_name) {
4896 Arm64Assembler assembler(GetGraph()->GetAllocator());
4897 switch (patch.GetType()) {
4898 case linker::LinkerPatch::Type::kCallRelative: {
4899 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4900 // to the generic JNI and interpreter trampolines.
4901 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4902 kArm64PointerSize).Int32Value());
4903 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4904 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4905 *debug_name = "MethodCallThunk";
4906 }
4907 break;
4908 }
4909 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4910 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4911 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4912 break;
4913 }
4914 default:
4915 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4916 UNREACHABLE();
4917 }
4918
4919 // Ensure we emit the literal pool if any.
4920 assembler.FinalizeCode();
4921 code->resize(assembler.CodeSize());
4922 MemoryRegion code_region(code->data(), code->size());
4923 assembler.FinalizeInstructions(code_region);
4924}
4925
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004926vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4927 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004928 value,
4929 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4930}
4931
Scott Wakeling97c72b72016-06-24 16:19:36 +01004932vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004933 return uint64_literals_.GetOrCreate(
4934 value,
4935 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004936}
4937
Andreas Gampe878d58c2015-01-15 23:24:00 -08004938void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004939 // Explicit clinit checks triggered by static invokes must have been pruned by
4940 // art::PrepareForRegisterAllocation.
4941 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004942
Andreas Gampe878d58c2015-01-15 23:24:00 -08004943 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004944 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004945 return;
4946 }
4947
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004948 {
4949 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4950 // are no pools emitted.
4951 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4952 LocationSummary* locations = invoke->GetLocations();
4953 codegen_->GenerateStaticOrDirectCall(
4954 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4955 }
4956
4957 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004958}
4959
4960void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004961 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004962 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004963 return;
4964 }
4965
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004966 {
4967 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4968 // are no pools emitted.
4969 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4970 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4971 DCHECK(!codegen_->IsLeafMethod());
4972 }
4973
4974 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004975}
4976
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004977HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4978 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004979 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004980 case HLoadClass::LoadKind::kInvalid:
4981 LOG(FATAL) << "UNREACHABLE";
4982 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004983 case HLoadClass::LoadKind::kReferrersClass:
4984 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004985 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004986 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004987 case HLoadClass::LoadKind::kBssEntry:
4988 DCHECK(!Runtime::Current()->UseJitCompilation());
4989 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004990 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004991 DCHECK(Runtime::Current()->UseJitCompilation());
4992 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004993 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004994 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004995 break;
4996 }
4997 return desired_class_load_kind;
4998}
4999
Alexandre Rames67555f72014-11-18 10:55:16 +00005000void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005001 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005002 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005003 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005004 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005005 cls,
5006 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005007 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005008 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005009 return;
5010 }
Vladimir Marko41559982017-01-06 14:04:23 +00005011 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005012
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005013 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5014 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005015 ? LocationSummary::kCallOnSlowPath
5016 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005017 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005018 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005019 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005020 }
5021
Vladimir Marko41559982017-01-06 14:04:23 +00005022 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005023 locations->SetInAt(0, Location::RequiresRegister());
5024 }
5025 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005026 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5027 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5028 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00005029 RegisterSet caller_saves = RegisterSet::Empty();
5030 InvokeRuntimeCallingConvention calling_convention;
5031 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5032 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005033 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5034 DataType::Type::kReference).GetCode());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005035 locations->SetCustomSlowPathCallerSaves(caller_saves);
5036 } else {
5037 // For non-Baker read barrier we have a temp-clobbering call.
5038 }
5039 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005040}
5041
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005042// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5043// move.
5044void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005045 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005046 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005047 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005048 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005049 return;
5050 }
Vladimir Marko41559982017-01-06 14:04:23 +00005051 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005052
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005053 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005054 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005055
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005056 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5057 ? kWithoutReadBarrier
5058 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005059 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005060 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005061 case HLoadClass::LoadKind::kReferrersClass: {
5062 DCHECK(!cls->CanCallRuntime());
5063 DCHECK(!cls->MustGenerateClinitCheck());
5064 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5065 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005066 codegen_->GenerateGcRootFieldLoad(cls,
5067 out_loc,
5068 current_method,
5069 ArtMethod::DeclaringClassOffset().Int32Value(),
5070 /* fixup_label */ nullptr,
5071 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005072 break;
5073 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005074 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005075 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005076 // Add ADRP with its PC-relative type patch.
5077 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005078 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005079 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005080 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005081 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005082 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005083 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005084 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005085 break;
5086 }
5087 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005088 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005089 uint32_t address = dchecked_integral_cast<uint32_t>(
5090 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5091 DCHECK_NE(address, 0u);
5092 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005093 break;
5094 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005095 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005096 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005097 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5098 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5099 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005100 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005101 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005102 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005103 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005104 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005105 break;
5106 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005107 case HLoadClass::LoadKind::kBssEntry: {
5108 // Add ADRP with its PC-relative Class .bss entry patch.
5109 const DexFile& dex_file = cls->GetDexFile();
5110 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005111 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
5112 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5113 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005114 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005115 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005116 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005117 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005118 codegen_->GenerateGcRootFieldLoad(cls,
5119 out_loc,
5120 temp,
5121 /* offset placeholder */ 0u,
5122 ldr_label,
5123 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005124 generate_null_check = true;
5125 break;
5126 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005127 case HLoadClass::LoadKind::kJitTableAddress: {
5128 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5129 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005130 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005131 codegen_->GenerateGcRootFieldLoad(cls,
5132 out_loc,
5133 out.X(),
5134 /* offset */ 0,
5135 /* fixup_label */ nullptr,
5136 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005137 break;
5138 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005139 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005140 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005141 LOG(FATAL) << "UNREACHABLE";
5142 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005143 }
5144
Vladimir Markoea4c1262017-02-06 19:59:33 +00005145 bool do_clinit = cls->MustGenerateClinitCheck();
5146 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005147 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005148 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00005149 cls, cls, cls->GetDexPc(), do_clinit);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005150 codegen_->AddSlowPath(slow_path);
5151 if (generate_null_check) {
5152 __ Cbz(out, slow_path->GetEntryLabel());
5153 }
5154 if (cls->MustGenerateClinitCheck()) {
5155 GenerateClassInitializationCheck(slow_path, out);
5156 } else {
5157 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005158 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005159 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005160 }
5161}
5162
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005163void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5164 InvokeRuntimeCallingConvention calling_convention;
5165 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5166 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5167}
5168
5169void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5170 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5171}
5172
Orion Hodson18259d72018-04-12 11:18:23 +01005173void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5174 InvokeRuntimeCallingConvention calling_convention;
5175 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5176 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5177}
5178
5179void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5180 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5181}
5182
David Brazdilcb1c0552015-08-04 16:22:25 +01005183static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005184 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005185}
5186
Alexandre Rames67555f72014-11-18 10:55:16 +00005187void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5188 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005189 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005190 locations->SetOut(Location::RequiresRegister());
5191}
5192
5193void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005194 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5195}
5196
5197void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005198 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005199}
5200
5201void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5202 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005203}
5204
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005205HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5206 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005207 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005208 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005209 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005210 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005211 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005212 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005213 case HLoadString::LoadKind::kJitTableAddress:
5214 DCHECK(Runtime::Current()->UseJitCompilation());
5215 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005216 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005217 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005218 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005219 }
5220 return desired_string_load_kind;
5221}
5222
Alexandre Rames67555f72014-11-18 10:55:16 +00005223void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005224 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005225 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005226 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005227 InvokeRuntimeCallingConvention calling_convention;
5228 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5229 } else {
5230 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005231 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5232 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005233 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005234 RegisterSet caller_saves = RegisterSet::Empty();
5235 InvokeRuntimeCallingConvention calling_convention;
5236 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5237 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005238 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5239 DataType::Type::kReference).GetCode());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005240 locations->SetCustomSlowPathCallerSaves(caller_saves);
5241 } else {
5242 // For non-Baker read barrier we have a temp-clobbering call.
5243 }
5244 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005245 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005246}
5247
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005248// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5249// move.
5250void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005251 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005252 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005253
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005254 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005255 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005256 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005257 // Add ADRP with its PC-relative String patch.
5258 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005259 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005260 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005261 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005262 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005263 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005264 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005265 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005266 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005267 }
5268 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005269 uint32_t address = dchecked_integral_cast<uint32_t>(
5270 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5271 DCHECK_NE(address, 0u);
5272 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005273 return;
5274 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005275 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005276 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005277 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5278 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5279 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005280 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005281 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005282 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005283 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005284 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5285 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005286 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005287 case HLoadString::LoadKind::kBssEntry: {
5288 // Add ADRP with its PC-relative String .bss entry patch.
5289 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005290 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005291 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof3c52b42017-11-17 17:32:12 +00005292 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005293 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005294 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005295 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005296 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005297 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005298 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005299 codegen_->GenerateGcRootFieldLoad(load,
5300 out_loc,
5301 temp,
5302 /* offset placeholder */ 0u,
5303 ldr_label,
5304 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005305 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005306 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005307 codegen_->AddSlowPath(slow_path);
5308 __ Cbz(out.X(), slow_path->GetEntryLabel());
5309 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005310 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005311 return;
5312 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005313 case HLoadString::LoadKind::kJitTableAddress: {
5314 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005315 load->GetStringIndex(),
5316 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005317 codegen_->GenerateGcRootFieldLoad(load,
5318 out_loc,
5319 out.X(),
5320 /* offset */ 0,
5321 /* fixup_label */ nullptr,
5322 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005323 return;
5324 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005325 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005326 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005327 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005328
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005329 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005330 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005331 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005332 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005333 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5334 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005335 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005336}
5337
Alexandre Rames5319def2014-10-23 10:03:10 +01005338void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005339 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005340 locations->SetOut(Location::ConstantLocation(constant));
5341}
5342
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005343void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005344 // Will be generated at use site.
5345}
5346
Alexandre Rames67555f72014-11-18 10:55:16 +00005347void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005348 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5349 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005350 InvokeRuntimeCallingConvention calling_convention;
5351 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5352}
5353
5354void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005355 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005356 instruction,
5357 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005358 if (instruction->IsEnter()) {
5359 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5360 } else {
5361 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5362 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005363 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005364}
5365
Alexandre Rames42d641b2014-10-27 14:00:51 +00005366void LocationsBuilderARM64::VisitMul(HMul* mul) {
5367 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005368 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005369 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005370 case DataType::Type::kInt32:
5371 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005372 locations->SetInAt(0, Location::RequiresRegister());
5373 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005375 break;
5376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005377 case DataType::Type::kFloat32:
5378 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005379 locations->SetInAt(0, Location::RequiresFpuRegister());
5380 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005381 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005382 break;
5383
5384 default:
5385 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5386 }
5387}
5388
5389void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5390 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 case DataType::Type::kInt32:
5392 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005393 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5394 break;
5395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005396 case DataType::Type::kFloat32:
5397 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005398 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005399 break;
5400
5401 default:
5402 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5403 }
5404}
5405
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005406void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5407 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005408 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005409 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005410 case DataType::Type::kInt32:
5411 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005412 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005414 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005415
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005416 case DataType::Type::kFloat32:
5417 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005418 locations->SetInAt(0, Location::RequiresFpuRegister());
5419 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005420 break;
5421
5422 default:
5423 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5424 }
5425}
5426
5427void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5428 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 case DataType::Type::kInt32:
5430 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005431 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5432 break;
5433
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005434 case DataType::Type::kFloat32:
5435 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005436 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005437 break;
5438
5439 default:
5440 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5441 }
5442}
5443
5444void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005445 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5446 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005447 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005448 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005449 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5450 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005451}
5452
5453void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005454 // Note: if heap poisoning is enabled, the entry point takes cares
5455 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005456 QuickEntrypointEnum entrypoint =
5457 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5458 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005459 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005460 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005461}
5462
Alexandre Rames5319def2014-10-23 10:03:10 +01005463void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005464 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5465 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005466 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005467 if (instruction->IsStringAlloc()) {
5468 locations->AddTemp(LocationFrom(kArtMethodRegister));
5469 } else {
5470 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005471 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005472 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005473}
5474
5475void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005476 // Note: if heap poisoning is enabled, the entry point takes cares
5477 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005478 if (instruction->IsStringAlloc()) {
5479 // String is allocated through StringFactory. Call NewEmptyString entry point.
5480 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005481 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005482 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5483 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005484
5485 {
5486 // Ensure the pc position is recorded immediately after the `blr` instruction.
5487 ExactAssemblyScope eas(GetVIXLAssembler(),
5488 kInstructionSize,
5489 CodeBufferCheckScope::kExactSize);
5490 __ blr(lr);
5491 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5492 }
David Brazdil6de19382016-01-08 17:37:10 +00005493 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005494 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005495 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005496 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005497 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005498}
5499
5500void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005501 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005502 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005504}
5505
5506void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005507 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005508 case DataType::Type::kInt32:
5509 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005510 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005511 break;
5512
5513 default:
5514 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5515 }
5516}
5517
David Brazdil66d126e2015-04-03 16:02:44 +01005518void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005519 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005520 locations->SetInAt(0, Location::RequiresRegister());
5521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5522}
5523
5524void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005525 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005526}
5527
Alexandre Rames5319def2014-10-23 10:03:10 +01005528void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005529 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5530 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005531}
5532
Calin Juravle2ae48182016-03-16 14:05:09 +00005533void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5534 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005535 return;
5536 }
Artem Serov914d7a82017-02-07 14:33:49 +00005537 {
5538 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5539 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5540 Location obj = instruction->GetLocations()->InAt(0);
5541 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5542 RecordPcInfo(instruction, instruction->GetDexPc());
5543 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005544}
5545
Calin Juravle2ae48182016-03-16 14:05:09 +00005546void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005547 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005548 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005549
5550 LocationSummary* locations = instruction->GetLocations();
5551 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005552
5553 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005554}
5555
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005556void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005557 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005558}
5559
Alexandre Rames67555f72014-11-18 10:55:16 +00005560void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5561 HandleBinaryOp(instruction);
5562}
5563
5564void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5565 HandleBinaryOp(instruction);
5566}
5567
Alexandre Rames3e69f162014-12-10 10:36:50 +00005568void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5569 LOG(FATAL) << "Unreachable";
5570}
5571
5572void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005573 if (instruction->GetNext()->IsSuspendCheck() &&
5574 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5575 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5576 // The back edge will generate the suspend check.
5577 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5578 }
5579
Alexandre Rames3e69f162014-12-10 10:36:50 +00005580 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5581}
5582
Alexandre Rames5319def2014-10-23 10:03:10 +01005583void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005584 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005585 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5586 if (location.IsStackSlot()) {
5587 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5588 } else if (location.IsDoubleStackSlot()) {
5589 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5590 }
5591 locations->SetOut(location);
5592}
5593
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005594void InstructionCodeGeneratorARM64::VisitParameterValue(
5595 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005596 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005597}
5598
5599void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5600 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005601 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005602 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005603}
5604
5605void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5606 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5607 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005608}
5609
5610void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005611 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005612 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005613 locations->SetInAt(i, Location::Any());
5614 }
5615 locations->SetOut(Location::Any());
5616}
5617
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005618void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005619 LOG(FATAL) << "Unreachable";
5620}
5621
Serban Constantinescu02164b32014-11-13 14:05:07 +00005622void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005623 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005624 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005626 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005627 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005628
5629 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005630 case DataType::Type::kInt32:
5631 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005632 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005633 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005634 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5635 break;
5636
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 case DataType::Type::kFloat32:
5638 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005639 InvokeRuntimeCallingConvention calling_convention;
5640 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5641 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5642 locations->SetOut(calling_convention.GetReturnLocation(type));
5643
5644 break;
5645 }
5646
Serban Constantinescu02164b32014-11-13 14:05:07 +00005647 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005648 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005649 }
5650}
5651
5652void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005653 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005654
Serban Constantinescu02164b32014-11-13 14:05:07 +00005655 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 case DataType::Type::kInt32:
5657 case DataType::Type::kInt64: {
Zheng Xuc6667102015-05-15 16:08:45 +08005658 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005659 break;
5660 }
5661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005662 case DataType::Type::kFloat32:
5663 case DataType::Type::kFloat64: {
5664 QuickEntrypointEnum entrypoint =
5665 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005666 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005667 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005668 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5669 } else {
5670 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5671 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005672 break;
5673 }
5674
Serban Constantinescu02164b32014-11-13 14:05:07 +00005675 default:
5676 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005677 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005678 }
5679}
5680
Aart Bik1f8d51b2018-02-15 10:42:37 -08005681void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005682 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005683}
5684
Aart Bik1f8d51b2018-02-15 10:42:37 -08005685void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005686 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005687}
5688
5689void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005690 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005691}
5692
5693void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005694 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005695}
5696
Aart Bik3dad3412018-02-28 12:01:46 -08005697void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5698 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5699 switch (abs->GetResultType()) {
5700 case DataType::Type::kInt32:
5701 case DataType::Type::kInt64:
5702 locations->SetInAt(0, Location::RequiresRegister());
5703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5704 break;
5705 case DataType::Type::kFloat32:
5706 case DataType::Type::kFloat64:
5707 locations->SetInAt(0, Location::RequiresFpuRegister());
5708 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5709 break;
5710 default:
5711 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5712 }
5713}
5714
5715void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5716 switch (abs->GetResultType()) {
5717 case DataType::Type::kInt32:
5718 case DataType::Type::kInt64: {
5719 Register in_reg = InputRegisterAt(abs, 0);
5720 Register out_reg = OutputRegister(abs);
5721 __ Cmp(in_reg, Operand(0));
5722 __ Cneg(out_reg, in_reg, lt);
5723 break;
5724 }
5725 case DataType::Type::kFloat32:
5726 case DataType::Type::kFloat64: {
5727 FPRegister in_reg = InputFPRegisterAt(abs, 0);
5728 FPRegister out_reg = OutputFPRegister(abs);
5729 __ Fabs(out_reg, in_reg);
5730 break;
5731 }
5732 default:
5733 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5734 }
5735}
5736
Igor Murashkind01745e2017-04-05 16:40:31 -07005737void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5738 constructor_fence->SetLocations(nullptr);
5739}
5740
5741void InstructionCodeGeneratorARM64::VisitConstructorFence(
5742 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5743 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5744}
5745
Calin Juravle27df7582015-04-17 19:12:31 +01005746void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5747 memory_barrier->SetLocations(nullptr);
5748}
5749
5750void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005751 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005752}
5753
Alexandre Rames5319def2014-10-23 10:03:10 +01005754void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005755 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005757 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005758}
5759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005760void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005761 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005762}
5763
5764void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5765 instruction->SetLocations(nullptr);
5766}
5767
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005768void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005769 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005770}
5771
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005772void LocationsBuilderARM64::VisitRor(HRor* ror) {
5773 HandleBinaryOp(ror);
5774}
5775
5776void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5777 HandleBinaryOp(ror);
5778}
5779
Serban Constantinescu02164b32014-11-13 14:05:07 +00005780void LocationsBuilderARM64::VisitShl(HShl* shl) {
5781 HandleShift(shl);
5782}
5783
5784void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5785 HandleShift(shl);
5786}
5787
5788void LocationsBuilderARM64::VisitShr(HShr* shr) {
5789 HandleShift(shr);
5790}
5791
5792void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5793 HandleShift(shr);
5794}
5795
Alexandre Rames5319def2014-10-23 10:03:10 +01005796void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005797 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005798}
5799
5800void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005801 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005802}
5803
Alexandre Rames67555f72014-11-18 10:55:16 +00005804void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005805 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005806}
5807
5808void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005809 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005810}
5811
5812void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005813 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005814}
5815
Alexandre Rames67555f72014-11-18 10:55:16 +00005816void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005817 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005818}
5819
Calin Juravlee460d1d2015-09-29 04:52:17 +01005820void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5821 HUnresolvedInstanceFieldGet* instruction) {
5822 FieldAccessCallingConventionARM64 calling_convention;
5823 codegen_->CreateUnresolvedFieldLocationSummary(
5824 instruction, instruction->GetFieldType(), calling_convention);
5825}
5826
5827void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5828 HUnresolvedInstanceFieldGet* instruction) {
5829 FieldAccessCallingConventionARM64 calling_convention;
5830 codegen_->GenerateUnresolvedFieldAccess(instruction,
5831 instruction->GetFieldType(),
5832 instruction->GetFieldIndex(),
5833 instruction->GetDexPc(),
5834 calling_convention);
5835}
5836
5837void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5838 HUnresolvedInstanceFieldSet* instruction) {
5839 FieldAccessCallingConventionARM64 calling_convention;
5840 codegen_->CreateUnresolvedFieldLocationSummary(
5841 instruction, instruction->GetFieldType(), calling_convention);
5842}
5843
5844void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5845 HUnresolvedInstanceFieldSet* instruction) {
5846 FieldAccessCallingConventionARM64 calling_convention;
5847 codegen_->GenerateUnresolvedFieldAccess(instruction,
5848 instruction->GetFieldType(),
5849 instruction->GetFieldIndex(),
5850 instruction->GetDexPc(),
5851 calling_convention);
5852}
5853
5854void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5855 HUnresolvedStaticFieldGet* instruction) {
5856 FieldAccessCallingConventionARM64 calling_convention;
5857 codegen_->CreateUnresolvedFieldLocationSummary(
5858 instruction, instruction->GetFieldType(), calling_convention);
5859}
5860
5861void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5862 HUnresolvedStaticFieldGet* instruction) {
5863 FieldAccessCallingConventionARM64 calling_convention;
5864 codegen_->GenerateUnresolvedFieldAccess(instruction,
5865 instruction->GetFieldType(),
5866 instruction->GetFieldIndex(),
5867 instruction->GetDexPc(),
5868 calling_convention);
5869}
5870
5871void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5872 HUnresolvedStaticFieldSet* instruction) {
5873 FieldAccessCallingConventionARM64 calling_convention;
5874 codegen_->CreateUnresolvedFieldLocationSummary(
5875 instruction, instruction->GetFieldType(), calling_convention);
5876}
5877
5878void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5879 HUnresolvedStaticFieldSet* instruction) {
5880 FieldAccessCallingConventionARM64 calling_convention;
5881 codegen_->GenerateUnresolvedFieldAccess(instruction,
5882 instruction->GetFieldType(),
5883 instruction->GetFieldIndex(),
5884 instruction->GetDexPc(),
5885 calling_convention);
5886}
5887
Alexandre Rames5319def2014-10-23 10:03:10 +01005888void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005889 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5890 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005891 // In suspend check slow path, usually there are no caller-save registers at all.
5892 // If SIMD instructions are present, however, we force spilling all live SIMD
5893 // registers in full width (since the runtime only saves/restores lower part).
5894 locations->SetCustomSlowPathCallerSaves(
5895 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005896}
5897
5898void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005899 HBasicBlock* block = instruction->GetBlock();
5900 if (block->GetLoopInformation() != nullptr) {
5901 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5902 // The back edge will generate the suspend check.
5903 return;
5904 }
5905 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5906 // The goto will generate the suspend check.
5907 return;
5908 }
5909 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005910 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005911}
5912
Alexandre Rames67555f72014-11-18 10:55:16 +00005913void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005914 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5915 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005916 InvokeRuntimeCallingConvention calling_convention;
5917 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5918}
5919
5920void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005921 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005922 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005923}
5924
5925void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5926 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005927 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 DataType::Type input_type = conversion->GetInputType();
5929 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005930 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5931 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005932 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5933 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005934 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5935 }
5936
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005938 locations->SetInAt(0, Location::RequiresFpuRegister());
5939 } else {
5940 locations->SetInAt(0, Location::RequiresRegister());
5941 }
5942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005944 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5945 } else {
5946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5947 }
5948}
5949
5950void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 DataType::Type result_type = conversion->GetResultType();
5952 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005953
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005954 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5955 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5958 int result_size = DataType::Size(result_type);
5959 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005960 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005961 Register output = OutputRegister(conversion);
5962 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005963 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005964 // 'int' values are used directly as W registers, discarding the top
5965 // bits, so we don't need to sign-extend and can just perform a move.
5966 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5967 // top 32 bits of the target register. We theoretically could leave those
5968 // bits unchanged, but we would have to make sure that no code uses a
5969 // 32bit input value as a 64bit value assuming that the top 32 bits are
5970 // zero.
5971 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005972 } else if (DataType::IsUnsignedType(result_type) ||
5973 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5974 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005975 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005976 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005977 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005978 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005979 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005980 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5981 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005982 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005983 } else if (DataType::IsFloatingPointType(result_type) &&
5984 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005985 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5986 } else {
5987 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5988 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005989 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005990}
Alexandre Rames67555f72014-11-18 10:55:16 +00005991
Serban Constantinescu02164b32014-11-13 14:05:07 +00005992void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5993 HandleShift(ushr);
5994}
5995
5996void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5997 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005998}
5999
6000void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6001 HandleBinaryOp(instruction);
6002}
6003
6004void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6005 HandleBinaryOp(instruction);
6006}
6007
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006008void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006009 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006010 LOG(FATAL) << "Unreachable";
6011}
6012
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006013void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006014 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006015 LOG(FATAL) << "Unreachable";
6016}
6017
Mark Mendellfe57faa2015-09-18 09:26:15 -04006018// Simple implementation of packed switch - generate cascaded compare/jumps.
6019void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6020 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006021 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006022 locations->SetInAt(0, Location::RequiresRegister());
6023}
6024
6025void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6026 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006027 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006028 Register value_reg = InputRegisterAt(switch_instr, 0);
6029 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6030
Zheng Xu3927c8b2015-11-18 17:46:25 +08006031 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006032 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006033 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6034 // make sure we don't emit it if the target may run out of range.
6035 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6036 // ranges and emit the tables only as required.
6037 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006038
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006039 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006040 // Current instruction id is an upper bound of the number of HIRs in the graph.
6041 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6042 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006043 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6044 Register temp = temps.AcquireW();
6045 __ Subs(temp, value_reg, Operand(lower_bound));
6046
Zheng Xu3927c8b2015-11-18 17:46:25 +08006047 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006048 // Jump to successors[0] if value == lower_bound.
6049 __ B(eq, codegen_->GetLabelOf(successors[0]));
6050 int32_t last_index = 0;
6051 for (; num_entries - last_index > 2; last_index += 2) {
6052 __ Subs(temp, temp, Operand(2));
6053 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6054 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6055 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6056 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6057 }
6058 if (num_entries - last_index == 2) {
6059 // The last missing case_value.
6060 __ Cmp(temp, Operand(1));
6061 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006062 }
6063
6064 // And the default for any other value.
6065 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6066 __ B(codegen_->GetLabelOf(default_block));
6067 }
6068 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006069 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006070
6071 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6072
6073 // Below instructions should use at most one blocked register. Since there are two blocked
6074 // registers, we are free to block one.
6075 Register temp_w = temps.AcquireW();
6076 Register index;
6077 // Remove the bias.
6078 if (lower_bound != 0) {
6079 index = temp_w;
6080 __ Sub(index, value_reg, Operand(lower_bound));
6081 } else {
6082 index = value_reg;
6083 }
6084
6085 // Jump to default block if index is out of the range.
6086 __ Cmp(index, Operand(num_entries));
6087 __ B(hs, codegen_->GetLabelOf(default_block));
6088
6089 // In current VIXL implementation, it won't require any blocked registers to encode the
6090 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6091 // register pressure.
6092 Register table_base = temps.AcquireX();
6093 // Load jump offset from the table.
6094 __ Adr(table_base, jump_table->GetTableStartLabel());
6095 Register jump_offset = temp_w;
6096 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6097
6098 // Jump to target block by branching to table_base(pc related) + offset.
6099 Register target_address = table_base;
6100 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6101 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006102 }
6103}
6104
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006105void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6106 HInstruction* instruction,
6107 Location out,
6108 uint32_t offset,
6109 Location maybe_temp,
6110 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006112 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006113 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006114 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006115 if (kUseBakerReadBarrier) {
6116 // Load with fast path based Baker's read barrier.
6117 // /* HeapReference<Object> */ out = *(out + offset)
6118 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6119 out,
6120 out_reg,
6121 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006122 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006123 /* needs_null_check */ false,
6124 /* use_load_acquire */ false);
6125 } else {
6126 // Load with slow path based read barrier.
6127 // Save the value of `out` into `maybe_temp` before overwriting it
6128 // in the following move operation, as we will need it for the
6129 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006130 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006131 __ Mov(temp_reg, out_reg);
6132 // /* HeapReference<Object> */ out = *(out + offset)
6133 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6134 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6135 }
6136 } else {
6137 // Plain load with no read barrier.
6138 // /* HeapReference<Object> */ out = *(out + offset)
6139 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6140 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6141 }
6142}
6143
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006144void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6145 HInstruction* instruction,
6146 Location out,
6147 Location obj,
6148 uint32_t offset,
6149 Location maybe_temp,
6150 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006151 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006152 Register out_reg = RegisterFrom(out, type);
6153 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006154 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006155 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006156 if (kUseBakerReadBarrier) {
6157 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006158 // /* HeapReference<Object> */ out = *(obj + offset)
6159 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6160 out,
6161 obj_reg,
6162 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006163 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006164 /* needs_null_check */ false,
6165 /* use_load_acquire */ false);
6166 } else {
6167 // Load with slow path based read barrier.
6168 // /* HeapReference<Object> */ out = *(obj + offset)
6169 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6170 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6171 }
6172 } else {
6173 // Plain load with no read barrier.
6174 // /* HeapReference<Object> */ out = *(obj + offset)
6175 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6176 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6177 }
6178}
6179
Vladimir Markoca1e0382018-04-11 09:58:41 +00006180void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006181 HInstruction* instruction,
6182 Location root,
6183 Register obj,
6184 uint32_t offset,
6185 vixl::aarch64::Label* fixup_label,
6186 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006187 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006189 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006190 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006191 if (kUseBakerReadBarrier) {
6192 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006193 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006194 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
6195 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006196 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6197 // the Marking Register) to decide whether we need to enter
6198 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006199 //
6200 // We use link-time generated thunks for the slow path. That thunk
6201 // checks the reference and jumps to the entrypoint if needed.
6202 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006203 // lr = &return_address;
6204 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006205 // if (mr) { // Thread::Current()->GetIsGcMarking()
6206 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006207 // }
6208 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00006209
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006210 UseScratchRegisterScope temps(GetVIXLAssembler());
6211 DCHECK(temps.IsAvailable(ip0));
6212 DCHECK(temps.IsAvailable(ip1));
6213 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006214 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
6215 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00006216
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006217 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6218 vixl::aarch64::Label return_address;
6219 __ adr(lr, &return_address);
6220 if (fixup_label != nullptr) {
6221 __ Bind(fixup_label);
6222 }
6223 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6224 "GC root LDR must be 2 instruction (8B) before the return address label.");
6225 __ ldr(root_reg, MemOperand(obj.X(), offset));
6226 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006227 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006228 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006229 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006230 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6231 // the Marking Register) to decide whether we need to enter
6232 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006233 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006234 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006235 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006236 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006237 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6238 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006239 // }
Roland Levillain44015862016-01-22 11:47:17 +00006240
Roland Levillain97c46462017-05-11 14:04:03 +01006241 // Slow path marking the GC root `root`. The entrypoint will
6242 // be loaded by the slow path code.
6243 SlowPathCodeARM64* slow_path =
Vladimir Markoca1e0382018-04-11 09:58:41 +00006244 new (GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
6245 AddSlowPath(slow_path);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006246
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006247 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6248 if (fixup_label == nullptr) {
6249 __ Ldr(root_reg, MemOperand(obj, offset));
6250 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006251 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006252 }
6253 static_assert(
6254 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6255 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6256 "have different sizes.");
6257 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6258 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6259 "have different sizes.");
6260
Roland Levillain97c46462017-05-11 14:04:03 +01006261 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006262 __ Bind(slow_path->GetExitLabel());
6263 }
Roland Levillain44015862016-01-22 11:47:17 +00006264 } else {
6265 // GC root loaded through a slow path for read barriers other
6266 // than Baker's.
6267 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006268 if (fixup_label == nullptr) {
6269 __ Add(root_reg.X(), obj.X(), offset);
6270 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006271 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006272 }
Roland Levillain44015862016-01-22 11:47:17 +00006273 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006274 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006275 }
6276 } else {
6277 // Plain GC root load with no read barrier.
6278 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006279 if (fixup_label == nullptr) {
6280 __ Ldr(root_reg, MemOperand(obj, offset));
6281 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006282 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006283 }
Roland Levillain44015862016-01-22 11:47:17 +00006284 // Note that GC roots are not affected by heap poisoning, thus we
6285 // do not have to unpoison `root_reg` here.
6286 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006287 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006288}
6289
6290void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6291 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006292 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006293 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006294 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006295 bool needs_null_check,
6296 bool use_load_acquire) {
6297 DCHECK(kEmitCompilerReadBarrier);
6298 DCHECK(kUseBakerReadBarrier);
6299
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006300 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6301 !use_load_acquire &&
6302 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006303 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6304 // Marking Register) to decide whether we need to enter the slow
6305 // path to mark the reference. Then, in the slow path, check the
6306 // gray bit in the lock word of the reference's holder (`obj`) to
6307 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006308 //
6309 // We use link-time generated thunks for the slow path. That thunk checks
6310 // the holder and jumps to the entrypoint if needed. If the holder is not
6311 // gray, it creates a fake dependency and returns to the LDR instruction.
6312 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006313 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006314 // if (mr) { // Thread::Current()->GetIsGcMarking()
6315 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006316 // }
6317 // not_gray_return_address:
6318 // // Original reference load. If the offset is too large to fit
6319 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006320 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006321 // gray_return_address:
6322
6323 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6324 Register base = obj;
6325 if (offset >= kReferenceLoadMinFarOffset) {
6326 DCHECK(maybe_temp.IsRegister());
6327 base = WRegisterFrom(maybe_temp);
6328 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6329 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6330 offset &= (kReferenceLoadMinFarOffset - 1u);
6331 }
6332 UseScratchRegisterScope temps(GetVIXLAssembler());
6333 DCHECK(temps.IsAvailable(ip0));
6334 DCHECK(temps.IsAvailable(ip1));
6335 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006336 uint32_t custom_data = EncodeBakerReadBarrierFieldData(base.GetCode(), obj.GetCode());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006337 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6338
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006339 {
6340 EmissionCheckScope guard(GetVIXLAssembler(),
6341 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6342 vixl::aarch64::Label return_address;
6343 __ adr(lr, &return_address);
6344 __ Bind(cbnz_label);
6345 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6346 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6347 "Field LDR must be 1 instruction (4B) before the return address label; "
6348 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006349 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006350 __ ldr(ref_reg, MemOperand(base.X(), offset));
6351 if (needs_null_check) {
6352 MaybeRecordImplicitNullCheck(instruction);
6353 }
6354 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6355 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006356 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006357 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006358 return;
6359 }
6360
Roland Levillain44015862016-01-22 11:47:17 +00006361 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006362 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006363 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006364 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006365 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6366 ref,
6367 obj,
6368 offset,
6369 no_index,
6370 no_scale_factor,
6371 temp,
6372 needs_null_check,
6373 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006374}
6375
6376void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6377 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006378 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006379 uint32_t data_offset,
6380 Location index,
6381 Register temp,
6382 bool needs_null_check) {
6383 DCHECK(kEmitCompilerReadBarrier);
6384 DCHECK(kUseBakerReadBarrier);
6385
Vladimir Marko66d691d2017-04-07 17:53:39 +01006386 static_assert(
6387 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6388 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006389 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006390
6391 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6392 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006393 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6394 // Marking Register) to decide whether we need to enter the slow
6395 // path to mark the reference. Then, in the slow path, check the
6396 // gray bit in the lock word of the reference's holder (`obj`) to
6397 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006398 //
6399 // We use link-time generated thunks for the slow path. That thunk checks
6400 // the holder and jumps to the entrypoint if needed. If the holder is not
6401 // gray, it creates a fake dependency and returns to the LDR instruction.
6402 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006403 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006404 // if (mr) { // Thread::Current()->GetIsGcMarking()
6405 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006406 // }
6407 // not_gray_return_address:
6408 // // Original reference load. If the offset is too large to fit
6409 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006410 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006411 // gray_return_address:
6412
6413 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6415 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006416
6417 UseScratchRegisterScope temps(GetVIXLAssembler());
6418 DCHECK(temps.IsAvailable(ip0));
6419 DCHECK(temps.IsAvailable(ip1));
6420 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006421 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006422 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6423
Vladimir Marko66d691d2017-04-07 17:53:39 +01006424 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006425 {
6426 EmissionCheckScope guard(GetVIXLAssembler(),
6427 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6428 vixl::aarch64::Label return_address;
6429 __ adr(lr, &return_address);
6430 __ Bind(cbnz_label);
6431 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6432 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6433 "Array LDR must be 1 instruction (4B) before the return address label; "
6434 " 2 instructions (8B) for heap poisoning.");
6435 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6436 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6437 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6438 __ Bind(&return_address);
6439 }
6440 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006441 return;
6442 }
6443
Roland Levillain44015862016-01-22 11:47:17 +00006444 // Array cells are never volatile variables, therefore array loads
6445 // never use Load-Acquire instructions on ARM64.
6446 const bool use_load_acquire = false;
6447
6448 // /* HeapReference<Object> */ ref =
6449 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006450 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6451 ref,
6452 obj,
6453 data_offset,
6454 index,
6455 scale_factor,
6456 temp,
6457 needs_null_check,
6458 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006459}
6460
6461void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6462 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006463 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006464 uint32_t offset,
6465 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006466 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006467 Register temp,
6468 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006469 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006470 DCHECK(kEmitCompilerReadBarrier);
6471 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006472 // If we are emitting an array load, we should not be using a
6473 // Load Acquire instruction. In other words:
6474 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6475 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006476
Roland Levillain97c46462017-05-11 14:04:03 +01006477 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6478 // Marking Register) to decide whether we need to enter the slow
6479 // path to mark the reference. Then, in the slow path, check the
6480 // gray bit in the lock word of the reference's holder (`obj`) to
6481 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006482 //
Roland Levillain97c46462017-05-11 14:04:03 +01006483 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006484 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006485 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6486 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6487 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6488 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6489 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006490 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6491 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006492 // }
6493 // } else {
6494 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006495 // }
Roland Levillain44015862016-01-22 11:47:17 +00006496
Roland Levillainba650a42017-03-06 13:52:32 +00006497 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006498 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006499 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006500 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006501 instruction,
6502 ref,
6503 obj,
6504 offset,
6505 index,
6506 scale_factor,
6507 needs_null_check,
6508 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006509 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006510 AddSlowPath(slow_path);
6511
Roland Levillain97c46462017-05-11 14:04:03 +01006512 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006513 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006514 GenerateRawReferenceLoad(
6515 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006516 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006517 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006518}
6519
Roland Levillainff487002017-03-07 16:50:01 +00006520void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6521 Location ref,
6522 Register obj,
6523 Location field_offset,
6524 Register temp,
6525 bool needs_null_check,
6526 bool use_load_acquire) {
6527 DCHECK(kEmitCompilerReadBarrier);
6528 DCHECK(kUseBakerReadBarrier);
6529 // If we are emitting an array load, we should not be using a
6530 // Load Acquire instruction. In other words:
6531 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6532 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6533
Roland Levillain97c46462017-05-11 14:04:03 +01006534 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6535 // Marking Register) to decide whether we need to enter the slow
6536 // path to update the reference field within `obj`. Then, in the
6537 // slow path, check the gray bit in the lock word of the reference's
6538 // holder (`obj`) to decide whether to mark `ref` and update the
6539 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006540 //
Roland Levillain97c46462017-05-11 14:04:03 +01006541 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006542 // // Slow path.
6543 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6544 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6545 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6546 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6547 // if (is_gray) {
6548 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006549 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6550 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006551 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6552 // }
6553 // }
6554
6555 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006556 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006557 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006558 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006559 instruction,
6560 ref,
6561 obj,
6562 /* offset */ 0u,
6563 /* index */ field_offset,
6564 /* scale_factor */ 0u /* "times 1" */,
6565 needs_null_check,
6566 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006567 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006568 AddSlowPath(slow_path);
6569
Roland Levillain97c46462017-05-11 14:04:03 +01006570 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006571 // Fast path: the GC is not marking: nothing to do (the field is
6572 // up-to-date, and we don't need to load the reference).
6573 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006574 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006575}
6576
Roland Levillainba650a42017-03-06 13:52:32 +00006577void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6578 Location ref,
6579 Register obj,
6580 uint32_t offset,
6581 Location index,
6582 size_t scale_factor,
6583 bool needs_null_check,
6584 bool use_load_acquire) {
6585 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006586 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006587 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006588
Roland Levillainba650a42017-03-06 13:52:32 +00006589 // If needed, vixl::EmissionCheckScope guards are used to ensure
6590 // that no pools are emitted between the load (macro) instruction
6591 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006592
Roland Levillain44015862016-01-22 11:47:17 +00006593 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006594 // Load types involving an "index": ArrayGet,
6595 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6596 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006597 if (use_load_acquire) {
6598 // UnsafeGetObjectVolatile intrinsic case.
6599 // Register `index` is not an index in an object array, but an
6600 // offset to an object reference field within object `obj`.
6601 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6602 DCHECK(instruction->GetLocations()->Intrinsified());
6603 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6604 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006605 DCHECK_EQ(offset, 0u);
6606 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006607 DCHECK_EQ(needs_null_check, false);
6608 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006609 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6610 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006611 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006612 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6613 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006614 if (index.IsConstant()) {
6615 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006616 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006617 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006618 if (needs_null_check) {
6619 MaybeRecordImplicitNullCheck(instruction);
6620 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006621 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006622 UseScratchRegisterScope temps(GetVIXLAssembler());
6623 Register temp = temps.AcquireW();
6624 __ Add(temp, obj, offset);
6625 {
6626 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6627 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6628 if (needs_null_check) {
6629 MaybeRecordImplicitNullCheck(instruction);
6630 }
6631 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006632 }
Roland Levillain44015862016-01-22 11:47:17 +00006633 }
Roland Levillain44015862016-01-22 11:47:17 +00006634 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006635 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006636 MemOperand field = HeapOperand(obj, offset);
6637 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006638 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6639 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006640 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006641 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006642 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006643 if (needs_null_check) {
6644 MaybeRecordImplicitNullCheck(instruction);
6645 }
Roland Levillain44015862016-01-22 11:47:17 +00006646 }
6647 }
6648
6649 // Object* ref = ref_addr->AsMirrorPtr()
6650 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006651}
6652
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006653void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6654 // The following condition is a compile-time one, so it does not have a run-time cost.
6655 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6656 // The following condition is a run-time one; it is executed after the
6657 // previous compile-time test, to avoid penalizing non-debug builds.
6658 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6659 UseScratchRegisterScope temps(GetVIXLAssembler());
6660 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6661 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6662 }
6663 }
6664}
6665
Roland Levillain44015862016-01-22 11:47:17 +00006666void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6667 Location out,
6668 Location ref,
6669 Location obj,
6670 uint32_t offset,
6671 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006672 DCHECK(kEmitCompilerReadBarrier);
6673
Roland Levillain44015862016-01-22 11:47:17 +00006674 // Insert a slow path based read barrier *after* the reference load.
6675 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006676 // If heap poisoning is enabled, the unpoisoning of the loaded
6677 // reference will be carried out by the runtime within the slow
6678 // path.
6679 //
6680 // Note that `ref` currently does not get unpoisoned (when heap
6681 // poisoning is enabled), which is alright as the `ref` argument is
6682 // not used by the artReadBarrierSlow entry point.
6683 //
6684 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006685 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006686 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6687 AddSlowPath(slow_path);
6688
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006689 __ B(slow_path->GetEntryLabel());
6690 __ Bind(slow_path->GetExitLabel());
6691}
6692
Roland Levillain44015862016-01-22 11:47:17 +00006693void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6694 Location out,
6695 Location ref,
6696 Location obj,
6697 uint32_t offset,
6698 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006699 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006700 // Baker's read barriers shall be handled by the fast path
6701 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6702 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006703 // If heap poisoning is enabled, unpoisoning will be taken care of
6704 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006705 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006706 } else if (kPoisonHeapReferences) {
6707 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6708 }
6709}
6710
Roland Levillain44015862016-01-22 11:47:17 +00006711void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6712 Location out,
6713 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006714 DCHECK(kEmitCompilerReadBarrier);
6715
Roland Levillain44015862016-01-22 11:47:17 +00006716 // Insert a slow path based read barrier *after* the GC root load.
6717 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006718 // Note that GC roots are not affected by heap poisoning, so we do
6719 // not need to do anything special for this here.
6720 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006721 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006722 AddSlowPath(slow_path);
6723
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006724 __ B(slow_path->GetEntryLabel());
6725 __ Bind(slow_path->GetExitLabel());
6726}
6727
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006728void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6729 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006730 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006731 locations->SetInAt(0, Location::RequiresRegister());
6732 locations->SetOut(Location::RequiresRegister());
6733}
6734
6735void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6736 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006737 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006738 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006739 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006740 __ Ldr(XRegisterFrom(locations->Out()),
6741 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006742 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006743 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006744 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006745 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6746 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006747 __ Ldr(XRegisterFrom(locations->Out()),
6748 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006749 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006750}
6751
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006752static void PatchJitRootUse(uint8_t* code,
6753 const uint8_t* roots_data,
6754 vixl::aarch64::Literal<uint32_t>* literal,
6755 uint64_t index_in_table) {
6756 uint32_t literal_offset = literal->GetOffset();
6757 uintptr_t address =
6758 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6759 uint8_t* data = code + literal_offset;
6760 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6761}
6762
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006763void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6764 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006765 const StringReference& string_reference = entry.first;
6766 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006767 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006768 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006769 }
6770 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006771 const TypeReference& type_reference = entry.first;
6772 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006773 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006774 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006775 }
6776}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006777
Alexandre Rames67555f72014-11-18 10:55:16 +00006778#undef __
6779#undef QUICK_ENTRY_POINT
6780
Vladimir Markoca1e0382018-04-11 09:58:41 +00006781#define __ assembler.GetVIXLAssembler()->
6782
6783static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6784 vixl::aarch64::Register base_reg,
6785 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006786 vixl::aarch64::Label* slow_path,
6787 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006788 // Load the lock word containing the rb_state.
6789 __ Ldr(ip0.W(), lock_word);
6790 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6791 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6792 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6793 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6794 static_assert(
6795 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6796 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006797 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6798 if (throw_npe != nullptr) {
6799 __ Bind(throw_npe);
6800 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006801 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6802 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6803 "Field LDR must be 1 instruction (4B) before the return address label; "
6804 " 2 instructions (8B) for heap poisoning.");
6805 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6806 // Introduce a dependency on the lock_word including rb_state,
6807 // to prevent load-load reordering, and without using
6808 // a memory barrier (which would be more expensive).
6809 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6810 __ Br(lr); // And return back to the function.
6811 // Note: The fake dependency is unnecessary for the slow path.
6812}
6813
6814// Load the read barrier introspection entrypoint in register `entrypoint`.
6815static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6816 vixl::aarch64::Register entrypoint) {
6817 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6818 DCHECK_EQ(ip0.GetCode(), 16u);
6819 const int32_t entry_point_offset =
6820 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6821 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6822}
6823
6824void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6825 uint32_t encoded_data,
6826 /*out*/ std::string* debug_name) {
6827 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6828 switch (kind) {
6829 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006830 auto base_reg =
6831 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6832 CheckValidReg(base_reg.GetCode());
6833 auto holder_reg =
6834 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6835 CheckValidReg(holder_reg.GetCode());
6836 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6837 temps.Exclude(ip0, ip1);
Vladimir Marko7a695052018-04-12 10:26:50 +01006838 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
6839 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
6840 // null-check the holder as we do not necessarily do that check before going to the thunk.
6841 vixl::aarch64::Label throw_npe_label;
6842 vixl::aarch64::Label* throw_npe = nullptr;
6843 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
6844 throw_npe = &throw_npe_label;
6845 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006846 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006847 // Check if the holder is gray and, if not, add fake dependency to the base register
6848 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6849 // to load the reference and call the entrypoint that performs further checks on the
6850 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006851 vixl::aarch64::Label slow_path;
6852 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006853 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006854 __ Bind(&slow_path);
6855 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6856 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6857 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6858 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6859 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6860 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6861 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006862 break;
6863 }
6864 case BakerReadBarrierKind::kArray: {
6865 auto base_reg =
6866 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6867 CheckValidReg(base_reg.GetCode());
6868 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6869 BakerReadBarrierSecondRegField::Decode(encoded_data));
6870 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6871 temps.Exclude(ip0, ip1);
6872 vixl::aarch64::Label slow_path;
6873 int32_t data_offset =
6874 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6875 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6876 DCHECK_LT(lock_word.GetOffset(), 0);
6877 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6878 __ Bind(&slow_path);
6879 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6880 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6881 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6882 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6883 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6884 // a switch case target based on the index register.
6885 __ Mov(ip0, base_reg); // Move the base register to ip0.
6886 __ Br(ip1); // Jump to the entrypoint's array switch case.
6887 break;
6888 }
6889 case BakerReadBarrierKind::kGcRoot: {
6890 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6891 // and it does not have a forwarding address), call the correct introspection entrypoint;
6892 // otherwise return the reference (or the extracted forwarding address).
6893 // There is no gray bit check for GC roots.
6894 auto root_reg =
6895 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6896 CheckValidReg(root_reg.GetCode());
6897 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6898 BakerReadBarrierSecondRegField::Decode(encoded_data));
6899 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6900 temps.Exclude(ip0, ip1);
6901 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6902 __ Cbz(root_reg, &return_label);
6903 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6904 __ Ldr(ip0.W(), lock_word);
6905 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6906 __ Bind(&return_label);
6907 __ Br(lr);
6908 __ Bind(&not_marked);
6909 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6910 __ B(&forwarding_address, mi);
6911 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6912 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6913 // art_quick_read_barrier_mark_introspection_gc_roots.
6914 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6915 __ Mov(ip0.W(), root_reg);
6916 __ Br(ip1);
6917 __ Bind(&forwarding_address);
6918 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6919 __ Br(lr);
6920 break;
6921 }
6922 default:
6923 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6924 UNREACHABLE();
6925 }
6926
6927 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
6928 std::ostringstream oss;
6929 oss << "BakerReadBarrierThunk";
6930 switch (kind) {
6931 case BakerReadBarrierKind::kField:
6932 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6933 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6934 break;
6935 case BakerReadBarrierKind::kArray:
6936 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6937 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6938 BakerReadBarrierSecondRegField::Decode(encoded_data));
6939 break;
6940 case BakerReadBarrierKind::kGcRoot:
6941 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6942 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6943 BakerReadBarrierSecondRegField::Decode(encoded_data));
6944 break;
6945 }
6946 *debug_name = oss.str();
6947 }
6948}
6949
6950#undef __
6951
Alexandre Rames5319def2014-10-23 10:03:10 +01006952} // namespace arm64
6953} // namespace art