blob: ad4b5cf339df2c8a346ba357c811f5f822d1cd82 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010030#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070031#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080032#include "intrinsics.h"
33#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "thread.h"
40#include "utils/arm64/assembler_arm64.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000045using vixl::ExactAssemblyScope;
46using vixl::CodeBufferCheckScope;
47using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49#ifdef __
50#error "ARM64 Codegen VIXL macro-assembler macro already defined."
51#endif
52
Alexandre Rames5319def2014-10-23 10:03:10 +010053namespace art {
54
Roland Levillain22ccc3a2015-11-24 13:10:05 +000055template<class MirrorType>
56class GcRoot;
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058namespace arm64 {
59
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::ARM64EncodableConstantOrRegister;
61using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::CPURegisterFrom;
63using helpers::DRegisterFrom;
64using helpers::FPRegisterFrom;
65using helpers::HeapOperand;
66using helpers::HeapOperandFrom;
67using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010071using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::LocationFrom;
75using helpers::OperandFromMemOperand;
76using helpers::OutputCPURegister;
77using helpers::OutputFPRegister;
78using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010079using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080080using helpers::RegisterFrom;
Aart Bik1f8d51b2018-02-15 10:42:37 -080081using helpers::SRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080082using helpers::StackOperandFrom;
83using helpers::VIXLRegCodeFromART;
84using helpers::WRegisterFrom;
85using helpers::XRegisterFrom;
86
Vladimir Markof3e0ee22015-12-17 15:23:13 +000087// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080088// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
89// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000090static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010091
Vladimir Markof4f2daa2017-03-20 18:26:59 +000092// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
93// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
94// For the Baker read barrier implementation using link-generated thunks we need to split
95// the offset explicitly.
96constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
97
98// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010099constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +0100100constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +0100101constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000102
103// Some instructions have special requirements for a temporary, for example
104// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
105// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
106// loads with large offsets need a fixed register to limit the number of link-time
107// thunks we generate. For these and similar cases, we want to reserve a specific
108// register that's neither callee-save nor an argument register. We choose x15.
109inline Location FixedTempLocation() {
110 return Location::RegisterLocation(x15.GetCode());
111}
112
Alexandre Rames5319def2014-10-23 10:03:10 +0100113inline Condition ARM64Condition(IfCondition cond) {
114 switch (cond) {
115 case kCondEQ: return eq;
116 case kCondNE: return ne;
117 case kCondLT: return lt;
118 case kCondLE: return le;
119 case kCondGT: return gt;
120 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700121 case kCondB: return lo;
122 case kCondBE: return ls;
123 case kCondA: return hi;
124 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100125 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000126 LOG(FATAL) << "Unreachable";
127 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100128}
129
Vladimir Markod6e069b2016-01-18 11:11:01 +0000130inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
131 // The ARM64 condition codes can express all the necessary branches, see the
132 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
133 // There is no dex instruction or HIR that would need the missing conditions
134 // "equal or unordered" or "not equal".
135 switch (cond) {
136 case kCondEQ: return eq;
137 case kCondNE: return ne /* unordered */;
138 case kCondLT: return gt_bias ? cc : lt /* unordered */;
139 case kCondLE: return gt_bias ? ls : le /* unordered */;
140 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
141 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
142 default:
143 LOG(FATAL) << "UNREACHABLE";
144 UNREACHABLE();
145 }
146}
147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000149 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
150 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
151 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100152 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000153 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000155 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100156 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000157 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100159 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000160 } else {
161 return LocationFrom(w0);
162 }
163}
164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100165Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000166 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100167}
168
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100169// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
170#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700171#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100172
Zheng Xuda403092015-04-24 17:35:39 +0800173// Calculate memory accessing operand for save/restore live registers.
174static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800176 int64_t spill_offset,
177 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
179 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
180 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800181 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100182 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800183 codegen->GetNumberOfFloatingPointRegisters()));
184
Vladimir Marko804b03f2016-09-14 16:26:36 +0100185 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100186 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
187 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800188
189 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
190 UseScratchRegisterScope temps(masm);
191
192 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100193 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
194 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800195 int64_t reg_size = kXRegSizeInBytes;
196 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
197 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100198 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800199 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
200 // If the offset does not fit in the instruction's immediate field, use an alternate register
201 // to compute the base address(float point registers spill base address).
202 Register new_base = temps.AcquireSameSizeAs(base);
203 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
204 base = new_base;
205 spill_offset = -core_spill_size;
206 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
207 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
208 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
209 }
210
211 if (is_save) {
212 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
213 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
214 } else {
215 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
216 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
217 }
218}
219
220void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800221 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
223 for (uint32_t i : LowToHighBits(core_spills)) {
224 // If the register holds an object, update the stack mask.
225 if (locations->RegisterContainsObject(i)) {
226 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800227 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
229 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
230 saved_core_stack_offsets_[i] = stack_offset;
231 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800232 }
233
Vladimir Marko804b03f2016-09-14 16:26:36 +0100234 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
235 for (uint32_t i : LowToHighBits(fp_spills)) {
236 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
237 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
238 saved_fpu_stack_offsets_[i] = stack_offset;
239 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800240 }
241
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800244 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
245}
246
247void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100248 SaveRestoreLiveRegistersHelper(codegen,
249 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800250 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
251}
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
254 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000255 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100258 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100260
Alexandre Rames5319def2014-10-23 10:03:10 +0100261 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000262 if (instruction_->CanThrowIntoCatchBlock()) {
263 // Live registers will be restored in the catch block if caught.
264 SaveLiveRegisters(codegen, instruction_->GetLocations());
265 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000266 // We're moving two locations to locations that could overlap, so we need a parallel
267 // move resolver.
268 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100269 codegen->EmitParallelMoves(locations->InAt(0),
270 LocationFrom(calling_convention.GetRegisterAt(0)),
271 DataType::Type::kInt32,
272 locations->InAt(1),
273 LocationFrom(calling_convention.GetRegisterAt(1)),
274 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000275 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
276 ? kQuickThrowStringBounds
277 : kQuickThrowArrayBounds;
278 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100279 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800280 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 }
282
Alexandre Rames8158f282015-08-07 10:26:17 +0100283 bool IsFatal() const OVERRIDE { return true; }
284
Alexandre Rames9931f312015-06-19 14:47:01 +0100285 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
286
Alexandre Rames5319def2014-10-23 10:03:10 +0100287 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100288 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
289};
290
Alexandre Rames67555f72014-11-18 10:55:16 +0000291class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
292 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000293 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000294
295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
296 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
297 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000298 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 }
301
Alexandre Rames8158f282015-08-07 10:26:17 +0100302 bool IsFatal() const OVERRIDE { return true; }
303
Alexandre Rames9931f312015-06-19 14:47:01 +0100304 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
305
Alexandre Rames67555f72014-11-18 10:55:16 +0000306 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
308};
309
310class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
311 public:
312 LoadClassSlowPathARM64(HLoadClass* cls,
313 HInstruction* at,
314 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000315 bool do_clinit)
Vladimir Markoea4c1262017-02-06 19:59:33 +0000316 : SlowPathCodeARM64(at),
317 cls_(cls),
318 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000319 do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000320 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
321 }
322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000324 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000325 Location out = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
327
328 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000329 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000330
Vladimir Markof3c52b42017-11-17 17:32:12 +0000331 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000332 dex::TypeIndex type_index = cls_->GetTypeIndex();
333 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000334 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
335 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000336 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800337 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100338 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800339 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100340 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800341 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000342
343 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000344 if (out.IsValid()) {
345 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100346 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000347 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000348 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000349 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 __ B(GetExitLabel());
351 }
352
Alexandre Rames9931f312015-06-19 14:47:01 +0100353 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
354
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 private:
356 // The class this slow path will load.
357 HLoadClass* const cls_;
358
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 // The dex PC of `at_`.
360 const uint32_t dex_pc_;
361
362 // Whether to initialize the class.
363 const bool do_clinit_;
364
365 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
366};
367
Vladimir Markoaad75c62016-10-03 08:46:48 +0000368class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
369 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000370 explicit LoadStringSlowPathARM64(HLoadString* instruction)
371 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372
373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 LocationSummary* locations = instruction_->GetLocations();
375 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
376 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
377
378 __ Bind(GetEntryLabel());
379 SaveLiveRegisters(codegen, locations);
380
Vladimir Markof3c52b42017-11-17 17:32:12 +0000381 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000382 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
383 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000384 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
385 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100386 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000387 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
388
389 RestoreLiveRegisters(codegen, locations);
390
Vladimir Markoaad75c62016-10-03 08:46:48 +0000391 __ B(GetExitLabel());
392 }
393
394 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
395
396 private:
397 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
398};
399
Alexandre Rames5319def2014-10-23 10:03:10 +0100400class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
401 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000402 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100403
Alexandre Rames67555f72014-11-18 10:55:16 +0000404 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
405 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100406 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000407 if (instruction_->CanThrowIntoCatchBlock()) {
408 // Live registers will be restored in the catch block if caught.
409 SaveLiveRegisters(codegen, instruction_->GetLocations());
410 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000411 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
412 instruction_,
413 instruction_->GetDexPc(),
414 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800415 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100416 }
417
Alexandre Rames8158f282015-08-07 10:26:17 +0100418 bool IsFatal() const OVERRIDE { return true; }
419
Alexandre Rames9931f312015-06-19 14:47:01 +0100420 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
421
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100423 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
424};
425
426class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
427 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100428 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000429 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100430
Alexandre Rames67555f72014-11-18 10:55:16 +0000431 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100432 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000433 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100434 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100435 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000436 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800437 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100438 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000439 if (successor_ == nullptr) {
440 __ B(GetReturnLabel());
441 } else {
442 __ B(arm64_codegen->GetLabelOf(successor_));
443 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100444 }
445
Scott Wakeling97c72b72016-06-24 16:19:36 +0100446 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100447 DCHECK(successor_ == nullptr);
448 return &return_label_;
449 }
450
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100451 HBasicBlock* GetSuccessor() const {
452 return successor_;
453 }
454
Alexandre Rames9931f312015-06-19 14:47:01 +0100455 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
456
Alexandre Rames5319def2014-10-23 10:03:10 +0100457 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100458 // If not null, the block to branch to after the suspend check.
459 HBasicBlock* const successor_;
460
461 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100462 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100463
464 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
465};
466
Alexandre Rames67555f72014-11-18 10:55:16 +0000467class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
468 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000470 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000471
472 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800474
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475 DCHECK(instruction_->IsCheckCast()
476 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
477 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100478 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479
Alexandre Rames67555f72014-11-18 10:55:16 +0000480 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481
Vladimir Marko87584542017-12-12 17:47:52 +0000482 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483 SaveLiveRegisters(codegen, locations);
484 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
486 // We're moving two locations to locations that could overlap, so we need a parallel
487 // move resolver.
488 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800489 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800490 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100491 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800492 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800493 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100494 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000495 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800497 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100498 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000499 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
500 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
501 } else {
502 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800503 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
504 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000505 }
506
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000507 if (!is_fatal_) {
508 RestoreLiveRegisters(codegen, locations);
509 __ B(GetExitLabel());
510 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000511 }
512
Alexandre Rames9931f312015-06-19 14:47:01 +0100513 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100514 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100515
Alexandre Rames67555f72014-11-18 10:55:16 +0000516 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000517 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000518
Alexandre Rames67555f72014-11-18 10:55:16 +0000519 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
520};
521
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700522class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
523 public:
Aart Bik42249c32016-01-07 15:33:50 -0800524 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000525 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700526
527 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800528 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700529 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100530 LocationSummary* locations = instruction_->GetLocations();
531 SaveLiveRegisters(codegen, locations);
532 InvokeRuntimeCallingConvention calling_convention;
533 __ Mov(calling_convention.GetRegisterAt(0),
534 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000535 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100536 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700537 }
538
Alexandre Rames9931f312015-06-19 14:47:01 +0100539 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
540
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700541 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700542 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
543};
544
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100545class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
546 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000547 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100548
549 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
550 LocationSummary* locations = instruction_->GetLocations();
551 __ Bind(GetEntryLabel());
552 SaveLiveRegisters(codegen, locations);
553
554 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100555 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100556 parallel_move.AddMove(
557 locations->InAt(0),
558 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100559 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100560 nullptr);
561 parallel_move.AddMove(
562 locations->InAt(1),
563 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100564 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100565 nullptr);
566 parallel_move.AddMove(
567 locations->InAt(2),
568 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100569 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100570 nullptr);
571 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
572
573 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000574 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100575 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
576 RestoreLiveRegisters(codegen, locations);
577 __ B(GetExitLabel());
578 }
579
580 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
581
582 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100583 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
584};
585
Zheng Xu3927c8b2015-11-18 17:46:25 +0800586void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
587 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000588 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800589
590 // We are about to use the assembler to place literals directly. Make sure we have enough
591 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000592 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
593 num_entries * sizeof(int32_t),
594 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800595
596 __ Bind(&table_start_);
597 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
598 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100599 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800600 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
603 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
604 Literal<int32_t> literal(jump_offset);
605 __ place(&literal);
606 }
607}
608
Roland Levillain54f869e2017-03-06 13:54:11 +0000609// Abstract base class for read barrier slow paths marking a reference
610// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000611//
Roland Levillain54f869e2017-03-06 13:54:11 +0000612// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100613// barrier marking runtime entry point to be invoked or an empty
614// location; in the latter case, the read barrier marking runtime
615// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000616class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
617 protected:
618 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
619 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000620 DCHECK(kEmitCompilerReadBarrier);
621 }
622
Roland Levillain54f869e2017-03-06 13:54:11 +0000623 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000624
Roland Levillain54f869e2017-03-06 13:54:11 +0000625 // Generate assembly code calling the read barrier marking runtime
626 // entry point (ReadBarrierMarkRegX).
627 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000628 // No need to save live registers; it's taken care of by the
629 // entrypoint. Also, there is no need to update the stack mask,
630 // as this runtime call will not trigger a garbage collection.
631 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
632 DCHECK_NE(ref_.reg(), LR);
633 DCHECK_NE(ref_.reg(), WSP);
634 DCHECK_NE(ref_.reg(), WZR);
635 // IP0 is used internally by the ReadBarrierMarkRegX entry point
636 // as a temporary, it cannot be the entry point's input/output.
637 DCHECK_NE(ref_.reg(), IP0);
638 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
639 // "Compact" slow path, saving two moves.
640 //
641 // Instead of using the standard runtime calling convention (input
642 // and output in W0):
643 //
644 // W0 <- ref
645 // W0 <- ReadBarrierMark(W0)
646 // ref <- W0
647 //
648 // we just use rX (the register containing `ref`) as input and output
649 // of a dedicated entrypoint:
650 //
651 // rX <- ReadBarrierMarkRegX(rX)
652 //
653 if (entrypoint_.IsValid()) {
654 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
655 __ Blr(XRegisterFrom(entrypoint_));
656 } else {
657 // Entrypoint is not already loaded, load from the thread.
658 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100659 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000660 // This runtime call does not require a stack map.
661 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
662 }
663 }
664
665 // The location (register) of the marked object reference.
666 const Location ref_;
667
668 // The location of the entrypoint if it is already loaded.
669 const Location entrypoint_;
670
Roland Levillain54f869e2017-03-06 13:54:11 +0000671 private:
672 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
673};
674
Alexandre Rames5319def2014-10-23 10:03:10 +0100675// Slow path marking an object reference `ref` during a read
676// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000677// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100678//
679// This means that after the execution of this slow path, `ref` will
680// always be up-to-date, but `obj.field` may not; i.e., after the
681// flip, `ref` will be a to-space reference, but `obj.field` will
682// probably still be a from-space reference (unless it gets updated by
683// another thread, or if another thread installed another object
684// reference (different from `ref`) in `obj.field`).
685//
Roland Levillain97c46462017-05-11 14:04:03 +0100686// Argument `entrypoint` must be a register location holding the read
687// barrier marking runtime entry point to be invoked or an empty
688// location; in the latter case, the read barrier marking runtime
689// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000690class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 public:
692 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
693 Location ref,
694 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000695 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100696 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100697 }
698
699 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
700
701 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000702 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100703 DCHECK(locations->CanCall());
704 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000705 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000706 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
707 << "Unexpected instruction in read barrier marking slow path: "
708 << instruction_->DebugName();
709
710 __ Bind(GetEntryLabel());
711 GenerateReadBarrierMarkRuntimeCall(codegen);
712 __ B(GetExitLabel());
713 }
714
715 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000716 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
717};
718
Roland Levillain54f869e2017-03-06 13:54:11 +0000719// Slow path loading `obj`'s lock word, loading a reference from
720// object `*(obj + offset + (index << scale_factor))` into `ref`, and
721// marking `ref` if `obj` is gray according to the lock word (Baker
722// read barrier). The field `obj.field` in the object `obj` holding
723// this reference does not get updated by this slow path after marking
724// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
725// below for that).
726//
727// This means that after the execution of this slow path, `ref` will
728// always be up-to-date, but `obj.field` may not; i.e., after the
729// flip, `ref` will be a to-space reference, but `obj.field` will
730// probably still be a from-space reference (unless it gets updated by
731// another thread, or if another thread installed another object
732// reference (different from `ref`) in `obj.field`).
733//
734// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100735// barrier marking runtime entry point to be invoked or an empty
736// location; in the latter case, the read barrier marking runtime
737// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000738class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
739 public:
740 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
741 Location ref,
742 Register obj,
743 uint32_t offset,
744 Location index,
745 size_t scale_factor,
746 bool needs_null_check,
747 bool use_load_acquire,
748 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100749 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000750 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
751 obj_(obj),
752 offset_(offset),
753 index_(index),
754 scale_factor_(scale_factor),
755 needs_null_check_(needs_null_check),
756 use_load_acquire_(use_load_acquire),
757 temp_(temp) {
758 DCHECK(kEmitCompilerReadBarrier);
759 DCHECK(kUseBakerReadBarrier);
760 }
761
762 const char* GetDescription() const OVERRIDE {
763 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
764 }
765
766 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
767 LocationSummary* locations = instruction_->GetLocations();
768 DCHECK(locations->CanCall());
769 DCHECK(ref_.IsRegister()) << ref_;
770 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
771 DCHECK(obj_.IsW());
772 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100773 DCHECK(instruction_->IsInstanceFieldGet() ||
774 instruction_->IsStaticFieldGet() ||
775 instruction_->IsArrayGet() ||
776 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100777 instruction_->IsInstanceOf() ||
778 instruction_->IsCheckCast() ||
779 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
780 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
781 << "Unexpected instruction in read barrier marking slow path: "
782 << instruction_->DebugName();
783 // The read barrier instrumentation of object ArrayGet
784 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000785 // instruction.
786 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000787 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
788
Roland Levillain54f869e2017-03-06 13:54:11 +0000789 // Temporary register `temp_`, used to store the lock word, must
790 // not be IP0 nor IP1, as we may use them to emit the reference
791 // load (in the call to GenerateRawReferenceLoad below), and we
792 // need the lock word to still be in `temp_` after the reference
793 // load.
794 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
795 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
796
Alexandre Rames5319def2014-10-23 10:03:10 +0100797 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000798
799 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
800 // inserted after the original load. However, in fast path based
801 // Baker's read barriers, we need to perform the load of
802 // mirror::Object::monitor_ *before* the original reference load.
803 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000804 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100805 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000806 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
807 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
808 // HeapReference<mirror::Object> ref = *src; // Original reference load.
809 // bool is_gray = (rb_state == ReadBarrier::GrayState());
810 // if (is_gray) {
811 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
812 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000813 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000814 // Note: the original implementation in ReadBarrier::Barrier is
815 // slightly more complex as it performs additional checks that we do
816 // not do here for performance reasons.
817
818 // /* int32_t */ monitor = obj->monitor_
819 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
820 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
821 if (needs_null_check_) {
822 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100823 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000824 // /* LockWord */ lock_word = LockWord(monitor)
825 static_assert(sizeof(LockWord) == sizeof(int32_t),
826 "art::LockWord and int32_t have different sizes.");
827
828 // Introduce a dependency on the lock_word including rb_state,
829 // to prevent load-load reordering, and without using
830 // a memory barrier (which would be more expensive).
831 // `obj` is unchanged by this operation, but its value now depends
832 // on `temp`.
833 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
834
835 // The actual reference load.
836 // A possible implicit null check has already been handled above.
837 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
838 arm64_codegen->GenerateRawReferenceLoad(instruction_,
839 ref_,
840 obj_,
841 offset_,
842 index_,
843 scale_factor_,
844 /* needs_null_check */ false,
845 use_load_acquire_);
846
847 // Mark the object `ref` when `obj` is gray.
848 //
849 // if (rb_state == ReadBarrier::GrayState())
850 // ref = ReadBarrier::Mark(ref);
851 //
852 // Given the numeric representation, it's enough to check the low bit of the rb_state.
853 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
854 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
855 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
856 GenerateReadBarrierMarkRuntimeCall(codegen);
857
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000858 __ B(GetExitLabel());
859 }
860
861 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000862 // The register containing the object holding the marked object reference field.
863 Register obj_;
864 // The offset, index and scale factor to access the reference in `obj_`.
865 uint32_t offset_;
866 Location index_;
867 size_t scale_factor_;
868 // Is a null check required?
869 bool needs_null_check_;
870 // Should this reference load use Load-Acquire semantics?
871 bool use_load_acquire_;
872 // A temporary register used to hold the lock word of `obj_`.
873 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000874
Roland Levillain54f869e2017-03-06 13:54:11 +0000875 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000876};
877
Roland Levillain54f869e2017-03-06 13:54:11 +0000878// Slow path loading `obj`'s lock word, loading a reference from
879// object `*(obj + offset + (index << scale_factor))` into `ref`, and
880// marking `ref` if `obj` is gray according to the lock word (Baker
881// read barrier). If needed, this slow path also atomically updates
882// the field `obj.field` in the object `obj` holding this reference
883// after marking (contrary to
884// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
885// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100886//
887// This means that after the execution of this slow path, both `ref`
888// and `obj.field` will be up-to-date; i.e., after the flip, both will
889// hold the same to-space reference (unless another thread installed
890// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000891//
Roland Levillain54f869e2017-03-06 13:54:11 +0000892// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100893// barrier marking runtime entry point to be invoked or an empty
894// location; in the latter case, the read barrier marking runtime
895// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000896class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
897 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100898 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100899 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
900 HInstruction* instruction,
901 Location ref,
902 Register obj,
903 uint32_t offset,
904 Location index,
905 size_t scale_factor,
906 bool needs_null_check,
907 bool use_load_acquire,
908 Register temp,
909 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000910 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100911 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000912 offset_(offset),
913 index_(index),
914 scale_factor_(scale_factor),
915 needs_null_check_(needs_null_check),
916 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000917 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100918 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000919 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100920 }
921
922 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000923 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100924 }
925
926 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
927 LocationSummary* locations = instruction_->GetLocations();
928 Register ref_reg = WRegisterFrom(ref_);
929 DCHECK(locations->CanCall());
930 DCHECK(ref_.IsRegister()) << ref_;
931 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000932 DCHECK(obj_.IsW());
933 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
934
935 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100936 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
937 << "Unexpected instruction in read barrier marking and field updating slow path: "
938 << instruction_->DebugName();
939 DCHECK(instruction_->GetLocations()->Intrinsified());
940 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000941 DCHECK_EQ(offset_, 0u);
942 DCHECK_EQ(scale_factor_, 0u);
943 DCHECK_EQ(use_load_acquire_, false);
944 // The location of the offset of the marked reference field within `obj_`.
945 Location field_offset = index_;
946 DCHECK(field_offset.IsRegister()) << field_offset;
947
948 // Temporary register `temp_`, used to store the lock word, must
949 // not be IP0 nor IP1, as we may use them to emit the reference
950 // load (in the call to GenerateRawReferenceLoad below), and we
951 // need the lock word to still be in `temp_` after the reference
952 // load.
953 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
954 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100955
956 __ Bind(GetEntryLabel());
957
Roland Levillainff487002017-03-07 16:50:01 +0000958 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
959 //
960 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
961 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
962 // HeapReference<mirror::Object> ref = *src; // Original reference load.
963 // bool is_gray = (rb_state == ReadBarrier::GrayState());
964 // if (is_gray) {
965 // old_ref = ref;
966 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
967 // compareAndSwapObject(obj, field_offset, old_ref, ref);
968 // }
969
Roland Levillain54f869e2017-03-06 13:54:11 +0000970 // /* int32_t */ monitor = obj->monitor_
971 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
972 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
973 if (needs_null_check_) {
974 codegen->MaybeRecordImplicitNullCheck(instruction_);
975 }
976 // /* LockWord */ lock_word = LockWord(monitor)
977 static_assert(sizeof(LockWord) == sizeof(int32_t),
978 "art::LockWord and int32_t have different sizes.");
979
980 // Introduce a dependency on the lock_word including rb_state,
981 // to prevent load-load reordering, and without using
982 // a memory barrier (which would be more expensive).
983 // `obj` is unchanged by this operation, but its value now depends
984 // on `temp`.
985 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
986
987 // The actual reference load.
988 // A possible implicit null check has already been handled above.
989 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
990 arm64_codegen->GenerateRawReferenceLoad(instruction_,
991 ref_,
992 obj_,
993 offset_,
994 index_,
995 scale_factor_,
996 /* needs_null_check */ false,
997 use_load_acquire_);
998
999 // Mark the object `ref` when `obj` is gray.
1000 //
1001 // if (rb_state == ReadBarrier::GrayState())
1002 // ref = ReadBarrier::Mark(ref);
1003 //
1004 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1005 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1006 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1007 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1008
1009 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001010 // Note that we cannot use IP to save the old reference, as IP is
1011 // used internally by the ReadBarrierMarkRegX entry point, and we
1012 // need the old reference after the call to that entry point.
1013 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1014 __ Mov(temp_.W(), ref_reg);
1015
Roland Levillain54f869e2017-03-06 13:54:11 +00001016 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001017
1018 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001019 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001020 //
1021 // Note that this field could also hold a different object, if
1022 // another thread had concurrently changed it. In that case, the
1023 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1024 // (CAS) operation below would abort the CAS, leaving the field
1025 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001026 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001027 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001028
1029 // Update the the holder's field atomically. This may fail if
1030 // mutator updates before us, but it's OK. This is achieved
1031 // using a strong compare-and-set (CAS) operation with relaxed
1032 // memory synchronization ordering, where the expected value is
1033 // the old reference and the desired value is the new reference.
1034
1035 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1036 UseScratchRegisterScope temps(masm);
1037
1038 // Convenience aliases.
1039 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001040 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001041 Register expected = temp_.W();
1042 Register value = ref_reg;
1043 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1044 Register tmp_value = temps.AcquireW(); // Value in memory.
1045
1046 __ Add(tmp_ptr, base.X(), Operand(offset));
1047
1048 if (kPoisonHeapReferences) {
1049 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1050 if (value.Is(expected)) {
1051 // Do not poison `value`, as it is the same register as
1052 // `expected`, which has just been poisoned.
1053 } else {
1054 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1055 }
1056 }
1057
1058 // do {
1059 // tmp_value = [tmp_ptr] - expected;
1060 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1061
Roland Levillain24a4d112016-10-26 13:10:46 +01001062 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001063 __ Bind(&loop_head);
1064 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1065 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001066 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001067 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1068 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001069 __ B(&exit_loop);
1070 __ Bind(&comparison_failed);
1071 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001072 __ Bind(&exit_loop);
1073
1074 if (kPoisonHeapReferences) {
1075 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1076 if (value.Is(expected)) {
1077 // Do not unpoison `value`, as it is the same register as
1078 // `expected`, which has just been unpoisoned.
1079 } else {
1080 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1081 }
1082 }
1083
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001084 __ B(GetExitLabel());
1085 }
1086
1087 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001088 // The register containing the object holding the marked object reference field.
1089 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001090 // The offset, index and scale factor to access the reference in `obj_`.
1091 uint32_t offset_;
1092 Location index_;
1093 size_t scale_factor_;
1094 // Is a null check required?
1095 bool needs_null_check_;
1096 // Should this reference load use Load-Acquire semantics?
1097 bool use_load_acquire_;
1098 // A temporary register used to hold the lock word of `obj_`; and
1099 // also to hold the original reference value, when the reference is
1100 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001101 const Register temp_;
1102
Roland Levillain54f869e2017-03-06 13:54:11 +00001103 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001104};
1105
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001106// Slow path generating a read barrier for a heap reference.
1107class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1108 public:
1109 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1110 Location out,
1111 Location ref,
1112 Location obj,
1113 uint32_t offset,
1114 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001115 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001116 out_(out),
1117 ref_(ref),
1118 obj_(obj),
1119 offset_(offset),
1120 index_(index) {
1121 DCHECK(kEmitCompilerReadBarrier);
1122 // If `obj` is equal to `out` or `ref`, it means the initial object
1123 // has been overwritten by (or after) the heap object reference load
1124 // to be instrumented, e.g.:
1125 //
1126 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001127 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001128 //
1129 // In that case, we have lost the information about the original
1130 // object, and the emitted read barrier cannot work properly.
1131 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1132 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1133 }
1134
1135 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1136 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1137 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001139 DCHECK(locations->CanCall());
1140 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001141 DCHECK(instruction_->IsInstanceFieldGet() ||
1142 instruction_->IsStaticFieldGet() ||
1143 instruction_->IsArrayGet() ||
1144 instruction_->IsInstanceOf() ||
1145 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001146 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001147 << "Unexpected instruction in read barrier for heap reference slow path: "
1148 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001149 // The read barrier instrumentation of object ArrayGet
1150 // instructions does not support the HIntermediateAddress
1151 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001152 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001153 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001154
1155 __ Bind(GetEntryLabel());
1156
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001157 SaveLiveRegisters(codegen, locations);
1158
1159 // We may have to change the index's value, but as `index_` is a
1160 // constant member (like other "inputs" of this slow path),
1161 // introduce a copy of it, `index`.
1162 Location index = index_;
1163 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001164 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001165 if (instruction_->IsArrayGet()) {
1166 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001167 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1169 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1170 // We are about to change the value of `index_reg` (see the
1171 // calls to vixl::MacroAssembler::Lsl and
1172 // vixl::MacroAssembler::Mov below), but it has
1173 // not been saved by the previous call to
1174 // art::SlowPathCode::SaveLiveRegisters, as it is a
1175 // callee-save register --
1176 // art::SlowPathCode::SaveLiveRegisters does not consider
1177 // callee-save registers, as it has been designed with the
1178 // assumption that callee-save registers are supposed to be
1179 // handled by the called function. So, as a callee-save
1180 // register, `index_reg` _would_ eventually be saved onto
1181 // the stack, but it would be too late: we would have
1182 // changed its value earlier. Therefore, we manually save
1183 // it here into another freely available register,
1184 // `free_reg`, chosen of course among the caller-save
1185 // registers (as a callee-save `free_reg` register would
1186 // exhibit the same problem).
1187 //
1188 // Note we could have requested a temporary register from
1189 // the register allocator instead; but we prefer not to, as
1190 // this is a slow path, and we know we can find a
1191 // caller-save register that is available.
1192 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1193 __ Mov(free_reg.W(), index_reg);
1194 index_reg = free_reg;
1195 index = LocationFrom(index_reg);
1196 } else {
1197 // The initial register stored in `index_` has already been
1198 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1199 // (as it is not a callee-save register), so we can freely
1200 // use it.
1201 }
1202 // Shifting the index value contained in `index_reg` by the scale
1203 // factor (2) cannot overflow in practice, as the runtime is
1204 // unable to allocate object arrays with a size larger than
1205 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001207 static_assert(
1208 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1209 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1210 __ Add(index_reg, index_reg, Operand(offset_));
1211 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001212 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1213 // intrinsics, `index_` is not shifted by a scale factor of 2
1214 // (as in the case of ArrayGet), as it is actually an offset
1215 // to an object field within an object.
1216 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001217 DCHECK(instruction_->GetLocations()->Intrinsified());
1218 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1219 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1220 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001221 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001222 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001223 }
1224 }
1225
1226 // We're moving two or three locations to locations that could
1227 // overlap, so we need a parallel move resolver.
1228 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001229 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001230 parallel_move.AddMove(ref_,
1231 LocationFrom(calling_convention.GetRegisterAt(0)),
1232 type,
1233 nullptr);
1234 parallel_move.AddMove(obj_,
1235 LocationFrom(calling_convention.GetRegisterAt(1)),
1236 type,
1237 nullptr);
1238 if (index.IsValid()) {
1239 parallel_move.AddMove(index,
1240 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001241 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001242 nullptr);
1243 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1244 } else {
1245 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1246 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1247 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001248 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001249 instruction_,
1250 instruction_->GetDexPc(),
1251 this);
1252 CheckEntrypointTypes<
1253 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1254 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1255
1256 RestoreLiveRegisters(codegen, locations);
1257
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001258 __ B(GetExitLabel());
1259 }
1260
1261 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1262
1263 private:
1264 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001265 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1266 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001267 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1268 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1269 return Register(VIXLRegCodeFromART(i), kXRegSize);
1270 }
1271 }
1272 // We shall never fail to find a free caller-save register, as
1273 // there are more than two core caller-save registers on ARM64
1274 // (meaning it is possible to find one which is different from
1275 // `ref` and `obj`).
1276 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1277 LOG(FATAL) << "Could not find a free register";
1278 UNREACHABLE();
1279 }
1280
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001281 const Location out_;
1282 const Location ref_;
1283 const Location obj_;
1284 const uint32_t offset_;
1285 // An additional location containing an index to an array.
1286 // Only used for HArrayGet and the UnsafeGetObject &
1287 // UnsafeGetObjectVolatile intrinsics.
1288 const Location index_;
1289
1290 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1291};
1292
1293// Slow path generating a read barrier for a GC root.
1294class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1295 public:
1296 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001297 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001298 DCHECK(kEmitCompilerReadBarrier);
1299 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001300
1301 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1302 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001303 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001304 DCHECK(locations->CanCall());
1305 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001306 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1307 << "Unexpected instruction in read barrier for GC root slow path: "
1308 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001309
1310 __ Bind(GetEntryLabel());
1311 SaveLiveRegisters(codegen, locations);
1312
1313 InvokeRuntimeCallingConvention calling_convention;
1314 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1315 // The argument of the ReadBarrierForRootSlow is not a managed
1316 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1317 // thus we need a 64-bit move here, and we cannot use
1318 //
1319 // arm64_codegen->MoveLocation(
1320 // LocationFrom(calling_convention.GetRegisterAt(0)),
1321 // root_,
1322 // type);
1323 //
1324 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001325 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001326 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001327 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001328 instruction_,
1329 instruction_->GetDexPc(),
1330 this);
1331 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1332 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1333
1334 RestoreLiveRegisters(codegen, locations);
1335 __ B(GetExitLabel());
1336 }
1337
1338 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1339
1340 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001341 const Location out_;
1342 const Location root_;
1343
1344 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1345};
1346
Alexandre Rames5319def2014-10-23 10:03:10 +01001347#undef __
1348
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001349Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001351 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 LOG(FATAL) << "Unreachable type " << type;
1353 }
1354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001355 if (DataType::IsFloatingPointType(type) &&
Alexandre Rames5319def2014-10-23 10:03:10 +01001356 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001357 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001358 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001359 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1360 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1361 } else {
1362 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001363 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1364 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001365 }
1366
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001367 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001368 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001369 return next_location;
1370}
1371
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001372Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001373 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001374}
1375
Serban Constantinescu579885a2015-02-22 20:51:33 +00001376CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1377 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001378 const CompilerOptions& compiler_options,
1379 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 : CodeGenerator(graph,
1381 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001382 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001383 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001384 callee_saved_core_registers.GetList(),
1385 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001386 compiler_options,
1387 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001388 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1389 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001390 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001391 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001392 move_resolver_(graph->GetAllocator(), this),
1393 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001394 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001395 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001396 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001397 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001398 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001399 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001400 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001401 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001402 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001403 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001404 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1405 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001406 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001407 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001408 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001409 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001410 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001411 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001412}
Alexandre Rames5319def2014-10-23 10:03:10 +01001413
Alexandre Rames67555f72014-11-18 10:55:16 +00001414#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001415
Zheng Xu3927c8b2015-11-18 17:46:25 +08001416void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001417 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001418 jump_table->EmitTable(this);
1419 }
1420}
1421
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001422void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001423 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001424 // Ensure we emit the literal pool.
1425 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001426
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001427 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001428
1429 // Verify Baker read barrier linker patches.
1430 if (kIsDebugBuild) {
1431 ArrayRef<const uint8_t> code = allocator->GetMemory();
1432 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
1433 DCHECK(info.label.IsBound());
1434 uint32_t literal_offset = info.label.GetLocation();
1435 DCHECK_ALIGNED(literal_offset, 4u);
1436
1437 auto GetInsn = [&code](uint32_t offset) {
1438 DCHECK_ALIGNED(offset, 4u);
1439 return
1440 (static_cast<uint32_t>(code[offset + 0]) << 0) +
1441 (static_cast<uint32_t>(code[offset + 1]) << 8) +
1442 (static_cast<uint32_t>(code[offset + 2]) << 16)+
1443 (static_cast<uint32_t>(code[offset + 3]) << 24);
1444 };
1445
1446 const uint32_t encoded_data = info.custom_data;
1447 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
1448 // Check that the next instruction matches the expected LDR.
1449 switch (kind) {
1450 case BakerReadBarrierKind::kField: {
1451 DCHECK_GE(code.size() - literal_offset, 8u);
1452 uint32_t next_insn = GetInsn(literal_offset + 4u);
1453 // LDR (immediate) with correct base_reg.
1454 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1455 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1456 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
1457 break;
1458 }
1459 case BakerReadBarrierKind::kArray: {
1460 DCHECK_GE(code.size() - literal_offset, 8u);
1461 uint32_t next_insn = GetInsn(literal_offset + 4u);
1462 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
1463 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
1464 CheckValidReg(next_insn & 0x1fu); // Check destination register.
1465 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1466 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
1467 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1468 break;
1469 }
1470 case BakerReadBarrierKind::kGcRoot: {
1471 DCHECK_GE(literal_offset, 4u);
1472 uint32_t prev_insn = GetInsn(literal_offset - 4u);
1473 // LDR (immediate) with correct root_reg.
1474 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
1475 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg);
1476 break;
1477 }
1478 default:
1479 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1480 UNREACHABLE();
1481 }
1482 }
1483 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001484}
1485
Zheng Xuad4450e2015-04-17 18:48:56 +08001486void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1487 // Note: There are 6 kinds of moves:
1488 // 1. constant -> GPR/FPR (non-cycle)
1489 // 2. constant -> stack (non-cycle)
1490 // 3. GPR/FPR -> GPR/FPR
1491 // 4. GPR/FPR -> stack
1492 // 5. stack -> GPR/FPR
1493 // 6. stack -> stack (non-cycle)
1494 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1495 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1496 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1497 // dependency.
1498 vixl_temps_.Open(GetVIXLAssembler());
1499}
1500
1501void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1502 vixl_temps_.Close();
1503}
1504
1505Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001506 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1507 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1508 || kind == Location::kSIMDStackSlot);
1509 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1510 ? Location::kFpuRegister
1511 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001512 Location scratch = GetScratchLocation(kind);
1513 if (!scratch.Equals(Location::NoLocation())) {
1514 return scratch;
1515 }
1516 // Allocate from VIXL temp registers.
1517 if (kind == Location::kRegister) {
1518 scratch = LocationFrom(vixl_temps_.AcquireX());
1519 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001520 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001521 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1522 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1523 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001524 }
1525 AddScratchLocation(scratch);
1526 return scratch;
1527}
1528
1529void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1530 if (loc.IsRegister()) {
1531 vixl_temps_.Release(XRegisterFrom(loc));
1532 } else {
1533 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001534 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001535 }
1536 RemoveScratchLocation(loc);
1537}
1538
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001540 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001541 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001542}
1543
Alexandre Rames5319def2014-10-23 10:03:10 +01001544void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001545 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001546 __ Bind(&frame_entry_label_);
1547
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001548 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1549 UseScratchRegisterScope temps(masm);
1550 Register temp = temps.AcquireX();
1551 __ Ldrh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1552 __ Add(temp, temp, 1);
1553 __ Strh(temp, MemOperand(kArtMethodRegister, ArtMethod::HotnessCountOffset().Int32Value()));
1554 }
1555
Vladimir Marko33bff252017-11-01 14:35:42 +00001556 bool do_overflow_check =
1557 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001558 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001559 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001560 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001561 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001562 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001563 {
1564 // Ensure that between load and RecordPcInfo there are no pools emitted.
1565 ExactAssemblyScope eas(GetVIXLAssembler(),
1566 kInstructionSize,
1567 CodeBufferCheckScope::kExactSize);
1568 __ ldr(wzr, MemOperand(temp, 0));
1569 RecordPcInfo(nullptr, 0);
1570 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001571 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001572
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001573 if (!HasEmptyFrame()) {
1574 int frame_size = GetFrameSize();
1575 // Stack layout:
1576 // sp[frame_size - 8] : lr.
1577 // ... : other preserved core registers.
1578 // ... : other preserved fp registers.
1579 // ... : reserved frame space.
1580 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001581
1582 // Save the current method if we need it. Note that we do not
1583 // do this in HCurrentMethod, as the instruction might have been removed
1584 // in the SSA graph.
1585 if (RequiresCurrentMethod()) {
1586 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001587 } else {
1588 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001589 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001590 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001591 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1592 frame_size - GetCoreSpillSize());
1593 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1594 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001595
1596 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1597 // Initialize should_deoptimize flag to 0.
1598 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1599 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1600 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001601 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001602
1603 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001604}
1605
1606void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001607 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001608 if (!HasEmptyFrame()) {
1609 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001610 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1611 frame_size - FrameEntrySpillSize());
1612 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1613 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001614 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001615 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001616 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001617 __ Ret();
1618 GetAssembler()->cfi().RestoreState();
1619 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001620}
1621
Scott Wakeling97c72b72016-06-24 16:19:36 +01001622CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001623 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001624 return CPURegList(CPURegister::kRegister, kXRegSize,
1625 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001626}
1627
Scott Wakeling97c72b72016-06-24 16:19:36 +01001628CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001629 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1630 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001631 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1632 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001633}
1634
Alexandre Rames5319def2014-10-23 10:03:10 +01001635void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1636 __ Bind(GetLabelOf(block));
1637}
1638
Calin Juravle175dc732015-08-25 15:42:32 +01001639void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1640 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001641 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001642}
1643
Calin Juravlee460d1d2015-09-29 04:52:17 +01001644void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1645 if (location.IsRegister()) {
1646 locations->AddTemp(location);
1647 } else {
1648 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1649 }
1650}
1651
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001652void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001653 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001654 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001655 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001656 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001657 if (value_can_be_null) {
1658 __ Cbz(value, &done);
1659 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001660 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001661 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001662 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001663 if (value_can_be_null) {
1664 __ Bind(&done);
1665 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001666}
1667
David Brazdil58282f42016-01-14 12:45:10 +00001668void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001669 // Blocked core registers:
1670 // lr : Runtime reserved.
1671 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001672 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001673 // ip1 : VIXL core temp.
1674 // ip0 : VIXL core temp.
1675 //
1676 // Blocked fp registers:
1677 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001678 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1679 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001680 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001681 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001682 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001683
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001684 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001685 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001686 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001687 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001688
David Brazdil58282f42016-01-14 12:45:10 +00001689 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001690 // Stubs do not save callee-save floating point registers. If the graph
1691 // is debuggable, we need to deal with these registers differently. For
1692 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001693 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1694 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001695 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001696 }
1697 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001698}
1699
Alexandre Rames3e69f162014-12-10 10:36:50 +00001700size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1701 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1702 __ Str(reg, MemOperand(sp, stack_index));
1703 return kArm64WordSize;
1704}
1705
1706size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1707 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1708 __ Ldr(reg, MemOperand(sp, stack_index));
1709 return kArm64WordSize;
1710}
1711
1712size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1713 FPRegister reg = FPRegister(reg_id, kDRegSize);
1714 __ Str(reg, MemOperand(sp, stack_index));
1715 return kArm64WordSize;
1716}
1717
1718size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1719 FPRegister reg = FPRegister(reg_id, kDRegSize);
1720 __ Ldr(reg, MemOperand(sp, stack_index));
1721 return kArm64WordSize;
1722}
1723
Alexandre Rames5319def2014-10-23 10:03:10 +01001724void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001725 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001726}
1727
1728void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001729 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001730}
1731
Alexandre Rames67555f72014-11-18 10:55:16 +00001732void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001733 if (constant->IsIntConstant()) {
1734 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1735 } else if (constant->IsLongConstant()) {
1736 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1737 } else if (constant->IsNullConstant()) {
1738 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001739 } else if (constant->IsFloatConstant()) {
1740 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1741 } else {
1742 DCHECK(constant->IsDoubleConstant());
1743 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1744 }
1745}
1746
Alexandre Rames3e69f162014-12-10 10:36:50 +00001747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001748static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001749 DCHECK(constant.IsConstant());
1750 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001752 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001753 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1754 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1755 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1756 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001757}
1758
Roland Levillain952b2352017-05-03 19:49:14 +01001759// Allocate a scratch register from the VIXL pool, querying first
1760// the floating-point register pool, and then the core register
1761// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001762// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1763// using a different allocation strategy.
1764static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1765 vixl::aarch64::UseScratchRegisterScope* temps,
1766 int size_in_bits) {
1767 return masm->GetScratchFPRegisterList()->IsEmpty()
1768 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1769 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1770}
1771
Calin Juravlee460d1d2015-09-29 04:52:17 +01001772void CodeGeneratorARM64::MoveLocation(Location destination,
1773 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001774 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001775 if (source.Equals(destination)) {
1776 return;
1777 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001778
1779 // A valid move can always be inferred from the destination and source
1780 // locations. When moving from and to a register, the argument type can be
1781 // used to generate 32bit instead of 64bit moves. In debug mode we also
1782 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001783 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001784
1785 if (destination.IsRegister() || destination.IsFpuRegister()) {
1786 if (unspecified_type) {
1787 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1788 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001789 (src_cst != nullptr && (src_cst->IsIntConstant()
1790 || src_cst->IsFloatConstant()
1791 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001792 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001793 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001794 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001795 // If the source is a double stack slot or a 64bit constant, a 64bit
1796 // type is appropriate. Else the source is a register, and since the
1797 // type has not been specified, we chose a 64bit type to force a 64bit
1798 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001799 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001800 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1803 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001804 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001805 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1806 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1807 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001808 } else if (source.IsSIMDStackSlot()) {
1809 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001810 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001811 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001812 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001813 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001814 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001815 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001816 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001817 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 DataType::Type source_type = DataType::Is64BitType(dst_type)
1819 ? DataType::Type::kInt64
1820 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001821 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1822 }
1823 } else {
1824 DCHECK(source.IsFpuRegister());
1825 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 DataType::Type source_type = DataType::Is64BitType(dst_type)
1827 ? DataType::Type::kFloat64
1828 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001829 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1830 } else {
1831 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001832 if (GetGraph()->HasSIMD()) {
1833 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1834 } else {
1835 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1836 }
1837 }
1838 }
1839 } else if (destination.IsSIMDStackSlot()) {
1840 if (source.IsFpuRegister()) {
1841 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1842 } else {
1843 DCHECK(source.IsSIMDStackSlot());
1844 UseScratchRegisterScope temps(GetVIXLAssembler());
1845 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1846 Register temp = temps.AcquireX();
1847 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1848 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1849 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1850 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1851 } else {
1852 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1853 __ Ldr(temp, StackOperandFrom(source));
1854 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001855 }
1856 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001857 } else { // The destination is not a register. It must be a stack slot.
1858 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1859 if (source.IsRegister() || source.IsFpuRegister()) {
1860 if (unspecified_type) {
1861 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001862 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001863 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 dst_type =
1865 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001866 }
1867 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001868 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1869 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001870 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001871 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001872 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1873 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001874 UseScratchRegisterScope temps(GetVIXLAssembler());
1875 HConstant* src_cst = source.GetConstant();
1876 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001877 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001878 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1879 ? Register(xzr)
1880 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001881 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001882 if (src_cst->IsIntConstant()) {
1883 temp = temps.AcquireW();
1884 } else if (src_cst->IsLongConstant()) {
1885 temp = temps.AcquireX();
1886 } else if (src_cst->IsFloatConstant()) {
1887 temp = temps.AcquireS();
1888 } else {
1889 DCHECK(src_cst->IsDoubleConstant());
1890 temp = temps.AcquireD();
1891 }
1892 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001893 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001894 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001895 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001896 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001897 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001898 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001899 // Use any scratch register (a core or a floating-point one)
1900 // from VIXL scratch register pools as a temporary.
1901 //
1902 // We used to only use the FP scratch register pool, but in some
1903 // rare cases the only register from this pool (D31) would
1904 // already be used (e.g. within a ParallelMove instruction, when
1905 // a move is blocked by a another move requiring a scratch FP
1906 // register, which would reserve D31). To prevent this issue, we
1907 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001908 //
1909 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001910 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001911 // use AcquireFPOrCoreCPURegisterOfSize instead of
1912 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1913 // allocates core scratch registers first.
1914 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1915 GetVIXLAssembler(),
1916 &temps,
1917 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001918 __ Ldr(temp, StackOperandFrom(source));
1919 __ Str(temp, StackOperandFrom(destination));
1920 }
1921 }
1922}
1923
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001924void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001925 CPURegister dst,
1926 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001927 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001928 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001929 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001930 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001931 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001933 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001936 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001937 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001938 case DataType::Type::kInt16:
1939 __ Ldrsh(Register(dst), src);
1940 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 case DataType::Type::kInt32:
1942 case DataType::Type::kReference:
1943 case DataType::Type::kInt64:
1944 case DataType::Type::kFloat32:
1945 case DataType::Type::kFloat64:
1946 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001947 __ Ldr(dst, src);
1948 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001949 case DataType::Type::kUint32:
1950 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001952 LOG(FATAL) << "Unreachable type " << type;
1953 }
1954}
1955
Calin Juravle77520bc2015-01-12 18:45:46 +00001956void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001957 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001958 const MemOperand& src,
1959 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001960 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001961 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001962 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001963 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001964
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001965 DCHECK(!src.IsPreIndex());
1966 DCHECK(!src.IsPostIndex());
1967
1968 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001969 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001970 {
1971 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1972 MemOperand base = MemOperand(temp_base);
1973 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001974 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001975 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001977 {
1978 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1979 __ ldarb(Register(dst), base);
1980 if (needs_null_check) {
1981 MaybeRecordImplicitNullCheck(instruction);
1982 }
1983 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001984 if (type == DataType::Type::kInt8) {
1985 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001986 }
1987 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001988 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001990 {
1991 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1992 __ ldarh(Register(dst), base);
1993 if (needs_null_check) {
1994 MaybeRecordImplicitNullCheck(instruction);
1995 }
1996 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001997 if (type == DataType::Type::kInt16) {
1998 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1999 }
Artem Serov914d7a82017-02-07 14:33:49 +00002000 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002001 case DataType::Type::kInt32:
2002 case DataType::Type::kReference:
2003 case DataType::Type::kInt64:
2004 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002005 {
2006 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2007 __ ldar(Register(dst), base);
2008 if (needs_null_check) {
2009 MaybeRecordImplicitNullCheck(instruction);
2010 }
2011 }
2012 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 case DataType::Type::kFloat32:
2014 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00002015 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002017
Artem Serov914d7a82017-02-07 14:33:49 +00002018 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2019 {
2020 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2021 __ ldar(temp, base);
2022 if (needs_null_check) {
2023 MaybeRecordImplicitNullCheck(instruction);
2024 }
2025 }
2026 __ Fmov(FPRegister(dst), temp);
2027 break;
Roland Levillain44015862016-01-22 11:47:17 +00002028 }
Aart Bik66c158e2018-01-31 12:55:04 -08002029 case DataType::Type::kUint32:
2030 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002031 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00002032 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002033 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002034 }
2035}
2036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002037void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002038 CPURegister src,
2039 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002040 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002042 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002044 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002045 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 case DataType::Type::kUint16:
2047 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002048 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002049 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002050 case DataType::Type::kInt32:
2051 case DataType::Type::kReference:
2052 case DataType::Type::kInt64:
2053 case DataType::Type::kFloat32:
2054 case DataType::Type::kFloat64:
2055 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002056 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002057 break;
Aart Bik66c158e2018-01-31 12:55:04 -08002058 case DataType::Type::kUint32:
2059 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002060 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061 LOG(FATAL) << "Unreachable type " << type;
2062 }
2063}
2064
Artem Serov914d7a82017-02-07 14:33:49 +00002065void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002067 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002068 const MemOperand& dst,
2069 bool needs_null_check) {
2070 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002071 UseScratchRegisterScope temps(GetVIXLAssembler());
2072 Register temp_base = temps.AcquireX();
2073
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002074 DCHECK(!dst.IsPreIndex());
2075 DCHECK(!dst.IsPostIndex());
2076
2077 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002078 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002079 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002080 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002081 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002082 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002084 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002085 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002086 {
2087 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2088 __ stlrb(Register(src), base);
2089 if (needs_null_check) {
2090 MaybeRecordImplicitNullCheck(instruction);
2091 }
2092 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002093 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 case DataType::Type::kUint16:
2095 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002096 {
2097 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2098 __ stlrh(Register(src), base);
2099 if (needs_null_check) {
2100 MaybeRecordImplicitNullCheck(instruction);
2101 }
2102 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002103 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 case DataType::Type::kInt32:
2105 case DataType::Type::kReference:
2106 case DataType::Type::kInt64:
2107 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002108 {
2109 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2110 __ stlr(Register(src), base);
2111 if (needs_null_check) {
2112 MaybeRecordImplicitNullCheck(instruction);
2113 }
2114 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002115 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 case DataType::Type::kFloat32:
2117 case DataType::Type::kFloat64: {
2118 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002119 Register temp_src;
2120 if (src.IsZero()) {
2121 // The zero register is used to avoid synthesizing zero constants.
2122 temp_src = Register(src);
2123 } else {
2124 DCHECK(src.IsFPRegister());
2125 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2126 __ Fmov(temp_src, FPRegister(src));
2127 }
Artem Serov914d7a82017-02-07 14:33:49 +00002128 {
2129 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2130 __ stlr(temp_src, base);
2131 if (needs_null_check) {
2132 MaybeRecordImplicitNullCheck(instruction);
2133 }
2134 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002135 break;
2136 }
Aart Bik66c158e2018-01-31 12:55:04 -08002137 case DataType::Type::kUint32:
2138 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002139 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002140 LOG(FATAL) << "Unreachable type " << type;
2141 }
2142}
2143
Calin Juravle175dc732015-08-25 15:42:32 +01002144void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2145 HInstruction* instruction,
2146 uint32_t dex_pc,
2147 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002148 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002149
2150 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2151 {
2152 // Ensure the pc position is recorded immediately after the `blr` instruction.
2153 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2154 __ blr(lr);
2155 if (EntrypointRequiresStackMap(entrypoint)) {
2156 RecordPcInfo(instruction, dex_pc, slow_path);
2157 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002158 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002159}
2160
Roland Levillaindec8f632016-07-22 17:10:06 +01002161void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2162 HInstruction* instruction,
2163 SlowPathCode* slow_path) {
2164 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002165 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2166 __ Blr(lr);
2167}
2168
Alexandre Rames67555f72014-11-18 10:55:16 +00002169void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002170 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002171 UseScratchRegisterScope temps(GetVIXLAssembler());
2172 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00002173 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2174 const size_t status_byte_offset =
2175 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2176 constexpr uint32_t shifted_initialized_value =
2177 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002178
Serban Constantinescu02164b32014-11-13 14:05:07 +00002179 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002180 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Vladimir Markodc682aa2018-01-04 18:42:57 +00002181 __ Add(temp, class_reg, status_byte_offset);
Igor Murashkin86083f72017-10-27 10:59:04 -07002182 __ Ldarb(temp, HeapOperand(temp));
Vladimir Markodc682aa2018-01-04 18:42:57 +00002183 __ Cmp(temp, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00002184 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002185 __ Bind(slow_path->GetExitLabel());
2186}
Alexandre Rames5319def2014-10-23 10:03:10 +01002187
Vladimir Marko175e7862018-03-27 09:03:13 +00002188void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
2189 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
2190 uint32_t path_to_root = check->GetBitstringPathToRoot();
2191 uint32_t mask = check->GetBitstringMask();
2192 DCHECK(IsPowerOfTwo(mask + 1));
2193 size_t mask_bits = WhichPowerOf2(mask + 1);
2194
2195 if (mask_bits == 16u) {
2196 // Load only the bitstring part of the status word.
2197 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2198 } else {
2199 // /* uint32_t */ temp = temp->status_
2200 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
2201 // Extract the bitstring bits.
2202 __ Ubfx(temp, temp, 0, mask_bits);
2203 }
2204 // Compare the bitstring bits to `path_to_root`.
2205 __ Cmp(temp, path_to_root);
2206}
2207
Roland Levillain44015862016-01-22 11:47:17 +00002208void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002209 BarrierType type = BarrierAll;
2210
2211 switch (kind) {
2212 case MemBarrierKind::kAnyAny:
2213 case MemBarrierKind::kAnyStore: {
2214 type = BarrierAll;
2215 break;
2216 }
2217 case MemBarrierKind::kLoadAny: {
2218 type = BarrierReads;
2219 break;
2220 }
2221 case MemBarrierKind::kStoreStore: {
2222 type = BarrierWrites;
2223 break;
2224 }
2225 default:
2226 LOG(FATAL) << "Unexpected memory barrier " << kind;
2227 }
2228 __ Dmb(InnerShareable, type);
2229}
2230
Serban Constantinescu02164b32014-11-13 14:05:07 +00002231void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2232 HBasicBlock* successor) {
2233 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002234 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2235 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002236 slow_path =
2237 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002238 instruction->SetSlowPath(slow_path);
2239 codegen_->AddSlowPath(slow_path);
2240 if (successor != nullptr) {
2241 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002242 }
2243 } else {
2244 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2245 }
2246
Serban Constantinescu02164b32014-11-13 14:05:07 +00002247 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2248 Register temp = temps.AcquireW();
2249
Andreas Gampe542451c2016-07-26 09:02:02 -07002250 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002251 if (successor == nullptr) {
2252 __ Cbnz(temp, slow_path->GetEntryLabel());
2253 __ Bind(slow_path->GetReturnLabel());
2254 } else {
2255 __ Cbz(temp, codegen_->GetLabelOf(successor));
2256 __ B(slow_path->GetEntryLabel());
2257 // slow_path will return to GetLabelOf(successor).
2258 }
2259}
2260
Alexandre Rames5319def2014-10-23 10:03:10 +01002261InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2262 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002263 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002264 assembler_(codegen->GetAssembler()),
2265 codegen_(codegen) {}
2266
Alexandre Rames67555f72014-11-18 10:55:16 +00002267void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002268 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002269 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002270 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002271 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 case DataType::Type::kInt32:
2273 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002274 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002275 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002277 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 case DataType::Type::kFloat32:
2280 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002281 locations->SetInAt(0, Location::RequiresFpuRegister());
2282 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002284 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002285
Alexandre Rames5319def2014-10-23 10:03:10 +01002286 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002287 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002288 }
2289}
2290
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002291void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2292 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002293 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2294
2295 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002297 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002298 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2299 object_field_get_with_read_barrier
2300 ? LocationSummary::kCallOnSlowPath
2301 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002302 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002303 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002304 // We need a temporary register for the read barrier marking slow
2305 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002306 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2307 !Runtime::Current()->UseJitCompilation() &&
2308 !field_info.IsVolatile()) {
2309 // If link-time thunks for the Baker read barrier are enabled, for AOT
2310 // non-volatile loads we need a temporary only if the offset is too big.
2311 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2312 locations->AddTemp(FixedTempLocation());
2313 }
2314 } else {
2315 locations->AddTemp(Location::RequiresRegister());
2316 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002317 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002318 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002320 locations->SetOut(Location::RequiresFpuRegister());
2321 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002322 // The output overlaps for an object field get when read barriers
2323 // are enabled: we do not want the load to overwrite the object's
2324 // location, as we need it to emit the read barrier.
2325 locations->SetOut(
2326 Location::RequiresRegister(),
2327 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002328 }
2329}
2330
2331void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2332 const FieldInfo& field_info) {
2333 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002334 LocationSummary* locations = instruction->GetLocations();
2335 Location base_loc = locations->InAt(0);
2336 Location out = locations->Out();
2337 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002338 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2339 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002340 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002343 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002344 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002345 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002347 Location maybe_temp =
2348 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002349 // Note that potential implicit null checks are handled in this
2350 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2351 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2352 instruction,
2353 out,
2354 base,
2355 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002356 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002357 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002358 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002359 } else {
2360 // General case.
2361 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002362 // Note that a potential implicit null check is handled in this
2363 // CodeGeneratorARM64::LoadAcquire call.
2364 // NB: LoadAcquire will record the pc info if needed.
2365 codegen_->LoadAcquire(
2366 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002367 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002368 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2369 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002370 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002371 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002372 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002373 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002374 // If read barriers are enabled, emit read barriers other than
2375 // Baker's using a slow path (and also unpoison the loaded
2376 // reference, if heap poisoning is enabled).
2377 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2378 }
Roland Levillain4d027112015-07-01 15:41:14 +01002379 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002380}
2381
2382void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2383 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002384 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002385 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002386 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2387 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002389 locations->SetInAt(1, Location::RequiresFpuRegister());
2390 } else {
2391 locations->SetInAt(1, Location::RequiresRegister());
2392 }
2393}
2394
2395void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002396 const FieldInfo& field_info,
2397 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002398 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2399
2400 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002401 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002402 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002403 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002405
Roland Levillain4d027112015-07-01 15:41:14 +01002406 {
2407 // We use a block to end the scratch scope before the write barrier, thus
2408 // freeing the temporary registers so they can be used in `MarkGCCard`.
2409 UseScratchRegisterScope temps(GetVIXLAssembler());
2410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002412 DCHECK(value.IsW());
2413 Register temp = temps.AcquireW();
2414 __ Mov(temp, value.W());
2415 GetAssembler()->PoisonHeapReference(temp.W());
2416 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002417 }
Roland Levillain4d027112015-07-01 15:41:14 +01002418
2419 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002420 codegen_->StoreRelease(
2421 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002422 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002423 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2424 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002425 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2426 codegen_->MaybeRecordImplicitNullCheck(instruction);
2427 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002428 }
2429
2430 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002431 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002432 }
2433}
2434
Alexandre Rames67555f72014-11-18 10:55:16 +00002435void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002437
2438 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kInt32:
2440 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002441 Register dst = OutputRegister(instr);
2442 Register lhs = InputRegisterAt(instr, 0);
2443 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002444 if (instr->IsAdd()) {
2445 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002446 } else if (instr->IsAnd()) {
2447 __ And(dst, lhs, rhs);
2448 } else if (instr->IsOr()) {
2449 __ Orr(dst, lhs, rhs);
2450 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002451 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002452 } else if (instr->IsRor()) {
2453 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002454 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002455 __ Ror(dst, lhs, shift);
2456 } else {
2457 // Ensure shift distance is in the same size register as the result. If
2458 // we are rotating a long and the shift comes in a w register originally,
2459 // we don't need to sxtw for use as an x since the shift distances are
2460 // all & reg_bits - 1.
2461 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2462 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002463 } else if (instr->IsMin() || instr->IsMax()) {
2464 __ Cmp(lhs, rhs);
2465 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002466 } else {
2467 DCHECK(instr->IsXor());
2468 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002469 }
2470 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002471 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 case DataType::Type::kFloat32:
2473 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002474 FPRegister dst = OutputFPRegister(instr);
2475 FPRegister lhs = InputFPRegisterAt(instr, 0);
2476 FPRegister rhs = InputFPRegisterAt(instr, 1);
2477 if (instr->IsAdd()) {
2478 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002479 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002480 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002481 } else if (instr->IsMin()) {
2482 __ Fmin(dst, lhs, rhs);
2483 } else if (instr->IsMax()) {
2484 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002485 } else {
2486 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002487 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002488 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002489 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002490 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002491 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002492 }
2493}
2494
Serban Constantinescu02164b32014-11-13 14:05:07 +00002495void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2496 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2497
Vladimir Markoca6fff82017-10-03 14:49:14 +01002498 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002499 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002500 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kInt32:
2502 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002503 locations->SetInAt(0, Location::RequiresRegister());
2504 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002506 break;
2507 }
2508 default:
2509 LOG(FATAL) << "Unexpected shift type " << type;
2510 }
2511}
2512
2513void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2514 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002517 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kInt32:
2519 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002520 Register dst = OutputRegister(instr);
2521 Register lhs = InputRegisterAt(instr, 0);
2522 Operand rhs = InputOperandAt(instr, 1);
2523 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002524 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002526 if (instr->IsShl()) {
2527 __ Lsl(dst, lhs, shift_value);
2528 } else if (instr->IsShr()) {
2529 __ Asr(dst, lhs, shift_value);
2530 } else {
2531 __ Lsr(dst, lhs, shift_value);
2532 }
2533 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002534 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002535
2536 if (instr->IsShl()) {
2537 __ Lsl(dst, lhs, rhs_reg);
2538 } else if (instr->IsShr()) {
2539 __ Asr(dst, lhs, rhs_reg);
2540 } else {
2541 __ Lsr(dst, lhs, rhs_reg);
2542 }
2543 }
2544 break;
2545 }
2546 default:
2547 LOG(FATAL) << "Unexpected shift operation type " << type;
2548 }
2549}
2550
Alexandre Rames5319def2014-10-23 10:03:10 +01002551void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002552 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002553}
2554
2555void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002556 HandleBinaryOp(instruction);
2557}
2558
2559void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2560 HandleBinaryOp(instruction);
2561}
2562
2563void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2564 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002565}
2566
Artem Serov7fc63502016-02-09 17:15:29 +00002567void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002568 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002569 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002570 locations->SetInAt(0, Location::RequiresRegister());
2571 // There is no immediate variant of negated bitwise instructions in AArch64.
2572 locations->SetInAt(1, Location::RequiresRegister());
2573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2574}
2575
Artem Serov7fc63502016-02-09 17:15:29 +00002576void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002577 Register dst = OutputRegister(instr);
2578 Register lhs = InputRegisterAt(instr, 0);
2579 Register rhs = InputRegisterAt(instr, 1);
2580
2581 switch (instr->GetOpKind()) {
2582 case HInstruction::kAnd:
2583 __ Bic(dst, lhs, rhs);
2584 break;
2585 case HInstruction::kOr:
2586 __ Orn(dst, lhs, rhs);
2587 break;
2588 case HInstruction::kXor:
2589 __ Eon(dst, lhs, rhs);
2590 break;
2591 default:
2592 LOG(FATAL) << "Unreachable";
2593 }
2594}
2595
Anton Kirilov74234da2017-01-13 14:42:47 +00002596void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2597 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2599 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002600 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002601 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002602 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2603 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2604 } else {
2605 locations->SetInAt(0, Location::RequiresRegister());
2606 }
2607 locations->SetInAt(1, Location::RequiresRegister());
2608 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2609}
2610
Anton Kirilov74234da2017-01-13 14:42:47 +00002611void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2612 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002614 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002615 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002616 Register out = OutputRegister(instruction);
2617 Register left;
2618 if (kind != HInstruction::kNeg) {
2619 left = InputRegisterAt(instruction, 0);
2620 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002621 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002622 // shifter operand operation, the IR generating `right_reg` (input to the type
2623 // conversion) can have a different type from the current instruction's type,
2624 // so we manually indicate the type.
2625 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002626 Operand right_operand(0);
2627
Anton Kirilov74234da2017-01-13 14:42:47 +00002628 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2629 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002630 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2631 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002632 right_operand = Operand(right_reg,
2633 helpers::ShiftFromOpKind(op_kind),
2634 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002635 }
2636
2637 // Logical binary operations do not support extension operations in the
2638 // operand. Note that VIXL would still manage if it was passed by generating
2639 // the extension as a separate instruction.
2640 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2641 DCHECK(!right_operand.IsExtendedRegister() ||
2642 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2643 kind != HInstruction::kNeg));
2644 switch (kind) {
2645 case HInstruction::kAdd:
2646 __ Add(out, left, right_operand);
2647 break;
2648 case HInstruction::kAnd:
2649 __ And(out, left, right_operand);
2650 break;
2651 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002652 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002653 __ Neg(out, right_operand);
2654 break;
2655 case HInstruction::kOr:
2656 __ Orr(out, left, right_operand);
2657 break;
2658 case HInstruction::kSub:
2659 __ Sub(out, left, right_operand);
2660 break;
2661 case HInstruction::kXor:
2662 __ Eor(out, left, right_operand);
2663 break;
2664 default:
2665 LOG(FATAL) << "Unexpected operation kind: " << kind;
2666 UNREACHABLE();
2667 }
2668}
2669
Artem Serov328429f2016-07-06 16:23:04 +01002670void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002671 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002672 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002673 locations->SetInAt(0, Location::RequiresRegister());
2674 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002676}
2677
Roland Levillain19c54192016-11-04 13:44:09 +00002678void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002679 __ Add(OutputRegister(instruction),
2680 InputRegisterAt(instruction, 0),
2681 Operand(InputOperandAt(instruction, 1)));
2682}
2683
Artem Serove1811ed2017-04-27 16:50:47 +01002684void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2685 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002686 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002687
2688 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2689
2690 locations->SetInAt(0, Location::RequiresRegister());
2691 // For byte case we don't need to shift the index variable so we can encode the data offset into
2692 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2693 // data offset constant generation out of the loop and reduce the critical path length in the
2694 // loop.
2695 locations->SetInAt(1, shift->GetValue() == 0
2696 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2697 : Location::RequiresRegister());
2698 locations->SetInAt(2, Location::ConstantLocation(shift));
2699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2700}
2701
2702void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2703 HIntermediateAddressIndex* instruction) {
2704 Register index_reg = InputRegisterAt(instruction, 0);
2705 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2706 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2707
2708 if (shift == 0) {
2709 __ Add(OutputRegister(instruction), index_reg, offset);
2710 } else {
2711 Register offset_reg = InputRegisterAt(instruction, 1);
2712 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2713 }
2714}
2715
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002716void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002717 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002718 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002719 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2720 if (instr->GetOpKind() == HInstruction::kSub &&
2721 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002722 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002723 // Don't allocate register for Mneg instruction.
2724 } else {
2725 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2726 Location::RequiresRegister());
2727 }
2728 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2729 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002730 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2731}
2732
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002733void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002734 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002735 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2736 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002737
2738 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2739 // This fixup should be carried out for all multiply-accumulate instructions:
2740 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002742 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2743 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002744 vixl::aarch64::Instruction* prev =
2745 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002746 if (prev->IsLoadOrStore()) {
2747 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002748 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002749 __ nop();
2750 }
2751 }
2752
2753 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002754 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002755 __ Madd(res, mul_left, mul_right, accumulator);
2756 } else {
2757 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002758 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002759 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002760 __ Mneg(res, mul_left, mul_right);
2761 } else {
2762 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2763 __ Msub(res, mul_left, mul_right, accumulator);
2764 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002765 }
2766}
2767
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002768void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002769 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002770 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002771 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002772 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2773 object_array_get_with_read_barrier
2774 ? LocationSummary::kCallOnSlowPath
2775 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002776 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002777 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002778 // We need a temporary register for the read barrier marking slow
2779 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002780 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2781 !Runtime::Current()->UseJitCompilation() &&
2782 instruction->GetIndex()->IsConstant()) {
2783 // Array loads with constant index are treated as field loads.
2784 // If link-time thunks for the Baker read barrier are enabled, for AOT
2785 // constant index loads we need a temporary only if the offset is too big.
2786 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2787 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002789 if (offset >= kReferenceLoadMinFarOffset) {
2790 locations->AddTemp(FixedTempLocation());
2791 }
2792 } else {
2793 locations->AddTemp(Location::RequiresRegister());
2794 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002795 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002796 locations->SetInAt(0, Location::RequiresRegister());
2797 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002799 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2800 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002801 // The output overlaps in the case of an object array get with
2802 // read barriers enabled: we do not want the move to overwrite the
2803 // array's location, as we need it to emit the read barrier.
2804 locations->SetOut(
2805 Location::RequiresRegister(),
2806 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002807 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002808}
2809
2810void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002812 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002813 LocationSummary* locations = instruction->GetLocations();
2814 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002815 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002816 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002817 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2818 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002819 MacroAssembler* masm = GetVIXLAssembler();
2820 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002821
Roland Levillain19c54192016-11-04 13:44:09 +00002822 // The read barrier instrumentation of object ArrayGet instructions
2823 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002825 instruction->GetArray()->IsIntermediateAddress() &&
2826 kEmitCompilerReadBarrier));
2827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002829 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002830 // Note that a potential implicit null check is handled in the
2831 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002832 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002833 if (index.IsConstant()) {
2834 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002836 Location maybe_temp =
2837 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2838 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2839 out,
2840 obj.W(),
2841 offset,
2842 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002843 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002844 /* use_load_acquire */ false);
2845 } else {
2846 Register temp = WRegisterFrom(locations->GetTemp(0));
2847 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002848 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002849 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002850 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002851 // General case.
2852 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002853 Register length;
2854 if (maybe_compressed_char_at) {
2855 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2856 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002857 {
2858 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2859 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2860
2861 if (instruction->GetArray()->IsIntermediateAddress()) {
2862 DCHECK_LT(count_offset, offset);
2863 int64_t adjusted_offset =
2864 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2865 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2866 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2867 } else {
2868 __ Ldr(length, HeapOperand(obj, count_offset));
2869 }
2870 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002871 }
jessicahandojo05765752016-09-09 19:01:32 -07002872 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002873 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002874 if (maybe_compressed_char_at) {
2875 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002876 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2877 "Expecting 0=compressed, 1=uncompressed");
2878 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002879 __ Ldrb(Register(OutputCPURegister(instruction)),
2880 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2881 __ B(&done);
2882 __ Bind(&uncompressed_load);
2883 __ Ldrh(Register(OutputCPURegister(instruction)),
2884 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2885 __ Bind(&done);
2886 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002888 source = HeapOperand(obj, offset);
2889 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002890 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002891 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002892 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002893 // We do not need to compute the intermediate address from the array: the
2894 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002895 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002896 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002897 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002898 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2899 }
2900 temp = obj;
2901 } else {
2902 __ Add(temp, obj, offset);
2903 }
jessicahandojo05765752016-09-09 19:01:32 -07002904 if (maybe_compressed_char_at) {
2905 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002906 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2907 "Expecting 0=compressed, 1=uncompressed");
2908 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002909 __ Ldrb(Register(OutputCPURegister(instruction)),
2910 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2911 __ B(&done);
2912 __ Bind(&uncompressed_load);
2913 __ Ldrh(Register(OutputCPURegister(instruction)),
2914 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2915 __ Bind(&done);
2916 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002918 }
Roland Levillain44015862016-01-22 11:47:17 +00002919 }
jessicahandojo05765752016-09-09 19:01:32 -07002920 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002921 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2922 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002923 codegen_->Load(type, OutputCPURegister(instruction), source);
2924 codegen_->MaybeRecordImplicitNullCheck(instruction);
2925 }
Roland Levillain44015862016-01-22 11:47:17 +00002926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002928 static_assert(
2929 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2930 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2931 Location obj_loc = locations->InAt(0);
2932 if (index.IsConstant()) {
2933 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2934 } else {
2935 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2936 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002937 }
Roland Levillain4d027112015-07-01 15:41:14 +01002938 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002939}
2940
Alexandre Rames5319def2014-10-23 10:03:10 +01002941void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002942 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002943 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002944 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002945}
2946
2947void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002948 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002949 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002950 {
2951 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2952 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2953 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2954 codegen_->MaybeRecordImplicitNullCheck(instruction);
2955 }
jessicahandojo05765752016-09-09 19:01:32 -07002956 // Mask out compression flag from String's array length.
2957 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002958 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002959 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002960}
2961
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002962void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002964
2965 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002966 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002968 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002969 LocationSummary::kCallOnSlowPath :
2970 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002971 locations->SetInAt(0, Location::RequiresRegister());
2972 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002973 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2974 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002975 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002976 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002977 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002978 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002979 }
2980}
2981
2982void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002984 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002985 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002986 bool needs_write_barrier =
2987 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002988
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002989 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002990 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002991 CPURegister source = value;
2992 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002994 MemOperand destination = HeapOperand(array);
2995 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002996
2997 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002998 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002999 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003001 destination = HeapOperand(array, offset);
3002 } else {
3003 UseScratchRegisterScope temps(masm);
3004 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01003005 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003006 // We do not need to compute the intermediate address from the array: the
3007 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01003008 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003009 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01003010 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01003011 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
3012 }
3013 temp = array;
3014 } else {
3015 __ Add(temp, array, offset);
3016 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003017 destination = HeapOperand(temp,
3018 XRegisterFrom(index),
3019 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003021 }
Artem Serov914d7a82017-02-07 14:33:49 +00003022 {
3023 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3024 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3025 codegen_->Store(value_type, value, destination);
3026 codegen_->MaybeRecordImplicitNullCheck(instruction);
3027 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003028 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003029 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003030 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003031 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003032 {
3033 // We use a block to end the scratch scope before the write barrier, thus
3034 // freeing the temporary registers so they can be used in `MarkGCCard`.
3035 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003036 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003037 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003038 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003039 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003040 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003041 destination = HeapOperand(temp,
3042 XRegisterFrom(index),
3043 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003045 }
3046
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003047 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3048 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3049 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3050
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003051 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003052 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003053 codegen_->AddSlowPath(slow_path);
3054 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003055 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003056 __ Cbnz(Register(value), &non_zero);
3057 if (!index.IsConstant()) {
3058 __ Add(temp, array, offset);
3059 }
Artem Serov914d7a82017-02-07 14:33:49 +00003060 {
3061 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3062 // emitted.
3063 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3064 __ Str(wzr, destination);
3065 codegen_->MaybeRecordImplicitNullCheck(instruction);
3066 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003067 __ B(&done);
3068 __ Bind(&non_zero);
3069 }
3070
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003071 // Note that when Baker read barriers are enabled, the type
3072 // checks are performed without read barriers. This is fine,
3073 // even in the case where a class object is in the from-space
3074 // after the flip, as a comparison involving such a type would
3075 // not produce a false positive; it may of course produce a
3076 // false negative, in which case we would take the ArraySet
3077 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003078
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003079 Register temp2 = temps.AcquireSameSizeAs(array);
3080 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003081 {
3082 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3083 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3084 __ Ldr(temp, HeapOperand(array, class_offset));
3085 codegen_->MaybeRecordImplicitNullCheck(instruction);
3086 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003087 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003088
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003089 // /* HeapReference<Class> */ temp = temp->component_type_
3090 __ Ldr(temp, HeapOperand(temp, component_offset));
3091 // /* HeapReference<Class> */ temp2 = value->klass_
3092 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3093 // If heap poisoning is enabled, no need to unpoison `temp`
3094 // nor `temp2`, as we are comparing two poisoned references.
3095 __ Cmp(temp, temp2);
3096 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003097
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003098 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3099 vixl::aarch64::Label do_put;
3100 __ B(eq, &do_put);
3101 // If heap poisoning is enabled, the `temp` reference has
3102 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003103 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3104
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003105 // /* HeapReference<Class> */ temp = temp->super_class_
3106 __ Ldr(temp, HeapOperand(temp, super_offset));
3107 // If heap poisoning is enabled, no need to unpoison
3108 // `temp`, as we are comparing against null below.
3109 __ Cbnz(temp, slow_path->GetEntryLabel());
3110 __ Bind(&do_put);
3111 } else {
3112 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003113 }
3114 }
3115
3116 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003117 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003118 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003119 __ Mov(temp2, value.W());
3120 GetAssembler()->PoisonHeapReference(temp2);
3121 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003122 }
3123
3124 if (!index.IsConstant()) {
3125 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003126 } else {
3127 // We no longer need the `temp` here so release it as the store below may
3128 // need a scratch register (if the constant index makes the offset too large)
3129 // and the poisoned `source` could be using the other scratch register.
3130 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003131 }
Artem Serov914d7a82017-02-07 14:33:49 +00003132 {
3133 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3134 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3135 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003136
Artem Serov914d7a82017-02-07 14:33:49 +00003137 if (!may_need_runtime_call_for_type_check) {
3138 codegen_->MaybeRecordImplicitNullCheck(instruction);
3139 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003140 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003141 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003142
3143 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3144
3145 if (done.IsLinked()) {
3146 __ Bind(&done);
3147 }
3148
3149 if (slow_path != nullptr) {
3150 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003151 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003152 }
3153}
3154
Alexandre Rames67555f72014-11-18 10:55:16 +00003155void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003156 RegisterSet caller_saves = RegisterSet::Empty();
3157 InvokeRuntimeCallingConvention calling_convention;
3158 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3159 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3160 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003161 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003162 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003163}
3164
3165void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003166 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003167 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003168 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003169 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3170 __ B(slow_path->GetEntryLabel(), hs);
3171}
3172
Alexandre Rames67555f72014-11-18 10:55:16 +00003173void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3174 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003175 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003176 locations->SetInAt(0, Location::RequiresRegister());
3177 if (check->HasUses()) {
3178 locations->SetOut(Location::SameAsFirstInput());
3179 }
3180}
3181
3182void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3183 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003184 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Alexandre Rames67555f72014-11-18 10:55:16 +00003185 check->GetLoadClass(), check, check->GetDexPc(), true);
3186 codegen_->AddSlowPath(slow_path);
3187 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3188}
3189
Roland Levillain1a653882016-03-18 18:05:57 +00003190static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3191 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3192 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3193}
3194
3195void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3196 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3197 Location rhs_loc = instruction->GetLocations()->InAt(1);
3198 if (rhs_loc.IsConstant()) {
3199 // 0.0 is the only immediate that can be encoded directly in
3200 // an FCMP instruction.
3201 //
3202 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3203 // specify that in a floating-point comparison, positive zero
3204 // and negative zero are considered equal, so we can use the
3205 // literal 0.0 for both cases here.
3206 //
3207 // Note however that some methods (Float.equal, Float.compare,
3208 // Float.compareTo, Double.equal, Double.compare,
3209 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3210 // StrictMath.min) consider 0.0 to be (strictly) greater than
3211 // -0.0. So if we ever translate calls to these methods into a
3212 // HCompare instruction, we must handle the -0.0 case with
3213 // care here.
3214 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3215 __ Fcmp(lhs_reg, 0.0);
3216 } else {
3217 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3218 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003219}
3220
Serban Constantinescu02164b32014-11-13 14:05:07 +00003221void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003222 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003223 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003224 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003225 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003226 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003227 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003229 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003230 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 case DataType::Type::kInt32:
3232 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003233 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003234 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3236 break;
3237 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003238 case DataType::Type::kFloat32:
3239 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003240 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003241 locations->SetInAt(1,
3242 IsFloatingPointZeroConstant(compare->InputAt(1))
3243 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3244 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003245 locations->SetOut(Location::RequiresRegister());
3246 break;
3247 }
3248 default:
3249 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3250 }
3251}
3252
3253void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003254 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003255
3256 // 0 if: left == right
3257 // 1 if: left > right
3258 // -1 if: left < right
3259 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003260 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003261 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003262 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003264 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003265 case DataType::Type::kInt32:
3266 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003267 Register result = OutputRegister(compare);
3268 Register left = InputRegisterAt(compare, 0);
3269 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003270 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003271 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3272 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003273 break;
3274 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003275 case DataType::Type::kFloat32:
3276 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003277 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003278 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003279 __ Cset(result, ne);
3280 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003281 break;
3282 }
3283 default:
3284 LOG(FATAL) << "Unimplemented compare type " << in_type;
3285 }
3286}
3287
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003288void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003289 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003291 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003292 locations->SetInAt(0, Location::RequiresFpuRegister());
3293 locations->SetInAt(1,
3294 IsFloatingPointZeroConstant(instruction->InputAt(1))
3295 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3296 : Location::RequiresFpuRegister());
3297 } else {
3298 // Integer cases.
3299 locations->SetInAt(0, Location::RequiresRegister());
3300 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3301 }
3302
David Brazdilb3e773e2016-01-26 11:28:37 +00003303 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003305 }
3306}
3307
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003308void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003309 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003310 return;
3311 }
3312
3313 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003314 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003315 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003316
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003317 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003318 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003319 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003320 } else {
3321 // Integer cases.
3322 Register lhs = InputRegisterAt(instruction, 0);
3323 Operand rhs = InputOperandAt(instruction, 1);
3324 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003325 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003326 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003327}
3328
3329#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3330 M(Equal) \
3331 M(NotEqual) \
3332 M(LessThan) \
3333 M(LessThanOrEqual) \
3334 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003335 M(GreaterThanOrEqual) \
3336 M(Below) \
3337 M(BelowOrEqual) \
3338 M(Above) \
3339 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003340#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003341void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3342void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003343FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003344#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003345#undef FOR_EACH_CONDITION_INSTRUCTION
3346
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003347void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
3348 int64_t imm = Int64ConstantFrom(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003349 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003350 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3351
3352 Register out = OutputRegister(instruction);
3353 Register dividend = InputRegisterAt(instruction, 0);
Zheng Xuc6667102015-05-15 16:08:45 +08003354 int ctz_imm = CTZ(abs_imm);
3355
3356 UseScratchRegisterScope temps(GetVIXLAssembler());
3357 Register temp = temps.AcquireSameSizeAs(out);
3358
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003359 __ Add(temp, dividend, abs_imm - 1);
3360 __ Cmp(dividend, 0);
3361 __ Csel(out, temp, dividend, lt);
3362 if (imm > 0) {
3363 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003364 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003365 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003366 }
3367}
3368
3369void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(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());
3379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003380 DataType::Type type = instruction->GetResultType();
3381 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003382
3383 int64_t magic;
3384 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003385 CalculateMagicAndShiftForDivRem(
3386 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003387
3388 UseScratchRegisterScope temps(GetVIXLAssembler());
3389 Register temp = temps.AcquireSameSizeAs(out);
3390
3391 // temp = get_high(dividend * magic)
3392 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003393 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003394 __ Smulh(temp, dividend, temp);
3395 } else {
3396 __ Smull(temp.X(), dividend, temp);
3397 __ Lsr(temp.X(), temp.X(), 32);
3398 }
3399
3400 if (imm > 0 && magic < 0) {
3401 __ Add(temp, temp, dividend);
3402 } else if (imm < 0 && magic > 0) {
3403 __ Sub(temp, temp, dividend);
3404 }
3405
3406 if (shift != 0) {
3407 __ Asr(temp, temp, shift);
3408 }
3409
3410 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003411 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003412 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003414 // TODO: Strength reduction for msub.
3415 Register temp_imm = temps.AcquireSameSizeAs(out);
3416 __ Mov(temp_imm, imm);
3417 __ Msub(out, temp, temp_imm, dividend);
3418 }
3419}
3420
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003421void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
3422 int64_t imm = Int64ConstantFrom(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003423
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003424 if (imm == 0) {
3425 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3426 return;
3427 }
Zheng Xuc6667102015-05-15 16:08:45 +08003428
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003429 if (IsPowerOfTwo(AbsOrMin(imm))) {
3430 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003431 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003432 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3433 DCHECK(imm < -2 || imm > 2) << imm;
3434 GenerateDivRemWithAnyConstant(instruction);
3435 }
3436}
3437
3438void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3439 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3440 << instruction->GetResultType();
3441
3442 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3443 GenerateIntDivForConstDenom(instruction);
3444 } else {
3445 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003446 Register dividend = InputRegisterAt(instruction, 0);
3447 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003448 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003449 }
3450}
3451
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003452void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3453 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003454 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003455 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003456 case DataType::Type::kInt32:
3457 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003458 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003459 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3461 break;
3462
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003463 case DataType::Type::kFloat32:
3464 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003465 locations->SetInAt(0, Location::RequiresFpuRegister());
3466 locations->SetInAt(1, Location::RequiresFpuRegister());
3467 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3468 break;
3469
3470 default:
3471 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3472 }
3473}
3474
3475void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003477 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003478 case DataType::Type::kInt32:
3479 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003480 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003481 break;
3482
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003483 case DataType::Type::kFloat32:
3484 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003485 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3486 break;
3487
3488 default:
3489 LOG(FATAL) << "Unexpected div type " << type;
3490 }
3491}
3492
Alexandre Rames67555f72014-11-18 10:55:16 +00003493void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003494 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003495 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003496}
3497
3498void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3499 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003500 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003501 codegen_->AddSlowPath(slow_path);
3502 Location value = instruction->GetLocations()->InAt(0);
3503
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003504 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003506 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003507 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003508 return;
3509 }
3510
Alexandre Rames67555f72014-11-18 10:55:16 +00003511 if (value.IsConstant()) {
3512 int64_t divisor = Int64ConstantFrom(value);
3513 if (divisor == 0) {
3514 __ B(slow_path->GetEntryLabel());
3515 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003516 // A division by a non-null constant is valid. We don't need to perform
3517 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003518 }
3519 } else {
3520 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3521 }
3522}
3523
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003524void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3525 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003526 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003527 locations->SetOut(Location::ConstantLocation(constant));
3528}
3529
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003530void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3531 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003532 // Will be generated at use site.
3533}
3534
Alexandre Rames5319def2014-10-23 10:03:10 +01003535void LocationsBuilderARM64::VisitExit(HExit* exit) {
3536 exit->SetLocations(nullptr);
3537}
3538
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003539void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003540}
3541
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003542void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3543 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003544 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003545 locations->SetOut(Location::ConstantLocation(constant));
3546}
3547
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003548void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003549 // Will be generated at use site.
3550}
3551
David Brazdilfc6a86a2015-06-26 10:33:45 +00003552void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003553 if (successor->IsExitBlock()) {
3554 DCHECK(got->GetPrevious()->AlwaysThrows());
3555 return; // no code needed
3556 }
3557
Serban Constantinescu02164b32014-11-13 14:05:07 +00003558 HBasicBlock* block = got->GetBlock();
3559 HInstruction* previous = got->GetPrevious();
3560 HLoopInformation* info = block->GetLoopInformation();
3561
David Brazdil46e2a392015-03-16 17:31:52 +00003562 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00003563 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3564 UseScratchRegisterScope temps(GetVIXLAssembler());
3565 Register temp1 = temps.AcquireX();
3566 Register temp2 = temps.AcquireX();
3567 __ Ldr(temp1, MemOperand(sp, 0));
3568 __ Ldrh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3569 __ Add(temp2, temp2, 1);
3570 __ Strh(temp2, MemOperand(temp1, ArtMethod::HotnessCountOffset().Int32Value()));
3571 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003572 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3573 return;
3574 }
3575 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3576 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003577 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003578 }
3579 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003580 __ B(codegen_->GetLabelOf(successor));
3581 }
3582}
3583
David Brazdilfc6a86a2015-06-26 10:33:45 +00003584void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3585 got->SetLocations(nullptr);
3586}
3587
3588void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3589 HandleGoto(got, got->GetSuccessor());
3590}
3591
3592void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3593 try_boundary->SetLocations(nullptr);
3594}
3595
3596void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3597 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3598 if (!successor->IsExitBlock()) {
3599 HandleGoto(try_boundary, successor);
3600 }
3601}
3602
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003603void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003604 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003605 vixl::aarch64::Label* true_target,
3606 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003607 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003608
David Brazdil0debae72015-11-12 18:37:00 +00003609 if (true_target == nullptr && false_target == nullptr) {
3610 // Nothing to do. The code always falls through.
3611 return;
3612 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003613 // Constant condition, statically compared against "true" (integer value 1).
3614 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003615 if (true_target != nullptr) {
3616 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003617 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003618 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003619 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003620 if (false_target != nullptr) {
3621 __ B(false_target);
3622 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003623 }
David Brazdil0debae72015-11-12 18:37:00 +00003624 return;
3625 }
3626
3627 // The following code generates these patterns:
3628 // (1) true_target == nullptr && false_target != nullptr
3629 // - opposite condition true => branch to false_target
3630 // (2) true_target != nullptr && false_target == nullptr
3631 // - condition true => branch to true_target
3632 // (3) true_target != nullptr && false_target != nullptr
3633 // - condition true => branch to true_target
3634 // - branch to false_target
3635 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003636 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003637 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003638 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003639 if (true_target == nullptr) {
3640 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3641 } else {
3642 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3643 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003644 } else {
3645 // The condition instruction has not been materialized, use its inputs as
3646 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003647 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 DataType::Type type = condition->InputAt(0)->GetType();
3650 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003651 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003652 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003653 IfCondition opposite_condition = condition->GetOppositeCondition();
3654 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003655 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003656 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003657 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003658 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003659 // Integer cases.
3660 Register lhs = InputRegisterAt(condition, 0);
3661 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003662
3663 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003664 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003665 if (true_target == nullptr) {
3666 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3667 non_fallthrough_target = false_target;
3668 } else {
3669 arm64_cond = ARM64Condition(condition->GetCondition());
3670 non_fallthrough_target = true_target;
3671 }
3672
Aart Bik086d27e2016-01-20 17:02:00 -08003673 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003674 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003675 switch (arm64_cond) {
3676 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003677 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003678 break;
3679 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003680 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003681 break;
3682 case lt:
3683 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003684 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003685 break;
3686 case ge:
3687 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003689 break;
3690 default:
3691 // Without the `static_cast` the compiler throws an error for
3692 // `-Werror=sign-promo`.
3693 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3694 }
3695 } else {
3696 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003697 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003698 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003699 }
3700 }
David Brazdil0debae72015-11-12 18:37:00 +00003701
3702 // If neither branch falls through (case 3), the conditional branch to `true_target`
3703 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3704 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003705 __ B(false_target);
3706 }
3707}
3708
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003709void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003710 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003711 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003712 locations->SetInAt(0, Location::RequiresRegister());
3713 }
3714}
3715
3716void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003717 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3718 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003719 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3720 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3721 true_target = nullptr;
3722 }
3723 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3724 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3725 false_target = nullptr;
3726 }
David Brazdil0debae72015-11-12 18:37:00 +00003727 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003728}
3729
3730void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003731 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003732 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003733 InvokeRuntimeCallingConvention calling_convention;
3734 RegisterSet caller_saves = RegisterSet::Empty();
3735 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3736 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003737 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003738 locations->SetInAt(0, Location::RequiresRegister());
3739 }
3740}
3741
3742void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003743 SlowPathCodeARM64* slow_path =
3744 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003745 GenerateTestAndBranch(deoptimize,
3746 /* condition_input_index */ 0,
3747 slow_path->GetEntryLabel(),
3748 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003749}
3750
Mingyao Yang063fc772016-08-02 11:02:54 -07003751void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003752 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003753 LocationSummary(flag, LocationSummary::kNoCall);
3754 locations->SetOut(Location::RequiresRegister());
3755}
3756
3757void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3758 __ Ldr(OutputRegister(flag),
3759 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3760}
3761
David Brazdilc0b601b2016-02-08 14:20:45 +00003762static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3763 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003765}
3766
Alexandre Rames880f1192016-06-13 16:04:50 +01003767static inline Condition GetConditionForSelect(HCondition* condition) {
3768 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003769 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3770 : ARM64Condition(cond);
3771}
3772
David Brazdil74eb1b22015-12-14 11:44:01 +00003773void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003774 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003775 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003776 locations->SetInAt(0, Location::RequiresFpuRegister());
3777 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003778 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003779 } else {
3780 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3781 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3782 bool is_true_value_constant = cst_true_value != nullptr;
3783 bool is_false_value_constant = cst_false_value != nullptr;
3784 // Ask VIXL whether we should synthesize constants in registers.
3785 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3786 Operand true_op = is_true_value_constant ?
3787 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3788 Operand false_op = is_false_value_constant ?
3789 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3790 bool true_value_in_register = false;
3791 bool false_value_in_register = false;
3792 MacroAssembler::GetCselSynthesisInformation(
3793 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3794 true_value_in_register |= !is_true_value_constant;
3795 false_value_in_register |= !is_false_value_constant;
3796
3797 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3798 : Location::ConstantLocation(cst_true_value));
3799 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3800 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003802 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003803
David Brazdil74eb1b22015-12-14 11:44:01 +00003804 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3805 locations->SetInAt(2, Location::RequiresRegister());
3806 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003807}
3808
3809void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003810 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003811 Condition csel_cond;
3812
3813 if (IsBooleanValueOrMaterializedCondition(cond)) {
3814 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003815 // Use the condition flags set by the previous instruction.
3816 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003817 } else {
3818 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003819 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003820 }
3821 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003822 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003823 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003824 } else {
3825 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003826 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003827 }
3828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003830 __ Fcsel(OutputFPRegister(select),
3831 InputFPRegisterAt(select, 1),
3832 InputFPRegisterAt(select, 0),
3833 csel_cond);
3834 } else {
3835 __ Csel(OutputRegister(select),
3836 InputOperandAt(select, 1),
3837 InputOperandAt(select, 0),
3838 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003839 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003840}
3841
David Srbecky0cf44932015-12-09 14:09:59 +00003842void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003843 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003844}
3845
David Srbeckyd28f4a02016-03-14 17:14:24 +00003846void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3847 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003848}
3849
3850void CodeGeneratorARM64::GenerateNop() {
3851 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003852}
3853
Alexandre Rames5319def2014-10-23 10:03:10 +01003854void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003855 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003856}
3857
3858void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003859 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003860}
3861
3862void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003863 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003864}
3865
3866void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003867 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003868}
3869
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003870// Temp is used for read barrier.
3871static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3872 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003873 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003874 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3875 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3876 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3877 return 1;
3878 }
3879 return 0;
3880}
3881
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003882// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003883// interface pointer, one for loading the current interface.
3884// The other checks have one temp for loading the object's class.
3885static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3886 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3887 return 3;
3888 }
3889 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003890}
3891
Alexandre Rames67555f72014-11-18 10:55:16 +00003892void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003893 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003895 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003897 case TypeCheckKind::kExactCheck:
3898 case TypeCheckKind::kAbstractClassCheck:
3899 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003900 case TypeCheckKind::kArrayObjectCheck: {
3901 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3902 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3903 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003905 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003906 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003907 case TypeCheckKind::kUnresolvedCheck:
3908 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003909 call_kind = LocationSummary::kCallOnSlowPath;
3910 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003911 case TypeCheckKind::kBitstringCheck:
3912 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003913 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003914
Vladimir Markoca6fff82017-10-03 14:49:14 +01003915 LocationSummary* locations =
3916 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003917 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003918 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003919 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003921 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3922 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3923 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3924 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3925 } else {
3926 locations->SetInAt(1, Location::RequiresRegister());
3927 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003928 // The "out" register is used as a temporary, so it overlaps with the inputs.
3929 // Note that TypeCheckSlowPathARM64 uses this register too.
3930 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003931 // Add temps if necessary for read barriers.
3932 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003933}
3934
3935void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003936 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003937 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003938 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003940 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3941 ? Register()
3942 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003943 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003944 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003945 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3946 DCHECK_LE(num_temps, 1u);
3947 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003948 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3949 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3950 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3951 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003952
Scott Wakeling97c72b72016-06-24 16:19:36 +01003953 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003954 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003955
3956 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003957 // Avoid null check if we know `obj` is not null.
3958 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 __ Cbz(obj, &zero);
3960 }
3961
Roland Levillain44015862016-01-22 11:47:17 +00003962 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003963 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003964 ReadBarrierOption read_barrier_option =
3965 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003966 // /* HeapReference<Class> */ out = obj->klass_
3967 GenerateReferenceLoadTwoRegisters(instruction,
3968 out_loc,
3969 obj_loc,
3970 class_offset,
3971 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003972 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003973 __ Cmp(out, cls);
3974 __ Cset(out, eq);
3975 if (zero.IsLinked()) {
3976 __ B(&done);
3977 }
3978 break;
3979 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003980
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003981 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003982 ReadBarrierOption read_barrier_option =
3983 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003984 // /* HeapReference<Class> */ out = obj->klass_
3985 GenerateReferenceLoadTwoRegisters(instruction,
3986 out_loc,
3987 obj_loc,
3988 class_offset,
3989 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003990 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003991 // If the class is abstract, we eagerly fetch the super class of the
3992 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003993 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003994 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003995 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003996 GenerateReferenceLoadOneRegister(instruction,
3997 out_loc,
3998 super_offset,
3999 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004000 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004001 // If `out` is null, we use it for the result, and jump to `done`.
4002 __ Cbz(out, &done);
4003 __ Cmp(out, cls);
4004 __ B(ne, &loop);
4005 __ Mov(out, 1);
4006 if (zero.IsLinked()) {
4007 __ B(&done);
4008 }
4009 break;
4010 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004011
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004012 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004013 ReadBarrierOption read_barrier_option =
4014 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004015 // /* HeapReference<Class> */ out = obj->klass_
4016 GenerateReferenceLoadTwoRegisters(instruction,
4017 out_loc,
4018 obj_loc,
4019 class_offset,
4020 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004021 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004022 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004023 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004024 __ Bind(&loop);
4025 __ Cmp(out, cls);
4026 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004027 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004028 GenerateReferenceLoadOneRegister(instruction,
4029 out_loc,
4030 super_offset,
4031 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004032 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004033 __ Cbnz(out, &loop);
4034 // If `out` is null, we use it for the result, and jump to `done`.
4035 __ B(&done);
4036 __ Bind(&success);
4037 __ Mov(out, 1);
4038 if (zero.IsLinked()) {
4039 __ B(&done);
4040 }
4041 break;
4042 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004043
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004044 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00004045 ReadBarrierOption read_barrier_option =
4046 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004047 // /* HeapReference<Class> */ out = obj->klass_
4048 GenerateReferenceLoadTwoRegisters(instruction,
4049 out_loc,
4050 obj_loc,
4051 class_offset,
4052 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004053 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004054 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004055 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004056 __ Cmp(out, cls);
4057 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004058 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004059 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004060 GenerateReferenceLoadOneRegister(instruction,
4061 out_loc,
4062 component_offset,
4063 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00004064 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004065 // If `out` is null, we use it for the result, and jump to `done`.
4066 __ Cbz(out, &done);
4067 __ Ldrh(out, HeapOperand(out, primitive_offset));
4068 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4069 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004070 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004071 __ Mov(out, 1);
4072 __ B(&done);
4073 break;
4074 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004075
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004076 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004077 // No read barrier since the slow path will retry upon failure.
4078 // /* HeapReference<Class> */ out = obj->klass_
4079 GenerateReferenceLoadTwoRegisters(instruction,
4080 out_loc,
4081 obj_loc,
4082 class_offset,
4083 maybe_temp_loc,
4084 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004085 __ Cmp(out, cls);
4086 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004087 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4088 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004089 codegen_->AddSlowPath(slow_path);
4090 __ B(ne, slow_path->GetEntryLabel());
4091 __ Mov(out, 1);
4092 if (zero.IsLinked()) {
4093 __ B(&done);
4094 }
4095 break;
4096 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097
Calin Juravle98893e12015-10-02 21:05:03 +01004098 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004099 case TypeCheckKind::kInterfaceCheck: {
4100 // Note that we indeed only call on slow path, but we always go
4101 // into the slow path for the unresolved and interface check
4102 // cases.
4103 //
4104 // We cannot directly call the InstanceofNonTrivial runtime
4105 // entry point without resorting to a type checking slow path
4106 // here (i.e. by calling InvokeRuntime directly), as it would
4107 // require to assign fixed registers for the inputs of this
4108 // HInstanceOf instruction (following the runtime calling
4109 // convention), which might be cluttered by the potential first
4110 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004111 //
4112 // TODO: Introduce a new runtime entry point taking the object
4113 // to test (instead of its class) as argument, and let it deal
4114 // with the read barrier issues. This will let us refactor this
4115 // case of the `switch` code as it was previously (with a direct
4116 // call to the runtime not using a type checking slow path).
4117 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004119 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4120 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004121 codegen_->AddSlowPath(slow_path);
4122 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004123 if (zero.IsLinked()) {
4124 __ B(&done);
4125 }
4126 break;
4127 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004128
4129 case TypeCheckKind::kBitstringCheck: {
4130 // /* HeapReference<Class> */ temp = obj->klass_
4131 GenerateReferenceLoadTwoRegisters(instruction,
4132 out_loc,
4133 obj_loc,
4134 class_offset,
4135 maybe_temp_loc,
4136 kWithoutReadBarrier);
4137
4138 GenerateBitstringTypeCheckCompare(instruction, out);
4139 __ Cset(out, eq);
4140 if (zero.IsLinked()) {
4141 __ B(&done);
4142 }
4143 break;
4144 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 }
4146
4147 if (zero.IsLinked()) {
4148 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004149 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150 }
4151
4152 if (done.IsLinked()) {
4153 __ Bind(&done);
4154 }
4155
4156 if (slow_path != nullptr) {
4157 __ Bind(slow_path->GetExitLabel());
4158 }
4159}
4160
4161void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004162 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00004163 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004164 LocationSummary* locations =
4165 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004166 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00004167 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
4168 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
4169 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
4170 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
4171 } else {
4172 locations->SetInAt(1, Location::RequiresRegister());
4173 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004174 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4175 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004176}
4177
4178void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004179 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004180 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004182 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00004183 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
4184 ? Register()
4185 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004186 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4187 DCHECK_GE(num_temps, 1u);
4188 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004189 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004190 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4191 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004192 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004193 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4194 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4195 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4196 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4197 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4198 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4199 const uint32_t object_array_data_offset =
4200 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004201
Vladimir Marko87584542017-12-12 17:47:52 +00004202 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004203 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004204 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4205 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004206 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207
Scott Wakeling97c72b72016-06-24 16:19:36 +01004208 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004209 // Avoid null check if we know obj is not null.
4210 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004211 __ Cbz(obj, &done);
4212 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004213
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004214 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004215 case TypeCheckKind::kExactCheck:
4216 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004217 // /* HeapReference<Class> */ temp = obj->klass_
4218 GenerateReferenceLoadTwoRegisters(instruction,
4219 temp_loc,
4220 obj_loc,
4221 class_offset,
4222 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004223 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225 __ Cmp(temp, cls);
4226 // Jump to slow path for throwing the exception or doing a
4227 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004228 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004229 break;
4230 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004232 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004233 // /* HeapReference<Class> */ temp = obj->klass_
4234 GenerateReferenceLoadTwoRegisters(instruction,
4235 temp_loc,
4236 obj_loc,
4237 class_offset,
4238 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004239 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004240
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004241 // If the class is abstract, we eagerly fetch the super class of the
4242 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004243 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004244 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004245 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004246 GenerateReferenceLoadOneRegister(instruction,
4247 temp_loc,
4248 super_offset,
4249 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004250 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004251
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004252 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4253 // exception.
4254 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4255 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004256 __ Cmp(temp, cls);
4257 __ B(ne, &loop);
4258 break;
4259 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004261 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004262 // /* HeapReference<Class> */ temp = obj->klass_
4263 GenerateReferenceLoadTwoRegisters(instruction,
4264 temp_loc,
4265 obj_loc,
4266 class_offset,
4267 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004268 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004269
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004270 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004271 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004272 __ Bind(&loop);
4273 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004274 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004275
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004276 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004277 GenerateReferenceLoadOneRegister(instruction,
4278 temp_loc,
4279 super_offset,
4280 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004281 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004282
4283 // If the class reference currently in `temp` is not null, jump
4284 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004285 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004286 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004287 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004288 break;
4289 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004290
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004291 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292 // /* HeapReference<Class> */ temp = obj->klass_
4293 GenerateReferenceLoadTwoRegisters(instruction,
4294 temp_loc,
4295 obj_loc,
4296 class_offset,
4297 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004298 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004299
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004300 // Do an exact check.
4301 __ Cmp(temp, cls);
4302 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004303
4304 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004305 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004306 GenerateReferenceLoadOneRegister(instruction,
4307 temp_loc,
4308 component_offset,
4309 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004310 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004311
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004312 // If the component type is null, jump to the slow path to throw the exception.
4313 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4314 // Otherwise, the object is indeed an array. Further check that this component type is not a
4315 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004316 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4317 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004318 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004319 break;
4320 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004321
Calin Juravle98893e12015-10-02 21:05:03 +01004322 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004323 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004324 //
4325 // We cannot directly call the CheckCast runtime entry point
4326 // without resorting to a type checking slow path here (i.e. by
4327 // calling InvokeRuntime directly), as it would require to
4328 // assign fixed registers for the inputs of this HInstanceOf
4329 // instruction (following the runtime calling convention), which
4330 // might be cluttered by the potential first read barrier
4331 // emission at the beginning of this method.
4332 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004333 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004334 case TypeCheckKind::kInterfaceCheck: {
4335 // /* HeapReference<Class> */ temp = obj->klass_
4336 GenerateReferenceLoadTwoRegisters(instruction,
4337 temp_loc,
4338 obj_loc,
4339 class_offset,
4340 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004341 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004342
4343 // /* HeapReference<Class> */ temp = temp->iftable_
4344 GenerateReferenceLoadTwoRegisters(instruction,
4345 temp_loc,
4346 temp_loc,
4347 iftable_offset,
4348 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004349 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004350 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004351 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004352 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004353 vixl::aarch64::Label start_loop;
4354 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004355 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004356 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4357 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004358 // Go to next interface.
4359 __ Add(temp, temp, 2 * kHeapReferenceSize);
4360 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004361 // Compare the classes and continue the loop if they do not match.
4362 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4363 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004364 break;
4365 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004366
4367 case TypeCheckKind::kBitstringCheck: {
4368 // /* HeapReference<Class> */ temp = obj->klass_
4369 GenerateReferenceLoadTwoRegisters(instruction,
4370 temp_loc,
4371 obj_loc,
4372 class_offset,
4373 maybe_temp2_loc,
4374 kWithoutReadBarrier);
4375
4376 GenerateBitstringTypeCheckCompare(instruction, temp);
4377 __ B(ne, type_check_slow_path->GetEntryLabel());
4378 break;
4379 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004380 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004381 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004382
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004383 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004384}
4385
Alexandre Rames5319def2014-10-23 10:03:10 +01004386void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004387 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004388 locations->SetOut(Location::ConstantLocation(constant));
4389}
4390
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004391void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004392 // Will be generated at use site.
4393}
4394
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004395void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004396 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004397 locations->SetOut(Location::ConstantLocation(constant));
4398}
4399
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004400void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004401 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004402}
4403
Calin Juravle175dc732015-08-25 15:42:32 +01004404void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4405 // The trampoline uses the same calling convention as dex calling conventions,
4406 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4407 // the method_idx.
4408 HandleInvoke(invoke);
4409}
4410
4411void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4412 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004413 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004414}
4415
Alexandre Rames5319def2014-10-23 10:03:10 +01004416void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004417 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004418 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004419}
4420
Alexandre Rames67555f72014-11-18 10:55:16 +00004421void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4422 HandleInvoke(invoke);
4423}
4424
4425void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4426 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004427 LocationSummary* locations = invoke->GetLocations();
4428 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004429 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004430 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004431 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004432
4433 // The register ip1 is required to be used for the hidden argument in
4434 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004435 MacroAssembler* masm = GetVIXLAssembler();
4436 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004437 scratch_scope.Exclude(ip1);
4438 __ Mov(ip1, invoke->GetDexMethodIndex());
4439
Artem Serov914d7a82017-02-07 14:33:49 +00004440 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004441 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004442 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004443 {
4444 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4445 // /* HeapReference<Class> */ temp = temp->klass_
4446 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4447 codegen_->MaybeRecordImplicitNullCheck(invoke);
4448 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004449 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004450 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004451 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004452 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004453 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004454 }
Artem Serov914d7a82017-02-07 14:33:49 +00004455
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004456 // Instead of simply (possibly) unpoisoning `temp` here, we should
4457 // emit a read barrier for the previous class reference load.
4458 // However this is not required in practice, as this is an
4459 // intermediate/temporary reference and because the current
4460 // concurrent copying collector keeps the from-space memory
4461 // intact/accessible until the end of the marking phase (the
4462 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004463 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004464 __ Ldr(temp,
4465 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4466 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004467 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004468 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004469 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004470 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004471 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004472
4473 {
4474 // Ensure the pc position is recorded immediately after the `blr` instruction.
4475 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4476
4477 // lr();
4478 __ blr(lr);
4479 DCHECK(!codegen_->IsLeafMethod());
4480 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4481 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004482
4483 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004484}
4485
4486void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004487 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004488 if (intrinsic.TryDispatch(invoke)) {
4489 return;
4490 }
4491
Alexandre Rames67555f72014-11-18 10:55:16 +00004492 HandleInvoke(invoke);
4493}
4494
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004495void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004496 // Explicit clinit checks triggered by static invokes must have been pruned by
4497 // art::PrepareForRegisterAllocation.
4498 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004499
Vladimir Markoca6fff82017-10-03 14:49:14 +01004500 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004501 if (intrinsic.TryDispatch(invoke)) {
4502 return;
4503 }
4504
Alexandre Rames67555f72014-11-18 10:55:16 +00004505 HandleInvoke(invoke);
4506}
4507
Andreas Gampe878d58c2015-01-15 23:24:00 -08004508static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4509 if (invoke->GetLocations()->Intrinsified()) {
4510 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4511 intrinsic.Dispatch(invoke);
4512 return true;
4513 }
4514 return false;
4515}
4516
Vladimir Markodc151b22015-10-15 18:02:30 +01004517HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4518 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004519 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004520 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004521 return desired_dispatch_info;
4522}
4523
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004524void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4525 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004526 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004527 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4528 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004529 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4530 uint32_t offset =
4531 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004532 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004533 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004534 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004535 }
Vladimir Marko58155012015-08-19 12:49:41 +00004536 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004537 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004538 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004539 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4540 DCHECK(GetCompilerOptions().IsBootImage());
4541 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004542 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004543 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4544 // Add ADD with its PC-relative method patch.
4545 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004546 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004547 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4548 break;
4549 }
Vladimir Marko58155012015-08-19 12:49:41 +00004550 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4551 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004552 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004553 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004554 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4555 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004556 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004557 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4558 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4559 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4560 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4561 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4562 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4563 break;
4564 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004565 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004566 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004567 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4568 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004569 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004570 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004571 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004572 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004573 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004574 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004575 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004576 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4577 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4578 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004579 }
4580 }
4581
4582 switch (invoke->GetCodePtrLocation()) {
4583 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004584 {
4585 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4586 ExactAssemblyScope eas(GetVIXLAssembler(),
4587 kInstructionSize,
4588 CodeBufferCheckScope::kExactSize);
4589 __ bl(&frame_entry_label_);
4590 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4591 }
Vladimir Marko58155012015-08-19 12:49:41 +00004592 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004593 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4594 // LR = callee_method->entry_point_from_quick_compiled_code_;
4595 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004596 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004597 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004598 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004599 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004600 ExactAssemblyScope eas(GetVIXLAssembler(),
4601 kInstructionSize,
4602 CodeBufferCheckScope::kExactSize);
4603 // lr()
4604 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004605 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004606 }
Vladimir Marko58155012015-08-19 12:49:41 +00004607 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004608 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004609
Andreas Gampe878d58c2015-01-15 23:24:00 -08004610 DCHECK(!IsLeafMethod());
4611}
4612
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004613void CodeGeneratorARM64::GenerateVirtualCall(
4614 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004615 // Use the calling convention instead of the location of the receiver, as
4616 // intrinsics may have put the receiver in a different register. In the intrinsics
4617 // slow path, the arguments have been moved to the right place, so here we are
4618 // guaranteed that the receiver is the first register of the calling convention.
4619 InvokeDexCallingConvention calling_convention;
4620 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004621 Register temp = XRegisterFrom(temp_in);
4622 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4623 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4624 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004625 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004626
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004627 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004628
4629 {
4630 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4631 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4632 // /* HeapReference<Class> */ temp = receiver->klass_
4633 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4634 MaybeRecordImplicitNullCheck(invoke);
4635 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004636 // Instead of simply (possibly) unpoisoning `temp` here, we should
4637 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004638 // intermediate/temporary reference and because the current
4639 // concurrent copying collector keeps the from-space memory
4640 // intact/accessible until the end of the marking phase (the
4641 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004642 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4643 // temp = temp->GetMethodAt(method_offset);
4644 __ Ldr(temp, MemOperand(temp, method_offset));
4645 // lr = temp->GetEntryPoint();
4646 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004647 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004648 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004649 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4650 // lr();
4651 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004652 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004653 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004654}
4655
Orion Hodsonac141392017-01-13 11:53:47 +00004656void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4657 HandleInvoke(invoke);
4658}
4659
4660void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4661 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004662 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004663}
4664
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004665void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4666 HandleInvoke(invoke);
4667}
4668
4669void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4670 codegen_->GenerateInvokeCustomCall(invoke);
4671 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
4672}
4673
Vladimir Markob066d432018-01-03 13:14:37 +00004674vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4675 uint32_t boot_image_offset,
4676 vixl::aarch64::Label* adrp_label) {
4677 return NewPcRelativePatch(
4678 /* dex_file */ nullptr, boot_image_offset, adrp_label, &boot_image_method_patches_);
4679}
4680
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004681vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004682 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004683 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004684 return NewPcRelativePatch(
4685 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004686}
4687
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004688vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4689 MethodReference target_method,
4690 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004691 return NewPcRelativePatch(
4692 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004693}
4694
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004695vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004696 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004697 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004698 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004699 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004700}
4701
Vladimir Marko1998cd02017-01-13 13:02:58 +00004702vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4703 const DexFile& dex_file,
4704 dex::TypeIndex type_index,
4705 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004706 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004707}
4708
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004709vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004710 const DexFile& dex_file,
4711 dex::StringIndex string_index,
4712 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004713 return NewPcRelativePatch(
4714 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004715}
4716
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004717vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4718 const DexFile& dex_file,
4719 dex::StringIndex string_index,
4720 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004721 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004722}
4723
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004724vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4725 baker_read_barrier_patches_.emplace_back(custom_data);
4726 return &baker_read_barrier_patches_.back().label;
4727}
4728
Scott Wakeling97c72b72016-06-24 16:19:36 +01004729vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004730 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004731 uint32_t offset_or_index,
4732 vixl::aarch64::Label* adrp_label,
4733 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004734 // Add a patch entry and return the label.
4735 patches->emplace_back(dex_file, offset_or_index);
4736 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004737 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004738 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4739 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4740 return label;
4741}
4742
Scott Wakeling97c72b72016-06-24 16:19:36 +01004743vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4744 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004745 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004746}
4747
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004748vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004749 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004750 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004751 return jit_string_patches_.GetOrCreate(
4752 StringReference(&dex_file, string_index),
4753 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4754}
4755
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004756vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004757 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004758 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004759 return jit_class_patches_.GetOrCreate(
4760 TypeReference(&dex_file, type_index),
4761 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4762}
4763
Vladimir Markoaad75c62016-10-03 08:46:48 +00004764void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4765 vixl::aarch64::Register reg) {
4766 DCHECK(reg.IsX());
4767 SingleEmissionCheckScope guard(GetVIXLAssembler());
4768 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004769 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004770}
4771
4772void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4773 vixl::aarch64::Register out,
4774 vixl::aarch64::Register base) {
4775 DCHECK(out.IsX());
4776 DCHECK(base.IsX());
4777 SingleEmissionCheckScope guard(GetVIXLAssembler());
4778 __ Bind(fixup_label);
4779 __ add(out, base, Operand(/* offset placeholder */ 0));
4780}
4781
4782void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4783 vixl::aarch64::Register out,
4784 vixl::aarch64::Register base) {
4785 DCHECK(base.IsX());
4786 SingleEmissionCheckScope guard(GetVIXLAssembler());
4787 __ Bind(fixup_label);
4788 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4789}
4790
Vladimir Markoeebb8212018-06-05 14:57:24 +01004791void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
4792 uint32_t boot_image_offset) {
4793 DCHECK(!GetCompilerOptions().IsBootImage());
4794 if (GetCompilerOptions().GetCompilePic()) {
4795 DCHECK(Runtime::Current()->IsAotCompiler());
4796 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4797 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4798 EmitAdrpPlaceholder(adrp_label, reg.X());
4799 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4800 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4801 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4802 } else {
4803 gc::Heap* heap = Runtime::Current()->GetHeap();
4804 DCHECK(!heap->GetBootImageSpaces().empty());
4805 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_offset;
4806 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4807 }
4808}
4809
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004810template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004811inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4812 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004813 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004814 for (const PcRelativePatchInfo& info : infos) {
4815 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004816 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004817 info.pc_insn_label->GetLocation(),
4818 info.offset_or_index));
4819 }
4820}
4821
Vladimir Markob066d432018-01-03 13:14:37 +00004822linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
4823 const DexFile* target_dex_file,
4824 uint32_t pc_insn_offset,
4825 uint32_t boot_image_offset) {
4826 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
4827 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
4828}
4829
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004830void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004831 DCHECK(linker_patches->empty());
4832 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004833 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004834 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004835 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004836 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004837 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004838 string_bss_entry_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004839 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004840 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004841 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004842 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004843 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004844 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004845 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004846 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004847 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004848 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00004849 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
4850 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004851 DCHECK(boot_image_type_patches_.empty());
4852 DCHECK(boot_image_string_patches_.empty());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004853 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004854 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4855 method_bss_entry_patches_, linker_patches);
4856 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4857 type_bss_entry_patches_, linker_patches);
4858 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4859 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004860 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004861 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4862 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004863 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004864 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004865}
4866
Vladimir Markoca1e0382018-04-11 09:58:41 +00004867bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
4868 return patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
4869 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4870}
4871
4872void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4873 /*out*/ ArenaVector<uint8_t>* code,
4874 /*out*/ std::string* debug_name) {
4875 Arm64Assembler assembler(GetGraph()->GetAllocator());
4876 switch (patch.GetType()) {
4877 case linker::LinkerPatch::Type::kCallRelative: {
4878 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4879 // to the generic JNI and interpreter trampolines.
4880 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4881 kArm64PointerSize).Int32Value());
4882 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4883 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4884 *debug_name = "MethodCallThunk";
4885 }
4886 break;
4887 }
4888 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4889 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4890 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4891 break;
4892 }
4893 default:
4894 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4895 UNREACHABLE();
4896 }
4897
4898 // Ensure we emit the literal pool if any.
4899 assembler.FinalizeCode();
4900 code->resize(assembler.CodeSize());
4901 MemoryRegion code_region(code->data(), code->size());
4902 assembler.FinalizeInstructions(code_region);
4903}
4904
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004905vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4906 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004907 value,
4908 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4909}
4910
Scott Wakeling97c72b72016-06-24 16:19:36 +01004911vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004912 return uint64_literals_.GetOrCreate(
4913 value,
4914 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004915}
4916
Andreas Gampe878d58c2015-01-15 23:24:00 -08004917void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004918 // Explicit clinit checks triggered by static invokes must have been pruned by
4919 // art::PrepareForRegisterAllocation.
4920 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004921
Andreas Gampe878d58c2015-01-15 23:24:00 -08004922 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004923 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004924 return;
4925 }
4926
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004927 {
4928 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4929 // are no pools emitted.
4930 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4931 LocationSummary* locations = invoke->GetLocations();
4932 codegen_->GenerateStaticOrDirectCall(
4933 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4934 }
4935
4936 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004937}
4938
4939void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004940 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004941 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004942 return;
4943 }
4944
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004945 {
4946 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4947 // are no pools emitted.
4948 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4949 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4950 DCHECK(!codegen_->IsLeafMethod());
4951 }
4952
4953 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004954}
4955
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004956HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4957 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004958 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004959 case HLoadClass::LoadKind::kInvalid:
4960 LOG(FATAL) << "UNREACHABLE";
4961 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004962 case HLoadClass::LoadKind::kReferrersClass:
4963 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004964 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004965 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004966 case HLoadClass::LoadKind::kBssEntry:
4967 DCHECK(!Runtime::Current()->UseJitCompilation());
4968 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004969 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004970 DCHECK(Runtime::Current()->UseJitCompilation());
4971 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004972 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004973 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004974 break;
4975 }
4976 return desired_class_load_kind;
4977}
4978
Alexandre Rames67555f72014-11-18 10:55:16 +00004979void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004980 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004981 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004982 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004983 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004984 cls,
4985 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004986 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004987 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004988 return;
4989 }
Vladimir Marko41559982017-01-06 14:04:23 +00004990 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004991
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004992 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4993 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004994 ? LocationSummary::kCallOnSlowPath
4995 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004996 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004997 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004998 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004999 }
5000
Vladimir Marko41559982017-01-06 14:04:23 +00005001 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005002 locations->SetInAt(0, Location::RequiresRegister());
5003 }
5004 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005005 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
5006 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5007 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markoea4c1262017-02-06 19:59:33 +00005008 RegisterSet caller_saves = RegisterSet::Empty();
5009 InvokeRuntimeCallingConvention calling_convention;
5010 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5011 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5013 DataType::Type::kReference).GetCode());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005014 locations->SetCustomSlowPathCallerSaves(caller_saves);
5015 } else {
5016 // For non-Baker read barrier we have a temp-clobbering call.
5017 }
5018 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005019}
5020
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005021// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5022// move.
5023void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005024 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005025 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005026 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005027 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01005028 return;
5029 }
Vladimir Marko41559982017-01-06 14:04:23 +00005030 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005031
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005032 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01005033 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00005034
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005035 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5036 ? kWithoutReadBarrier
5037 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005038 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005039 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005040 case HLoadClass::LoadKind::kReferrersClass: {
5041 DCHECK(!cls->CanCallRuntime());
5042 DCHECK(!cls->MustGenerateClinitCheck());
5043 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5044 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00005045 codegen_->GenerateGcRootFieldLoad(cls,
5046 out_loc,
5047 current_method,
5048 ArtMethod::DeclaringClassOffset().Int32Value(),
5049 /* fixup_label */ nullptr,
5050 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005051 break;
5052 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005053 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005054 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005055 // Add ADRP with its PC-relative type patch.
5056 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08005057 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005058 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005059 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005060 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005061 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005062 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005063 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005064 break;
5065 }
5066 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005067 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005068 uint32_t address = dchecked_integral_cast<uint32_t>(
5069 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5070 DCHECK_NE(address, 0u);
5071 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005072 break;
5073 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005074 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005075 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005076 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
5077 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5078 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005079 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005080 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005081 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005082 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005083 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005084 break;
5085 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005086 case HLoadClass::LoadKind::kBssEntry: {
5087 // Add ADRP with its PC-relative Class .bss entry patch.
5088 const DexFile& dex_file = cls->GetDexFile();
5089 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005090 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
5091 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
5092 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005093 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005094 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005095 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005096 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005097 codegen_->GenerateGcRootFieldLoad(cls,
5098 out_loc,
5099 temp,
5100 /* offset placeholder */ 0u,
5101 ldr_label,
5102 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005103 generate_null_check = true;
5104 break;
5105 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005106 case HLoadClass::LoadKind::kJitTableAddress: {
5107 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5108 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005109 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005110 codegen_->GenerateGcRootFieldLoad(cls,
5111 out_loc,
5112 out.X(),
5113 /* offset */ 0,
5114 /* fixup_label */ nullptr,
5115 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005116 break;
5117 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005118 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005119 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005120 LOG(FATAL) << "UNREACHABLE";
5121 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005122 }
5123
Vladimir Markoea4c1262017-02-06 19:59:33 +00005124 bool do_clinit = cls->MustGenerateClinitCheck();
5125 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005126 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005127 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00005128 cls, cls, cls->GetDexPc(), do_clinit);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005129 codegen_->AddSlowPath(slow_path);
5130 if (generate_null_check) {
5131 __ Cbz(out, slow_path->GetEntryLabel());
5132 }
5133 if (cls->MustGenerateClinitCheck()) {
5134 GenerateClassInitializationCheck(slow_path, out);
5135 } else {
5136 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005137 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005138 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005139 }
5140}
5141
Orion Hodsondbaa5c72018-05-10 08:22:46 +01005142void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5143 InvokeRuntimeCallingConvention calling_convention;
5144 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5145 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
5146}
5147
5148void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
5149 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
5150}
5151
Orion Hodson18259d72018-04-12 11:18:23 +01005152void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
5153 InvokeRuntimeCallingConvention calling_convention;
5154 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
5155 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
5156}
5157
5158void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
5159 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
5160}
5161
David Brazdilcb1c0552015-08-04 16:22:25 +01005162static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005163 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005164}
5165
Alexandre Rames67555f72014-11-18 10:55:16 +00005166void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5167 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005168 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005169 locations->SetOut(Location::RequiresRegister());
5170}
5171
5172void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005173 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5174}
5175
5176void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005177 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005178}
5179
5180void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5181 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005182}
5183
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005184HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5185 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005186 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005187 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005188 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005189 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005190 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005191 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005192 case HLoadString::LoadKind::kJitTableAddress:
5193 DCHECK(Runtime::Current()->UseJitCompilation());
5194 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005195 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005196 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005197 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005198 }
5199 return desired_string_load_kind;
5200}
5201
Alexandre Rames67555f72014-11-18 10:55:16 +00005202void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005203 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005204 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005205 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005206 InvokeRuntimeCallingConvention calling_convention;
5207 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5208 } else {
5209 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005210 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5211 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005212 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005213 RegisterSet caller_saves = RegisterSet::Empty();
5214 InvokeRuntimeCallingConvention calling_convention;
5215 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5216 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5218 DataType::Type::kReference).GetCode());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005219 locations->SetCustomSlowPathCallerSaves(caller_saves);
5220 } else {
5221 // For non-Baker read barrier we have a temp-clobbering call.
5222 }
5223 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005224 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005225}
5226
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005227// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5228// move.
5229void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005230 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005231 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005232
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005233 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005234 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005235 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005236 // Add ADRP with its PC-relative String patch.
5237 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005238 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005239 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005240 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005241 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005242 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005243 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005244 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005245 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005246 }
5247 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005248 uint32_t address = dchecked_integral_cast<uint32_t>(
5249 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5250 DCHECK_NE(address, 0u);
5251 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005252 return;
5253 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005254 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005255 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005256 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5257 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5258 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005259 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005260 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005261 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005262 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005263 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5264 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005265 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005266 case HLoadString::LoadKind::kBssEntry: {
5267 // Add ADRP with its PC-relative String .bss entry patch.
5268 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005269 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005270 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof3c52b42017-11-17 17:32:12 +00005271 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005272 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005273 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005274 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005275 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005276 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005277 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoca1e0382018-04-11 09:58:41 +00005278 codegen_->GenerateGcRootFieldLoad(load,
5279 out_loc,
5280 temp,
5281 /* offset placeholder */ 0u,
5282 ldr_label,
5283 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005284 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005285 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005286 codegen_->AddSlowPath(slow_path);
5287 __ Cbz(out.X(), slow_path->GetEntryLabel());
5288 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005289 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005290 return;
5291 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005292 case HLoadString::LoadKind::kJitTableAddress: {
5293 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005294 load->GetStringIndex(),
5295 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005296 codegen_->GenerateGcRootFieldLoad(load,
5297 out_loc,
5298 out.X(),
5299 /* offset */ 0,
5300 /* fixup_label */ nullptr,
5301 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005302 return;
5303 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005304 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005305 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005306 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005307
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005308 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005309 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005310 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005311 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005312 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5313 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005314 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005315}
5316
Alexandre Rames5319def2014-10-23 10:03:10 +01005317void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005318 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005319 locations->SetOut(Location::ConstantLocation(constant));
5320}
5321
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005322void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005323 // Will be generated at use site.
5324}
5325
Alexandre Rames67555f72014-11-18 10:55:16 +00005326void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005327 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5328 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005329 InvokeRuntimeCallingConvention calling_convention;
5330 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5331}
5332
5333void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005334 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005335 instruction,
5336 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005337 if (instruction->IsEnter()) {
5338 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5339 } else {
5340 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5341 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005342 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005343}
5344
Alexandre Rames42d641b2014-10-27 14:00:51 +00005345void LocationsBuilderARM64::VisitMul(HMul* mul) {
5346 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005347 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005348 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005349 case DataType::Type::kInt32:
5350 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005351 locations->SetInAt(0, Location::RequiresRegister());
5352 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005354 break;
5355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 case DataType::Type::kFloat32:
5357 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005358 locations->SetInAt(0, Location::RequiresFpuRegister());
5359 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005360 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005361 break;
5362
5363 default:
5364 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5365 }
5366}
5367
5368void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5369 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 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5373 break;
5374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005375 case DataType::Type::kFloat32:
5376 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005377 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005378 break;
5379
5380 default:
5381 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5382 }
5383}
5384
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005385void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5386 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005387 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005388 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005389 case DataType::Type::kInt32:
5390 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005391 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005392 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005393 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005394
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005395 case DataType::Type::kFloat32:
5396 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005397 locations->SetInAt(0, Location::RequiresFpuRegister());
5398 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005399 break;
5400
5401 default:
5402 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5403 }
5404}
5405
5406void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5407 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005408 case DataType::Type::kInt32:
5409 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005410 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5411 break;
5412
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005413 case DataType::Type::kFloat32:
5414 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005415 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005416 break;
5417
5418 default:
5419 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5420 }
5421}
5422
5423void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005424 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5425 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005426 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005427 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005428 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5429 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005430}
5431
5432void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005433 // Note: if heap poisoning is enabled, the entry point takes cares
5434 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005435 QuickEntrypointEnum entrypoint =
5436 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5437 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005438 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005439 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005440}
5441
Alexandre Rames5319def2014-10-23 10:03:10 +01005442void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005443 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5444 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005445 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005446 if (instruction->IsStringAlloc()) {
5447 locations->AddTemp(LocationFrom(kArtMethodRegister));
5448 } else {
5449 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005450 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005451 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005452}
5453
5454void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005455 // Note: if heap poisoning is enabled, the entry point takes cares
5456 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005457 if (instruction->IsStringAlloc()) {
5458 // String is allocated through StringFactory. Call NewEmptyString entry point.
5459 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005460 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005461 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5462 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005463
5464 {
5465 // Ensure the pc position is recorded immediately after the `blr` instruction.
5466 ExactAssemblyScope eas(GetVIXLAssembler(),
5467 kInstructionSize,
5468 CodeBufferCheckScope::kExactSize);
5469 __ blr(lr);
5470 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5471 }
David Brazdil6de19382016-01-08 17:37:10 +00005472 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005473 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005474 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005475 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005476 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005477}
5478
5479void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005480 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005481 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005483}
5484
5485void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005486 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005487 case DataType::Type::kInt32:
5488 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005489 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005490 break;
5491
5492 default:
5493 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5494 }
5495}
5496
David Brazdil66d126e2015-04-03 16:02:44 +01005497void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005498 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005499 locations->SetInAt(0, Location::RequiresRegister());
5500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5501}
5502
5503void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005504 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005505}
5506
Alexandre Rames5319def2014-10-23 10:03:10 +01005507void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005508 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5509 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005510}
5511
Calin Juravle2ae48182016-03-16 14:05:09 +00005512void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5513 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005514 return;
5515 }
Artem Serov914d7a82017-02-07 14:33:49 +00005516 {
5517 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5518 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5519 Location obj = instruction->GetLocations()->InAt(0);
5520 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5521 RecordPcInfo(instruction, instruction->GetDexPc());
5522 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005523}
5524
Calin Juravle2ae48182016-03-16 14:05:09 +00005525void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005526 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005527 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005528
5529 LocationSummary* locations = instruction->GetLocations();
5530 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005531
5532 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005533}
5534
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005535void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005536 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005537}
5538
Alexandre Rames67555f72014-11-18 10:55:16 +00005539void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5540 HandleBinaryOp(instruction);
5541}
5542
5543void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5544 HandleBinaryOp(instruction);
5545}
5546
Alexandre Rames3e69f162014-12-10 10:36:50 +00005547void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5548 LOG(FATAL) << "Unreachable";
5549}
5550
5551void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005552 if (instruction->GetNext()->IsSuspendCheck() &&
5553 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5554 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5555 // The back edge will generate the suspend check.
5556 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5557 }
5558
Alexandre Rames3e69f162014-12-10 10:36:50 +00005559 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5560}
5561
Alexandre Rames5319def2014-10-23 10:03:10 +01005562void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005563 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005564 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5565 if (location.IsStackSlot()) {
5566 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5567 } else if (location.IsDoubleStackSlot()) {
5568 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5569 }
5570 locations->SetOut(location);
5571}
5572
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005573void InstructionCodeGeneratorARM64::VisitParameterValue(
5574 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005575 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005576}
5577
5578void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5579 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005580 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005581 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005582}
5583
5584void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5585 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5586 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005587}
5588
5589void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005590 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005591 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005592 locations->SetInAt(i, Location::Any());
5593 }
5594 locations->SetOut(Location::Any());
5595}
5596
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005597void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005598 LOG(FATAL) << "Unreachable";
5599}
5600
Serban Constantinescu02164b32014-11-13 14:05:07 +00005601void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005602 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005603 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005604 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005605 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005606 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005607
5608 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005609 case DataType::Type::kInt32:
5610 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005611 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005612 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005613 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5614 break;
5615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005616 case DataType::Type::kFloat32:
5617 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005618 InvokeRuntimeCallingConvention calling_convention;
5619 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5620 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5621 locations->SetOut(calling_convention.GetReturnLocation(type));
5622
5623 break;
5624 }
5625
Serban Constantinescu02164b32014-11-13 14:05:07 +00005626 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005627 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005628 }
5629}
5630
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005631void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
5632 int64_t imm = Int64ConstantFrom(instruction->GetLocations()->InAt(1));
5633 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5634 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5635
5636 Register out = OutputRegister(instruction);
5637 Register dividend = InputRegisterAt(instruction, 0);
5638 int ctz_imm = CTZ(abs_imm);
5639
5640 UseScratchRegisterScope temps(GetVIXLAssembler());
5641 Register temp = temps.AcquireSameSizeAs(out);
5642
5643 int bits = (instruction->GetResultType() == DataType::Type::kInt32) ? 32 : 64;
5644 __ Asr(temp, dividend, bits - 1);
5645 __ Lsr(temp, temp, bits - ctz_imm);
5646 __ Add(out, dividend, temp);
5647 __ And(out, out, abs_imm - 1);
5648 __ Sub(out, out, temp);
5649}
5650
5651void InstructionCodeGeneratorARM64::GenerateIntRemForOneOrMinusOneDenom(HRem *instruction) {
5652 int64_t imm = Int64ConstantFrom(instruction->GetLocations()->InAt(1));
5653 DCHECK(imm == 1 || imm == -1) << imm;
5654
5655 Register out = OutputRegister(instruction);
5656 __ Mov(out, 0);
5657}
5658
5659void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
5660 int64_t imm = Int64ConstantFrom(instruction->GetLocations()->InAt(1));
5661
5662 if (imm == 0) {
5663 // Do not generate anything.
5664 // DivZeroCheck would prevent any code to be executed.
5665 return;
5666 }
5667
5668 if (imm == 1 || imm == -1) {
5669 // TODO: These cases need to be optimized in InstructionSimplifier
5670 GenerateIntRemForOneOrMinusOneDenom(instruction);
5671 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
5672 GenerateIntRemForPower2Denom(instruction);
5673 } else {
5674 DCHECK(imm < -2 || imm > 2) << imm;
5675 GenerateDivRemWithAnyConstant(instruction);
5676 }
5677}
5678
5679void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5680 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5681 << instruction->GetResultType();
5682
5683 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5684 GenerateIntRemForConstDenom(instruction);
5685 } else {
5686 Register out = OutputRegister(instruction);
5687 Register dividend = InputRegisterAt(instruction, 0);
5688 Register divisor = InputRegisterAt(instruction, 1);
5689 UseScratchRegisterScope temps(GetVIXLAssembler());
5690 Register temp = temps.AcquireSameSizeAs(out);
5691 __ Sdiv(temp, dividend, divisor);
5692 __ Msub(out, temp, divisor, dividend);
5693 }
5694}
5695
Serban Constantinescu02164b32014-11-13 14:05:07 +00005696void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005697 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005698
Serban Constantinescu02164b32014-11-13 14:05:07 +00005699 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005700 case DataType::Type::kInt32:
5701 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005702 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005703 break;
5704 }
5705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005706 case DataType::Type::kFloat32:
5707 case DataType::Type::kFloat64: {
5708 QuickEntrypointEnum entrypoint =
5709 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005710 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005711 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005712 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5713 } else {
5714 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5715 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005716 break;
5717 }
5718
Serban Constantinescu02164b32014-11-13 14:05:07 +00005719 default:
5720 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005721 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005722 }
5723}
5724
Aart Bik1f8d51b2018-02-15 10:42:37 -08005725void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005726 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005727}
5728
Aart Bik1f8d51b2018-02-15 10:42:37 -08005729void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005730 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005731}
5732
5733void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005734 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005735}
5736
5737void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005738 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005739}
5740
Aart Bik3dad3412018-02-28 12:01:46 -08005741void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5742 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5743 switch (abs->GetResultType()) {
5744 case DataType::Type::kInt32:
5745 case DataType::Type::kInt64:
5746 locations->SetInAt(0, Location::RequiresRegister());
5747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5748 break;
5749 case DataType::Type::kFloat32:
5750 case DataType::Type::kFloat64:
5751 locations->SetInAt(0, Location::RequiresFpuRegister());
5752 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5753 break;
5754 default:
5755 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5756 }
5757}
5758
5759void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5760 switch (abs->GetResultType()) {
5761 case DataType::Type::kInt32:
5762 case DataType::Type::kInt64: {
5763 Register in_reg = InputRegisterAt(abs, 0);
5764 Register out_reg = OutputRegister(abs);
5765 __ Cmp(in_reg, Operand(0));
5766 __ Cneg(out_reg, in_reg, lt);
5767 break;
5768 }
5769 case DataType::Type::kFloat32:
5770 case DataType::Type::kFloat64: {
5771 FPRegister in_reg = InputFPRegisterAt(abs, 0);
5772 FPRegister out_reg = OutputFPRegister(abs);
5773 __ Fabs(out_reg, in_reg);
5774 break;
5775 }
5776 default:
5777 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5778 }
5779}
5780
Igor Murashkind01745e2017-04-05 16:40:31 -07005781void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5782 constructor_fence->SetLocations(nullptr);
5783}
5784
5785void InstructionCodeGeneratorARM64::VisitConstructorFence(
5786 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5787 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5788}
5789
Calin Juravle27df7582015-04-17 19:12:31 +01005790void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5791 memory_barrier->SetLocations(nullptr);
5792}
5793
5794void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005795 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005796}
5797
Alexandre Rames5319def2014-10-23 10:03:10 +01005798void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005799 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005801 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005802}
5803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005804void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005805 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005806}
5807
5808void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5809 instruction->SetLocations(nullptr);
5810}
5811
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005812void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005813 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005814}
5815
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005816void LocationsBuilderARM64::VisitRor(HRor* ror) {
5817 HandleBinaryOp(ror);
5818}
5819
5820void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5821 HandleBinaryOp(ror);
5822}
5823
Serban Constantinescu02164b32014-11-13 14:05:07 +00005824void LocationsBuilderARM64::VisitShl(HShl* shl) {
5825 HandleShift(shl);
5826}
5827
5828void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5829 HandleShift(shl);
5830}
5831
5832void LocationsBuilderARM64::VisitShr(HShr* shr) {
5833 HandleShift(shr);
5834}
5835
5836void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5837 HandleShift(shr);
5838}
5839
Alexandre Rames5319def2014-10-23 10:03:10 +01005840void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005841 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005842}
5843
5844void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005845 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005846}
5847
Alexandre Rames67555f72014-11-18 10:55:16 +00005848void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005849 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005850}
5851
5852void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005853 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005854}
5855
5856void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005857 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005858}
5859
Alexandre Rames67555f72014-11-18 10:55:16 +00005860void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005861 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005862}
5863
Calin Juravlee460d1d2015-09-29 04:52:17 +01005864void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5865 HUnresolvedInstanceFieldGet* instruction) {
5866 FieldAccessCallingConventionARM64 calling_convention;
5867 codegen_->CreateUnresolvedFieldLocationSummary(
5868 instruction, instruction->GetFieldType(), calling_convention);
5869}
5870
5871void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5872 HUnresolvedInstanceFieldGet* instruction) {
5873 FieldAccessCallingConventionARM64 calling_convention;
5874 codegen_->GenerateUnresolvedFieldAccess(instruction,
5875 instruction->GetFieldType(),
5876 instruction->GetFieldIndex(),
5877 instruction->GetDexPc(),
5878 calling_convention);
5879}
5880
5881void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5882 HUnresolvedInstanceFieldSet* instruction) {
5883 FieldAccessCallingConventionARM64 calling_convention;
5884 codegen_->CreateUnresolvedFieldLocationSummary(
5885 instruction, instruction->GetFieldType(), calling_convention);
5886}
5887
5888void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5889 HUnresolvedInstanceFieldSet* instruction) {
5890 FieldAccessCallingConventionARM64 calling_convention;
5891 codegen_->GenerateUnresolvedFieldAccess(instruction,
5892 instruction->GetFieldType(),
5893 instruction->GetFieldIndex(),
5894 instruction->GetDexPc(),
5895 calling_convention);
5896}
5897
5898void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5899 HUnresolvedStaticFieldGet* instruction) {
5900 FieldAccessCallingConventionARM64 calling_convention;
5901 codegen_->CreateUnresolvedFieldLocationSummary(
5902 instruction, instruction->GetFieldType(), calling_convention);
5903}
5904
5905void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5906 HUnresolvedStaticFieldGet* instruction) {
5907 FieldAccessCallingConventionARM64 calling_convention;
5908 codegen_->GenerateUnresolvedFieldAccess(instruction,
5909 instruction->GetFieldType(),
5910 instruction->GetFieldIndex(),
5911 instruction->GetDexPc(),
5912 calling_convention);
5913}
5914
5915void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5916 HUnresolvedStaticFieldSet* instruction) {
5917 FieldAccessCallingConventionARM64 calling_convention;
5918 codegen_->CreateUnresolvedFieldLocationSummary(
5919 instruction, instruction->GetFieldType(), calling_convention);
5920}
5921
5922void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5923 HUnresolvedStaticFieldSet* instruction) {
5924 FieldAccessCallingConventionARM64 calling_convention;
5925 codegen_->GenerateUnresolvedFieldAccess(instruction,
5926 instruction->GetFieldType(),
5927 instruction->GetFieldIndex(),
5928 instruction->GetDexPc(),
5929 calling_convention);
5930}
5931
Alexandre Rames5319def2014-10-23 10:03:10 +01005932void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005933 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5934 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005935 // In suspend check slow path, usually there are no caller-save registers at all.
5936 // If SIMD instructions are present, however, we force spilling all live SIMD
5937 // registers in full width (since the runtime only saves/restores lower part).
5938 locations->SetCustomSlowPathCallerSaves(
5939 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005940}
5941
5942void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005943 HBasicBlock* block = instruction->GetBlock();
5944 if (block->GetLoopInformation() != nullptr) {
5945 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5946 // The back edge will generate the suspend check.
5947 return;
5948 }
5949 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5950 // The goto will generate the suspend check.
5951 return;
5952 }
5953 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005954 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005955}
5956
Alexandre Rames67555f72014-11-18 10:55:16 +00005957void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005958 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5959 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005960 InvokeRuntimeCallingConvention calling_convention;
5961 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5962}
5963
5964void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005965 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005966 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005967}
5968
5969void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5970 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005971 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005972 DataType::Type input_type = conversion->GetInputType();
5973 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005974 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5975 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5977 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005978 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5979 }
5980
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005982 locations->SetInAt(0, Location::RequiresFpuRegister());
5983 } else {
5984 locations->SetInAt(0, Location::RequiresRegister());
5985 }
5986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005988 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5989 } else {
5990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5991 }
5992}
5993
5994void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 DataType::Type result_type = conversion->GetResultType();
5996 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005997
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005998 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5999 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006001 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
6002 int result_size = DataType::Size(result_type);
6003 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00006004 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006005 Register output = OutputRegister(conversion);
6006 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006007 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01006008 // 'int' values are used directly as W registers, discarding the top
6009 // bits, so we don't need to sign-extend and can just perform a move.
6010 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
6011 // top 32 bits of the target register. We theoretically could leave those
6012 // bits unchanged, but we would have to make sure that no code uses a
6013 // 32bit input value as a 64bit value assuming that the top 32 bits are
6014 // zero.
6015 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006016 } else if (DataType::IsUnsignedType(result_type) ||
6017 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
6018 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006019 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00006020 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00006021 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006022 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006023 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6025 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00006026 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006027 } else if (DataType::IsFloatingPointType(result_type) &&
6028 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00006029 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
6030 } else {
6031 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6032 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00006033 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00006034}
Alexandre Rames67555f72014-11-18 10:55:16 +00006035
Serban Constantinescu02164b32014-11-13 14:05:07 +00006036void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
6037 HandleShift(ushr);
6038}
6039
6040void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
6041 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00006042}
6043
6044void LocationsBuilderARM64::VisitXor(HXor* instruction) {
6045 HandleBinaryOp(instruction);
6046}
6047
6048void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
6049 HandleBinaryOp(instruction);
6050}
6051
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006052void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006053 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006054 LOG(FATAL) << "Unreachable";
6055}
6056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006057void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006058 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006059 LOG(FATAL) << "Unreachable";
6060}
6061
Mark Mendellfe57faa2015-09-18 09:26:15 -04006062// Simple implementation of packed switch - generate cascaded compare/jumps.
6063void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6064 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006065 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006066 locations->SetInAt(0, Location::RequiresRegister());
6067}
6068
6069void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6070 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08006071 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006072 Register value_reg = InputRegisterAt(switch_instr, 0);
6073 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6074
Zheng Xu3927c8b2015-11-18 17:46:25 +08006075 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01006076 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08006077 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
6078 // make sure we don't emit it if the target may run out of range.
6079 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
6080 // ranges and emit the tables only as required.
6081 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04006082
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006083 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08006084 // Current instruction id is an upper bound of the number of HIRs in the graph.
6085 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
6086 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006087 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6088 Register temp = temps.AcquireW();
6089 __ Subs(temp, value_reg, Operand(lower_bound));
6090
Zheng Xu3927c8b2015-11-18 17:46:25 +08006091 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006092 // Jump to successors[0] if value == lower_bound.
6093 __ B(eq, codegen_->GetLabelOf(successors[0]));
6094 int32_t last_index = 0;
6095 for (; num_entries - last_index > 2; last_index += 2) {
6096 __ Subs(temp, temp, Operand(2));
6097 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6098 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
6099 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6100 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
6101 }
6102 if (num_entries - last_index == 2) {
6103 // The last missing case_value.
6104 __ Cmp(temp, Operand(1));
6105 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08006106 }
6107
6108 // And the default for any other value.
6109 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6110 __ B(codegen_->GetLabelOf(default_block));
6111 }
6112 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01006113 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08006114
6115 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
6116
6117 // Below instructions should use at most one blocked register. Since there are two blocked
6118 // registers, we are free to block one.
6119 Register temp_w = temps.AcquireW();
6120 Register index;
6121 // Remove the bias.
6122 if (lower_bound != 0) {
6123 index = temp_w;
6124 __ Sub(index, value_reg, Operand(lower_bound));
6125 } else {
6126 index = value_reg;
6127 }
6128
6129 // Jump to default block if index is out of the range.
6130 __ Cmp(index, Operand(num_entries));
6131 __ B(hs, codegen_->GetLabelOf(default_block));
6132
6133 // In current VIXL implementation, it won't require any blocked registers to encode the
6134 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
6135 // register pressure.
6136 Register table_base = temps.AcquireX();
6137 // Load jump offset from the table.
6138 __ Adr(table_base, jump_table->GetTableStartLabel());
6139 Register jump_offset = temp_w;
6140 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
6141
6142 // Jump to target block by branching to table_base(pc related) + offset.
6143 Register target_address = table_base;
6144 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
6145 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006146 }
6147}
6148
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006149void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
6150 HInstruction* instruction,
6151 Location out,
6152 uint32_t offset,
6153 Location maybe_temp,
6154 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006156 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006157 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006158 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006159 if (kUseBakerReadBarrier) {
6160 // Load with fast path based Baker's read barrier.
6161 // /* HeapReference<Object> */ out = *(out + offset)
6162 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6163 out,
6164 out_reg,
6165 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006166 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006167 /* needs_null_check */ false,
6168 /* use_load_acquire */ false);
6169 } else {
6170 // Load with slow path based read barrier.
6171 // Save the value of `out` into `maybe_temp` before overwriting it
6172 // in the following move operation, as we will need it for the
6173 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006174 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00006175 __ Mov(temp_reg, out_reg);
6176 // /* HeapReference<Object> */ out = *(out + offset)
6177 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6178 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6179 }
6180 } else {
6181 // Plain load with no read barrier.
6182 // /* HeapReference<Object> */ out = *(out + offset)
6183 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6184 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6185 }
6186}
6187
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006188void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6189 HInstruction* instruction,
6190 Location out,
6191 Location obj,
6192 uint32_t offset,
6193 Location maybe_temp,
6194 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006195 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006196 Register out_reg = RegisterFrom(out, type);
6197 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006198 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006199 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006200 if (kUseBakerReadBarrier) {
6201 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006202 // /* HeapReference<Object> */ out = *(obj + offset)
6203 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6204 out,
6205 obj_reg,
6206 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006207 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006208 /* needs_null_check */ false,
6209 /* use_load_acquire */ false);
6210 } else {
6211 // Load with slow path based read barrier.
6212 // /* HeapReference<Object> */ out = *(obj + offset)
6213 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6214 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6215 }
6216 } else {
6217 // Plain load with no read barrier.
6218 // /* HeapReference<Object> */ out = *(obj + offset)
6219 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6220 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6221 }
6222}
6223
Vladimir Markoca1e0382018-04-11 09:58:41 +00006224void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006225 HInstruction* instruction,
6226 Location root,
6227 Register obj,
6228 uint32_t offset,
6229 vixl::aarch64::Label* fixup_label,
6230 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006231 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006232 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006233 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006234 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006235 if (kUseBakerReadBarrier) {
6236 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006237 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006238 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
6239 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006240 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6241 // the Marking Register) to decide whether we need to enter
6242 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006243 //
6244 // We use link-time generated thunks for the slow path. That thunk
6245 // checks the reference and jumps to the entrypoint if needed.
6246 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006247 // lr = &return_address;
6248 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006249 // if (mr) { // Thread::Current()->GetIsGcMarking()
6250 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006251 // }
6252 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00006253
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006254 UseScratchRegisterScope temps(GetVIXLAssembler());
6255 DCHECK(temps.IsAvailable(ip0));
6256 DCHECK(temps.IsAvailable(ip1));
6257 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006258 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
6259 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00006260
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006261 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6262 vixl::aarch64::Label return_address;
6263 __ adr(lr, &return_address);
6264 if (fixup_label != nullptr) {
6265 __ Bind(fixup_label);
6266 }
6267 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6268 "GC root LDR must be 2 instruction (8B) before the return address label.");
6269 __ ldr(root_reg, MemOperand(obj.X(), offset));
6270 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006271 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006272 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006273 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006274 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6275 // the Marking Register) to decide whether we need to enter
6276 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006277 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006278 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006279 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006280 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006281 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6282 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006283 // }
Roland Levillain44015862016-01-22 11:47:17 +00006284
Roland Levillain97c46462017-05-11 14:04:03 +01006285 // Slow path marking the GC root `root`. The entrypoint will
6286 // be loaded by the slow path code.
6287 SlowPathCodeARM64* slow_path =
Vladimir Markoca1e0382018-04-11 09:58:41 +00006288 new (GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
6289 AddSlowPath(slow_path);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006290
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006291 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6292 if (fixup_label == nullptr) {
6293 __ Ldr(root_reg, MemOperand(obj, offset));
6294 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006295 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006296 }
6297 static_assert(
6298 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6299 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6300 "have different sizes.");
6301 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6302 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6303 "have different sizes.");
6304
Roland Levillain97c46462017-05-11 14:04:03 +01006305 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006306 __ Bind(slow_path->GetExitLabel());
6307 }
Roland Levillain44015862016-01-22 11:47:17 +00006308 } else {
6309 // GC root loaded through a slow path for read barriers other
6310 // than Baker's.
6311 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006312 if (fixup_label == nullptr) {
6313 __ Add(root_reg.X(), obj.X(), offset);
6314 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006315 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006316 }
Roland Levillain44015862016-01-22 11:47:17 +00006317 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006318 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006319 }
6320 } else {
6321 // Plain GC root load with no read barrier.
6322 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006323 if (fixup_label == nullptr) {
6324 __ Ldr(root_reg, MemOperand(obj, offset));
6325 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006326 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006327 }
Roland Levillain44015862016-01-22 11:47:17 +00006328 // Note that GC roots are not affected by heap poisoning, thus we
6329 // do not have to unpoison `root_reg` here.
6330 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006331 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006332}
6333
6334void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6335 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006336 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006337 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006338 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006339 bool needs_null_check,
6340 bool use_load_acquire) {
6341 DCHECK(kEmitCompilerReadBarrier);
6342 DCHECK(kUseBakerReadBarrier);
6343
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006344 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6345 !use_load_acquire &&
6346 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006347 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6348 // Marking Register) to decide whether we need to enter the slow
6349 // path to mark the reference. Then, in the slow path, check the
6350 // gray bit in the lock word of the reference's holder (`obj`) to
6351 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006352 //
6353 // We use link-time generated thunks for the slow path. That thunk checks
6354 // the holder and jumps to the entrypoint if needed. If the holder is not
6355 // gray, it creates a fake dependency and returns to the LDR instruction.
6356 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006357 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006358 // if (mr) { // Thread::Current()->GetIsGcMarking()
6359 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006360 // }
6361 // not_gray_return_address:
6362 // // Original reference load. If the offset is too large to fit
6363 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006364 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006365 // gray_return_address:
6366
6367 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6368 Register base = obj;
6369 if (offset >= kReferenceLoadMinFarOffset) {
6370 DCHECK(maybe_temp.IsRegister());
6371 base = WRegisterFrom(maybe_temp);
6372 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6373 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6374 offset &= (kReferenceLoadMinFarOffset - 1u);
6375 }
6376 UseScratchRegisterScope temps(GetVIXLAssembler());
6377 DCHECK(temps.IsAvailable(ip0));
6378 DCHECK(temps.IsAvailable(ip1));
6379 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006380 uint32_t custom_data = EncodeBakerReadBarrierFieldData(base.GetCode(), obj.GetCode());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006381 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6382
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006383 {
6384 EmissionCheckScope guard(GetVIXLAssembler(),
6385 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6386 vixl::aarch64::Label return_address;
6387 __ adr(lr, &return_address);
6388 __ Bind(cbnz_label);
6389 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6390 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6391 "Field LDR must be 1 instruction (4B) before the return address label; "
6392 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006393 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006394 __ ldr(ref_reg, MemOperand(base.X(), offset));
6395 if (needs_null_check) {
6396 MaybeRecordImplicitNullCheck(instruction);
6397 }
6398 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6399 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006400 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006401 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006402 return;
6403 }
6404
Roland Levillain44015862016-01-22 11:47:17 +00006405 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006406 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006407 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006408 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006409 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6410 ref,
6411 obj,
6412 offset,
6413 no_index,
6414 no_scale_factor,
6415 temp,
6416 needs_null_check,
6417 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006418}
6419
6420void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6421 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006422 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006423 uint32_t data_offset,
6424 Location index,
6425 Register temp,
6426 bool needs_null_check) {
6427 DCHECK(kEmitCompilerReadBarrier);
6428 DCHECK(kUseBakerReadBarrier);
6429
Vladimir Marko66d691d2017-04-07 17:53:39 +01006430 static_assert(
6431 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6432 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006433 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006434
6435 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6436 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006437 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6438 // Marking Register) to decide whether we need to enter the slow
6439 // path to mark the reference. Then, in the slow path, check the
6440 // gray bit in the lock word of the reference's holder (`obj`) to
6441 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006442 //
6443 // We use link-time generated thunks for the slow path. That thunk checks
6444 // the holder and jumps to the entrypoint if needed. If the holder is not
6445 // gray, it creates a fake dependency and returns to the LDR instruction.
6446 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006447 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006448 // if (mr) { // Thread::Current()->GetIsGcMarking()
6449 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006450 // }
6451 // not_gray_return_address:
6452 // // Original reference load. If the offset is too large to fit
6453 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006454 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006455 // gray_return_address:
6456
6457 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006458 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6459 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006460
6461 UseScratchRegisterScope temps(GetVIXLAssembler());
6462 DCHECK(temps.IsAvailable(ip0));
6463 DCHECK(temps.IsAvailable(ip1));
6464 temps.Exclude(ip0, ip1);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006465 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006466 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6467
Vladimir Marko66d691d2017-04-07 17:53:39 +01006468 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006469 {
6470 EmissionCheckScope guard(GetVIXLAssembler(),
6471 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6472 vixl::aarch64::Label return_address;
6473 __ adr(lr, &return_address);
6474 __ Bind(cbnz_label);
6475 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6476 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6477 "Array LDR must be 1 instruction (4B) before the return address label; "
6478 " 2 instructions (8B) for heap poisoning.");
6479 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6480 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6481 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6482 __ Bind(&return_address);
6483 }
6484 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006485 return;
6486 }
6487
Roland Levillain44015862016-01-22 11:47:17 +00006488 // Array cells are never volatile variables, therefore array loads
6489 // never use Load-Acquire instructions on ARM64.
6490 const bool use_load_acquire = false;
6491
6492 // /* HeapReference<Object> */ ref =
6493 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006494 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6495 ref,
6496 obj,
6497 data_offset,
6498 index,
6499 scale_factor,
6500 temp,
6501 needs_null_check,
6502 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006503}
6504
6505void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6506 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006507 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006508 uint32_t offset,
6509 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006510 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006511 Register temp,
6512 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006513 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006514 DCHECK(kEmitCompilerReadBarrier);
6515 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006516 // If we are emitting an array load, we should not be using a
6517 // Load Acquire instruction. In other words:
6518 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6519 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006520
Roland Levillain97c46462017-05-11 14:04:03 +01006521 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6522 // Marking Register) to decide whether we need to enter the slow
6523 // path to mark the reference. Then, in the slow path, check the
6524 // gray bit in the lock word of the reference's holder (`obj`) to
6525 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006526 //
Roland Levillain97c46462017-05-11 14:04:03 +01006527 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006528 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006529 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6530 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6531 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6532 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6533 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006534 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6535 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006536 // }
6537 // } else {
6538 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006539 // }
Roland Levillain44015862016-01-22 11:47:17 +00006540
Roland Levillainba650a42017-03-06 13:52:32 +00006541 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006542 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006543 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006544 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006545 instruction,
6546 ref,
6547 obj,
6548 offset,
6549 index,
6550 scale_factor,
6551 needs_null_check,
6552 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006553 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006554 AddSlowPath(slow_path);
6555
Roland Levillain97c46462017-05-11 14:04:03 +01006556 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006557 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006558 GenerateRawReferenceLoad(
6559 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006560 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006561 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006562}
6563
Roland Levillainff487002017-03-07 16:50:01 +00006564void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6565 Location ref,
6566 Register obj,
6567 Location field_offset,
6568 Register temp,
6569 bool needs_null_check,
6570 bool use_load_acquire) {
6571 DCHECK(kEmitCompilerReadBarrier);
6572 DCHECK(kUseBakerReadBarrier);
6573 // If we are emitting an array load, we should not be using a
6574 // Load Acquire instruction. In other words:
6575 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6576 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6577
Roland Levillain97c46462017-05-11 14:04:03 +01006578 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6579 // Marking Register) to decide whether we need to enter the slow
6580 // path to update the reference field within `obj`. Then, in the
6581 // slow path, check the gray bit in the lock word of the reference's
6582 // holder (`obj`) to decide whether to mark `ref` and update the
6583 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006584 //
Roland Levillain97c46462017-05-11 14:04:03 +01006585 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006586 // // Slow path.
6587 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6588 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6589 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6590 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6591 // if (is_gray) {
6592 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006593 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6594 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006595 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6596 // }
6597 // }
6598
6599 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006600 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006601 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006602 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006603 instruction,
6604 ref,
6605 obj,
6606 /* offset */ 0u,
6607 /* index */ field_offset,
6608 /* scale_factor */ 0u /* "times 1" */,
6609 needs_null_check,
6610 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006611 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006612 AddSlowPath(slow_path);
6613
Roland Levillain97c46462017-05-11 14:04:03 +01006614 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006615 // Fast path: the GC is not marking: nothing to do (the field is
6616 // up-to-date, and we don't need to load the reference).
6617 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006618 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006619}
6620
Roland Levillainba650a42017-03-06 13:52:32 +00006621void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6622 Location ref,
6623 Register obj,
6624 uint32_t offset,
6625 Location index,
6626 size_t scale_factor,
6627 bool needs_null_check,
6628 bool use_load_acquire) {
6629 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006630 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006631 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006632
Roland Levillainba650a42017-03-06 13:52:32 +00006633 // If needed, vixl::EmissionCheckScope guards are used to ensure
6634 // that no pools are emitted between the load (macro) instruction
6635 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006636
Roland Levillain44015862016-01-22 11:47:17 +00006637 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006638 // Load types involving an "index": ArrayGet,
6639 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6640 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006641 if (use_load_acquire) {
6642 // UnsafeGetObjectVolatile intrinsic case.
6643 // Register `index` is not an index in an object array, but an
6644 // offset to an object reference field within object `obj`.
6645 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6646 DCHECK(instruction->GetLocations()->Intrinsified());
6647 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6648 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006649 DCHECK_EQ(offset, 0u);
6650 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006651 DCHECK_EQ(needs_null_check, false);
6652 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006653 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6654 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006655 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006656 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6657 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006658 if (index.IsConstant()) {
6659 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006660 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006661 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006662 if (needs_null_check) {
6663 MaybeRecordImplicitNullCheck(instruction);
6664 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006665 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006666 UseScratchRegisterScope temps(GetVIXLAssembler());
6667 Register temp = temps.AcquireW();
6668 __ Add(temp, obj, offset);
6669 {
6670 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6671 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6672 if (needs_null_check) {
6673 MaybeRecordImplicitNullCheck(instruction);
6674 }
6675 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006676 }
Roland Levillain44015862016-01-22 11:47:17 +00006677 }
Roland Levillain44015862016-01-22 11:47:17 +00006678 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006679 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006680 MemOperand field = HeapOperand(obj, offset);
6681 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006682 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6683 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006684 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006685 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006686 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006687 if (needs_null_check) {
6688 MaybeRecordImplicitNullCheck(instruction);
6689 }
Roland Levillain44015862016-01-22 11:47:17 +00006690 }
6691 }
6692
6693 // Object* ref = ref_addr->AsMirrorPtr()
6694 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006695}
6696
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006697void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6698 // The following condition is a compile-time one, so it does not have a run-time cost.
6699 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6700 // The following condition is a run-time one; it is executed after the
6701 // previous compile-time test, to avoid penalizing non-debug builds.
6702 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6703 UseScratchRegisterScope temps(GetVIXLAssembler());
6704 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6705 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6706 }
6707 }
6708}
6709
Roland Levillain44015862016-01-22 11:47:17 +00006710void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6711 Location out,
6712 Location ref,
6713 Location obj,
6714 uint32_t offset,
6715 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006716 DCHECK(kEmitCompilerReadBarrier);
6717
Roland Levillain44015862016-01-22 11:47:17 +00006718 // Insert a slow path based read barrier *after* the reference load.
6719 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006720 // If heap poisoning is enabled, the unpoisoning of the loaded
6721 // reference will be carried out by the runtime within the slow
6722 // path.
6723 //
6724 // Note that `ref` currently does not get unpoisoned (when heap
6725 // poisoning is enabled), which is alright as the `ref` argument is
6726 // not used by the artReadBarrierSlow entry point.
6727 //
6728 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006729 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006730 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6731 AddSlowPath(slow_path);
6732
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006733 __ B(slow_path->GetEntryLabel());
6734 __ Bind(slow_path->GetExitLabel());
6735}
6736
Roland Levillain44015862016-01-22 11:47:17 +00006737void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6738 Location out,
6739 Location ref,
6740 Location obj,
6741 uint32_t offset,
6742 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006743 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006744 // Baker's read barriers shall be handled by the fast path
6745 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6746 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006747 // If heap poisoning is enabled, unpoisoning will be taken care of
6748 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006749 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006750 } else if (kPoisonHeapReferences) {
6751 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6752 }
6753}
6754
Roland Levillain44015862016-01-22 11:47:17 +00006755void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6756 Location out,
6757 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006758 DCHECK(kEmitCompilerReadBarrier);
6759
Roland Levillain44015862016-01-22 11:47:17 +00006760 // Insert a slow path based read barrier *after* the GC root load.
6761 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006762 // Note that GC roots are not affected by heap poisoning, so we do
6763 // not need to do anything special for this here.
6764 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006765 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006766 AddSlowPath(slow_path);
6767
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006768 __ B(slow_path->GetEntryLabel());
6769 __ Bind(slow_path->GetExitLabel());
6770}
6771
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006772void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6773 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006774 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006775 locations->SetInAt(0, Location::RequiresRegister());
6776 locations->SetOut(Location::RequiresRegister());
6777}
6778
6779void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6780 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006781 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006782 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006783 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006784 __ Ldr(XRegisterFrom(locations->Out()),
6785 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006786 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006787 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006788 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006789 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6790 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006791 __ Ldr(XRegisterFrom(locations->Out()),
6792 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006793 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006794}
6795
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006796static void PatchJitRootUse(uint8_t* code,
6797 const uint8_t* roots_data,
6798 vixl::aarch64::Literal<uint32_t>* literal,
6799 uint64_t index_in_table) {
6800 uint32_t literal_offset = literal->GetOffset();
6801 uintptr_t address =
6802 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6803 uint8_t* data = code + literal_offset;
6804 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6805}
6806
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006807void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6808 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006809 const StringReference& string_reference = entry.first;
6810 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006811 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006812 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006813 }
6814 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006815 const TypeReference& type_reference = entry.first;
6816 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006817 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006818 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006819 }
6820}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006821
Alexandre Rames67555f72014-11-18 10:55:16 +00006822#undef __
6823#undef QUICK_ENTRY_POINT
6824
Vladimir Markoca1e0382018-04-11 09:58:41 +00006825#define __ assembler.GetVIXLAssembler()->
6826
6827static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6828 vixl::aarch64::Register base_reg,
6829 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006830 vixl::aarch64::Label* slow_path,
6831 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006832 // Load the lock word containing the rb_state.
6833 __ Ldr(ip0.W(), lock_word);
6834 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6835 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6836 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6837 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6838 static_assert(
6839 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6840 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006841 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6842 if (throw_npe != nullptr) {
6843 __ Bind(throw_npe);
6844 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006845 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6846 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6847 "Field LDR must be 1 instruction (4B) before the return address label; "
6848 " 2 instructions (8B) for heap poisoning.");
6849 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6850 // Introduce a dependency on the lock_word including rb_state,
6851 // to prevent load-load reordering, and without using
6852 // a memory barrier (which would be more expensive).
6853 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6854 __ Br(lr); // And return back to the function.
6855 // Note: The fake dependency is unnecessary for the slow path.
6856}
6857
6858// Load the read barrier introspection entrypoint in register `entrypoint`.
6859static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6860 vixl::aarch64::Register entrypoint) {
6861 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6862 DCHECK_EQ(ip0.GetCode(), 16u);
6863 const int32_t entry_point_offset =
6864 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6865 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6866}
6867
6868void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6869 uint32_t encoded_data,
6870 /*out*/ std::string* debug_name) {
6871 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6872 switch (kind) {
6873 case BakerReadBarrierKind::kField: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006874 auto base_reg =
6875 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6876 CheckValidReg(base_reg.GetCode());
6877 auto holder_reg =
6878 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6879 CheckValidReg(holder_reg.GetCode());
6880 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6881 temps.Exclude(ip0, ip1);
Vladimir Marko7a695052018-04-12 10:26:50 +01006882 // If base_reg differs from holder_reg, the offset was too large and we must have emitted
6883 // an explicit null check before the load. Otherwise, for implicit null checks, we need to
6884 // null-check the holder as we do not necessarily do that check before going to the thunk.
6885 vixl::aarch64::Label throw_npe_label;
6886 vixl::aarch64::Label* throw_npe = nullptr;
6887 if (GetCompilerOptions().GetImplicitNullChecks() && holder_reg.Is(base_reg)) {
6888 throw_npe = &throw_npe_label;
6889 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006890 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006891 // Check if the holder is gray and, if not, add fake dependency to the base register
6892 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6893 // to load the reference and call the entrypoint that performs further checks on the
6894 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006895 vixl::aarch64::Label slow_path;
6896 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006897 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006898 __ Bind(&slow_path);
6899 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6900 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6901 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6902 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6903 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6904 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6905 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006906 break;
6907 }
6908 case BakerReadBarrierKind::kArray: {
6909 auto base_reg =
6910 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6911 CheckValidReg(base_reg.GetCode());
6912 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6913 BakerReadBarrierSecondRegField::Decode(encoded_data));
6914 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6915 temps.Exclude(ip0, ip1);
6916 vixl::aarch64::Label slow_path;
6917 int32_t data_offset =
6918 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6919 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6920 DCHECK_LT(lock_word.GetOffset(), 0);
6921 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6922 __ Bind(&slow_path);
6923 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6924 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6925 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6926 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6927 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6928 // a switch case target based on the index register.
6929 __ Mov(ip0, base_reg); // Move the base register to ip0.
6930 __ Br(ip1); // Jump to the entrypoint's array switch case.
6931 break;
6932 }
6933 case BakerReadBarrierKind::kGcRoot: {
6934 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6935 // and it does not have a forwarding address), call the correct introspection entrypoint;
6936 // otherwise return the reference (or the extracted forwarding address).
6937 // There is no gray bit check for GC roots.
6938 auto root_reg =
6939 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6940 CheckValidReg(root_reg.GetCode());
6941 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6942 BakerReadBarrierSecondRegField::Decode(encoded_data));
6943 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6944 temps.Exclude(ip0, ip1);
6945 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6946 __ Cbz(root_reg, &return_label);
6947 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6948 __ Ldr(ip0.W(), lock_word);
6949 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6950 __ Bind(&return_label);
6951 __ Br(lr);
6952 __ Bind(&not_marked);
6953 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6954 __ B(&forwarding_address, mi);
6955 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6956 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6957 // art_quick_read_barrier_mark_introspection_gc_roots.
6958 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6959 __ Mov(ip0.W(), root_reg);
6960 __ Br(ip1);
6961 __ Bind(&forwarding_address);
6962 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6963 __ Br(lr);
6964 break;
6965 }
6966 default:
6967 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6968 UNREACHABLE();
6969 }
6970
6971 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
6972 std::ostringstream oss;
6973 oss << "BakerReadBarrierThunk";
6974 switch (kind) {
6975 case BakerReadBarrierKind::kField:
6976 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6977 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6978 break;
6979 case BakerReadBarrierKind::kArray:
6980 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6981 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6982 BakerReadBarrierSecondRegField::Decode(encoded_data));
6983 break;
6984 case BakerReadBarrierKind::kGcRoot:
6985 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6986 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6987 BakerReadBarrierSecondRegField::Decode(encoded_data));
6988 break;
6989 }
6990 *debug_name = oss.str();
6991 }
6992}
6993
6994#undef __
6995
Alexandre Rames5319def2014-10-23 10:03:10 +01006996} // namespace arm64
6997} // namespace art