blob: e01b7b78cb41b9be3f9e8659626d98c5ef2566a9 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080031#include "intrinsics.h"
32#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000033#include "linker/arm64/relative_patcher_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "thread.h"
40#include "utils/arm64/assembler_arm64.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000045using vixl::ExactAssemblyScope;
46using vixl::CodeBufferCheckScope;
47using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49#ifdef __
50#error "ARM64 Codegen VIXL macro-assembler macro already defined."
51#endif
52
Alexandre Rames5319def2014-10-23 10:03:10 +010053namespace art {
54
Roland Levillain22ccc3a2015-11-24 13:10:05 +000055template<class MirrorType>
56class GcRoot;
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058namespace arm64 {
59
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::ARM64EncodableConstantOrRegister;
61using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::CPURegisterFrom;
63using helpers::DRegisterFrom;
64using helpers::FPRegisterFrom;
65using helpers::HeapOperand;
66using helpers::HeapOperandFrom;
67using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010071using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010073using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::LocationFrom;
75using helpers::OperandFromMemOperand;
76using helpers::OutputCPURegister;
77using helpers::OutputFPRegister;
78using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010079using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080080using helpers::RegisterFrom;
81using helpers::StackOperandFrom;
82using helpers::VIXLRegCodeFromART;
83using helpers::WRegisterFrom;
84using helpers::XRegisterFrom;
85
Vladimir Markof3e0ee22015-12-17 15:23:13 +000086// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080087// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
88// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000089static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010090
Vladimir Markof4f2daa2017-03-20 18:26:59 +000091// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
92// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
93// For the Baker read barrier implementation using link-generated thunks we need to split
94// the offset explicitly.
95constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
96
97// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010098constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010099constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +0100100constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000101
102// Some instructions have special requirements for a temporary, for example
103// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
104// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
105// loads with large offsets need a fixed register to limit the number of link-time
106// thunks we generate. For these and similar cases, we want to reserve a specific
107// register that's neither callee-save nor an argument register. We choose x15.
108inline Location FixedTempLocation() {
109 return Location::RegisterLocation(x15.GetCode());
110}
111
Alexandre Rames5319def2014-10-23 10:03:10 +0100112inline Condition ARM64Condition(IfCondition cond) {
113 switch (cond) {
114 case kCondEQ: return eq;
115 case kCondNE: return ne;
116 case kCondLT: return lt;
117 case kCondLE: return le;
118 case kCondGT: return gt;
119 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700120 case kCondB: return lo;
121 case kCondBE: return ls;
122 case kCondA: return hi;
123 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100124 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000125 LOG(FATAL) << "Unreachable";
126 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100127}
128
Vladimir Markod6e069b2016-01-18 11:11:01 +0000129inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
130 // The ARM64 condition codes can express all the necessary branches, see the
131 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
132 // There is no dex instruction or HIR that would need the missing conditions
133 // "equal or unordered" or "not equal".
134 switch (cond) {
135 case kCondEQ: return eq;
136 case kCondNE: return ne /* unordered */;
137 case kCondLT: return gt_bias ? cc : lt /* unordered */;
138 case kCondLE: return gt_bias ? ls : le /* unordered */;
139 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
140 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
141 default:
142 LOG(FATAL) << "UNREACHABLE";
143 UNREACHABLE();
144 }
145}
146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000148 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
149 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
150 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000152 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000156 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100157 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100158 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000159 } else {
160 return LocationFrom(w0);
161 }
162}
163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100164Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000165 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100166}
167
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100168// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
169#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700170#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100171
Zheng Xuda403092015-04-24 17:35:39 +0800172// Calculate memory accessing operand for save/restore live registers.
173static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800175 int64_t spill_offset,
176 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
178 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
179 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800180 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100181 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800182 codegen->GetNumberOfFloatingPointRegisters()));
183
Vladimir Marko804b03f2016-09-14 16:26:36 +0100184 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100185 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
186 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800187
188 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
189 UseScratchRegisterScope temps(masm);
190
191 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
193 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800194 int64_t reg_size = kXRegSizeInBytes;
195 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
196 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100197 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800198 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
199 // If the offset does not fit in the instruction's immediate field, use an alternate register
200 // to compute the base address(float point registers spill base address).
201 Register new_base = temps.AcquireSameSizeAs(base);
202 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
203 base = new_base;
204 spill_offset = -core_spill_size;
205 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
206 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
207 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
208 }
209
210 if (is_save) {
211 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
212 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
213 } else {
214 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
215 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
216 }
217}
218
219void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800220 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100221 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
222 for (uint32_t i : LowToHighBits(core_spills)) {
223 // If the register holds an object, update the stack mask.
224 if (locations->RegisterContainsObject(i)) {
225 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100227 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
228 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
229 saved_core_stack_offsets_[i] = stack_offset;
230 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800231 }
232
Vladimir Marko804b03f2016-09-14 16:26:36 +0100233 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
234 for (uint32_t i : LowToHighBits(fp_spills)) {
235 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
236 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
237 saved_fpu_stack_offsets_[i] = stack_offset;
238 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800239 }
240
Vladimir Marko804b03f2016-09-14 16:26:36 +0100241 SaveRestoreLiveRegistersHelper(codegen,
242 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800243 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
244}
245
246void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100247 SaveRestoreLiveRegistersHelper(codegen,
248 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800249 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
250}
251
Alexandre Rames5319def2014-10-23 10:03:10 +0100252class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
253 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000254 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100257 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000258 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100259
Alexandre Rames5319def2014-10-23 10:03:10 +0100260 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000261 if (instruction_->CanThrowIntoCatchBlock()) {
262 // Live registers will be restored in the catch block if caught.
263 SaveLiveRegisters(codegen, instruction_->GetLocations());
264 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000265 // We're moving two locations to locations that could overlap, so we need a parallel
266 // move resolver.
267 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100268 codegen->EmitParallelMoves(locations->InAt(0),
269 LocationFrom(calling_convention.GetRegisterAt(0)),
270 DataType::Type::kInt32,
271 locations->InAt(1),
272 LocationFrom(calling_convention.GetRegisterAt(1)),
273 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000274 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
275 ? kQuickThrowStringBounds
276 : kQuickThrowArrayBounds;
277 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100278 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800279 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
285
Alexandre Rames5319def2014-10-23 10:03:10 +0100286 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100287 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
288};
289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
291 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000292 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000293
294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
295 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
296 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000297 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 }
300
Alexandre Rames8158f282015-08-07 10:26:17 +0100301 bool IsFatal() const OVERRIDE { return true; }
302
Alexandre Rames9931f312015-06-19 14:47:01 +0100303 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
304
Alexandre Rames67555f72014-11-18 10:55:16 +0000305 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000306 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
307};
308
309class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
310 public:
311 LoadClassSlowPathARM64(HLoadClass* cls,
312 HInstruction* at,
313 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000314 bool do_clinit,
315 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
316 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
317 : SlowPathCodeARM64(at),
318 cls_(cls),
319 dex_pc_(dex_pc),
320 do_clinit_(do_clinit),
321 bss_entry_temp_(bss_entry_temp),
322 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
324 }
325
326 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000327 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000328 Location out = locations->Out();
329 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
331
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000332 InvokeRuntimeCallingConvention calling_convention;
333 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
334 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
336 bool is_load_class_bss_entry =
337 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000338 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000339 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000340 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
341 DCHECK(
342 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000343 }
344
Alexandre Rames67555f72014-11-18 10:55:16 +0000345 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000346 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000347
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000348 dex::TypeIndex type_index = cls_->GetTypeIndex();
349 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000350 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
351 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000352 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800353 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100354 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800355 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100356 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800357 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000358
359 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 if (out.IsValid()) {
361 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100362 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000363 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000365 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000366 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000367 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000368 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 if (call_saves_everything_except_r0_ip0) {
371 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
372 } else {
373 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
374 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
375 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
376 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000377 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000378 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000379 {
380 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
381 __ Bind(strp_label);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100382 __ str(RegisterFrom(locations->Out(), DataType::Type::kReference),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000383 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000384 }
385 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000386 __ B(GetExitLabel());
387 }
388
Alexandre Rames9931f312015-06-19 14:47:01 +0100389 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
390
Alexandre Rames67555f72014-11-18 10:55:16 +0000391 private:
392 // The class this slow path will load.
393 HLoadClass* const cls_;
394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 // The dex PC of `at_`.
396 const uint32_t dex_pc_;
397
398 // Whether to initialize the class.
399 const bool do_clinit_;
400
Vladimir Markoea4c1262017-02-06 19:59:33 +0000401 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
402 vixl::aarch64::Register bss_entry_temp_;
403 vixl::aarch64::Label* bss_entry_adrp_label_;
404
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
406};
407
Vladimir Markoaad75c62016-10-03 08:46:48 +0000408class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
409 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100410 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
411 : SlowPathCodeARM64(instruction),
412 temp_(temp),
413 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 LocationSummary* locations = instruction_->GetLocations();
417 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
418 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
419
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000420 InvokeRuntimeCallingConvention calling_convention;
421 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
422 DCHECK(temp_.IsValid());
423 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
424 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100425
Vladimir Markoaad75c62016-10-03 08:46:48 +0000426 __ Bind(GetEntryLabel());
427 SaveLiveRegisters(codegen, locations);
428
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000429 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
430 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000431 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
432 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000434 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
435
436 RestoreLiveRegisters(codegen, locations);
437
438 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100440 if (!kUseReadBarrier || kUseBakerReadBarrier) {
441 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
442 } else {
443 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100444 adrp_label_ = arm64_codegen->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100445 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
446 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000447 vixl::aarch64::Label* strp_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100448 arm64_codegen->NewStringBssEntryPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000449 {
450 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
451 __ Bind(strp_label);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100452 __ str(RegisterFrom(locations->Out(), DataType::Type::kReference),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100453 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000454 }
455
456 __ B(GetExitLabel());
457 }
458
459 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
460
461 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100462 const Register temp_;
463 vixl::aarch64::Label* adrp_label_;
464
Vladimir Markoaad75c62016-10-03 08:46:48 +0000465 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
466};
467
Alexandre Rames5319def2014-10-23 10:03:10 +0100468class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
469 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000470 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100471
Alexandre Rames67555f72014-11-18 10:55:16 +0000472 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
473 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100474 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000475 if (instruction_->CanThrowIntoCatchBlock()) {
476 // Live registers will be restored in the catch block if caught.
477 SaveLiveRegisters(codegen, instruction_->GetLocations());
478 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000479 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
480 instruction_,
481 instruction_->GetDexPc(),
482 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800483 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 }
485
Alexandre Rames8158f282015-08-07 10:26:17 +0100486 bool IsFatal() const OVERRIDE { return true; }
487
Alexandre Rames9931f312015-06-19 14:47:01 +0100488 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
489
Alexandre Rames5319def2014-10-23 10:03:10 +0100490 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100491 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
492};
493
494class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
495 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100496 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000497 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100498
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100500 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000501 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100502 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100503 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000504 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800505 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100506 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000507 if (successor_ == nullptr) {
508 __ B(GetReturnLabel());
509 } else {
510 __ B(arm64_codegen->GetLabelOf(successor_));
511 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100512 }
513
Scott Wakeling97c72b72016-06-24 16:19:36 +0100514 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 DCHECK(successor_ == nullptr);
516 return &return_label_;
517 }
518
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100519 HBasicBlock* GetSuccessor() const {
520 return successor_;
521 }
522
Alexandre Rames9931f312015-06-19 14:47:01 +0100523 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
524
Alexandre Rames5319def2014-10-23 10:03:10 +0100525 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100526 // If not null, the block to branch to after the suspend check.
527 HBasicBlock* const successor_;
528
529 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100530 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100531
532 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
533};
534
Alexandre Rames67555f72014-11-18 10:55:16 +0000535class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
536 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000537 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000538 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000539
540 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000541 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800542
Alexandre Rames3e69f162014-12-10 10:36:50 +0000543 DCHECK(instruction_->IsCheckCast()
544 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
545 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100546 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000547
Alexandre Rames67555f72014-11-18 10:55:16 +0000548 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000549
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000550 if (!is_fatal_) {
551 SaveLiveRegisters(codegen, locations);
552 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000553
554 // We're moving two locations to locations that could overlap, so we need a parallel
555 // move resolver.
556 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800557 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800558 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100559 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800560 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800561 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000563 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000564 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800565 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100566 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000567 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
568 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
569 } else {
570 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800571 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
572 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000573 }
574
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000575 if (!is_fatal_) {
576 RestoreLiveRegisters(codegen, locations);
577 __ B(GetExitLabel());
578 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000579 }
580
Alexandre Rames9931f312015-06-19 14:47:01 +0100581 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100582 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100583
Alexandre Rames67555f72014-11-18 10:55:16 +0000584 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000585 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000586
Alexandre Rames67555f72014-11-18 10:55:16 +0000587 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
588};
589
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
591 public:
Aart Bik42249c32016-01-07 15:33:50 -0800592 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000593 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700594
595 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800596 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100598 LocationSummary* locations = instruction_->GetLocations();
599 SaveLiveRegisters(codegen, locations);
600 InvokeRuntimeCallingConvention calling_convention;
601 __ Mov(calling_convention.GetRegisterAt(0),
602 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000603 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100604 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700605 }
606
Alexandre Rames9931f312015-06-19 14:47:01 +0100607 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
608
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700609 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700610 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
611};
612
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100613class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
614 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000615 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
619 __ Bind(GetEntryLabel());
620 SaveLiveRegisters(codegen, locations);
621
622 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100623 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100624 parallel_move.AddMove(
625 locations->InAt(0),
626 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100627 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100628 nullptr);
629 parallel_move.AddMove(
630 locations->InAt(1),
631 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100632 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100633 nullptr);
634 parallel_move.AddMove(
635 locations->InAt(2),
636 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100637 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100638 nullptr);
639 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
640
641 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000642 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
644 RestoreLiveRegisters(codegen, locations);
645 __ B(GetExitLabel());
646 }
647
648 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
649
650 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100651 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
652};
653
Zheng Xu3927c8b2015-11-18 17:46:25 +0800654void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
655 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000656 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800657
658 // We are about to use the assembler to place literals directly. Make sure we have enough
659 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000660 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
661 num_entries * sizeof(int32_t),
662 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800663
664 __ Bind(&table_start_);
665 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
666 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100667 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800668 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100669 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800670 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
671 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
672 Literal<int32_t> literal(jump_offset);
673 __ place(&literal);
674 }
675}
676
Roland Levillain54f869e2017-03-06 13:54:11 +0000677// Abstract base class for read barrier slow paths marking a reference
678// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000679//
Roland Levillain54f869e2017-03-06 13:54:11 +0000680// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100681// barrier marking runtime entry point to be invoked or an empty
682// location; in the latter case, the read barrier marking runtime
683// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000684class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
685 protected:
686 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
687 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000688 DCHECK(kEmitCompilerReadBarrier);
689 }
690
Roland Levillain54f869e2017-03-06 13:54:11 +0000691 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000692
Roland Levillain54f869e2017-03-06 13:54:11 +0000693 // Generate assembly code calling the read barrier marking runtime
694 // entry point (ReadBarrierMarkRegX).
695 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000696 // No need to save live registers; it's taken care of by the
697 // entrypoint. Also, there is no need to update the stack mask,
698 // as this runtime call will not trigger a garbage collection.
699 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
700 DCHECK_NE(ref_.reg(), LR);
701 DCHECK_NE(ref_.reg(), WSP);
702 DCHECK_NE(ref_.reg(), WZR);
703 // IP0 is used internally by the ReadBarrierMarkRegX entry point
704 // as a temporary, it cannot be the entry point's input/output.
705 DCHECK_NE(ref_.reg(), IP0);
706 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
707 // "Compact" slow path, saving two moves.
708 //
709 // Instead of using the standard runtime calling convention (input
710 // and output in W0):
711 //
712 // W0 <- ref
713 // W0 <- ReadBarrierMark(W0)
714 // ref <- W0
715 //
716 // we just use rX (the register containing `ref`) as input and output
717 // of a dedicated entrypoint:
718 //
719 // rX <- ReadBarrierMarkRegX(rX)
720 //
721 if (entrypoint_.IsValid()) {
722 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
723 __ Blr(XRegisterFrom(entrypoint_));
724 } else {
725 // Entrypoint is not already loaded, load from the thread.
726 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100727 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000728 // This runtime call does not require a stack map.
729 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
730 }
731 }
732
733 // The location (register) of the marked object reference.
734 const Location ref_;
735
736 // The location of the entrypoint if it is already loaded.
737 const Location entrypoint_;
738
Roland Levillain54f869e2017-03-06 13:54:11 +0000739 private:
740 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
741};
742
Alexandre Rames5319def2014-10-23 10:03:10 +0100743// Slow path marking an object reference `ref` during a read
744// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000745// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100746//
747// This means that after the execution of this slow path, `ref` will
748// always be up-to-date, but `obj.field` may not; i.e., after the
749// flip, `ref` will be a to-space reference, but `obj.field` will
750// probably still be a from-space reference (unless it gets updated by
751// another thread, or if another thread installed another object
752// reference (different from `ref`) in `obj.field`).
753//
Roland Levillain97c46462017-05-11 14:04:03 +0100754// Argument `entrypoint` must be a register location holding the read
755// barrier marking runtime entry point to be invoked or an empty
756// location; in the latter case, the read barrier marking runtime
757// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000758class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100759 public:
760 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
761 Location ref,
762 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000763 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100764 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100765 }
766
767 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
768
769 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000770 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100771 DCHECK(locations->CanCall());
772 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000773 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000774 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
775 << "Unexpected instruction in read barrier marking slow path: "
776 << instruction_->DebugName();
777
778 __ Bind(GetEntryLabel());
779 GenerateReadBarrierMarkRuntimeCall(codegen);
780 __ B(GetExitLabel());
781 }
782
783 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000784 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
785};
786
Roland Levillain54f869e2017-03-06 13:54:11 +0000787// Slow path loading `obj`'s lock word, loading a reference from
788// object `*(obj + offset + (index << scale_factor))` into `ref`, and
789// marking `ref` if `obj` is gray according to the lock word (Baker
790// read barrier). The field `obj.field` in the object `obj` holding
791// this reference does not get updated by this slow path after marking
792// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
793// below for that).
794//
795// This means that after the execution of this slow path, `ref` will
796// always be up-to-date, but `obj.field` may not; i.e., after the
797// flip, `ref` will be a to-space reference, but `obj.field` will
798// probably still be a from-space reference (unless it gets updated by
799// another thread, or if another thread installed another object
800// reference (different from `ref`) in `obj.field`).
801//
802// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100803// barrier marking runtime entry point to be invoked or an empty
804// location; in the latter case, the read barrier marking runtime
805// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000806class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
807 public:
808 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
809 Location ref,
810 Register obj,
811 uint32_t offset,
812 Location index,
813 size_t scale_factor,
814 bool needs_null_check,
815 bool use_load_acquire,
816 Register temp,
Roland Levillain97c46462017-05-11 14:04:03 +0100817 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000818 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
819 obj_(obj),
820 offset_(offset),
821 index_(index),
822 scale_factor_(scale_factor),
823 needs_null_check_(needs_null_check),
824 use_load_acquire_(use_load_acquire),
825 temp_(temp) {
826 DCHECK(kEmitCompilerReadBarrier);
827 DCHECK(kUseBakerReadBarrier);
828 }
829
830 const char* GetDescription() const OVERRIDE {
831 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
832 }
833
834 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
835 LocationSummary* locations = instruction_->GetLocations();
836 DCHECK(locations->CanCall());
837 DCHECK(ref_.IsRegister()) << ref_;
838 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
839 DCHECK(obj_.IsW());
840 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100841 DCHECK(instruction_->IsInstanceFieldGet() ||
842 instruction_->IsStaticFieldGet() ||
843 instruction_->IsArrayGet() ||
844 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100845 instruction_->IsInstanceOf() ||
846 instruction_->IsCheckCast() ||
847 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
848 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
849 << "Unexpected instruction in read barrier marking slow path: "
850 << instruction_->DebugName();
851 // The read barrier instrumentation of object ArrayGet
852 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000853 // instruction.
854 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000855 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
856
Roland Levillain54f869e2017-03-06 13:54:11 +0000857 // Temporary register `temp_`, used to store the lock word, must
858 // not be IP0 nor IP1, as we may use them to emit the reference
859 // load (in the call to GenerateRawReferenceLoad below), and we
860 // need the lock word to still be in `temp_` after the reference
861 // load.
862 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
863 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
864
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000866
867 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
868 // inserted after the original load. However, in fast path based
869 // Baker's read barriers, we need to perform the load of
870 // mirror::Object::monitor_ *before* the original reference load.
871 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000872 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100873 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000874 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
875 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
876 // HeapReference<mirror::Object> ref = *src; // Original reference load.
877 // bool is_gray = (rb_state == ReadBarrier::GrayState());
878 // if (is_gray) {
879 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
880 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000881 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000882 // Note: the original implementation in ReadBarrier::Barrier is
883 // slightly more complex as it performs additional checks that we do
884 // not do here for performance reasons.
885
886 // /* int32_t */ monitor = obj->monitor_
887 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
888 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
889 if (needs_null_check_) {
890 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100891 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000892 // /* LockWord */ lock_word = LockWord(monitor)
893 static_assert(sizeof(LockWord) == sizeof(int32_t),
894 "art::LockWord and int32_t have different sizes.");
895
896 // Introduce a dependency on the lock_word including rb_state,
897 // to prevent load-load reordering, and without using
898 // a memory barrier (which would be more expensive).
899 // `obj` is unchanged by this operation, but its value now depends
900 // on `temp`.
901 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
902
903 // The actual reference load.
904 // A possible implicit null check has already been handled above.
905 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
906 arm64_codegen->GenerateRawReferenceLoad(instruction_,
907 ref_,
908 obj_,
909 offset_,
910 index_,
911 scale_factor_,
912 /* needs_null_check */ false,
913 use_load_acquire_);
914
915 // Mark the object `ref` when `obj` is gray.
916 //
917 // if (rb_state == ReadBarrier::GrayState())
918 // ref = ReadBarrier::Mark(ref);
919 //
920 // Given the numeric representation, it's enough to check the low bit of the rb_state.
921 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
922 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
923 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
924 GenerateReadBarrierMarkRuntimeCall(codegen);
925
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000926 __ B(GetExitLabel());
927 }
928
929 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 // The register containing the object holding the marked object reference field.
931 Register obj_;
932 // The offset, index and scale factor to access the reference in `obj_`.
933 uint32_t offset_;
934 Location index_;
935 size_t scale_factor_;
936 // Is a null check required?
937 bool needs_null_check_;
938 // Should this reference load use Load-Acquire semantics?
939 bool use_load_acquire_;
940 // A temporary register used to hold the lock word of `obj_`.
941 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000942
Roland Levillain54f869e2017-03-06 13:54:11 +0000943 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000944};
945
Roland Levillain54f869e2017-03-06 13:54:11 +0000946// Slow path loading `obj`'s lock word, loading a reference from
947// object `*(obj + offset + (index << scale_factor))` into `ref`, and
948// marking `ref` if `obj` is gray according to the lock word (Baker
949// read barrier). If needed, this slow path also atomically updates
950// the field `obj.field` in the object `obj` holding this reference
951// after marking (contrary to
952// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
953// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100954//
955// This means that after the execution of this slow path, both `ref`
956// and `obj.field` will be up-to-date; i.e., after the flip, both will
957// hold the same to-space reference (unless another thread installed
958// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000959//
Roland Levillain54f869e2017-03-06 13:54:11 +0000960// Argument `entrypoint` must be a register location holding the read
Roland Levillain97c46462017-05-11 14:04:03 +0100961// barrier marking runtime entry point to be invoked or an empty
962// location; in the latter case, the read barrier marking runtime
963// entry point will be loaded by the slow path code itself.
Roland Levillain54f869e2017-03-06 13:54:11 +0000964class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
965 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100966 public:
Roland Levillain97c46462017-05-11 14:04:03 +0100967 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
968 HInstruction* instruction,
969 Location ref,
970 Register obj,
971 uint32_t offset,
972 Location index,
973 size_t scale_factor,
974 bool needs_null_check,
975 bool use_load_acquire,
976 Register temp,
977 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000978 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100979 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000980 offset_(offset),
981 index_(index),
982 scale_factor_(scale_factor),
983 needs_null_check_(needs_null_check),
984 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000985 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100986 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000987 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100988 }
989
990 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100992 }
993
994 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
995 LocationSummary* locations = instruction_->GetLocations();
996 Register ref_reg = WRegisterFrom(ref_);
997 DCHECK(locations->CanCall());
998 DCHECK(ref_.IsRegister()) << ref_;
999 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +00001000 DCHECK(obj_.IsW());
1001 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
1002
1003 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001004 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
1005 << "Unexpected instruction in read barrier marking and field updating slow path: "
1006 << instruction_->DebugName();
1007 DCHECK(instruction_->GetLocations()->Intrinsified());
1008 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +00001009 DCHECK_EQ(offset_, 0u);
1010 DCHECK_EQ(scale_factor_, 0u);
1011 DCHECK_EQ(use_load_acquire_, false);
1012 // The location of the offset of the marked reference field within `obj_`.
1013 Location field_offset = index_;
1014 DCHECK(field_offset.IsRegister()) << field_offset;
1015
1016 // Temporary register `temp_`, used to store the lock word, must
1017 // not be IP0 nor IP1, as we may use them to emit the reference
1018 // load (in the call to GenerateRawReferenceLoad below), and we
1019 // need the lock word to still be in `temp_` after the reference
1020 // load.
1021 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1022 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001023
1024 __ Bind(GetEntryLabel());
1025
Roland Levillainff487002017-03-07 16:50:01 +00001026 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1027 //
1028 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1029 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1030 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1031 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1032 // if (is_gray) {
1033 // old_ref = ref;
1034 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1035 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1036 // }
1037
Roland Levillain54f869e2017-03-06 13:54:11 +00001038 // /* int32_t */ monitor = obj->monitor_
1039 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1040 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1041 if (needs_null_check_) {
1042 codegen->MaybeRecordImplicitNullCheck(instruction_);
1043 }
1044 // /* LockWord */ lock_word = LockWord(monitor)
1045 static_assert(sizeof(LockWord) == sizeof(int32_t),
1046 "art::LockWord and int32_t have different sizes.");
1047
1048 // Introduce a dependency on the lock_word including rb_state,
1049 // to prevent load-load reordering, and without using
1050 // a memory barrier (which would be more expensive).
1051 // `obj` is unchanged by this operation, but its value now depends
1052 // on `temp`.
1053 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1054
1055 // The actual reference load.
1056 // A possible implicit null check has already been handled above.
1057 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1058 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1059 ref_,
1060 obj_,
1061 offset_,
1062 index_,
1063 scale_factor_,
1064 /* needs_null_check */ false,
1065 use_load_acquire_);
1066
1067 // Mark the object `ref` when `obj` is gray.
1068 //
1069 // if (rb_state == ReadBarrier::GrayState())
1070 // ref = ReadBarrier::Mark(ref);
1071 //
1072 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1073 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1074 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1075 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1076
1077 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 // Note that we cannot use IP to save the old reference, as IP is
1079 // used internally by the ReadBarrierMarkRegX entry point, and we
1080 // need the old reference after the call to that entry point.
1081 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1082 __ Mov(temp_.W(), ref_reg);
1083
Roland Levillain54f869e2017-03-06 13:54:11 +00001084 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001085
1086 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001087 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001088 //
1089 // Note that this field could also hold a different object, if
1090 // another thread had concurrently changed it. In that case, the
1091 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1092 // (CAS) operation below would abort the CAS, leaving the field
1093 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001094 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001095 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001096
1097 // Update the the holder's field atomically. This may fail if
1098 // mutator updates before us, but it's OK. This is achieved
1099 // using a strong compare-and-set (CAS) operation with relaxed
1100 // memory synchronization ordering, where the expected value is
1101 // the old reference and the desired value is the new reference.
1102
1103 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1104 UseScratchRegisterScope temps(masm);
1105
1106 // Convenience aliases.
1107 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001108 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001109 Register expected = temp_.W();
1110 Register value = ref_reg;
1111 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1112 Register tmp_value = temps.AcquireW(); // Value in memory.
1113
1114 __ Add(tmp_ptr, base.X(), Operand(offset));
1115
1116 if (kPoisonHeapReferences) {
1117 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1118 if (value.Is(expected)) {
1119 // Do not poison `value`, as it is the same register as
1120 // `expected`, which has just been poisoned.
1121 } else {
1122 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1123 }
1124 }
1125
1126 // do {
1127 // tmp_value = [tmp_ptr] - expected;
1128 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1129
Roland Levillain24a4d112016-10-26 13:10:46 +01001130 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001131 __ Bind(&loop_head);
1132 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1133 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001134 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001135 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1136 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001137 __ B(&exit_loop);
1138 __ Bind(&comparison_failed);
1139 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001140 __ Bind(&exit_loop);
1141
1142 if (kPoisonHeapReferences) {
1143 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1144 if (value.Is(expected)) {
1145 // Do not unpoison `value`, as it is the same register as
1146 // `expected`, which has just been unpoisoned.
1147 } else {
1148 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1149 }
1150 }
1151
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001152 __ B(GetExitLabel());
1153 }
1154
1155 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001156 // The register containing the object holding the marked object reference field.
1157 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001158 // The offset, index and scale factor to access the reference in `obj_`.
1159 uint32_t offset_;
1160 Location index_;
1161 size_t scale_factor_;
1162 // Is a null check required?
1163 bool needs_null_check_;
1164 // Should this reference load use Load-Acquire semantics?
1165 bool use_load_acquire_;
1166 // A temporary register used to hold the lock word of `obj_`; and
1167 // also to hold the original reference value, when the reference is
1168 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001169 const Register temp_;
1170
Roland Levillain54f869e2017-03-06 13:54:11 +00001171 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001172};
1173
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001174// Slow path generating a read barrier for a heap reference.
1175class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1176 public:
1177 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1178 Location out,
1179 Location ref,
1180 Location obj,
1181 uint32_t offset,
1182 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001183 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001184 out_(out),
1185 ref_(ref),
1186 obj_(obj),
1187 offset_(offset),
1188 index_(index) {
1189 DCHECK(kEmitCompilerReadBarrier);
1190 // If `obj` is equal to `out` or `ref`, it means the initial object
1191 // has been overwritten by (or after) the heap object reference load
1192 // to be instrumented, e.g.:
1193 //
1194 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001195 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001196 //
1197 // In that case, we have lost the information about the original
1198 // object, and the emitted read barrier cannot work properly.
1199 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1200 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1201 }
1202
1203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1204 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1205 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001207 DCHECK(locations->CanCall());
1208 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001209 DCHECK(instruction_->IsInstanceFieldGet() ||
1210 instruction_->IsStaticFieldGet() ||
1211 instruction_->IsArrayGet() ||
1212 instruction_->IsInstanceOf() ||
1213 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001214 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001215 << "Unexpected instruction in read barrier for heap reference slow path: "
1216 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001217 // The read barrier instrumentation of object ArrayGet
1218 // instructions does not support the HIntermediateAddress
1219 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001220 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001221 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001222
1223 __ Bind(GetEntryLabel());
1224
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001225 SaveLiveRegisters(codegen, locations);
1226
1227 // We may have to change the index's value, but as `index_` is a
1228 // constant member (like other "inputs" of this slow path),
1229 // introduce a copy of it, `index`.
1230 Location index = index_;
1231 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001232 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001233 if (instruction_->IsArrayGet()) {
1234 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001235 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001236 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1237 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1238 // We are about to change the value of `index_reg` (see the
1239 // calls to vixl::MacroAssembler::Lsl and
1240 // vixl::MacroAssembler::Mov below), but it has
1241 // not been saved by the previous call to
1242 // art::SlowPathCode::SaveLiveRegisters, as it is a
1243 // callee-save register --
1244 // art::SlowPathCode::SaveLiveRegisters does not consider
1245 // callee-save registers, as it has been designed with the
1246 // assumption that callee-save registers are supposed to be
1247 // handled by the called function. So, as a callee-save
1248 // register, `index_reg` _would_ eventually be saved onto
1249 // the stack, but it would be too late: we would have
1250 // changed its value earlier. Therefore, we manually save
1251 // it here into another freely available register,
1252 // `free_reg`, chosen of course among the caller-save
1253 // registers (as a callee-save `free_reg` register would
1254 // exhibit the same problem).
1255 //
1256 // Note we could have requested a temporary register from
1257 // the register allocator instead; but we prefer not to, as
1258 // this is a slow path, and we know we can find a
1259 // caller-save register that is available.
1260 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1261 __ Mov(free_reg.W(), index_reg);
1262 index_reg = free_reg;
1263 index = LocationFrom(index_reg);
1264 } else {
1265 // The initial register stored in `index_` has already been
1266 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1267 // (as it is not a callee-save register), so we can freely
1268 // use it.
1269 }
1270 // Shifting the index value contained in `index_reg` by the scale
1271 // factor (2) cannot overflow in practice, as the runtime is
1272 // unable to allocate object arrays with a size larger than
1273 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001275 static_assert(
1276 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1277 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1278 __ Add(index_reg, index_reg, Operand(offset_));
1279 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001280 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1281 // intrinsics, `index_` is not shifted by a scale factor of 2
1282 // (as in the case of ArrayGet), as it is actually an offset
1283 // to an object field within an object.
1284 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001285 DCHECK(instruction_->GetLocations()->Intrinsified());
1286 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1287 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1288 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001289 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001290 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001291 }
1292 }
1293
1294 // We're moving two or three locations to locations that could
1295 // overlap, so we need a parallel move resolver.
1296 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001297 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001298 parallel_move.AddMove(ref_,
1299 LocationFrom(calling_convention.GetRegisterAt(0)),
1300 type,
1301 nullptr);
1302 parallel_move.AddMove(obj_,
1303 LocationFrom(calling_convention.GetRegisterAt(1)),
1304 type,
1305 nullptr);
1306 if (index.IsValid()) {
1307 parallel_move.AddMove(index,
1308 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001309 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001310 nullptr);
1311 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1312 } else {
1313 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1314 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1315 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001316 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001317 instruction_,
1318 instruction_->GetDexPc(),
1319 this);
1320 CheckEntrypointTypes<
1321 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1322 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1323
1324 RestoreLiveRegisters(codegen, locations);
1325
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001326 __ B(GetExitLabel());
1327 }
1328
1329 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1330
1331 private:
1332 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001333 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1334 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001335 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1336 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1337 return Register(VIXLRegCodeFromART(i), kXRegSize);
1338 }
1339 }
1340 // We shall never fail to find a free caller-save register, as
1341 // there are more than two core caller-save registers on ARM64
1342 // (meaning it is possible to find one which is different from
1343 // `ref` and `obj`).
1344 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1345 LOG(FATAL) << "Could not find a free register";
1346 UNREACHABLE();
1347 }
1348
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001349 const Location out_;
1350 const Location ref_;
1351 const Location obj_;
1352 const uint32_t offset_;
1353 // An additional location containing an index to an array.
1354 // Only used for HArrayGet and the UnsafeGetObject &
1355 // UnsafeGetObjectVolatile intrinsics.
1356 const Location index_;
1357
1358 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1359};
1360
1361// Slow path generating a read barrier for a GC root.
1362class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1363 public:
1364 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001365 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001366 DCHECK(kEmitCompilerReadBarrier);
1367 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001368
1369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1370 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001371 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001372 DCHECK(locations->CanCall());
1373 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001374 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1375 << "Unexpected instruction in read barrier for GC root slow path: "
1376 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001377
1378 __ Bind(GetEntryLabel());
1379 SaveLiveRegisters(codegen, locations);
1380
1381 InvokeRuntimeCallingConvention calling_convention;
1382 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1383 // The argument of the ReadBarrierForRootSlow is not a managed
1384 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1385 // thus we need a 64-bit move here, and we cannot use
1386 //
1387 // arm64_codegen->MoveLocation(
1388 // LocationFrom(calling_convention.GetRegisterAt(0)),
1389 // root_,
1390 // type);
1391 //
1392 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001393 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001394 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001395 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001396 instruction_,
1397 instruction_->GetDexPc(),
1398 this);
1399 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1400 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1401
1402 RestoreLiveRegisters(codegen, locations);
1403 __ B(GetExitLabel());
1404 }
1405
1406 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1407
1408 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001409 const Location out_;
1410 const Location root_;
1411
1412 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1413};
1414
Alexandre Rames5319def2014-10-23 10:03:10 +01001415#undef __
1416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001417Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001418 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001419 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001420 LOG(FATAL) << "Unreachable type " << type;
1421 }
1422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001423 if (DataType::IsFloatingPointType(type) &&
Alexandre Rames5319def2014-10-23 10:03:10 +01001424 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001425 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001426 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001427 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1428 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1429 } else {
1430 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001431 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1432 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001433 }
1434
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001435 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001436 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001437 return next_location;
1438}
1439
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001440Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001441 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001442}
1443
Serban Constantinescu579885a2015-02-22 20:51:33 +00001444CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1445 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001446 const CompilerOptions& compiler_options,
1447 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001448 : CodeGenerator(graph,
1449 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001450 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001451 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001452 callee_saved_core_registers.GetList(),
1453 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001454 compiler_options,
1455 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001456 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1457 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001458 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001459 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001460 move_resolver_(graph->GetAllocator(), this),
1461 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001462 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001463 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001464 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001465 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001466 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1467 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1468 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1469 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1470 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1471 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1472 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1473 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001474 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001475 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001476 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001477 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001478 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001479 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001480}
Alexandre Rames5319def2014-10-23 10:03:10 +01001481
Alexandre Rames67555f72014-11-18 10:55:16 +00001482#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001483
Zheng Xu3927c8b2015-11-18 17:46:25 +08001484void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001485 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001486 jump_table->EmitTable(this);
1487 }
1488}
1489
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001490void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001491 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001492 // Ensure we emit the literal pool.
1493 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001494
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001495 CodeGenerator::Finalize(allocator);
1496}
1497
Zheng Xuad4450e2015-04-17 18:48:56 +08001498void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1499 // Note: There are 6 kinds of moves:
1500 // 1. constant -> GPR/FPR (non-cycle)
1501 // 2. constant -> stack (non-cycle)
1502 // 3. GPR/FPR -> GPR/FPR
1503 // 4. GPR/FPR -> stack
1504 // 5. stack -> GPR/FPR
1505 // 6. stack -> stack (non-cycle)
1506 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1507 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1508 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1509 // dependency.
1510 vixl_temps_.Open(GetVIXLAssembler());
1511}
1512
1513void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1514 vixl_temps_.Close();
1515}
1516
1517Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001518 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1519 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1520 || kind == Location::kSIMDStackSlot);
1521 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1522 ? Location::kFpuRegister
1523 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001524 Location scratch = GetScratchLocation(kind);
1525 if (!scratch.Equals(Location::NoLocation())) {
1526 return scratch;
1527 }
1528 // Allocate from VIXL temp registers.
1529 if (kind == Location::kRegister) {
1530 scratch = LocationFrom(vixl_temps_.AcquireX());
1531 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001532 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001533 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1534 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1535 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001536 }
1537 AddScratchLocation(scratch);
1538 return scratch;
1539}
1540
1541void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1542 if (loc.IsRegister()) {
1543 vixl_temps_.Release(XRegisterFrom(loc));
1544 } else {
1545 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001546 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001547 }
1548 RemoveScratchLocation(loc);
1549}
1550
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001552 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001553 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001554}
1555
Alexandre Rames5319def2014-10-23 10:03:10 +01001556void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001557 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001558 __ Bind(&frame_entry_label_);
1559
Vladimir Marko33bff252017-11-01 14:35:42 +00001560 bool do_overflow_check =
1561 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001562 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001563 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001564 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001565 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001566 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001567 {
1568 // Ensure that between load and RecordPcInfo there are no pools emitted.
1569 ExactAssemblyScope eas(GetVIXLAssembler(),
1570 kInstructionSize,
1571 CodeBufferCheckScope::kExactSize);
1572 __ ldr(wzr, MemOperand(temp, 0));
1573 RecordPcInfo(nullptr, 0);
1574 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001575 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001576
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001577 if (!HasEmptyFrame()) {
1578 int frame_size = GetFrameSize();
1579 // Stack layout:
1580 // sp[frame_size - 8] : lr.
1581 // ... : other preserved core registers.
1582 // ... : other preserved fp registers.
1583 // ... : reserved frame space.
1584 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001585
1586 // Save the current method if we need it. Note that we do not
1587 // do this in HCurrentMethod, as the instruction might have been removed
1588 // in the SSA graph.
1589 if (RequiresCurrentMethod()) {
1590 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001591 } else {
1592 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001593 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001594 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001595 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1596 frame_size - GetCoreSpillSize());
1597 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1598 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001599
1600 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1601 // Initialize should_deoptimize flag to 0.
1602 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1603 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1604 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001605 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001606
1607 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001608}
1609
1610void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001611 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001612 if (!HasEmptyFrame()) {
1613 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001614 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1615 frame_size - FrameEntrySpillSize());
1616 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1617 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001618 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001619 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001620 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001621 __ Ret();
1622 GetAssembler()->cfi().RestoreState();
1623 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001624}
1625
Scott Wakeling97c72b72016-06-24 16:19:36 +01001626CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001627 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001628 return CPURegList(CPURegister::kRegister, kXRegSize,
1629 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001630}
1631
Scott Wakeling97c72b72016-06-24 16:19:36 +01001632CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001633 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1634 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001635 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1636 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001637}
1638
Alexandre Rames5319def2014-10-23 10:03:10 +01001639void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1640 __ Bind(GetLabelOf(block));
1641}
1642
Calin Juravle175dc732015-08-25 15:42:32 +01001643void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1644 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001645 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001646}
1647
Calin Juravlee460d1d2015-09-29 04:52:17 +01001648void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1649 if (location.IsRegister()) {
1650 locations->AddTemp(location);
1651 } else {
1652 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1653 }
1654}
1655
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001656void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001657 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001658 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001659 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001660 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001661 if (value_can_be_null) {
1662 __ Cbz(value, &done);
1663 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001664 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001666 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001667 if (value_can_be_null) {
1668 __ Bind(&done);
1669 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001670}
1671
David Brazdil58282f42016-01-14 12:45:10 +00001672void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001673 // Blocked core registers:
1674 // lr : Runtime reserved.
1675 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001676 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001677 // ip1 : VIXL core temp.
1678 // ip0 : VIXL core temp.
1679 //
1680 // Blocked fp registers:
1681 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001682 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1683 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001684 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001685 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001686 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001687
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001688 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001689 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001690 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001691 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001692
David Brazdil58282f42016-01-14 12:45:10 +00001693 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001694 // Stubs do not save callee-save floating point registers. If the graph
1695 // is debuggable, we need to deal with these registers differently. For
1696 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001697 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1698 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001699 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001700 }
1701 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001702}
1703
Alexandre Rames3e69f162014-12-10 10:36:50 +00001704size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1705 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1706 __ Str(reg, MemOperand(sp, stack_index));
1707 return kArm64WordSize;
1708}
1709
1710size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1711 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1712 __ Ldr(reg, MemOperand(sp, stack_index));
1713 return kArm64WordSize;
1714}
1715
1716size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1717 FPRegister reg = FPRegister(reg_id, kDRegSize);
1718 __ Str(reg, MemOperand(sp, stack_index));
1719 return kArm64WordSize;
1720}
1721
1722size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1723 FPRegister reg = FPRegister(reg_id, kDRegSize);
1724 __ Ldr(reg, MemOperand(sp, stack_index));
1725 return kArm64WordSize;
1726}
1727
Alexandre Rames5319def2014-10-23 10:03:10 +01001728void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001729 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001730}
1731
1732void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001733 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001734}
1735
Alexandre Rames67555f72014-11-18 10:55:16 +00001736void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001737 if (constant->IsIntConstant()) {
1738 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1739 } else if (constant->IsLongConstant()) {
1740 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1741 } else if (constant->IsNullConstant()) {
1742 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001743 } else if (constant->IsFloatConstant()) {
1744 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1745 } else {
1746 DCHECK(constant->IsDoubleConstant());
1747 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1748 }
1749}
1750
Alexandre Rames3e69f162014-12-10 10:36:50 +00001751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001752static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001753 DCHECK(constant.IsConstant());
1754 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001756 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001757 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1758 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1759 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1760 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001761}
1762
Roland Levillain952b2352017-05-03 19:49:14 +01001763// Allocate a scratch register from the VIXL pool, querying first
1764// the floating-point register pool, and then the core register
1765// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001766// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1767// using a different allocation strategy.
1768static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1769 vixl::aarch64::UseScratchRegisterScope* temps,
1770 int size_in_bits) {
1771 return masm->GetScratchFPRegisterList()->IsEmpty()
1772 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1773 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1774}
1775
Calin Juravlee460d1d2015-09-29 04:52:17 +01001776void CodeGeneratorARM64::MoveLocation(Location destination,
1777 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001779 if (source.Equals(destination)) {
1780 return;
1781 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001782
1783 // A valid move can always be inferred from the destination and source
1784 // locations. When moving from and to a register, the argument type can be
1785 // used to generate 32bit instead of 64bit moves. In debug mode we also
1786 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001788
1789 if (destination.IsRegister() || destination.IsFpuRegister()) {
1790 if (unspecified_type) {
1791 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1792 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001793 (src_cst != nullptr && (src_cst->IsIntConstant()
1794 || src_cst->IsFloatConstant()
1795 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001796 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001797 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001798 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001799 // If the source is a double stack slot or a 64bit constant, a 64bit
1800 // type is appropriate. Else the source is a register, and since the
1801 // type has not been specified, we chose a 64bit type to force a 64bit
1802 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001803 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001804 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001805 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001806 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1807 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001808 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1810 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1811 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001812 } else if (source.IsSIMDStackSlot()) {
1813 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001814 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001815 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001816 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001817 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001818 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001819 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001820 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001821 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 DataType::Type source_type = DataType::Is64BitType(dst_type)
1823 ? DataType::Type::kInt64
1824 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001825 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1826 }
1827 } else {
1828 DCHECK(source.IsFpuRegister());
1829 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 DataType::Type source_type = DataType::Is64BitType(dst_type)
1831 ? DataType::Type::kFloat64
1832 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001833 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1834 } else {
1835 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001836 if (GetGraph()->HasSIMD()) {
1837 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1838 } else {
1839 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1840 }
1841 }
1842 }
1843 } else if (destination.IsSIMDStackSlot()) {
1844 if (source.IsFpuRegister()) {
1845 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1846 } else {
1847 DCHECK(source.IsSIMDStackSlot());
1848 UseScratchRegisterScope temps(GetVIXLAssembler());
1849 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1850 Register temp = temps.AcquireX();
1851 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1852 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1853 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1854 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1855 } else {
1856 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1857 __ Ldr(temp, StackOperandFrom(source));
1858 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001859 }
1860 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001861 } else { // The destination is not a register. It must be a stack slot.
1862 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1863 if (source.IsRegister() || source.IsFpuRegister()) {
1864 if (unspecified_type) {
1865 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001866 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001867 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001868 dst_type =
1869 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001870 }
1871 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001872 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1873 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001874 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001875 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001876 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1877 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001878 UseScratchRegisterScope temps(GetVIXLAssembler());
1879 HConstant* src_cst = source.GetConstant();
1880 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001881 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001882 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1883 ? Register(xzr)
1884 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001885 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001886 if (src_cst->IsIntConstant()) {
1887 temp = temps.AcquireW();
1888 } else if (src_cst->IsLongConstant()) {
1889 temp = temps.AcquireX();
1890 } else if (src_cst->IsFloatConstant()) {
1891 temp = temps.AcquireS();
1892 } else {
1893 DCHECK(src_cst->IsDoubleConstant());
1894 temp = temps.AcquireD();
1895 }
1896 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001897 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001898 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001899 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001900 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001901 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001902 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001903 // Use any scratch register (a core or a floating-point one)
1904 // from VIXL scratch register pools as a temporary.
1905 //
1906 // We used to only use the FP scratch register pool, but in some
1907 // rare cases the only register from this pool (D31) would
1908 // already be used (e.g. within a ParallelMove instruction, when
1909 // a move is blocked by a another move requiring a scratch FP
1910 // register, which would reserve D31). To prevent this issue, we
1911 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001912 //
1913 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001914 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001915 // use AcquireFPOrCoreCPURegisterOfSize instead of
1916 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1917 // allocates core scratch registers first.
1918 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1919 GetVIXLAssembler(),
1920 &temps,
1921 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001922 __ Ldr(temp, StackOperandFrom(source));
1923 __ Str(temp, StackOperandFrom(destination));
1924 }
1925 }
1926}
1927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001928void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001929 CPURegister dst,
1930 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001931 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001933 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001934 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001935 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001936 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001937 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001938 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001939 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001940 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001941 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001942 case DataType::Type::kInt16:
1943 __ Ldrsh(Register(dst), src);
1944 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001945 case DataType::Type::kInt32:
1946 case DataType::Type::kReference:
1947 case DataType::Type::kInt64:
1948 case DataType::Type::kFloat32:
1949 case DataType::Type::kFloat64:
1950 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001951 __ Ldr(dst, src);
1952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001953 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001954 LOG(FATAL) << "Unreachable type " << type;
1955 }
1956}
1957
Calin Juravle77520bc2015-01-12 18:45:46 +00001958void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001959 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001960 const MemOperand& src,
1961 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001962 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001963 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001964 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001965 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001966
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001967 DCHECK(!src.IsPreIndex());
1968 DCHECK(!src.IsPostIndex());
1969
1970 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001971 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001972 {
1973 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1974 MemOperand base = MemOperand(temp_base);
1975 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001977 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001978 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001979 {
1980 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1981 __ ldarb(Register(dst), base);
1982 if (needs_null_check) {
1983 MaybeRecordImplicitNullCheck(instruction);
1984 }
1985 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001986 if (type == DataType::Type::kInt8) {
1987 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001988 }
1989 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001990 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001992 {
1993 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1994 __ ldarh(Register(dst), base);
1995 if (needs_null_check) {
1996 MaybeRecordImplicitNullCheck(instruction);
1997 }
1998 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001999 if (type == DataType::Type::kInt16) {
2000 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
2001 }
Artem Serov914d7a82017-02-07 14:33:49 +00002002 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002003 case DataType::Type::kInt32:
2004 case DataType::Type::kReference:
2005 case DataType::Type::kInt64:
2006 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002007 {
2008 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2009 __ ldar(Register(dst), base);
2010 if (needs_null_check) {
2011 MaybeRecordImplicitNullCheck(instruction);
2012 }
2013 }
2014 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002015 case DataType::Type::kFloat32:
2016 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00002017 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002018 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002019
Artem Serov914d7a82017-02-07 14:33:49 +00002020 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2021 {
2022 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2023 __ ldar(temp, base);
2024 if (needs_null_check) {
2025 MaybeRecordImplicitNullCheck(instruction);
2026 }
2027 }
2028 __ Fmov(FPRegister(dst), temp);
2029 break;
Roland Levillain44015862016-01-22 11:47:17 +00002030 }
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;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002058 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002059 LOG(FATAL) << "Unreachable type " << type;
2060 }
2061}
2062
Artem Serov914d7a82017-02-07 14:33:49 +00002063void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002064 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002065 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002066 const MemOperand& dst,
2067 bool needs_null_check) {
2068 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002069 UseScratchRegisterScope temps(GetVIXLAssembler());
2070 Register temp_base = temps.AcquireX();
2071
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002072 DCHECK(!dst.IsPreIndex());
2073 DCHECK(!dst.IsPostIndex());
2074
2075 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002076 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002077 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002078 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002079 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002080 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002081 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002082 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00002084 {
2085 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2086 __ stlrb(Register(src), base);
2087 if (needs_null_check) {
2088 MaybeRecordImplicitNullCheck(instruction);
2089 }
2090 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002091 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002092 case DataType::Type::kUint16:
2093 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00002094 {
2095 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2096 __ stlrh(Register(src), base);
2097 if (needs_null_check) {
2098 MaybeRecordImplicitNullCheck(instruction);
2099 }
2100 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002101 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 case DataType::Type::kInt32:
2103 case DataType::Type::kReference:
2104 case DataType::Type::kInt64:
2105 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002106 {
2107 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2108 __ stlr(Register(src), base);
2109 if (needs_null_check) {
2110 MaybeRecordImplicitNullCheck(instruction);
2111 }
2112 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002113 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 case DataType::Type::kFloat32:
2115 case DataType::Type::kFloat64: {
2116 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002117 Register temp_src;
2118 if (src.IsZero()) {
2119 // The zero register is used to avoid synthesizing zero constants.
2120 temp_src = Register(src);
2121 } else {
2122 DCHECK(src.IsFPRegister());
2123 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2124 __ Fmov(temp_src, FPRegister(src));
2125 }
Artem Serov914d7a82017-02-07 14:33:49 +00002126 {
2127 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2128 __ stlr(temp_src, base);
2129 if (needs_null_check) {
2130 MaybeRecordImplicitNullCheck(instruction);
2131 }
2132 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002133 break;
2134 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002136 LOG(FATAL) << "Unreachable type " << type;
2137 }
2138}
2139
Calin Juravle175dc732015-08-25 15:42:32 +01002140void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2141 HInstruction* instruction,
2142 uint32_t dex_pc,
2143 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002144 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002145
2146 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2147 {
2148 // Ensure the pc position is recorded immediately after the `blr` instruction.
2149 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2150 __ blr(lr);
2151 if (EntrypointRequiresStackMap(entrypoint)) {
2152 RecordPcInfo(instruction, dex_pc, slow_path);
2153 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002154 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002155}
2156
Roland Levillaindec8f632016-07-22 17:10:06 +01002157void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2158 HInstruction* instruction,
2159 SlowPathCode* slow_path) {
2160 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002161 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2162 __ Blr(lr);
2163}
2164
Alexandre Rames67555f72014-11-18 10:55:16 +00002165void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002166 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 UseScratchRegisterScope temps(GetVIXLAssembler());
2168 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002169 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2170
Serban Constantinescu02164b32014-11-13 14:05:07 +00002171 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002172 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2173 __ Add(temp, class_reg, status_offset);
2174 __ Ldar(temp, HeapOperand(temp));
2175 __ Cmp(temp, mirror::Class::kStatusInitialized);
2176 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002177 __ Bind(slow_path->GetExitLabel());
2178}
Alexandre Rames5319def2014-10-23 10:03:10 +01002179
Roland Levillain44015862016-01-22 11:47:17 +00002180void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002181 BarrierType type = BarrierAll;
2182
2183 switch (kind) {
2184 case MemBarrierKind::kAnyAny:
2185 case MemBarrierKind::kAnyStore: {
2186 type = BarrierAll;
2187 break;
2188 }
2189 case MemBarrierKind::kLoadAny: {
2190 type = BarrierReads;
2191 break;
2192 }
2193 case MemBarrierKind::kStoreStore: {
2194 type = BarrierWrites;
2195 break;
2196 }
2197 default:
2198 LOG(FATAL) << "Unexpected memory barrier " << kind;
2199 }
2200 __ Dmb(InnerShareable, type);
2201}
2202
Serban Constantinescu02164b32014-11-13 14:05:07 +00002203void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2204 HBasicBlock* successor) {
2205 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002206 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2207 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002208 slow_path =
2209 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002210 instruction->SetSlowPath(slow_path);
2211 codegen_->AddSlowPath(slow_path);
2212 if (successor != nullptr) {
2213 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002214 }
2215 } else {
2216 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2217 }
2218
Serban Constantinescu02164b32014-11-13 14:05:07 +00002219 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2220 Register temp = temps.AcquireW();
2221
Andreas Gampe542451c2016-07-26 09:02:02 -07002222 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002223 if (successor == nullptr) {
2224 __ Cbnz(temp, slow_path->GetEntryLabel());
2225 __ Bind(slow_path->GetReturnLabel());
2226 } else {
2227 __ Cbz(temp, codegen_->GetLabelOf(successor));
2228 __ B(slow_path->GetEntryLabel());
2229 // slow_path will return to GetLabelOf(successor).
2230 }
2231}
2232
Alexandre Rames5319def2014-10-23 10:03:10 +01002233InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2234 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002235 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002236 assembler_(codegen->GetAssembler()),
2237 codegen_(codegen) {}
2238
Alexandre Rames67555f72014-11-18 10:55:16 +00002239void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002240 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002241 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002243 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 case DataType::Type::kInt32:
2245 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01002246 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002247 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002249 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002250
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002251 case DataType::Type::kFloat32:
2252 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002253 locations->SetInAt(0, Location::RequiresFpuRegister());
2254 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002255 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002256 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002257
Alexandre Rames5319def2014-10-23 10:03:10 +01002258 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002259 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002260 }
2261}
2262
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002263void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2264 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002265 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2266
2267 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01002269 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2271 object_field_get_with_read_barrier
2272 ? LocationSummary::kCallOnSlowPath
2273 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002274 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002275 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002276 // We need a temporary register for the read barrier marking slow
2277 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002278 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2279 !Runtime::Current()->UseJitCompilation() &&
2280 !field_info.IsVolatile()) {
2281 // If link-time thunks for the Baker read barrier are enabled, for AOT
2282 // non-volatile loads we need a temporary only if the offset is too big.
2283 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2284 locations->AddTemp(FixedTempLocation());
2285 }
2286 } else {
2287 locations->AddTemp(Location::RequiresRegister());
2288 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002289 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002290 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002292 locations->SetOut(Location::RequiresFpuRegister());
2293 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 // The output overlaps for an object field get when read barriers
2295 // are enabled: we do not want the load to overwrite the object's
2296 // location, as we need it to emit the read barrier.
2297 locations->SetOut(
2298 Location::RequiresRegister(),
2299 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002300 }
2301}
2302
2303void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2304 const FieldInfo& field_info) {
2305 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002306 LocationSummary* locations = instruction->GetLocations();
2307 Location base_loc = locations->InAt(0);
2308 Location out = locations->Out();
2309 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01002310 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
2311 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002312 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002314 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01002315 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002316 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002317 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002319 Location maybe_temp =
2320 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002321 // Note that potential implicit null checks are handled in this
2322 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2323 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2324 instruction,
2325 out,
2326 base,
2327 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002328 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002329 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002330 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002331 } else {
2332 // General case.
2333 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002334 // Note that a potential implicit null check is handled in this
2335 // CodeGeneratorARM64::LoadAcquire call.
2336 // NB: LoadAcquire will record the pc info if needed.
2337 codegen_->LoadAcquire(
2338 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002339 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002340 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2341 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002342 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002343 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002344 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002345 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002346 // If read barriers are enabled, emit read barriers other than
2347 // Baker's using a slow path (and also unpoison the loaded
2348 // reference, if heap poisoning is enabled).
2349 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2350 }
Roland Levillain4d027112015-07-01 15:41:14 +01002351 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002352}
2353
2354void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2355 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002357 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002358 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2359 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002361 locations->SetInAt(1, Location::RequiresFpuRegister());
2362 } else {
2363 locations->SetInAt(1, Location::RequiresRegister());
2364 }
2365}
2366
2367void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002368 const FieldInfo& field_info,
2369 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002370 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2371
2372 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002373 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002374 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002375 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002377
Roland Levillain4d027112015-07-01 15:41:14 +01002378 {
2379 // We use a block to end the scratch scope before the write barrier, thus
2380 // freeing the temporary registers so they can be used in `MarkGCCard`.
2381 UseScratchRegisterScope temps(GetVIXLAssembler());
2382
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002383 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002384 DCHECK(value.IsW());
2385 Register temp = temps.AcquireW();
2386 __ Mov(temp, value.W());
2387 GetAssembler()->PoisonHeapReference(temp.W());
2388 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002389 }
Roland Levillain4d027112015-07-01 15:41:14 +01002390
2391 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002392 codegen_->StoreRelease(
2393 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002394 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002395 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2396 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002397 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2398 codegen_->MaybeRecordImplicitNullCheck(instruction);
2399 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002400 }
2401
2402 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002403 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002404 }
2405}
2406
Alexandre Rames67555f72014-11-18 10:55:16 +00002407void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002409
2410 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 case DataType::Type::kInt32:
2412 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002413 Register dst = OutputRegister(instr);
2414 Register lhs = InputRegisterAt(instr, 0);
2415 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002416 if (instr->IsAdd()) {
2417 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002418 } else if (instr->IsAnd()) {
2419 __ And(dst, lhs, rhs);
2420 } else if (instr->IsOr()) {
2421 __ Orr(dst, lhs, rhs);
2422 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002423 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002424 } else if (instr->IsRor()) {
2425 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002426 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002427 __ Ror(dst, lhs, shift);
2428 } else {
2429 // Ensure shift distance is in the same size register as the result. If
2430 // we are rotating a long and the shift comes in a w register originally,
2431 // we don't need to sxtw for use as an x since the shift distances are
2432 // all & reg_bits - 1.
2433 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2434 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002435 } else {
2436 DCHECK(instr->IsXor());
2437 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002438 }
2439 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002440 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 case DataType::Type::kFloat32:
2442 case DataType::Type::kFloat64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002443 FPRegister dst = OutputFPRegister(instr);
2444 FPRegister lhs = InputFPRegisterAt(instr, 0);
2445 FPRegister rhs = InputFPRegisterAt(instr, 1);
2446 if (instr->IsAdd()) {
2447 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002449 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002450 } else {
2451 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002452 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002453 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002454 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002455 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002456 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002457 }
2458}
2459
Serban Constantinescu02164b32014-11-13 14:05:07 +00002460void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2461 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2462
Vladimir Markoca6fff82017-10-03 14:49:14 +01002463 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002464 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002465 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 case DataType::Type::kInt32:
2467 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002468 locations->SetInAt(0, Location::RequiresRegister());
2469 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002471 break;
2472 }
2473 default:
2474 LOG(FATAL) << "Unexpected shift type " << type;
2475 }
2476}
2477
2478void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2479 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2480
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002482 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 case DataType::Type::kInt32:
2484 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002485 Register dst = OutputRegister(instr);
2486 Register lhs = InputRegisterAt(instr, 0);
2487 Operand rhs = InputOperandAt(instr, 1);
2488 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002489 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002491 if (instr->IsShl()) {
2492 __ Lsl(dst, lhs, shift_value);
2493 } else if (instr->IsShr()) {
2494 __ Asr(dst, lhs, shift_value);
2495 } else {
2496 __ Lsr(dst, lhs, shift_value);
2497 }
2498 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002499 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002500
2501 if (instr->IsShl()) {
2502 __ Lsl(dst, lhs, rhs_reg);
2503 } else if (instr->IsShr()) {
2504 __ Asr(dst, lhs, rhs_reg);
2505 } else {
2506 __ Lsr(dst, lhs, rhs_reg);
2507 }
2508 }
2509 break;
2510 }
2511 default:
2512 LOG(FATAL) << "Unexpected shift operation type " << type;
2513 }
2514}
2515
Alexandre Rames5319def2014-10-23 10:03:10 +01002516void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002517 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002518}
2519
2520void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002521 HandleBinaryOp(instruction);
2522}
2523
2524void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2525 HandleBinaryOp(instruction);
2526}
2527
2528void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2529 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002530}
2531
Artem Serov7fc63502016-02-09 17:15:29 +00002532void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002534 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002535 locations->SetInAt(0, Location::RequiresRegister());
2536 // There is no immediate variant of negated bitwise instructions in AArch64.
2537 locations->SetInAt(1, Location::RequiresRegister());
2538 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2539}
2540
Artem Serov7fc63502016-02-09 17:15:29 +00002541void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002542 Register dst = OutputRegister(instr);
2543 Register lhs = InputRegisterAt(instr, 0);
2544 Register rhs = InputRegisterAt(instr, 1);
2545
2546 switch (instr->GetOpKind()) {
2547 case HInstruction::kAnd:
2548 __ Bic(dst, lhs, rhs);
2549 break;
2550 case HInstruction::kOr:
2551 __ Orn(dst, lhs, rhs);
2552 break;
2553 case HInstruction::kXor:
2554 __ Eon(dst, lhs, rhs);
2555 break;
2556 default:
2557 LOG(FATAL) << "Unreachable";
2558 }
2559}
2560
Anton Kirilov74234da2017-01-13 14:42:47 +00002561void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2562 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002563 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2564 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002565 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002566 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002567 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2568 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2569 } else {
2570 locations->SetInAt(0, Location::RequiresRegister());
2571 }
2572 locations->SetInAt(1, Location::RequiresRegister());
2573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2574}
2575
Anton Kirilov74234da2017-01-13 14:42:47 +00002576void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2577 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002578 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002579 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002581 Register out = OutputRegister(instruction);
2582 Register left;
2583 if (kind != HInstruction::kNeg) {
2584 left = InputRegisterAt(instruction, 0);
2585 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002586 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002587 // shifter operand operation, the IR generating `right_reg` (input to the type
2588 // conversion) can have a different type from the current instruction's type,
2589 // so we manually indicate the type.
2590 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002591 Operand right_operand(0);
2592
Anton Kirilov74234da2017-01-13 14:42:47 +00002593 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2594 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002595 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2596 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002597 right_operand = Operand(right_reg,
2598 helpers::ShiftFromOpKind(op_kind),
2599 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002600 }
2601
2602 // Logical binary operations do not support extension operations in the
2603 // operand. Note that VIXL would still manage if it was passed by generating
2604 // the extension as a separate instruction.
2605 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2606 DCHECK(!right_operand.IsExtendedRegister() ||
2607 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2608 kind != HInstruction::kNeg));
2609 switch (kind) {
2610 case HInstruction::kAdd:
2611 __ Add(out, left, right_operand);
2612 break;
2613 case HInstruction::kAnd:
2614 __ And(out, left, right_operand);
2615 break;
2616 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002617 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002618 __ Neg(out, right_operand);
2619 break;
2620 case HInstruction::kOr:
2621 __ Orr(out, left, right_operand);
2622 break;
2623 case HInstruction::kSub:
2624 __ Sub(out, left, right_operand);
2625 break;
2626 case HInstruction::kXor:
2627 __ Eor(out, left, right_operand);
2628 break;
2629 default:
2630 LOG(FATAL) << "Unexpected operation kind: " << kind;
2631 UNREACHABLE();
2632 }
2633}
2634
Artem Serov328429f2016-07-06 16:23:04 +01002635void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002636 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002637 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002638 locations->SetInAt(0, Location::RequiresRegister());
2639 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002640 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002641}
2642
Roland Levillain19c54192016-11-04 13:44:09 +00002643void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002644 __ Add(OutputRegister(instruction),
2645 InputRegisterAt(instruction, 0),
2646 Operand(InputOperandAt(instruction, 1)));
2647}
2648
Artem Serove1811ed2017-04-27 16:50:47 +01002649void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2650 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002651 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002652
2653 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2654
2655 locations->SetInAt(0, Location::RequiresRegister());
2656 // For byte case we don't need to shift the index variable so we can encode the data offset into
2657 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2658 // data offset constant generation out of the loop and reduce the critical path length in the
2659 // loop.
2660 locations->SetInAt(1, shift->GetValue() == 0
2661 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2662 : Location::RequiresRegister());
2663 locations->SetInAt(2, Location::ConstantLocation(shift));
2664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2665}
2666
2667void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2668 HIntermediateAddressIndex* instruction) {
2669 Register index_reg = InputRegisterAt(instruction, 0);
2670 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2671 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2672
2673 if (shift == 0) {
2674 __ Add(OutputRegister(instruction), index_reg, offset);
2675 } else {
2676 Register offset_reg = InputRegisterAt(instruction, 1);
2677 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2678 }
2679}
2680
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002681void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002682 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002683 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002684 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2685 if (instr->GetOpKind() == HInstruction::kSub &&
2686 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002687 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002688 // Don't allocate register for Mneg instruction.
2689 } else {
2690 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2691 Location::RequiresRegister());
2692 }
2693 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2694 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002695 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2696}
2697
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002698void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002699 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002700 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2701 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002702
2703 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2704 // This fixup should be carried out for all multiply-accumulate instructions:
2705 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002707 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2708 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002709 vixl::aarch64::Instruction* prev =
2710 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002711 if (prev->IsLoadOrStore()) {
2712 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002713 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002714 __ nop();
2715 }
2716 }
2717
2718 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002719 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002720 __ Madd(res, mul_left, mul_right, accumulator);
2721 } else {
2722 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002723 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002724 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002725 __ Mneg(res, mul_left, mul_right);
2726 } else {
2727 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2728 __ Msub(res, mul_left, mul_right, accumulator);
2729 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002730 }
2731}
2732
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002733void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002734 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002736 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002737 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2738 object_array_get_with_read_barrier
2739 ? LocationSummary::kCallOnSlowPath
2740 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002741 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002742 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002743 // We need a temporary register for the read barrier marking slow
2744 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002745 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2746 !Runtime::Current()->UseJitCompilation() &&
2747 instruction->GetIndex()->IsConstant()) {
2748 // Array loads with constant index are treated as field loads.
2749 // If link-time thunks for the Baker read barrier are enabled, for AOT
2750 // constant index loads we need a temporary only if the offset is too big.
2751 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2752 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002754 if (offset >= kReferenceLoadMinFarOffset) {
2755 locations->AddTemp(FixedTempLocation());
2756 }
2757 } else {
2758 locations->AddTemp(Location::RequiresRegister());
2759 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002760 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002761 locations->SetInAt(0, Location::RequiresRegister());
2762 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002764 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2765 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002766 // The output overlaps in the case of an object array get with
2767 // read barriers enabled: we do not want the move to overwrite the
2768 // array's location, as we need it to emit the read barrier.
2769 locations->SetOut(
2770 Location::RequiresRegister(),
2771 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002772 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002773}
2774
2775void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002777 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002778 LocationSummary* locations = instruction->GetLocations();
2779 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002780 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002781 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002782 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2783 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002784 MacroAssembler* masm = GetVIXLAssembler();
2785 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002786
Roland Levillain19c54192016-11-04 13:44:09 +00002787 // The read barrier instrumentation of object ArrayGet instructions
2788 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002790 instruction->GetArray()->IsIntermediateAddress() &&
2791 kEmitCompilerReadBarrier));
2792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002793 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002794 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002795 // Note that a potential implicit null check is handled in the
2796 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002797 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002798 if (index.IsConstant()) {
2799 // Array load with a constant index can be treated as a field load.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002801 Location maybe_temp =
2802 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2803 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2804 out,
2805 obj.W(),
2806 offset,
2807 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002808 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002809 /* use_load_acquire */ false);
2810 } else {
2811 Register temp = WRegisterFrom(locations->GetTemp(0));
2812 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002813 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002814 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002815 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002816 // General case.
2817 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002818 Register length;
2819 if (maybe_compressed_char_at) {
2820 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2821 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002822 {
2823 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2824 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2825
2826 if (instruction->GetArray()->IsIntermediateAddress()) {
2827 DCHECK_LT(count_offset, offset);
2828 int64_t adjusted_offset =
2829 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2830 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2831 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2832 } else {
2833 __ Ldr(length, HeapOperand(obj, count_offset));
2834 }
2835 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002836 }
jessicahandojo05765752016-09-09 19:01:32 -07002837 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002838 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002839 if (maybe_compressed_char_at) {
2840 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002841 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2842 "Expecting 0=compressed, 1=uncompressed");
2843 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002844 __ Ldrb(Register(OutputCPURegister(instruction)),
2845 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2846 __ B(&done);
2847 __ Bind(&uncompressed_load);
2848 __ Ldrh(Register(OutputCPURegister(instruction)),
2849 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2850 __ Bind(&done);
2851 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002852 offset += Int64ConstantFrom(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002853 source = HeapOperand(obj, offset);
2854 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002855 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002856 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002857 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002858 // We do not need to compute the intermediate address from the array: the
2859 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002860 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002861 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002862 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002863 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2864 }
2865 temp = obj;
2866 } else {
2867 __ Add(temp, obj, offset);
2868 }
jessicahandojo05765752016-09-09 19:01:32 -07002869 if (maybe_compressed_char_at) {
2870 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002871 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2872 "Expecting 0=compressed, 1=uncompressed");
2873 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002874 __ Ldrb(Register(OutputCPURegister(instruction)),
2875 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2876 __ B(&done);
2877 __ Bind(&uncompressed_load);
2878 __ Ldrh(Register(OutputCPURegister(instruction)),
2879 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2880 __ Bind(&done);
2881 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002883 }
Roland Levillain44015862016-01-22 11:47:17 +00002884 }
jessicahandojo05765752016-09-09 19:01:32 -07002885 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002886 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2887 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002888 codegen_->Load(type, OutputCPURegister(instruction), source);
2889 codegen_->MaybeRecordImplicitNullCheck(instruction);
2890 }
Roland Levillain44015862016-01-22 11:47:17 +00002891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002893 static_assert(
2894 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2895 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2896 Location obj_loc = locations->InAt(0);
2897 if (index.IsConstant()) {
2898 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2899 } else {
2900 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2901 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002902 }
Roland Levillain4d027112015-07-01 15:41:14 +01002903 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002904}
2905
Alexandre Rames5319def2014-10-23 10:03:10 +01002906void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002907 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002908 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002909 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002910}
2911
2912void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002913 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002914 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002915 {
2916 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2917 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2918 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2919 codegen_->MaybeRecordImplicitNullCheck(instruction);
2920 }
jessicahandojo05765752016-09-09 19:01:32 -07002921 // Mask out compression flag from String's array length.
2922 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002923 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002924 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002925}
2926
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002927void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002929
2930 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002931 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002932 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002933 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002934 LocationSummary::kCallOnSlowPath :
2935 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002936 locations->SetInAt(0, Location::RequiresRegister());
2937 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002938 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2939 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002941 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002942 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002943 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002944 }
2945}
2946
2947void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002949 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002950 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002951 bool needs_write_barrier =
2952 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002953
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002954 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002955 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002956 CPURegister source = value;
2957 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002958 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002959 MemOperand destination = HeapOperand(array);
2960 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002961
2962 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002963 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002964 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002966 destination = HeapOperand(array, offset);
2967 } else {
2968 UseScratchRegisterScope temps(masm);
2969 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002970 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002971 // We do not need to compute the intermediate address from the array: the
2972 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002973 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002974 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002975 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002976 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2977 }
2978 temp = array;
2979 } else {
2980 __ Add(temp, array, offset);
2981 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002982 destination = HeapOperand(temp,
2983 XRegisterFrom(index),
2984 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002986 }
Artem Serov914d7a82017-02-07 14:33:49 +00002987 {
2988 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2989 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2990 codegen_->Store(value_type, value, destination);
2991 codegen_->MaybeRecordImplicitNullCheck(instruction);
2992 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002993 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002994 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002995 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002996 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002997 {
2998 // We use a block to end the scratch scope before the write barrier, thus
2999 // freeing the temporary registers so they can be used in `MarkGCCard`.
3000 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003001 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003002 if (index.IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 offset += Int64ConstantFrom(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003004 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003005 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003006 destination = HeapOperand(temp,
3007 XRegisterFrom(index),
3008 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003009 DataType::SizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003010 }
3011
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003012 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3013 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3014 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3015
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003016 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003017 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003018 codegen_->AddSlowPath(slow_path);
3019 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003020 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003021 __ Cbnz(Register(value), &non_zero);
3022 if (!index.IsConstant()) {
3023 __ Add(temp, array, offset);
3024 }
Artem Serov914d7a82017-02-07 14:33:49 +00003025 {
3026 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3027 // emitted.
3028 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3029 __ Str(wzr, destination);
3030 codegen_->MaybeRecordImplicitNullCheck(instruction);
3031 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003032 __ B(&done);
3033 __ Bind(&non_zero);
3034 }
3035
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003036 // Note that when Baker read barriers are enabled, the type
3037 // checks are performed without read barriers. This is fine,
3038 // even in the case where a class object is in the from-space
3039 // after the flip, as a comparison involving such a type would
3040 // not produce a false positive; it may of course produce a
3041 // false negative, in which case we would take the ArraySet
3042 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003043
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003044 Register temp2 = temps.AcquireSameSizeAs(array);
3045 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003046 {
3047 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3048 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3049 __ Ldr(temp, HeapOperand(array, class_offset));
3050 codegen_->MaybeRecordImplicitNullCheck(instruction);
3051 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003052 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003053
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003054 // /* HeapReference<Class> */ temp = temp->component_type_
3055 __ Ldr(temp, HeapOperand(temp, component_offset));
3056 // /* HeapReference<Class> */ temp2 = value->klass_
3057 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3058 // If heap poisoning is enabled, no need to unpoison `temp`
3059 // nor `temp2`, as we are comparing two poisoned references.
3060 __ Cmp(temp, temp2);
3061 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003062
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003063 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3064 vixl::aarch64::Label do_put;
3065 __ B(eq, &do_put);
3066 // If heap poisoning is enabled, the `temp` reference has
3067 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003068 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3069
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003070 // /* HeapReference<Class> */ temp = temp->super_class_
3071 __ Ldr(temp, HeapOperand(temp, super_offset));
3072 // If heap poisoning is enabled, no need to unpoison
3073 // `temp`, as we are comparing against null below.
3074 __ Cbnz(temp, slow_path->GetEntryLabel());
3075 __ Bind(&do_put);
3076 } else {
3077 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003078 }
3079 }
3080
3081 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003082 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003083 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003084 __ Mov(temp2, value.W());
3085 GetAssembler()->PoisonHeapReference(temp2);
3086 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003087 }
3088
3089 if (!index.IsConstant()) {
3090 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003091 } else {
3092 // We no longer need the `temp` here so release it as the store below may
3093 // need a scratch register (if the constant index makes the offset too large)
3094 // and the poisoned `source` could be using the other scratch register.
3095 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003096 }
Artem Serov914d7a82017-02-07 14:33:49 +00003097 {
3098 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3099 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3100 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003101
Artem Serov914d7a82017-02-07 14:33:49 +00003102 if (!may_need_runtime_call_for_type_check) {
3103 codegen_->MaybeRecordImplicitNullCheck(instruction);
3104 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003105 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003106 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003107
3108 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3109
3110 if (done.IsLinked()) {
3111 __ Bind(&done);
3112 }
3113
3114 if (slow_path != nullptr) {
3115 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003116 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003117 }
3118}
3119
Alexandre Rames67555f72014-11-18 10:55:16 +00003120void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003121 RegisterSet caller_saves = RegisterSet::Empty();
3122 InvokeRuntimeCallingConvention calling_convention;
3123 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3124 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3125 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003126 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003127 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003128}
3129
3130void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003131 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003132 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003133 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003134 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3135 __ B(slow_path->GetEntryLabel(), hs);
3136}
3137
Alexandre Rames67555f72014-11-18 10:55:16 +00003138void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3139 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003140 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00003141 locations->SetInAt(0, Location::RequiresRegister());
3142 if (check->HasUses()) {
3143 locations->SetOut(Location::SameAsFirstInput());
3144 }
3145}
3146
3147void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3148 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003149 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Alexandre Rames67555f72014-11-18 10:55:16 +00003150 check->GetLoadClass(), check, check->GetDexPc(), true);
3151 codegen_->AddSlowPath(slow_path);
3152 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3153}
3154
Roland Levillain1a653882016-03-18 18:05:57 +00003155static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3156 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3157 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3158}
3159
3160void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3161 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3162 Location rhs_loc = instruction->GetLocations()->InAt(1);
3163 if (rhs_loc.IsConstant()) {
3164 // 0.0 is the only immediate that can be encoded directly in
3165 // an FCMP instruction.
3166 //
3167 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3168 // specify that in a floating-point comparison, positive zero
3169 // and negative zero are considered equal, so we can use the
3170 // literal 0.0 for both cases here.
3171 //
3172 // Note however that some methods (Float.equal, Float.compare,
3173 // Float.compareTo, Double.equal, Double.compare,
3174 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3175 // StrictMath.min) consider 0.0 to be (strictly) greater than
3176 // -0.0. So if we ever translate calls to these methods into a
3177 // HCompare instruction, we must handle the -0.0 case with
3178 // care here.
3179 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3180 __ Fcmp(lhs_reg, 0.0);
3181 } else {
3182 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3183 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003184}
3185
Serban Constantinescu02164b32014-11-13 14:05:07 +00003186void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003187 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003188 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003189 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003190 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003192 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003193 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003194 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003195 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003196 case DataType::Type::kInt32:
3197 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003198 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003199 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3201 break;
3202 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003203 case DataType::Type::kFloat32:
3204 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003205 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003206 locations->SetInAt(1,
3207 IsFloatingPointZeroConstant(compare->InputAt(1))
3208 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3209 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003210 locations->SetOut(Location::RequiresRegister());
3211 break;
3212 }
3213 default:
3214 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3215 }
3216}
3217
3218void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003219 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00003220
3221 // 0 if: left == right
3222 // 1 if: left > right
3223 // -1 if: left < right
3224 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003226 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003229 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 case DataType::Type::kInt32:
3231 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003232 Register result = OutputRegister(compare);
3233 Register left = InputRegisterAt(compare, 0);
3234 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003235 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003236 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3237 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003238 break;
3239 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 case DataType::Type::kFloat32:
3241 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003242 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003243 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003244 __ Cset(result, ne);
3245 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003246 break;
3247 }
3248 default:
3249 LOG(FATAL) << "Unimplemented compare type " << in_type;
3250 }
3251}
3252
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003253void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003254 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003255
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003256 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003257 locations->SetInAt(0, Location::RequiresFpuRegister());
3258 locations->SetInAt(1,
3259 IsFloatingPointZeroConstant(instruction->InputAt(1))
3260 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3261 : Location::RequiresFpuRegister());
3262 } else {
3263 // Integer cases.
3264 locations->SetInAt(0, Location::RequiresRegister());
3265 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3266 }
3267
David Brazdilb3e773e2016-01-26 11:28:37 +00003268 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003270 }
3271}
3272
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003273void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003274 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003275 return;
3276 }
3277
3278 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003279 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003280 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003283 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003284 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003285 } else {
3286 // Integer cases.
3287 Register lhs = InputRegisterAt(instruction, 0);
3288 Operand rhs = InputOperandAt(instruction, 1);
3289 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003290 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003291 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003292}
3293
3294#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3295 M(Equal) \
3296 M(NotEqual) \
3297 M(LessThan) \
3298 M(LessThanOrEqual) \
3299 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003300 M(GreaterThanOrEqual) \
3301 M(Below) \
3302 M(BelowOrEqual) \
3303 M(Above) \
3304 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003305#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003306void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3307void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003308FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003309#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003310#undef FOR_EACH_CONDITION_INSTRUCTION
3311
Zheng Xuc6667102015-05-15 16:08:45 +08003312void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3313 DCHECK(instruction->IsDiv() || instruction->IsRem());
3314
3315 LocationSummary* locations = instruction->GetLocations();
3316 Location second = locations->InAt(1);
3317 DCHECK(second.IsConstant());
3318
3319 Register out = OutputRegister(instruction);
3320 Register dividend = InputRegisterAt(instruction, 0);
3321 int64_t imm = Int64FromConstant(second.GetConstant());
3322 DCHECK(imm == 1 || imm == -1);
3323
3324 if (instruction->IsRem()) {
3325 __ Mov(out, 0);
3326 } else {
3327 if (imm == 1) {
3328 __ Mov(out, dividend);
3329 } else {
3330 __ Neg(out, dividend);
3331 }
3332 }
3333}
3334
3335void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3336 DCHECK(instruction->IsDiv() || instruction->IsRem());
3337
3338 LocationSummary* locations = instruction->GetLocations();
3339 Location second = locations->InAt(1);
3340 DCHECK(second.IsConstant());
3341
3342 Register out = OutputRegister(instruction);
3343 Register dividend = InputRegisterAt(instruction, 0);
3344 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003345 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003346 int ctz_imm = CTZ(abs_imm);
3347
3348 UseScratchRegisterScope temps(GetVIXLAssembler());
3349 Register temp = temps.AcquireSameSizeAs(out);
3350
3351 if (instruction->IsDiv()) {
3352 __ Add(temp, dividend, abs_imm - 1);
3353 __ Cmp(dividend, 0);
3354 __ Csel(out, temp, dividend, lt);
3355 if (imm > 0) {
3356 __ Asr(out, out, ctz_imm);
3357 } else {
3358 __ Neg(out, Operand(out, ASR, ctz_imm));
3359 }
3360 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003361 int bits = instruction->GetResultType() == DataType::Type::kInt32 ? 32 : 64;
Zheng Xuc6667102015-05-15 16:08:45 +08003362 __ Asr(temp, dividend, bits - 1);
3363 __ Lsr(temp, temp, bits - ctz_imm);
3364 __ Add(out, dividend, temp);
3365 __ And(out, out, abs_imm - 1);
3366 __ Sub(out, out, temp);
3367 }
3368}
3369
3370void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3371 DCHECK(instruction->IsDiv() || instruction->IsRem());
3372
3373 LocationSummary* locations = instruction->GetLocations();
3374 Location second = locations->InAt(1);
3375 DCHECK(second.IsConstant());
3376
3377 Register out = OutputRegister(instruction);
3378 Register dividend = InputRegisterAt(instruction, 0);
3379 int64_t imm = Int64FromConstant(second.GetConstant());
3380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 DataType::Type type = instruction->GetResultType();
3382 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003383
3384 int64_t magic;
3385 int shift;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 CalculateMagicAndShiftForDivRem(
3387 imm, type == DataType::Type::kInt64 /* is_long */, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003388
3389 UseScratchRegisterScope temps(GetVIXLAssembler());
3390 Register temp = temps.AcquireSameSizeAs(out);
3391
3392 // temp = get_high(dividend * magic)
3393 __ Mov(temp, magic);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003394 if (type == DataType::Type::kInt64) {
Zheng Xuc6667102015-05-15 16:08:45 +08003395 __ Smulh(temp, dividend, temp);
3396 } else {
3397 __ Smull(temp.X(), dividend, temp);
3398 __ Lsr(temp.X(), temp.X(), 32);
3399 }
3400
3401 if (imm > 0 && magic < 0) {
3402 __ Add(temp, temp, dividend);
3403 } else if (imm < 0 && magic > 0) {
3404 __ Sub(temp, temp, dividend);
3405 }
3406
3407 if (shift != 0) {
3408 __ Asr(temp, temp, shift);
3409 }
3410
3411 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003412 __ Sub(out, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003413 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003414 __ Sub(temp, temp, Operand(temp, ASR, type == DataType::Type::kInt64 ? 63 : 31));
Zheng Xuc6667102015-05-15 16:08:45 +08003415 // TODO: Strength reduction for msub.
3416 Register temp_imm = temps.AcquireSameSizeAs(out);
3417 __ Mov(temp_imm, imm);
3418 __ Msub(out, temp, temp_imm, dividend);
3419 }
3420}
3421
3422void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3423 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003424 DataType::Type type = instruction->GetResultType();
3425 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003426
3427 LocationSummary* locations = instruction->GetLocations();
3428 Register out = OutputRegister(instruction);
3429 Location second = locations->InAt(1);
3430
3431 if (second.IsConstant()) {
3432 int64_t imm = Int64FromConstant(second.GetConstant());
3433
3434 if (imm == 0) {
3435 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3436 } else if (imm == 1 || imm == -1) {
3437 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003438 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003439 DivRemByPowerOfTwo(instruction);
3440 } else {
3441 DCHECK(imm <= -2 || imm >= 2);
3442 GenerateDivRemWithAnyConstant(instruction);
3443 }
3444 } else {
3445 Register dividend = InputRegisterAt(instruction, 0);
3446 Register divisor = InputRegisterAt(instruction, 1);
3447 if (instruction->IsDiv()) {
3448 __ Sdiv(out, dividend, divisor);
3449 } else {
3450 UseScratchRegisterScope temps(GetVIXLAssembler());
3451 Register temp = temps.AcquireSameSizeAs(out);
3452 __ Sdiv(temp, dividend, divisor);
3453 __ Msub(out, temp, divisor, dividend);
3454 }
3455 }
3456}
3457
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003458void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3459 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003460 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003461 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 case DataType::Type::kInt32:
3463 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003464 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003465 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3467 break;
3468
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003469 case DataType::Type::kFloat32:
3470 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003471 locations->SetInAt(0, Location::RequiresFpuRegister());
3472 locations->SetInAt(1, Location::RequiresFpuRegister());
3473 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3474 break;
3475
3476 default:
3477 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3478 }
3479}
3480
3481void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003482 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003483 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003484 case DataType::Type::kInt32:
3485 case DataType::Type::kInt64:
Zheng Xuc6667102015-05-15 16:08:45 +08003486 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003487 break;
3488
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 case DataType::Type::kFloat32:
3490 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003491 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3492 break;
3493
3494 default:
3495 LOG(FATAL) << "Unexpected div type " << type;
3496 }
3497}
3498
Alexandre Rames67555f72014-11-18 10:55:16 +00003499void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003500 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003501 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003502}
3503
3504void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3505 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003506 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003507 codegen_->AddSlowPath(slow_path);
3508 Location value = instruction->GetLocations()->InAt(0);
3509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003511
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003513 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003514 return;
3515 }
3516
Alexandre Rames67555f72014-11-18 10:55:16 +00003517 if (value.IsConstant()) {
3518 int64_t divisor = Int64ConstantFrom(value);
3519 if (divisor == 0) {
3520 __ B(slow_path->GetEntryLabel());
3521 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003522 // A division by a non-null constant is valid. We don't need to perform
3523 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003524 }
3525 } else {
3526 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3527 }
3528}
3529
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003530void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3531 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003532 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003533 locations->SetOut(Location::ConstantLocation(constant));
3534}
3535
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003536void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3537 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003538 // Will be generated at use site.
3539}
3540
Alexandre Rames5319def2014-10-23 10:03:10 +01003541void LocationsBuilderARM64::VisitExit(HExit* exit) {
3542 exit->SetLocations(nullptr);
3543}
3544
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003545void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003546}
3547
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003548void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3549 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003550 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003551 locations->SetOut(Location::ConstantLocation(constant));
3552}
3553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003554void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003555 // Will be generated at use site.
3556}
3557
David Brazdilfc6a86a2015-06-26 10:33:45 +00003558void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003559 DCHECK(!successor->IsExitBlock());
3560 HBasicBlock* block = got->GetBlock();
3561 HInstruction* previous = got->GetPrevious();
3562 HLoopInformation* info = block->GetLoopInformation();
3563
David Brazdil46e2a392015-03-16 17:31:52 +00003564 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003565 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3566 return;
3567 }
3568 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3569 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01003570 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003571 }
3572 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003573 __ B(codegen_->GetLabelOf(successor));
3574 }
3575}
3576
David Brazdilfc6a86a2015-06-26 10:33:45 +00003577void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3578 got->SetLocations(nullptr);
3579}
3580
3581void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3582 HandleGoto(got, got->GetSuccessor());
3583}
3584
3585void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3586 try_boundary->SetLocations(nullptr);
3587}
3588
3589void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3590 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3591 if (!successor->IsExitBlock()) {
3592 HandleGoto(try_boundary, successor);
3593 }
3594}
3595
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003596void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003597 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003598 vixl::aarch64::Label* true_target,
3599 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003600 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003601
David Brazdil0debae72015-11-12 18:37:00 +00003602 if (true_target == nullptr && false_target == nullptr) {
3603 // Nothing to do. The code always falls through.
3604 return;
3605 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003606 // Constant condition, statically compared against "true" (integer value 1).
3607 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003608 if (true_target != nullptr) {
3609 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003610 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003611 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003612 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003613 if (false_target != nullptr) {
3614 __ B(false_target);
3615 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003616 }
David Brazdil0debae72015-11-12 18:37:00 +00003617 return;
3618 }
3619
3620 // The following code generates these patterns:
3621 // (1) true_target == nullptr && false_target != nullptr
3622 // - opposite condition true => branch to false_target
3623 // (2) true_target != nullptr && false_target == nullptr
3624 // - condition true => branch to true_target
3625 // (3) true_target != nullptr && false_target != nullptr
3626 // - condition true => branch to true_target
3627 // - branch to false_target
3628 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003629 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003630 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003631 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003632 if (true_target == nullptr) {
3633 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3634 } else {
3635 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3636 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003637 } else {
3638 // The condition instruction has not been materialized, use its inputs as
3639 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003640 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003642 DataType::Type type = condition->InputAt(0)->GetType();
3643 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003644 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003645 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003646 IfCondition opposite_condition = condition->GetOppositeCondition();
3647 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003648 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003649 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003650 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003651 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003652 // Integer cases.
3653 Register lhs = InputRegisterAt(condition, 0);
3654 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003655
3656 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003657 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003658 if (true_target == nullptr) {
3659 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3660 non_fallthrough_target = false_target;
3661 } else {
3662 arm64_cond = ARM64Condition(condition->GetCondition());
3663 non_fallthrough_target = true_target;
3664 }
3665
Aart Bik086d27e2016-01-20 17:02:00 -08003666 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003667 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003668 switch (arm64_cond) {
3669 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003670 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003671 break;
3672 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003673 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003674 break;
3675 case lt:
3676 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003677 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003678 break;
3679 case ge:
3680 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003681 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003682 break;
3683 default:
3684 // Without the `static_cast` the compiler throws an error for
3685 // `-Werror=sign-promo`.
3686 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3687 }
3688 } else {
3689 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003690 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003691 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003692 }
3693 }
David Brazdil0debae72015-11-12 18:37:00 +00003694
3695 // If neither branch falls through (case 3), the conditional branch to `true_target`
3696 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3697 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003698 __ B(false_target);
3699 }
3700}
3701
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003702void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003703 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003704 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003705 locations->SetInAt(0, Location::RequiresRegister());
3706 }
3707}
3708
3709void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003710 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3711 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003712 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3713 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3714 true_target = nullptr;
3715 }
3716 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3717 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3718 false_target = nullptr;
3719 }
David Brazdil0debae72015-11-12 18:37:00 +00003720 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003721}
3722
3723void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003724 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003725 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003726 InvokeRuntimeCallingConvention calling_convention;
3727 RegisterSet caller_saves = RegisterSet::Empty();
3728 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3729 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003730 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003731 locations->SetInAt(0, Location::RequiresRegister());
3732 }
3733}
3734
3735void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003736 SlowPathCodeARM64* slow_path =
3737 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003738 GenerateTestAndBranch(deoptimize,
3739 /* condition_input_index */ 0,
3740 slow_path->GetEntryLabel(),
3741 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003742}
3743
Mingyao Yang063fc772016-08-02 11:02:54 -07003744void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003745 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003746 LocationSummary(flag, LocationSummary::kNoCall);
3747 locations->SetOut(Location::RequiresRegister());
3748}
3749
3750void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3751 __ Ldr(OutputRegister(flag),
3752 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3753}
3754
David Brazdilc0b601b2016-02-08 14:20:45 +00003755static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3756 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003758}
3759
Alexandre Rames880f1192016-06-13 16:04:50 +01003760static inline Condition GetConditionForSelect(HCondition* condition) {
3761 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003762 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3763 : ARM64Condition(cond);
3764}
3765
David Brazdil74eb1b22015-12-14 11:44:01 +00003766void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003767 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003768 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003769 locations->SetInAt(0, Location::RequiresFpuRegister());
3770 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003771 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003772 } else {
3773 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3774 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3775 bool is_true_value_constant = cst_true_value != nullptr;
3776 bool is_false_value_constant = cst_false_value != nullptr;
3777 // Ask VIXL whether we should synthesize constants in registers.
3778 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3779 Operand true_op = is_true_value_constant ?
3780 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3781 Operand false_op = is_false_value_constant ?
3782 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3783 bool true_value_in_register = false;
3784 bool false_value_in_register = false;
3785 MacroAssembler::GetCselSynthesisInformation(
3786 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3787 true_value_in_register |= !is_true_value_constant;
3788 false_value_in_register |= !is_false_value_constant;
3789
3790 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3791 : Location::ConstantLocation(cst_true_value));
3792 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3793 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003795 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003796
David Brazdil74eb1b22015-12-14 11:44:01 +00003797 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3798 locations->SetInAt(2, Location::RequiresRegister());
3799 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003800}
3801
3802void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003803 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003804 Condition csel_cond;
3805
3806 if (IsBooleanValueOrMaterializedCondition(cond)) {
3807 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003808 // Use the condition flags set by the previous instruction.
3809 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003810 } else {
3811 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003812 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003813 }
3814 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003815 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003816 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003817 } else {
3818 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003819 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003820 }
3821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003822 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003823 __ Fcsel(OutputFPRegister(select),
3824 InputFPRegisterAt(select, 1),
3825 InputFPRegisterAt(select, 0),
3826 csel_cond);
3827 } else {
3828 __ Csel(OutputRegister(select),
3829 InputOperandAt(select, 1),
3830 InputOperandAt(select, 0),
3831 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003832 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003833}
3834
David Srbecky0cf44932015-12-09 14:09:59 +00003835void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003836 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003837}
3838
David Srbeckyd28f4a02016-03-14 17:14:24 +00003839void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3840 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003841}
3842
3843void CodeGeneratorARM64::GenerateNop() {
3844 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003845}
3846
Alexandre Rames5319def2014-10-23 10:03:10 +01003847void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003848 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003849}
3850
3851void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003852 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003853}
3854
3855void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003856 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003857}
3858
3859void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003860 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003861}
3862
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003863// Temp is used for read barrier.
3864static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3865 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003866 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003867 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3868 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3869 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3870 return 1;
3871 }
3872 return 0;
3873}
3874
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003875// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003876// interface pointer, one for loading the current interface.
3877// The other checks have one temp for loading the object's class.
3878static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3879 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3880 return 3;
3881 }
3882 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003883}
3884
Alexandre Rames67555f72014-11-18 10:55:16 +00003885void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003886 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003887 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003888 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003889 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003890 case TypeCheckKind::kExactCheck:
3891 case TypeCheckKind::kAbstractClassCheck:
3892 case TypeCheckKind::kClassHierarchyCheck:
3893 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894 call_kind =
3895 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003896 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003897 break;
3898 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003899 case TypeCheckKind::kUnresolvedCheck:
3900 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003901 call_kind = LocationSummary::kCallOnSlowPath;
3902 break;
3903 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003904
Vladimir Markoca6fff82017-10-03 14:49:14 +01003905 LocationSummary* locations =
3906 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003907 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003908 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003909 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003910 locations->SetInAt(0, Location::RequiresRegister());
3911 locations->SetInAt(1, Location::RequiresRegister());
3912 // The "out" register is used as a temporary, so it overlaps with the inputs.
3913 // Note that TypeCheckSlowPathARM64 uses this register too.
3914 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003915 // Add temps if necessary for read barriers.
3916 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003917}
3918
3919void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003920 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003921 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003922 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003923 Register obj = InputRegisterAt(instruction, 0);
3924 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003925 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003926 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003927 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3928 DCHECK_LE(num_temps, 1u);
3929 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003930 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3931 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3932 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3933 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003934
Scott Wakeling97c72b72016-06-24 16:19:36 +01003935 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003936 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003937
3938 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003939 // Avoid null check if we know `obj` is not null.
3940 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003941 __ Cbz(obj, &zero);
3942 }
3943
Roland Levillain44015862016-01-22 11:47:17 +00003944 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003945 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003946 // /* HeapReference<Class> */ out = obj->klass_
3947 GenerateReferenceLoadTwoRegisters(instruction,
3948 out_loc,
3949 obj_loc,
3950 class_offset,
3951 maybe_temp_loc,
3952 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003953 __ Cmp(out, cls);
3954 __ Cset(out, eq);
3955 if (zero.IsLinked()) {
3956 __ B(&done);
3957 }
3958 break;
3959 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003962 // /* HeapReference<Class> */ out = obj->klass_
3963 GenerateReferenceLoadTwoRegisters(instruction,
3964 out_loc,
3965 obj_loc,
3966 class_offset,
3967 maybe_temp_loc,
3968 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003969 // If the class is abstract, we eagerly fetch the super class of the
3970 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003971 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003972 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003973 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003974 GenerateReferenceLoadOneRegister(instruction,
3975 out_loc,
3976 super_offset,
3977 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003978 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 // If `out` is null, we use it for the result, and jump to `done`.
3980 __ Cbz(out, &done);
3981 __ Cmp(out, cls);
3982 __ B(ne, &loop);
3983 __ Mov(out, 1);
3984 if (zero.IsLinked()) {
3985 __ B(&done);
3986 }
3987 break;
3988 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003989
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003990 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003991 // /* HeapReference<Class> */ out = obj->klass_
3992 GenerateReferenceLoadTwoRegisters(instruction,
3993 out_loc,
3994 obj_loc,
3995 class_offset,
3996 maybe_temp_loc,
3997 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003998 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003999 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004000 __ Bind(&loop);
4001 __ Cmp(out, cls);
4002 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004003 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004004 GenerateReferenceLoadOneRegister(instruction,
4005 out_loc,
4006 super_offset,
4007 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004008 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004009 __ Cbnz(out, &loop);
4010 // If `out` is null, we use it for the result, and jump to `done`.
4011 __ B(&done);
4012 __ Bind(&success);
4013 __ Mov(out, 1);
4014 if (zero.IsLinked()) {
4015 __ B(&done);
4016 }
4017 break;
4018 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004019
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004020 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004021 // /* HeapReference<Class> */ out = obj->klass_
4022 GenerateReferenceLoadTwoRegisters(instruction,
4023 out_loc,
4024 obj_loc,
4025 class_offset,
4026 maybe_temp_loc,
4027 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004028 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004029 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004030 __ Cmp(out, cls);
4031 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004032 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004033 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004034 GenerateReferenceLoadOneRegister(instruction,
4035 out_loc,
4036 component_offset,
4037 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004038 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004039 // If `out` is null, we use it for the result, and jump to `done`.
4040 __ Cbz(out, &done);
4041 __ Ldrh(out, HeapOperand(out, primitive_offset));
4042 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4043 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004044 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004045 __ Mov(out, 1);
4046 __ B(&done);
4047 break;
4048 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004050 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004051 // No read barrier since the slow path will retry upon failure.
4052 // /* HeapReference<Class> */ out = obj->klass_
4053 GenerateReferenceLoadTwoRegisters(instruction,
4054 out_loc,
4055 obj_loc,
4056 class_offset,
4057 maybe_temp_loc,
4058 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004059 __ Cmp(out, cls);
4060 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004061 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4062 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 codegen_->AddSlowPath(slow_path);
4064 __ B(ne, slow_path->GetEntryLabel());
4065 __ Mov(out, 1);
4066 if (zero.IsLinked()) {
4067 __ B(&done);
4068 }
4069 break;
4070 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004071
Calin Juravle98893e12015-10-02 21:05:03 +01004072 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004073 case TypeCheckKind::kInterfaceCheck: {
4074 // Note that we indeed only call on slow path, but we always go
4075 // into the slow path for the unresolved and interface check
4076 // cases.
4077 //
4078 // We cannot directly call the InstanceofNonTrivial runtime
4079 // entry point without resorting to a type checking slow path
4080 // here (i.e. by calling InvokeRuntime directly), as it would
4081 // require to assign fixed registers for the inputs of this
4082 // HInstanceOf instruction (following the runtime calling
4083 // convention), which might be cluttered by the potential first
4084 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004085 //
4086 // TODO: Introduce a new runtime entry point taking the object
4087 // to test (instead of its class) as argument, and let it deal
4088 // with the read barrier issues. This will let us refactor this
4089 // case of the `switch` code as it was previously (with a direct
4090 // call to the runtime not using a type checking slow path).
4091 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01004093 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4094 instruction, /* is_fatal */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004095 codegen_->AddSlowPath(slow_path);
4096 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004097 if (zero.IsLinked()) {
4098 __ B(&done);
4099 }
4100 break;
4101 }
4102 }
4103
4104 if (zero.IsLinked()) {
4105 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004106 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107 }
4108
4109 if (done.IsLinked()) {
4110 __ Bind(&done);
4111 }
4112
4113 if (slow_path != nullptr) {
4114 __ Bind(slow_path->GetExitLabel());
4115 }
4116}
4117
4118void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4119 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4120 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4121
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004122 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4123 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004124 case TypeCheckKind::kExactCheck:
4125 case TypeCheckKind::kAbstractClassCheck:
4126 case TypeCheckKind::kClassHierarchyCheck:
4127 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004128 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4129 LocationSummary::kCallOnSlowPath :
4130 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004131 break;
4132 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004133 case TypeCheckKind::kUnresolvedCheck:
4134 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004135 call_kind = LocationSummary::kCallOnSlowPath;
4136 break;
4137 }
4138
Vladimir Markoca6fff82017-10-03 14:49:14 +01004139 LocationSummary* locations =
4140 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004141 locations->SetInAt(0, Location::RequiresRegister());
4142 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004143 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4144 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145}
4146
4147void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004148 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004149 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004150 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004151 Register obj = InputRegisterAt(instruction, 0);
4152 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004153 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4154 DCHECK_GE(num_temps, 1u);
4155 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004156 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004157 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4158 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004159 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004160 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4161 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4162 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4163 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4164 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4165 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4166 const uint32_t object_array_data_offset =
4167 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004169 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004170 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4171 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4172 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004173 if (!kEmitCompilerReadBarrier) {
4174 is_type_check_slow_path_fatal =
4175 (type_check_kind == TypeCheckKind::kExactCheck ||
4176 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4177 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4178 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4179 !instruction->CanThrowIntoCatchBlock();
4180 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004182 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
4183 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004184 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004185
Scott Wakeling97c72b72016-06-24 16:19:36 +01004186 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004187 // Avoid null check if we know obj is not null.
4188 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004189 __ Cbz(obj, &done);
4190 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004191
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004192 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004193 case TypeCheckKind::kExactCheck:
4194 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004195 // /* HeapReference<Class> */ temp = obj->klass_
4196 GenerateReferenceLoadTwoRegisters(instruction,
4197 temp_loc,
4198 obj_loc,
4199 class_offset,
4200 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004201 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004202
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004203 __ Cmp(temp, cls);
4204 // Jump to slow path for throwing the exception or doing a
4205 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004206 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 break;
4208 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004209
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004210 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004211 // /* HeapReference<Class> */ temp = obj->klass_
4212 GenerateReferenceLoadTwoRegisters(instruction,
4213 temp_loc,
4214 obj_loc,
4215 class_offset,
4216 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004217 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004218
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004219 // If the class is abstract, we eagerly fetch the super class of the
4220 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004221 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004224 GenerateReferenceLoadOneRegister(instruction,
4225 temp_loc,
4226 super_offset,
4227 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004228 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004229
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004230 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4231 // exception.
4232 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4233 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004234 __ Cmp(temp, cls);
4235 __ B(ne, &loop);
4236 break;
4237 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004240 // /* HeapReference<Class> */ temp = obj->klass_
4241 GenerateReferenceLoadTwoRegisters(instruction,
4242 temp_loc,
4243 obj_loc,
4244 class_offset,
4245 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004246 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004247
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004248 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004249 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004250 __ Bind(&loop);
4251 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004252 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004253
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004254 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004255 GenerateReferenceLoadOneRegister(instruction,
4256 temp_loc,
4257 super_offset,
4258 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004259 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260
4261 // If the class reference currently in `temp` is not null, jump
4262 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004263 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004264 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004265 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004266 break;
4267 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004268
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004269 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004270 // /* HeapReference<Class> */ temp = obj->klass_
4271 GenerateReferenceLoadTwoRegisters(instruction,
4272 temp_loc,
4273 obj_loc,
4274 class_offset,
4275 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004276 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004277
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004278 // Do an exact check.
4279 __ Cmp(temp, cls);
4280 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004281
4282 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004283 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004284 GenerateReferenceLoadOneRegister(instruction,
4285 temp_loc,
4286 component_offset,
4287 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004288 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004289
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004290 // If the component type is null, jump to the slow path to throw the exception.
4291 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4292 // Otherwise, the object is indeed an array. Further check that this component type is not a
4293 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004294 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4295 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004296 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004297 break;
4298 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004299
Calin Juravle98893e12015-10-02 21:05:03 +01004300 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004301 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004302 //
4303 // We cannot directly call the CheckCast runtime entry point
4304 // without resorting to a type checking slow path here (i.e. by
4305 // calling InvokeRuntime directly), as it would require to
4306 // assign fixed registers for the inputs of this HInstanceOf
4307 // instruction (following the runtime calling convention), which
4308 // might be cluttered by the potential first read barrier
4309 // emission at the beginning of this method.
4310 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004311 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004312 case TypeCheckKind::kInterfaceCheck: {
4313 // /* HeapReference<Class> */ temp = obj->klass_
4314 GenerateReferenceLoadTwoRegisters(instruction,
4315 temp_loc,
4316 obj_loc,
4317 class_offset,
4318 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004319 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004320
4321 // /* HeapReference<Class> */ temp = temp->iftable_
4322 GenerateReferenceLoadTwoRegisters(instruction,
4323 temp_loc,
4324 temp_loc,
4325 iftable_offset,
4326 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004327 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004328 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004329 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004330 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004331 vixl::aarch64::Label start_loop;
4332 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004333 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004334 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4335 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004336 // Go to next interface.
4337 __ Add(temp, temp, 2 * kHeapReferenceSize);
4338 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004339 // Compare the classes and continue the loop if they do not match.
4340 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4341 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004342 break;
4343 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004344 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004345 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004346
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004347 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004348}
4349
Alexandre Rames5319def2014-10-23 10:03:10 +01004350void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004351 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004352 locations->SetOut(Location::ConstantLocation(constant));
4353}
4354
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004355void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004356 // Will be generated at use site.
4357}
4358
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004359void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004360 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004361 locations->SetOut(Location::ConstantLocation(constant));
4362}
4363
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004364void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004365 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004366}
4367
Calin Juravle175dc732015-08-25 15:42:32 +01004368void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4369 // The trampoline uses the same calling convention as dex calling conventions,
4370 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4371 // the method_idx.
4372 HandleInvoke(invoke);
4373}
4374
4375void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4376 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004377 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004378}
4379
Alexandre Rames5319def2014-10-23 10:03:10 +01004380void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004381 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004382 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004383}
4384
Alexandre Rames67555f72014-11-18 10:55:16 +00004385void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4386 HandleInvoke(invoke);
4387}
4388
4389void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4390 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004391 LocationSummary* locations = invoke->GetLocations();
4392 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004393 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004394 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004395 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004396
4397 // The register ip1 is required to be used for the hidden argument in
4398 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004399 MacroAssembler* masm = GetVIXLAssembler();
4400 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004401 scratch_scope.Exclude(ip1);
4402 __ Mov(ip1, invoke->GetDexMethodIndex());
4403
Artem Serov914d7a82017-02-07 14:33:49 +00004404 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004405 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004406 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004407 {
4408 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4409 // /* HeapReference<Class> */ temp = temp->klass_
4410 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4411 codegen_->MaybeRecordImplicitNullCheck(invoke);
4412 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004413 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004414 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004415 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004416 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004417 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004418 }
Artem Serov914d7a82017-02-07 14:33:49 +00004419
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004420 // Instead of simply (possibly) unpoisoning `temp` here, we should
4421 // emit a read barrier for the previous class reference load.
4422 // However this is not required in practice, as this is an
4423 // intermediate/temporary reference and because the current
4424 // concurrent copying collector keeps the from-space memory
4425 // intact/accessible until the end of the marking phase (the
4426 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004427 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004428 __ Ldr(temp,
4429 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4430 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004431 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004432 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004433 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004434 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004435 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004436
4437 {
4438 // Ensure the pc position is recorded immediately after the `blr` instruction.
4439 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4440
4441 // lr();
4442 __ blr(lr);
4443 DCHECK(!codegen_->IsLeafMethod());
4444 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4445 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004446
4447 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004448}
4449
4450void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004451 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004452 if (intrinsic.TryDispatch(invoke)) {
4453 return;
4454 }
4455
Alexandre Rames67555f72014-11-18 10:55:16 +00004456 HandleInvoke(invoke);
4457}
4458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004459void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004460 // Explicit clinit checks triggered by static invokes must have been pruned by
4461 // art::PrepareForRegisterAllocation.
4462 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004463
Vladimir Markoca6fff82017-10-03 14:49:14 +01004464 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004465 if (intrinsic.TryDispatch(invoke)) {
4466 return;
4467 }
4468
Alexandre Rames67555f72014-11-18 10:55:16 +00004469 HandleInvoke(invoke);
4470}
4471
Andreas Gampe878d58c2015-01-15 23:24:00 -08004472static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4473 if (invoke->GetLocations()->Intrinsified()) {
4474 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4475 intrinsic.Dispatch(invoke);
4476 return true;
4477 }
4478 return false;
4479}
4480
Vladimir Markodc151b22015-10-15 18:02:30 +01004481HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4482 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004483 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004484 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004485 return desired_dispatch_info;
4486}
4487
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004488void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4489 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004490 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004491 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4492 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004493 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4494 uint32_t offset =
4495 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004496 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004497 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004498 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004499 }
Vladimir Marko58155012015-08-19 12:49:41 +00004500 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004501 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004502 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004503 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4504 DCHECK(GetCompilerOptions().IsBootImage());
4505 // Add ADRP with its PC-relative method patch.
4506 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4507 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4508 // Add ADD with its PC-relative method patch.
4509 vixl::aarch64::Label* add_label =
4510 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4511 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4512 break;
4513 }
Vladimir Marko58155012015-08-19 12:49:41 +00004514 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4515 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004516 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004517 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004518 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko58155012015-08-19 12:49:41 +00004519 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004520 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4521 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004522 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004523 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004524 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004525 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004526 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004527 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004528 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004529 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4530 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4531 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004532 }
4533 }
4534
4535 switch (invoke->GetCodePtrLocation()) {
4536 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004537 {
4538 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4539 ExactAssemblyScope eas(GetVIXLAssembler(),
4540 kInstructionSize,
4541 CodeBufferCheckScope::kExactSize);
4542 __ bl(&frame_entry_label_);
4543 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4544 }
Vladimir Marko58155012015-08-19 12:49:41 +00004545 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004546 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4547 // LR = callee_method->entry_point_from_quick_compiled_code_;
4548 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004549 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004550 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004551 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004552 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004553 ExactAssemblyScope eas(GetVIXLAssembler(),
4554 kInstructionSize,
4555 CodeBufferCheckScope::kExactSize);
4556 // lr()
4557 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004558 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004559 }
Vladimir Marko58155012015-08-19 12:49:41 +00004560 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004561 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004562
Andreas Gampe878d58c2015-01-15 23:24:00 -08004563 DCHECK(!IsLeafMethod());
4564}
4565
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004566void CodeGeneratorARM64::GenerateVirtualCall(
4567 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004568 // Use the calling convention instead of the location of the receiver, as
4569 // intrinsics may have put the receiver in a different register. In the intrinsics
4570 // slow path, the arguments have been moved to the right place, so here we are
4571 // guaranteed that the receiver is the first register of the calling convention.
4572 InvokeDexCallingConvention calling_convention;
4573 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004574 Register temp = XRegisterFrom(temp_in);
4575 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4576 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4577 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004578 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004579
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004580 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004581
4582 {
4583 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4584 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4585 // /* HeapReference<Class> */ temp = receiver->klass_
4586 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4587 MaybeRecordImplicitNullCheck(invoke);
4588 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004589 // Instead of simply (possibly) unpoisoning `temp` here, we should
4590 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004591 // intermediate/temporary reference and because the current
4592 // concurrent copying collector keeps the from-space memory
4593 // intact/accessible until the end of the marking phase (the
4594 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004595 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4596 // temp = temp->GetMethodAt(method_offset);
4597 __ Ldr(temp, MemOperand(temp, method_offset));
4598 // lr = temp->GetEntryPoint();
4599 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004600 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004601 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004602 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, 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 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004607}
4608
Orion Hodsonac141392017-01-13 11:53:47 +00004609void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4610 HandleInvoke(invoke);
4611}
4612
4613void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4614 codegen_->GenerateInvokePolymorphicCall(invoke);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004615 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004616}
4617
Vladimir Marko65979462017-05-19 17:25:12 +01004618vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4619 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004620 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004621 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004622 target_method.index,
Vladimir Marko65979462017-05-19 17:25:12 +01004623 adrp_label,
4624 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004625}
4626
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004627vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4628 MethodReference target_method,
4629 vixl::aarch64::Label* adrp_label) {
4630 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004631 target_method.index,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004632 adrp_label,
4633 &method_bss_entry_patches_);
4634}
4635
Scott Wakeling97c72b72016-06-24 16:19:36 +01004636vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4637 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004638 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004639 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004640 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004641}
4642
Vladimir Marko1998cd02017-01-13 13:02:58 +00004643vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4644 const DexFile& dex_file,
4645 dex::TypeIndex type_index,
4646 vixl::aarch64::Label* adrp_label) {
4647 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4648}
4649
Vladimir Marko65979462017-05-19 17:25:12 +01004650vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4651 const DexFile& dex_file,
4652 dex::StringIndex string_index,
4653 vixl::aarch64::Label* adrp_label) {
4654 return
4655 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4656}
4657
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004658vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4659 const DexFile& dex_file,
4660 dex::StringIndex string_index,
4661 vixl::aarch64::Label* adrp_label) {
4662 return NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
4663}
4664
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004665vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4666 baker_read_barrier_patches_.emplace_back(custom_data);
4667 return &baker_read_barrier_patches_.back().label;
4668}
4669
Scott Wakeling97c72b72016-06-24 16:19:36 +01004670vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4671 const DexFile& dex_file,
4672 uint32_t offset_or_index,
4673 vixl::aarch64::Label* adrp_label,
4674 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004675 // Add a patch entry and return the label.
4676 patches->emplace_back(dex_file, offset_or_index);
4677 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004678 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004679 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4680 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4681 return label;
4682}
4683
Scott Wakeling97c72b72016-06-24 16:19:36 +01004684vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4685 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004686 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004687}
4688
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004689vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004690 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004691 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004692 return jit_string_patches_.GetOrCreate(
4693 StringReference(&dex_file, string_index),
4694 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4695}
4696
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004697vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004698 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004699 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004700 return jit_class_patches_.GetOrCreate(
4701 TypeReference(&dex_file, type_index),
4702 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4703}
4704
Vladimir Markoaad75c62016-10-03 08:46:48 +00004705void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4706 vixl::aarch64::Register reg) {
4707 DCHECK(reg.IsX());
4708 SingleEmissionCheckScope guard(GetVIXLAssembler());
4709 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004710 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004711}
4712
4713void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4714 vixl::aarch64::Register out,
4715 vixl::aarch64::Register base) {
4716 DCHECK(out.IsX());
4717 DCHECK(base.IsX());
4718 SingleEmissionCheckScope guard(GetVIXLAssembler());
4719 __ Bind(fixup_label);
4720 __ add(out, base, Operand(/* offset placeholder */ 0));
4721}
4722
4723void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4724 vixl::aarch64::Register out,
4725 vixl::aarch64::Register base) {
4726 DCHECK(base.IsX());
4727 SingleEmissionCheckScope guard(GetVIXLAssembler());
4728 __ Bind(fixup_label);
4729 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4730}
4731
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004732template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004733inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4734 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004735 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004736 for (const PcRelativePatchInfo& info : infos) {
4737 linker_patches->push_back(Factory(info.label.GetLocation(),
4738 &info.target_dex_file,
4739 info.pc_insn_label->GetLocation(),
4740 info.offset_or_index));
4741 }
4742}
4743
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004744void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004745 DCHECK(linker_patches->empty());
4746 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004747 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004748 method_bss_entry_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004749 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004750 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004751 pc_relative_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004752 string_bss_entry_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004753 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004754 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01004755 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004756 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4757 pc_relative_method_patches_, linker_patches);
4758 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4759 pc_relative_type_patches_, linker_patches);
4760 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4761 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004762 } else {
4763 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004764 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4765 pc_relative_type_patches_, linker_patches);
4766 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4767 pc_relative_string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004768 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004769 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4770 method_bss_entry_patches_, linker_patches);
4771 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4772 type_bss_entry_patches_, linker_patches);
4773 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4774 string_bss_entry_patches_, linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004775 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004776 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4777 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004778 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004779 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004780}
4781
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004782vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4783 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004784 value,
4785 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4786}
4787
Scott Wakeling97c72b72016-06-24 16:19:36 +01004788vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004789 return uint64_literals_.GetOrCreate(
4790 value,
4791 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004792}
4793
Andreas Gampe878d58c2015-01-15 23:24:00 -08004794void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004795 // Explicit clinit checks triggered by static invokes must have been pruned by
4796 // art::PrepareForRegisterAllocation.
4797 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004798
Andreas Gampe878d58c2015-01-15 23:24:00 -08004799 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004800 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004801 return;
4802 }
4803
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004804 {
4805 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4806 // are no pools emitted.
4807 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4808 LocationSummary* locations = invoke->GetLocations();
4809 codegen_->GenerateStaticOrDirectCall(
4810 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4811 }
4812
4813 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004814}
4815
4816void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004817 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004818 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004819 return;
4820 }
4821
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004822 {
4823 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4824 // are no pools emitted.
4825 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4826 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4827 DCHECK(!codegen_->IsLeafMethod());
4828 }
4829
4830 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004831}
4832
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004833HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4834 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004835 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004836 case HLoadClass::LoadKind::kInvalid:
4837 LOG(FATAL) << "UNREACHABLE";
4838 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004839 case HLoadClass::LoadKind::kReferrersClass:
4840 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004841 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004842 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004843 case HLoadClass::LoadKind::kBssEntry:
4844 DCHECK(!Runtime::Current()->UseJitCompilation());
4845 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004846 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004847 DCHECK(Runtime::Current()->UseJitCompilation());
4848 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004849 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004850 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004851 break;
4852 }
4853 return desired_class_load_kind;
4854}
4855
Alexandre Rames67555f72014-11-18 10:55:16 +00004856void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004857 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004858 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004859 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004860 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004861 cls,
4862 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004863 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004864 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004865 return;
4866 }
Vladimir Marko41559982017-01-06 14:04:23 +00004867 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004868
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004869 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4870 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004871 ? LocationSummary::kCallOnSlowPath
4872 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004873 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004874 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004875 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004876 }
4877
Vladimir Marko41559982017-01-06 14:04:23 +00004878 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004879 locations->SetInAt(0, Location::RequiresRegister());
4880 }
4881 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004882 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4883 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4884 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004885 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004886 RegisterSet caller_saves = RegisterSet::Empty();
4887 InvokeRuntimeCallingConvention calling_convention;
4888 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4889 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004890 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
4891 DataType::Type::kReference).GetCode());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004892 locations->SetCustomSlowPathCallerSaves(caller_saves);
4893 } else {
4894 // For non-Baker read barrier we have a temp-clobbering call.
4895 }
4896 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004897}
4898
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004899// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4900// move.
4901void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004902 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004903 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004904 codegen_->GenerateLoadClassRuntimeCall(cls);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004905 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004906 return;
4907 }
Vladimir Marko41559982017-01-06 14:04:23 +00004908 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004909
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004910 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004911 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004912 Register bss_entry_temp;
4913 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004914
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004915 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4916 ? kWithoutReadBarrier
4917 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004918 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004919 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004920 case HLoadClass::LoadKind::kReferrersClass: {
4921 DCHECK(!cls->CanCallRuntime());
4922 DCHECK(!cls->MustGenerateClinitCheck());
4923 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4924 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004925 GenerateGcRootFieldLoad(cls,
4926 out_loc,
4927 current_method,
4928 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004929 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004930 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004931 break;
4932 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004933 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004934 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004935 // Add ADRP with its PC-relative type patch.
4936 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004937 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004938 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004939 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004940 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004941 vixl::aarch64::Label* add_label =
4942 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004943 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004944 break;
4945 }
4946 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004947 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004948 uint32_t address = dchecked_integral_cast<uint32_t>(
4949 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4950 DCHECK_NE(address, 0u);
4951 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004952 break;
4953 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004954 case HLoadClass::LoadKind::kBootImageClassTable: {
4955 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4956 // Add ADRP with its PC-relative type patch.
4957 const DexFile& dex_file = cls->GetDexFile();
4958 dex::TypeIndex type_index = cls->GetTypeIndex();
4959 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
4960 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4961 // Add LDR with its PC-relative type patch.
4962 vixl::aarch64::Label* ldr_label =
4963 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
4964 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
4965 // Extract the reference from the slot data, i.e. clear the hash bits.
4966 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
4967 ComputeModifiedUtf8Hash(dex_file.StringByTypeIdx(type_index)));
4968 if (masked_hash != 0) {
4969 __ Sub(out.W(), out.W(), Operand(masked_hash));
4970 }
4971 break;
4972 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004973 case HLoadClass::LoadKind::kBssEntry: {
4974 // Add ADRP with its PC-relative Class .bss entry patch.
4975 const DexFile& dex_file = cls->GetDexFile();
4976 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004977 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004978 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4979 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004980 // Add LDR with its PC-relative Class patch.
4981 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004982 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004983 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4984 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004985 out_loc,
4986 bss_entry_temp,
4987 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004988 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004989 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004990 generate_null_check = true;
4991 break;
4992 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004993 case HLoadClass::LoadKind::kJitTableAddress: {
4994 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4995 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004996 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004997 GenerateGcRootFieldLoad(cls,
4998 out_loc,
4999 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005000 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01005001 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005002 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005003 break;
5004 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005005 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005006 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005007 LOG(FATAL) << "UNREACHABLE";
5008 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005009 }
5010
Vladimir Markoea4c1262017-02-06 19:59:33 +00005011 bool do_clinit = cls->MustGenerateClinitCheck();
5012 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005013 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005014 SlowPathCodeARM64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005015 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005016 codegen_->AddSlowPath(slow_path);
5017 if (generate_null_check) {
5018 __ Cbz(out, slow_path->GetEntryLabel());
5019 }
5020 if (cls->MustGenerateClinitCheck()) {
5021 GenerateClassInitializationCheck(slow_path, out);
5022 } else {
5023 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005024 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005025 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005026 }
5027}
5028
David Brazdilcb1c0552015-08-04 16:22:25 +01005029static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005030 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005031}
5032
Alexandre Rames67555f72014-11-18 10:55:16 +00005033void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5034 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005035 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005036 locations->SetOut(Location::RequiresRegister());
5037}
5038
5039void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005040 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5041}
5042
5043void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005044 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005045}
5046
5047void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5048 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005049}
5050
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005051HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5052 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005053 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005054 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005055 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005056 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005057 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005058 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005059 case HLoadString::LoadKind::kJitTableAddress:
5060 DCHECK(Runtime::Current()->UseJitCompilation());
5061 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005062 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005063 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005064 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005065 }
5066 return desired_string_load_kind;
5067}
5068
Alexandre Rames67555f72014-11-18 10:55:16 +00005069void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005070 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005071 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005072 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005073 InvokeRuntimeCallingConvention calling_convention;
5074 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5075 } else {
5076 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005077 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5078 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005079 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005080 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005081 RegisterSet caller_saves = RegisterSet::Empty();
5082 InvokeRuntimeCallingConvention calling_convention;
5083 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5084 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005085 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
5086 DataType::Type::kReference).GetCode());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005087 locations->SetCustomSlowPathCallerSaves(caller_saves);
5088 } else {
5089 // For non-Baker read barrier we have a temp-clobbering call.
5090 }
5091 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005093}
5094
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005095// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5096// move.
5097void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005098 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005099 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005100
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005101 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005102 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005103 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005104 // Add ADRP with its PC-relative String patch.
5105 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005106 const dex::StringIndex string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01005107 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005108 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005109 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005110 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005111 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005112 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005113 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005114 }
5115 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005116 uint32_t address = dchecked_integral_cast<uint32_t>(
5117 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5118 DCHECK_NE(address, 0u);
5119 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005120 return;
5121 }
5122 case HLoadString::LoadKind::kBootImageInternTable: {
5123 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5124 // Add ADRP with its PC-relative String patch.
5125 const DexFile& dex_file = load->GetDexFile();
5126 const dex::StringIndex string_index = load->GetStringIndex();
5127 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
5128 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
5129 // Add LDR with its PC-relative String patch.
5130 vixl::aarch64::Label* ldr_label =
5131 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
5132 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5133 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005134 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005135 case HLoadString::LoadKind::kBssEntry: {
5136 // Add ADRP with its PC-relative String .bss entry patch.
5137 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005138 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005139 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005140 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005141 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005142 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005143 // Add LDR with its .bss entry String patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005144 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005145 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005146 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005147 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005148 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005149 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005150 /* offset placeholder */ 0u,
5151 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005152 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005153 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005154 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005155 codegen_->AddSlowPath(slow_path);
5156 __ Cbz(out.X(), slow_path->GetEntryLabel());
5157 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005158 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005159 return;
5160 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005161 case HLoadString::LoadKind::kJitTableAddress: {
5162 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005163 load->GetStringIndex(),
5164 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005165 GenerateGcRootFieldLoad(load,
5166 out_loc,
5167 out.X(),
5168 /* offset */ 0,
5169 /* fixup_label */ nullptr,
5170 kCompilerReadBarrierOption);
5171 return;
5172 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005173 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005174 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005175 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005176
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005177 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005178 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005179 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005180 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005181 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5182 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005183 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005184}
5185
Alexandre Rames5319def2014-10-23 10:03:10 +01005186void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005187 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005188 locations->SetOut(Location::ConstantLocation(constant));
5189}
5190
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005191void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005192 // Will be generated at use site.
5193}
5194
Alexandre Rames67555f72014-11-18 10:55:16 +00005195void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005196 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5197 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005198 InvokeRuntimeCallingConvention calling_convention;
5199 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5200}
5201
5202void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005203 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005204 instruction,
5205 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005206 if (instruction->IsEnter()) {
5207 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5208 } else {
5209 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5210 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005211 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005212}
5213
Alexandre Rames42d641b2014-10-27 14:00:51 +00005214void LocationsBuilderARM64::VisitMul(HMul* mul) {
5215 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005216 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005217 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005218 case DataType::Type::kInt32:
5219 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005220 locations->SetInAt(0, Location::RequiresRegister());
5221 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005222 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005223 break;
5224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005225 case DataType::Type::kFloat32:
5226 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005227 locations->SetInAt(0, Location::RequiresFpuRegister());
5228 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005229 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005230 break;
5231
5232 default:
5233 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5234 }
5235}
5236
5237void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5238 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005239 case DataType::Type::kInt32:
5240 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005241 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5242 break;
5243
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005244 case DataType::Type::kFloat32:
5245 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005246 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005247 break;
5248
5249 default:
5250 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5251 }
5252}
5253
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005254void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5255 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005256 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005257 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005258 case DataType::Type::kInt32:
5259 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005260 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005262 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005264 case DataType::Type::kFloat32:
5265 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005266 locations->SetInAt(0, Location::RequiresFpuRegister());
5267 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005268 break;
5269
5270 default:
5271 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5272 }
5273}
5274
5275void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5276 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 case DataType::Type::kInt32:
5278 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005279 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5280 break;
5281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 case DataType::Type::kFloat32:
5283 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005284 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005285 break;
5286
5287 default:
5288 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5289 }
5290}
5291
5292void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005293 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5294 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005295 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005296 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005297 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5298 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005299}
5300
5301void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005302 // Note: if heap poisoning is enabled, the entry point takes cares
5303 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005304 QuickEntrypointEnum entrypoint =
5305 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5306 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005307 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005308 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005309}
5310
Alexandre Rames5319def2014-10-23 10:03:10 +01005311void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005312 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5313 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005314 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005315 if (instruction->IsStringAlloc()) {
5316 locations->AddTemp(LocationFrom(kArtMethodRegister));
5317 } else {
5318 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005319 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005320 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005321}
5322
5323void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005324 // Note: if heap poisoning is enabled, the entry point takes cares
5325 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005326 if (instruction->IsStringAlloc()) {
5327 // String is allocated through StringFactory. Call NewEmptyString entry point.
5328 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005329 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005330 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5331 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005332
5333 {
5334 // Ensure the pc position is recorded immediately after the `blr` instruction.
5335 ExactAssemblyScope eas(GetVIXLAssembler(),
5336 kInstructionSize,
5337 CodeBufferCheckScope::kExactSize);
5338 __ blr(lr);
5339 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5340 }
David Brazdil6de19382016-01-08 17:37:10 +00005341 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005342 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005343 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005344 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005345 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005346}
5347
5348void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005349 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005350 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005352}
5353
5354void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005355 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 case DataType::Type::kInt32:
5357 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005358 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005359 break;
5360
5361 default:
5362 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5363 }
5364}
5365
David Brazdil66d126e2015-04-03 16:02:44 +01005366void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005367 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005368 locations->SetInAt(0, Location::RequiresRegister());
5369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5370}
5371
5372void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005373 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005374}
5375
Alexandre Rames5319def2014-10-23 10:03:10 +01005376void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005377 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5378 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005379}
5380
Calin Juravle2ae48182016-03-16 14:05:09 +00005381void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5382 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005383 return;
5384 }
Artem Serov914d7a82017-02-07 14:33:49 +00005385 {
5386 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5387 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5388 Location obj = instruction->GetLocations()->InAt(0);
5389 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5390 RecordPcInfo(instruction, instruction->GetDexPc());
5391 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005392}
5393
Calin Juravle2ae48182016-03-16 14:05:09 +00005394void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005395 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005396 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005397
5398 LocationSummary* locations = instruction->GetLocations();
5399 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005400
5401 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005402}
5403
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005404void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005405 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005406}
5407
Alexandre Rames67555f72014-11-18 10:55:16 +00005408void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5409 HandleBinaryOp(instruction);
5410}
5411
5412void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5413 HandleBinaryOp(instruction);
5414}
5415
Alexandre Rames3e69f162014-12-10 10:36:50 +00005416void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5417 LOG(FATAL) << "Unreachable";
5418}
5419
5420void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005421 if (instruction->GetNext()->IsSuspendCheck() &&
5422 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5423 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5424 // The back edge will generate the suspend check.
5425 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5426 }
5427
Alexandre Rames3e69f162014-12-10 10:36:50 +00005428 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5429}
5430
Alexandre Rames5319def2014-10-23 10:03:10 +01005431void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005432 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005433 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5434 if (location.IsStackSlot()) {
5435 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5436 } else if (location.IsDoubleStackSlot()) {
5437 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5438 }
5439 locations->SetOut(location);
5440}
5441
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005442void InstructionCodeGeneratorARM64::VisitParameterValue(
5443 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005444 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005445}
5446
5447void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5448 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005449 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005450 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005451}
5452
5453void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5454 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5455 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005456}
5457
5458void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005459 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005460 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005461 locations->SetInAt(i, Location::Any());
5462 }
5463 locations->SetOut(Location::Any());
5464}
5465
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005466void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005467 LOG(FATAL) << "Unreachable";
5468}
5469
Serban Constantinescu02164b32014-11-13 14:05:07 +00005470void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005471 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005472 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005473 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005474 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005475 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005476
5477 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005478 case DataType::Type::kInt32:
5479 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005480 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005481 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5483 break;
5484
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005485 case DataType::Type::kFloat32:
5486 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005487 InvokeRuntimeCallingConvention calling_convention;
5488 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5489 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5490 locations->SetOut(calling_convention.GetReturnLocation(type));
5491
5492 break;
5493 }
5494
Serban Constantinescu02164b32014-11-13 14:05:07 +00005495 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005496 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005497 }
5498}
5499
5500void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005502
Serban Constantinescu02164b32014-11-13 14:05:07 +00005503 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 case DataType::Type::kInt32:
5505 case DataType::Type::kInt64: {
Zheng Xuc6667102015-05-15 16:08:45 +08005506 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005507 break;
5508 }
5509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005510 case DataType::Type::kFloat32:
5511 case DataType::Type::kFloat64: {
5512 QuickEntrypointEnum entrypoint =
5513 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005514 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005516 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5517 } else {
5518 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5519 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005520 break;
5521 }
5522
Serban Constantinescu02164b32014-11-13 14:05:07 +00005523 default:
5524 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005525 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005526 }
5527}
5528
Igor Murashkind01745e2017-04-05 16:40:31 -07005529void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5530 constructor_fence->SetLocations(nullptr);
5531}
5532
5533void InstructionCodeGeneratorARM64::VisitConstructorFence(
5534 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5535 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5536}
5537
Calin Juravle27df7582015-04-17 19:12:31 +01005538void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5539 memory_barrier->SetLocations(nullptr);
5540}
5541
5542void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005543 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005544}
5545
Alexandre Rames5319def2014-10-23 10:03:10 +01005546void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005547 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005549 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005550}
5551
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005552void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005553 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005554}
5555
5556void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5557 instruction->SetLocations(nullptr);
5558}
5559
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005560void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005561 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005562}
5563
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005564void LocationsBuilderARM64::VisitRor(HRor* ror) {
5565 HandleBinaryOp(ror);
5566}
5567
5568void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5569 HandleBinaryOp(ror);
5570}
5571
Serban Constantinescu02164b32014-11-13 14:05:07 +00005572void LocationsBuilderARM64::VisitShl(HShl* shl) {
5573 HandleShift(shl);
5574}
5575
5576void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5577 HandleShift(shl);
5578}
5579
5580void LocationsBuilderARM64::VisitShr(HShr* shr) {
5581 HandleShift(shr);
5582}
5583
5584void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5585 HandleShift(shr);
5586}
5587
Alexandre Rames5319def2014-10-23 10:03:10 +01005588void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005589 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005590}
5591
5592void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005593 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005594}
5595
Alexandre Rames67555f72014-11-18 10:55:16 +00005596void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005597 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005598}
5599
5600void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005601 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005602}
5603
5604void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005605 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005606}
5607
Alexandre Rames67555f72014-11-18 10:55:16 +00005608void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005609 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005610}
5611
Calin Juravlee460d1d2015-09-29 04:52:17 +01005612void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5613 HUnresolvedInstanceFieldGet* instruction) {
5614 FieldAccessCallingConventionARM64 calling_convention;
5615 codegen_->CreateUnresolvedFieldLocationSummary(
5616 instruction, instruction->GetFieldType(), calling_convention);
5617}
5618
5619void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5620 HUnresolvedInstanceFieldGet* instruction) {
5621 FieldAccessCallingConventionARM64 calling_convention;
5622 codegen_->GenerateUnresolvedFieldAccess(instruction,
5623 instruction->GetFieldType(),
5624 instruction->GetFieldIndex(),
5625 instruction->GetDexPc(),
5626 calling_convention);
5627}
5628
5629void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5630 HUnresolvedInstanceFieldSet* instruction) {
5631 FieldAccessCallingConventionARM64 calling_convention;
5632 codegen_->CreateUnresolvedFieldLocationSummary(
5633 instruction, instruction->GetFieldType(), calling_convention);
5634}
5635
5636void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5637 HUnresolvedInstanceFieldSet* instruction) {
5638 FieldAccessCallingConventionARM64 calling_convention;
5639 codegen_->GenerateUnresolvedFieldAccess(instruction,
5640 instruction->GetFieldType(),
5641 instruction->GetFieldIndex(),
5642 instruction->GetDexPc(),
5643 calling_convention);
5644}
5645
5646void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5647 HUnresolvedStaticFieldGet* instruction) {
5648 FieldAccessCallingConventionARM64 calling_convention;
5649 codegen_->CreateUnresolvedFieldLocationSummary(
5650 instruction, instruction->GetFieldType(), calling_convention);
5651}
5652
5653void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5654 HUnresolvedStaticFieldGet* instruction) {
5655 FieldAccessCallingConventionARM64 calling_convention;
5656 codegen_->GenerateUnresolvedFieldAccess(instruction,
5657 instruction->GetFieldType(),
5658 instruction->GetFieldIndex(),
5659 instruction->GetDexPc(),
5660 calling_convention);
5661}
5662
5663void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5664 HUnresolvedStaticFieldSet* instruction) {
5665 FieldAccessCallingConventionARM64 calling_convention;
5666 codegen_->CreateUnresolvedFieldLocationSummary(
5667 instruction, instruction->GetFieldType(), calling_convention);
5668}
5669
5670void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5671 HUnresolvedStaticFieldSet* instruction) {
5672 FieldAccessCallingConventionARM64 calling_convention;
5673 codegen_->GenerateUnresolvedFieldAccess(instruction,
5674 instruction->GetFieldType(),
5675 instruction->GetFieldIndex(),
5676 instruction->GetDexPc(),
5677 calling_convention);
5678}
5679
Alexandre Rames5319def2014-10-23 10:03:10 +01005680void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005681 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5682 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005683 // In suspend check slow path, usually there are no caller-save registers at all.
5684 // If SIMD instructions are present, however, we force spilling all live SIMD
5685 // registers in full width (since the runtime only saves/restores lower part).
5686 locations->SetCustomSlowPathCallerSaves(
5687 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005688}
5689
5690void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005691 HBasicBlock* block = instruction->GetBlock();
5692 if (block->GetLoopInformation() != nullptr) {
5693 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5694 // The back edge will generate the suspend check.
5695 return;
5696 }
5697 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5698 // The goto will generate the suspend check.
5699 return;
5700 }
5701 GenerateSuspendCheck(instruction, nullptr);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01005702 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005703}
5704
Alexandre Rames67555f72014-11-18 10:55:16 +00005705void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005706 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5707 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005708 InvokeRuntimeCallingConvention calling_convention;
5709 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5710}
5711
5712void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005713 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005714 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005715}
5716
5717void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5718 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005719 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 DataType::Type input_type = conversion->GetInputType();
5721 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005722 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5723 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5725 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005726 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5727 }
5728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005729 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005730 locations->SetInAt(0, Location::RequiresFpuRegister());
5731 } else {
5732 locations->SetInAt(0, Location::RequiresRegister());
5733 }
5734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005735 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005736 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5737 } else {
5738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5739 }
5740}
5741
5742void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005743 DataType::Type result_type = conversion->GetResultType();
5744 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005745
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005746 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5747 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5750 int result_size = DataType::Size(result_type);
5751 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005752 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005753 Register output = OutputRegister(conversion);
5754 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005755 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005756 // 'int' values are used directly as W registers, discarding the top
5757 // bits, so we don't need to sign-extend and can just perform a move.
5758 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5759 // top 32 bits of the target register. We theoretically could leave those
5760 // bits unchanged, but we would have to make sure that no code uses a
5761 // 32bit input value as a 64bit value assuming that the top 32 bits are
5762 // zero.
5763 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005764 } else if (DataType::IsUnsignedType(result_type) ||
5765 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5766 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005767 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005768 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005769 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005770 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005771 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5773 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005774 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 } else if (DataType::IsFloatingPointType(result_type) &&
5776 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005777 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5778 } else {
5779 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5780 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005781 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005782}
Alexandre Rames67555f72014-11-18 10:55:16 +00005783
Serban Constantinescu02164b32014-11-13 14:05:07 +00005784void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5785 HandleShift(ushr);
5786}
5787
5788void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5789 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005790}
5791
5792void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5793 HandleBinaryOp(instruction);
5794}
5795
5796void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5797 HandleBinaryOp(instruction);
5798}
5799
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005800void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005801 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005802 LOG(FATAL) << "Unreachable";
5803}
5804
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005805void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005806 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005807 LOG(FATAL) << "Unreachable";
5808}
5809
Mark Mendellfe57faa2015-09-18 09:26:15 -04005810// Simple implementation of packed switch - generate cascaded compare/jumps.
5811void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5812 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005813 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005814 locations->SetInAt(0, Location::RequiresRegister());
5815}
5816
5817void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5818 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005819 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005820 Register value_reg = InputRegisterAt(switch_instr, 0);
5821 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5822
Zheng Xu3927c8b2015-11-18 17:46:25 +08005823 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005824 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005825 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5826 // make sure we don't emit it if the target may run out of range.
5827 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5828 // ranges and emit the tables only as required.
5829 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005830
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005831 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005832 // Current instruction id is an upper bound of the number of HIRs in the graph.
5833 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5834 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005835 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5836 Register temp = temps.AcquireW();
5837 __ Subs(temp, value_reg, Operand(lower_bound));
5838
Zheng Xu3927c8b2015-11-18 17:46:25 +08005839 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005840 // Jump to successors[0] if value == lower_bound.
5841 __ B(eq, codegen_->GetLabelOf(successors[0]));
5842 int32_t last_index = 0;
5843 for (; num_entries - last_index > 2; last_index += 2) {
5844 __ Subs(temp, temp, Operand(2));
5845 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5846 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5847 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5848 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5849 }
5850 if (num_entries - last_index == 2) {
5851 // The last missing case_value.
5852 __ Cmp(temp, Operand(1));
5853 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005854 }
5855
5856 // And the default for any other value.
5857 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5858 __ B(codegen_->GetLabelOf(default_block));
5859 }
5860 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005861 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005862
5863 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5864
5865 // Below instructions should use at most one blocked register. Since there are two blocked
5866 // registers, we are free to block one.
5867 Register temp_w = temps.AcquireW();
5868 Register index;
5869 // Remove the bias.
5870 if (lower_bound != 0) {
5871 index = temp_w;
5872 __ Sub(index, value_reg, Operand(lower_bound));
5873 } else {
5874 index = value_reg;
5875 }
5876
5877 // Jump to default block if index is out of the range.
5878 __ Cmp(index, Operand(num_entries));
5879 __ B(hs, codegen_->GetLabelOf(default_block));
5880
5881 // In current VIXL implementation, it won't require any blocked registers to encode the
5882 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5883 // register pressure.
5884 Register table_base = temps.AcquireX();
5885 // Load jump offset from the table.
5886 __ Adr(table_base, jump_table->GetTableStartLabel());
5887 Register jump_offset = temp_w;
5888 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5889
5890 // Jump to target block by branching to table_base(pc related) + offset.
5891 Register target_address = table_base;
5892 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5893 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005894 }
5895}
5896
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005897void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5898 HInstruction* instruction,
5899 Location out,
5900 uint32_t offset,
5901 Location maybe_temp,
5902 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005903 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005904 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005905 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005906 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005907 if (kUseBakerReadBarrier) {
5908 // Load with fast path based Baker's read barrier.
5909 // /* HeapReference<Object> */ out = *(out + offset)
5910 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5911 out,
5912 out_reg,
5913 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005914 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005915 /* needs_null_check */ false,
5916 /* use_load_acquire */ false);
5917 } else {
5918 // Load with slow path based read barrier.
5919 // Save the value of `out` into `maybe_temp` before overwriting it
5920 // in the following move operation, as we will need it for the
5921 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005922 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005923 __ Mov(temp_reg, out_reg);
5924 // /* HeapReference<Object> */ out = *(out + offset)
5925 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5926 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5927 }
5928 } else {
5929 // Plain load with no read barrier.
5930 // /* HeapReference<Object> */ out = *(out + offset)
5931 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5932 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5933 }
5934}
5935
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005936void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5937 HInstruction* instruction,
5938 Location out,
5939 Location obj,
5940 uint32_t offset,
5941 Location maybe_temp,
5942 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005944 Register out_reg = RegisterFrom(out, type);
5945 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005946 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005947 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005948 if (kUseBakerReadBarrier) {
5949 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005950 // /* HeapReference<Object> */ out = *(obj + offset)
5951 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5952 out,
5953 obj_reg,
5954 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005955 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005956 /* needs_null_check */ false,
5957 /* use_load_acquire */ false);
5958 } else {
5959 // Load with slow path based read barrier.
5960 // /* HeapReference<Object> */ out = *(obj + offset)
5961 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5962 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5963 }
5964 } else {
5965 // Plain load with no read barrier.
5966 // /* HeapReference<Object> */ out = *(obj + offset)
5967 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5968 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5969 }
5970}
5971
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005972void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5973 HInstruction* instruction,
5974 Location root,
5975 Register obj,
5976 uint32_t offset,
5977 vixl::aarch64::Label* fixup_label,
5978 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005979 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005980 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005981 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005982 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005983 if (kUseBakerReadBarrier) {
5984 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005985 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005986 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5987 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01005988 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
5989 // the Marking Register) to decide whether we need to enter
5990 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005991 //
5992 // We use link-time generated thunks for the slow path. That thunk
5993 // checks the reference and jumps to the entrypoint if needed.
5994 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005995 // lr = &return_address;
5996 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01005997 // if (mr) { // Thread::Current()->GetIsGcMarking()
5998 // goto gc_root_thunk<root_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005999 // }
6000 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00006001
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006002 UseScratchRegisterScope temps(GetVIXLAssembler());
6003 DCHECK(temps.IsAvailable(ip0));
6004 DCHECK(temps.IsAvailable(ip1));
6005 temps.Exclude(ip0, ip1);
6006 uint32_t custom_data =
6007 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
6008 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00006009
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006010 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6011 vixl::aarch64::Label return_address;
6012 __ adr(lr, &return_address);
6013 if (fixup_label != nullptr) {
6014 __ Bind(fixup_label);
6015 }
6016 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6017 "GC root LDR must be 2 instruction (8B) before the return address label.");
6018 __ ldr(root_reg, MemOperand(obj.X(), offset));
6019 __ Bind(cbnz_label);
Roland Levillain97c46462017-05-11 14:04:03 +01006020 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006021 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006022 } else {
Roland Levillain97c46462017-05-11 14:04:03 +01006023 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6024 // the Marking Register) to decide whether we need to enter
6025 // the slow path to mark the GC root.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006026 //
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006027 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
Roland Levillain97c46462017-05-11 14:04:03 +01006028 // if (mr) { // Thread::Current()->GetIsGcMarking()
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006029 // // Slow path.
Roland Levillain97c46462017-05-11 14:04:03 +01006030 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6031 // root = entrypoint(root); // root = ReadBarrier::Mark(root); // Entry point call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006032 // }
Roland Levillain44015862016-01-22 11:47:17 +00006033
Roland Levillain97c46462017-05-11 14:04:03 +01006034 // Slow path marking the GC root `root`. The entrypoint will
6035 // be loaded by the slow path code.
6036 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006037 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathARM64(instruction, root);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006038 codegen_->AddSlowPath(slow_path);
6039
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006040 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6041 if (fixup_label == nullptr) {
6042 __ Ldr(root_reg, MemOperand(obj, offset));
6043 } else {
6044 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6045 }
6046 static_assert(
6047 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6048 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6049 "have different sizes.");
6050 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6051 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6052 "have different sizes.");
6053
Roland Levillain97c46462017-05-11 14:04:03 +01006054 __ Cbnz(mr, slow_path->GetEntryLabel());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006055 __ Bind(slow_path->GetExitLabel());
6056 }
Roland Levillain44015862016-01-22 11:47:17 +00006057 } else {
6058 // GC root loaded through a slow path for read barriers other
6059 // than Baker's.
6060 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006061 if (fixup_label == nullptr) {
6062 __ Add(root_reg.X(), obj.X(), offset);
6063 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006064 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006065 }
Roland Levillain44015862016-01-22 11:47:17 +00006066 // /* mirror::Object* */ root = root->Read()
6067 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6068 }
6069 } else {
6070 // Plain GC root load with no read barrier.
6071 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006072 if (fixup_label == nullptr) {
6073 __ Ldr(root_reg, MemOperand(obj, offset));
6074 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006075 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006076 }
Roland Levillain44015862016-01-22 11:47:17 +00006077 // Note that GC roots are not affected by heap poisoning, thus we
6078 // do not have to unpoison `root_reg` here.
6079 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006080 codegen_->MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006081}
6082
6083void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6084 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006085 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006086 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006087 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006088 bool needs_null_check,
6089 bool use_load_acquire) {
6090 DCHECK(kEmitCompilerReadBarrier);
6091 DCHECK(kUseBakerReadBarrier);
6092
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006093 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6094 !use_load_acquire &&
6095 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006096 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6097 // Marking Register) to decide whether we need to enter the slow
6098 // path to mark the reference. Then, in the slow path, check the
6099 // gray bit in the lock word of the reference's holder (`obj`) to
6100 // decide whether to mark `ref` or not.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006101 //
6102 // We use link-time generated thunks for the slow path. That thunk checks
6103 // the holder and jumps to the entrypoint if needed. If the holder is not
6104 // gray, it creates a fake dependency and returns to the LDR instruction.
6105 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006106 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006107 // if (mr) { // Thread::Current()->GetIsGcMarking()
6108 // goto field_thunk<holder_reg, base_reg>(lr)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006109 // }
6110 // not_gray_return_address:
6111 // // Original reference load. If the offset is too large to fit
6112 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006113 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006114 // gray_return_address:
6115
6116 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6117 Register base = obj;
6118 if (offset >= kReferenceLoadMinFarOffset) {
6119 DCHECK(maybe_temp.IsRegister());
6120 base = WRegisterFrom(maybe_temp);
6121 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6122 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6123 offset &= (kReferenceLoadMinFarOffset - 1u);
6124 }
6125 UseScratchRegisterScope temps(GetVIXLAssembler());
6126 DCHECK(temps.IsAvailable(ip0));
6127 DCHECK(temps.IsAvailable(ip1));
6128 temps.Exclude(ip0, ip1);
6129 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6130 base.GetCode(),
6131 obj.GetCode());
6132 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6133
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006134 {
6135 EmissionCheckScope guard(GetVIXLAssembler(),
6136 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6137 vixl::aarch64::Label return_address;
6138 __ adr(lr, &return_address);
6139 __ Bind(cbnz_label);
6140 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6141 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6142 "Field LDR must be 1 instruction (4B) before the return address label; "
6143 " 2 instructions (8B) for heap poisoning.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006145 __ ldr(ref_reg, MemOperand(base.X(), offset));
6146 if (needs_null_check) {
6147 MaybeRecordImplicitNullCheck(instruction);
6148 }
6149 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6150 __ Bind(&return_address);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006151 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006152 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006153 return;
6154 }
6155
Roland Levillain44015862016-01-22 11:47:17 +00006156 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006157 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006158 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006159 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006160 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6161 ref,
6162 obj,
6163 offset,
6164 no_index,
6165 no_scale_factor,
6166 temp,
6167 needs_null_check,
6168 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006169}
6170
6171void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6172 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006173 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006174 uint32_t data_offset,
6175 Location index,
6176 Register temp,
6177 bool needs_null_check) {
6178 DCHECK(kEmitCompilerReadBarrier);
6179 DCHECK(kUseBakerReadBarrier);
6180
Vladimir Marko66d691d2017-04-07 17:53:39 +01006181 static_assert(
6182 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6183 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006184 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006185
6186 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6187 !Runtime::Current()->UseJitCompilation()) {
Roland Levillain97c46462017-05-11 14:04:03 +01006188 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6189 // Marking Register) to decide whether we need to enter the slow
6190 // path to mark the reference. Then, in the slow path, check the
6191 // gray bit in the lock word of the reference's holder (`obj`) to
6192 // decide whether to mark `ref` or not.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006193 //
6194 // We use link-time generated thunks for the slow path. That thunk checks
6195 // the holder and jumps to the entrypoint if needed. If the holder is not
6196 // gray, it creates a fake dependency and returns to the LDR instruction.
6197 //
Vladimir Marko66d691d2017-04-07 17:53:39 +01006198 // lr = &gray_return_address;
Roland Levillain97c46462017-05-11 14:04:03 +01006199 // if (mr) { // Thread::Current()->GetIsGcMarking()
6200 // goto array_thunk<base_reg>(lr)
Vladimir Marko66d691d2017-04-07 17:53:39 +01006201 // }
6202 // not_gray_return_address:
6203 // // Original reference load. If the offset is too large to fit
6204 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006205 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006206 // gray_return_address:
6207
6208 DCHECK(index.IsValid());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006209 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6210 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006211
6212 UseScratchRegisterScope temps(GetVIXLAssembler());
6213 DCHECK(temps.IsAvailable(ip0));
6214 DCHECK(temps.IsAvailable(ip1));
6215 temps.Exclude(ip0, ip1);
6216 uint32_t custom_data =
6217 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6218 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6219
Vladimir Marko66d691d2017-04-07 17:53:39 +01006220 __ Add(temp.X(), obj.X(), Operand(data_offset));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006221 {
6222 EmissionCheckScope guard(GetVIXLAssembler(),
6223 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6224 vixl::aarch64::Label return_address;
6225 __ adr(lr, &return_address);
6226 __ Bind(cbnz_label);
6227 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6228 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6229 "Array LDR must be 1 instruction (4B) before the return address label; "
6230 " 2 instructions (8B) for heap poisoning.");
6231 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6232 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6233 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6234 __ Bind(&return_address);
6235 }
6236 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__, /* temp_loc */ LocationFrom(ip1));
Vladimir Marko66d691d2017-04-07 17:53:39 +01006237 return;
6238 }
6239
Roland Levillain44015862016-01-22 11:47:17 +00006240 // Array cells are never volatile variables, therefore array loads
6241 // never use Load-Acquire instructions on ARM64.
6242 const bool use_load_acquire = false;
6243
6244 // /* HeapReference<Object> */ ref =
6245 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006246 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6247 ref,
6248 obj,
6249 data_offset,
6250 index,
6251 scale_factor,
6252 temp,
6253 needs_null_check,
6254 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006255}
6256
6257void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6258 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006259 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006260 uint32_t offset,
6261 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006262 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006263 Register temp,
6264 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006265 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006266 DCHECK(kEmitCompilerReadBarrier);
6267 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006268 // If we are emitting an array load, we should not be using a
6269 // Load Acquire instruction. In other words:
6270 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6271 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006272
Roland Levillain97c46462017-05-11 14:04:03 +01006273 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6274 // Marking Register) to decide whether we need to enter the slow
6275 // path to mark the reference. Then, in the slow path, check the
6276 // gray bit in the lock word of the reference's holder (`obj`) to
6277 // decide whether to mark `ref` or not.
Roland Levillain44015862016-01-22 11:47:17 +00006278 //
Roland Levillain97c46462017-05-11 14:04:03 +01006279 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainba650a42017-03-06 13:52:32 +00006280 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006281 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6282 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6283 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6284 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6285 // if (is_gray) {
Roland Levillain97c46462017-05-11 14:04:03 +01006286 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6287 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain54f869e2017-03-06 13:54:11 +00006288 // }
6289 // } else {
6290 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006291 // }
Roland Levillain44015862016-01-22 11:47:17 +00006292
Roland Levillainba650a42017-03-06 13:52:32 +00006293 // Slow path marking the object `ref` when the GC is marking. The
Roland Levillain97c46462017-05-11 14:04:03 +01006294 // entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006295 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006296 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006297 instruction,
6298 ref,
6299 obj,
6300 offset,
6301 index,
6302 scale_factor,
6303 needs_null_check,
6304 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006305 temp);
Roland Levillainba650a42017-03-06 13:52:32 +00006306 AddSlowPath(slow_path);
6307
Roland Levillain97c46462017-05-11 14:04:03 +01006308 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006309 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006310 GenerateRawReferenceLoad(
6311 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006312 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006313 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainba650a42017-03-06 13:52:32 +00006314}
6315
Roland Levillainff487002017-03-07 16:50:01 +00006316void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6317 Location ref,
6318 Register obj,
6319 Location field_offset,
6320 Register temp,
6321 bool needs_null_check,
6322 bool use_load_acquire) {
6323 DCHECK(kEmitCompilerReadBarrier);
6324 DCHECK(kUseBakerReadBarrier);
6325 // If we are emitting an array load, we should not be using a
6326 // Load Acquire instruction. In other words:
6327 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6328 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6329
Roland Levillain97c46462017-05-11 14:04:03 +01006330 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6331 // Marking Register) to decide whether we need to enter the slow
6332 // path to update the reference field within `obj`. Then, in the
6333 // slow path, check the gray bit in the lock word of the reference's
6334 // holder (`obj`) to decide whether to mark `ref` and update the
6335 // field or not.
Roland Levillainff487002017-03-07 16:50:01 +00006336 //
Roland Levillain97c46462017-05-11 14:04:03 +01006337 // if (mr) { // Thread::Current()->GetIsGcMarking()
Roland Levillainff487002017-03-07 16:50:01 +00006338 // // Slow path.
6339 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6340 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6341 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6342 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6343 // if (is_gray) {
6344 // old_ref = ref;
Roland Levillain97c46462017-05-11 14:04:03 +01006345 // entrypoint = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6346 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00006347 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6348 // }
6349 // }
6350
6351 // Slow path updating the object reference at address `obj + field_offset`
Roland Levillain97c46462017-05-11 14:04:03 +01006352 // when the GC is marking. The entrypoint will be loaded by the slow path code.
Roland Levillainff487002017-03-07 16:50:01 +00006353 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006354 new (GetScopedAllocator()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
Roland Levillainff487002017-03-07 16:50:01 +00006355 instruction,
6356 ref,
6357 obj,
6358 /* offset */ 0u,
6359 /* index */ field_offset,
6360 /* scale_factor */ 0u /* "times 1" */,
6361 needs_null_check,
6362 use_load_acquire,
Roland Levillain97c46462017-05-11 14:04:03 +01006363 temp);
Roland Levillainff487002017-03-07 16:50:01 +00006364 AddSlowPath(slow_path);
6365
Roland Levillain97c46462017-05-11 14:04:03 +01006366 __ Cbnz(mr, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006367 // Fast path: the GC is not marking: nothing to do (the field is
6368 // up-to-date, and we don't need to load the reference).
6369 __ Bind(slow_path->GetExitLabel());
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006370 MaybeGenerateMarkingRegisterCheck(/* code */ __LINE__);
Roland Levillainff487002017-03-07 16:50:01 +00006371}
6372
Roland Levillainba650a42017-03-06 13:52:32 +00006373void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6374 Location ref,
6375 Register obj,
6376 uint32_t offset,
6377 Location index,
6378 size_t scale_factor,
6379 bool needs_null_check,
6380 bool use_load_acquire) {
6381 DCHECK(obj.IsW());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006383 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006384
Roland Levillainba650a42017-03-06 13:52:32 +00006385 // If needed, vixl::EmissionCheckScope guards are used to ensure
6386 // that no pools are emitted between the load (macro) instruction
6387 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006388
Roland Levillain44015862016-01-22 11:47:17 +00006389 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006390 // Load types involving an "index": ArrayGet,
6391 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6392 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006393 if (use_load_acquire) {
6394 // UnsafeGetObjectVolatile intrinsic case.
6395 // Register `index` is not an index in an object array, but an
6396 // offset to an object reference field within object `obj`.
6397 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6398 DCHECK(instruction->GetLocations()->Intrinsified());
6399 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6400 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006401 DCHECK_EQ(offset, 0u);
6402 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006403 DCHECK_EQ(needs_null_check, false);
6404 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006405 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6406 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006407 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006408 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6409 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006410 if (index.IsConstant()) {
6411 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006412 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006413 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006414 if (needs_null_check) {
6415 MaybeRecordImplicitNullCheck(instruction);
6416 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006417 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006418 UseScratchRegisterScope temps(GetVIXLAssembler());
6419 Register temp = temps.AcquireW();
6420 __ Add(temp, obj, offset);
6421 {
6422 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6423 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6424 if (needs_null_check) {
6425 MaybeRecordImplicitNullCheck(instruction);
6426 }
6427 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006428 }
Roland Levillain44015862016-01-22 11:47:17 +00006429 }
Roland Levillain44015862016-01-22 11:47:17 +00006430 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006431 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006432 MemOperand field = HeapOperand(obj, offset);
6433 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006434 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6435 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006436 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006437 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006438 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006439 if (needs_null_check) {
6440 MaybeRecordImplicitNullCheck(instruction);
6441 }
Roland Levillain44015862016-01-22 11:47:17 +00006442 }
6443 }
6444
6445 // Object* ref = ref_addr->AsMirrorPtr()
6446 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006447}
6448
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006449void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6450 // The following condition is a compile-time one, so it does not have a run-time cost.
6451 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6452 // The following condition is a run-time one; it is executed after the
6453 // previous compile-time test, to avoid penalizing non-debug builds.
6454 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6455 UseScratchRegisterScope temps(GetVIXLAssembler());
6456 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6457 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6458 }
6459 }
6460}
6461
Roland Levillain44015862016-01-22 11:47:17 +00006462void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6463 Location out,
6464 Location ref,
6465 Location obj,
6466 uint32_t offset,
6467 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006468 DCHECK(kEmitCompilerReadBarrier);
6469
Roland Levillain44015862016-01-22 11:47:17 +00006470 // Insert a slow path based read barrier *after* the reference load.
6471 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006472 // If heap poisoning is enabled, the unpoisoning of the loaded
6473 // reference will be carried out by the runtime within the slow
6474 // path.
6475 //
6476 // Note that `ref` currently does not get unpoisoned (when heap
6477 // poisoning is enabled), which is alright as the `ref` argument is
6478 // not used by the artReadBarrierSlow entry point.
6479 //
6480 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006481 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006482 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6483 AddSlowPath(slow_path);
6484
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006485 __ B(slow_path->GetEntryLabel());
6486 __ Bind(slow_path->GetExitLabel());
6487}
6488
Roland Levillain44015862016-01-22 11:47:17 +00006489void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6490 Location out,
6491 Location ref,
6492 Location obj,
6493 uint32_t offset,
6494 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006495 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006496 // Baker's read barriers shall be handled by the fast path
6497 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6498 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006499 // If heap poisoning is enabled, unpoisoning will be taken care of
6500 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006501 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006502 } else if (kPoisonHeapReferences) {
6503 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6504 }
6505}
6506
Roland Levillain44015862016-01-22 11:47:17 +00006507void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6508 Location out,
6509 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006510 DCHECK(kEmitCompilerReadBarrier);
6511
Roland Levillain44015862016-01-22 11:47:17 +00006512 // Insert a slow path based read barrier *after* the GC root load.
6513 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006514 // Note that GC roots are not affected by heap poisoning, so we do
6515 // not need to do anything special for this here.
6516 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006517 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006518 AddSlowPath(slow_path);
6519
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006520 __ B(slow_path->GetEntryLabel());
6521 __ Bind(slow_path->GetExitLabel());
6522}
6523
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006524void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6525 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006526 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006527 locations->SetInAt(0, Location::RequiresRegister());
6528 locations->SetOut(Location::RequiresRegister());
6529}
6530
6531void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6532 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006533 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006534 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006535 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006536 __ Ldr(XRegisterFrom(locations->Out()),
6537 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006538 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006539 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006540 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006541 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6542 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006543 __ Ldr(XRegisterFrom(locations->Out()),
6544 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006545 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006546}
6547
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006548static void PatchJitRootUse(uint8_t* code,
6549 const uint8_t* roots_data,
6550 vixl::aarch64::Literal<uint32_t>* literal,
6551 uint64_t index_in_table) {
6552 uint32_t literal_offset = literal->GetOffset();
6553 uintptr_t address =
6554 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6555 uint8_t* data = code + literal_offset;
6556 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6557}
6558
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006559void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6560 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006561 const StringReference& string_reference = entry.first;
6562 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006563 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006564 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006565 }
6566 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006567 const TypeReference& type_reference = entry.first;
6568 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006569 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006570 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006571 }
6572}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006573
Alexandre Rames67555f72014-11-18 10:55:16 +00006574#undef __
6575#undef QUICK_ENTRY_POINT
6576
Alexandre Rames5319def2014-10-23 10:03:10 +01006577} // namespace arm64
6578} // namespace art